Indefero

Indefero Git Source Tree


Root/src/IDF/Scm/Monotone/Stdio.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
require_once 'IDF/Scm/Monotone/IStdio.php';
 
/**
 * Monotone stdio class
 *
 * Connects to a monotone process and executes commands via its
 * stdio interface
 *
 * @author Thomas Keller <me@thomaskeller.biz>
 */
class IDF_Scm_Monotone_Stdio implements IDF_Scm_Monotone_IStdio
{
    /** this is the most recent STDIO version. The number is output
        at the protocol start. Older versions of monotone (prior 0.47)
        do not output it and are therefor incompatible */
    public static $SUPPORTED_STDIO_VERSION = 2;
 
    private $project;
    private $proc;
    private $pipes;
    private $oob;
    private $cmdnum;
    private $lastcmd;
 
    /**
     * Constructor - starts the stdio process
     *
     * @param IDF_Project
     */
    public function __construct(IDF_Project $project)
    {
        $this->project = $project;
        $this->start();
    }
 
    /**
     * Destructor - stops the stdio process
     */
    public function __destruct()
    {
        $this->stop();
    }
 
    /**
     * Returns a string with additional options which are passed to
     * an mtn instance connecting to remote databases
     *
     * @return string
     */
    private function _getAuthOptions()
    {
        $prjconf = $this->project->getConf();
        $name = $prjconf->getVal('mtn_client_key_name', false);
        $hash = $prjconf->getVal('mtn_client_key_hash', false);
 
        if (!$name || !$hash) {
            throw new IDF_Scm_Exception(sprintf(
                __('Monotone client key name or hash not in project conf.')
            ));
        }
 
        $keydir = Pluf::f('tmp_folder').'/mtn-client-keys';
        if (!file_exists($keydir)) {
            if (!mkdir($keydir)) {
                throw new IDF_Scm_Exception(sprintf(
                    __('The key directory %s could not be created.'), $keydir
                ));
            }
        }
 
        // in case somebody cleaned out the cache, we restore the key here
        $keyfile = $keydir . '/' . $name .'.'. $hash;
        if (!file_exists($keyfile)) {
            $data = $prjconf->getVal('mtn_client_key_data');
            if (!file_put_contents($keyfile, $data, LOCK_EX)) {
                throw new IDF_Scm_Exception(sprintf(
                    __('Could not write client key "%s"'), $keyfile
                ));
            }
        }
 
        return sprintf('--keydir=%s --key=%s ',
               escapeshellarg($keydir),
               escapeshellarg($hash)
        );
    }
 
    /**
     * Starts the stdio process and resets the command counter
     */
    public function start()
    {
        if (is_resource($this->proc))
            $this->stop();
 
        $remote_db_access = Pluf::f('mtn_db_access', 'remote') == 'remote';
 
        $cmd = Pluf::f('idf_exec_cmd_prefix', '') .
               Pluf::f('mtn_path', 'mtn') . ' ';
 
        $opts = Pluf::f('mtn_opts', array());
        foreach ($opts as $opt) {
            $cmd .= sprintf('%s ', escapeshellarg($opt));
        }
 
        if ($remote_db_access) {
            $cmd .= $this->_getAuthOptions();
            $host = sprintf(Pluf::f('mtn_remote_url'), $this->project->shortname);
            $cmd .= sprintf('automate remote_stdio %s', escapeshellarg($host));
        }
        else
        {
            $repo = sprintf(Pluf::f('mtn_repositories'), $this->project->shortname);
            if (!file_exists($repo)) {
                throw new IDF_Scm_Exception(
                    "repository file '$repo' does not exist"
                );
            }
            $cmd .= sprintf('--db %s automate stdio', escapeshellarg($repo));
        }
 
        $descriptors = array(
            0 => array('pipe', 'r'),
            1 => array('pipe', 'w'),
            2 => array('pipe', 'w'),
        );
 
        $env = array('LANG' => 'en_US.UTF-8');
        $this->proc = proc_open($cmd, $descriptors, $this->pipes,
                                null, $env);
 
        if (!is_resource($this->proc)) {
            throw new IDF_Scm_Exception('could not start stdio process');
        }
 
        $this->_checkVersion();
 
        $this->cmdnum = -1;
    }
 
    /**
     * Stops the stdio process and closes all pipes
     */
    public function stop()
    {
        if (!is_resource($this->proc))
            return;
 
        fclose($this->pipes[0]);
        fclose($this->pipes[1]);
        fclose($this->pipes[2]);
 
        proc_close($this->proc);
        $this->proc = null;
    }
 
    /**
     * select()'s on stdout and returns true as soon as we got new
     * data to read, false if the select() timed out
     *
     * @return boolean
     * @throws IDF_Scm_Exception
     */
    private function _waitForReadyRead()
    {
        if (!is_resource($this->pipes[1]))
            return false;
 
        $read = array($this->pipes[1], $this->pipes[2]);
        $write = $except = null;
        $streamsChanged = stream_select(
            $read, $write, $except, 0, 20000
        );
 
        if ($streamsChanged === false) {
            throw new IDF_Scm_Exception(
                'Could not select() on read pipe'
            );
        }
 
        if ($streamsChanged == 0) {
            return false;
        }
 
        return true;
    }
 
    /**
     * Checks the version of the used stdio protocol
     *
     * @throws IDF_Scm_Exception
     */
    private function _checkVersion()
    {
        $this->_waitForReadyRead();
 
        $version = fgets($this->pipes[1]);
        if ($version === false) {
            throw new IDF_Scm_Exception(
                "Could not determine stdio version, stderr is:\n".
                $this->_readStderr()
            );
        }
 
        if (!preg_match('/^format-version: (\d+)$/', $version, $m) ||
            $m[1] != self::$SUPPORTED_STDIO_VERSION)
        {
            throw new IDF_Scm_Exception(
                'stdio format version mismatch, expected "'.
                self::$SUPPORTED_STDIO_VERSION.'", got "'.@$m[1].'"'
            );
        }
 
        fgets($this->pipes[1]);
    }
 
    /**
     * Writes a command to stdio
     *
     * @param array
     * @param array
     * @throws IDF_Scm_Exception
     */
    private function _write(array $args, array $options = array())
    {
        $cmd = '';
        if (count($options) > 0) {
            $cmd = 'o';
            foreach ($options as $k => $vals) {
                if (!is_array($vals))
                    $vals = array($vals);
 
                foreach ($vals as $v) {
                    $cmd .= strlen((string)$k) . ':' . (string)$k;
                    $cmd .= strlen((string)$v) . ':' . (string)$v;
                }
            }
            $cmd .= 'e ';
        }
 
        $cmd .= 'l';
        foreach ($args as $arg) {
            $cmd .= strlen((string)$arg) . ':' . (string)$arg;
        }
        $cmd .= "e\n";
 
        if (!fwrite($this->pipes[0], $cmd)) {
            throw new IDF_Scm_Exception("could not write '$cmd' to process");
        }
 
        $this->lastcmd = $cmd;
        $this->cmdnum++;
    }
 
    /**
     * Reads all output from stderr and returns it
     *
     * @return string
     */
    private function _readStderr()
    {
        $err = "";
        while (($line = fgets($this->pipes[2])) !== false) {
            $err .= $line;
        }
        return empty($err) ? '<empty>' : $err;
    }
 
    /**
     * Reads the last output from the stdio process, parses and returns it
     *
     * @return string
     * @throws IDF_Scm_Exception
     */
    private function _readStdout()
    {
        $this->oob = array('w' => array(),
                           'p' => array(),
                           't' => array(),
                           'e' => array());
 
        $output = "";
        $errcode = 0;
 
        while (true) {
            if (!$this->_waitForReadyRead())
                continue;
 
            $data = array(0,"",0);
            $idx = 0;
            while (true) {
                $c = fgetc($this->pipes[1]);
                if ($c === false) {
                    throw new IDF_Scm_Exception(
                        "No data on stdin, stderr is:\n".
                        $this->_readStderr()
                    );
                }
 
                if ($c == ':') {
                    if ($idx == 2)
                        break;
 
                    ++$idx;
                    continue;
                }
 
                if (is_numeric($c))
                    $data[$idx] = $data[$idx] * 10 + $c;
                else
                    $data[$idx] .= $c;
            }
 
            // sanity
            if ($this->cmdnum != $data[0]) {
                throw new IDF_Scm_Exception(
                    'command numbers out of sync; expected '.
                    $this->cmdnum .', got '. $data[0]
                );
            }
 
            $toRead = $data[2];
            $buffer = "";
            while ($toRead > 0) {
                $buffer .= fread($this->pipes[1], $toRead);
                $toRead = $data[2] - strlen($buffer);
            }
 
            switch ($data[1]) {
                case 'w':
                case 'p':
                case 't':
                case 'e':
                    $this->oob[$data[1]][] = $buffer;
                    continue;
                case 'm':
                    $output .= $buffer;
                    continue;
                case 'l':
                    $errcode = $buffer;
                    break 2;
            }
        }
 
        if ($errcode != 0) {
            throw new IDF_Scm_Exception(
                "command '{$this->lastcmd}' returned error code $errcode: ".
                implode(' ', $this->oob['e'])
            );
        }
 
        return $output;
    }
 
    /**
     * Executes a command over stdio and returns its result
     *
     * @param array Array of arguments
     * @param array Array of options as key-value pairs. Multiple options
     *              can be defined in sub-arrays, like
     *              "r" => array("123...", "456...")
     * @return string
     */
    public function exec(array $args, array $options = array())
    {
        $this->_write($args, $options);
        return $this->_readStdout();
    }
 
    /**
     * Returns the last out-of-band output for a previously executed
     * command as associative array with 'e' (error), 'w' (warning),
     * 'p' (progress) and 't' (ticker, unparsed) as keys
     *
     * @return array
     */
    public function getLastOutOfBandOutput()
    {
        return $this->oob;
    }
}

Archive Download this file

Page rendered in 0.33803s using 11 queries.