~xnox/merge-o-matic/add-proposed

« back to all changes in this revision

Viewing changes to util/shell.py

  • Committer: Colin Watson
  • Date: 2012-05-21 23:14:11 UTC
  • Revision ID: cjwatson@canonical.com-20120521231411-vv46gwgryy5ve2yc
Set DEB_VENDOR=<appropriate distro> when unpacking sources.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
    """
43
43
 
44
44
    def __init__(self, args, mode="x", stdin=None, stdout=None, stderr=None,
45
 
                 chdir=None, okstatus=(0,)):
 
45
                 chdir=None, okstatus=(0,), env=None):
46
46
        """Spawn a child process.
47
47
 
48
48
        The command name and its arguments should be provided as a tuple or
81
81
        Non-zero exit statuses are normally treated as a failure and an
82
82
        exception raised, if this is undesired specify the allowed exit
83
83
        status as a list or tuple in the okstatus argument.
 
84
 
 
85
        To set environment variables in the child, pass a dictionary in the
 
86
        env argument.  This should normally be a copy of os.environ with
 
87
        additional changes.  If not passed or None, the child inherits the
 
88
        parent's environment.
84
89
        """
85
90
        self.args = list(args)
86
91
        self.okstatus = okstatus
98
103
        if chdir is not None and not os.path.isdir(chdir):
99
104
            raise OSError, "cannot chdir to %s" % chdir
100
105
 
 
106
        if env is None:
 
107
            self.env = None
 
108
        else:
 
109
            self.env = dict(env)
 
110
 
101
111
        self.open(stdin, stdout, stderr, chdir)
102
112
 
103
113
    def open(self, stdin, stdout, stderr, chdir):
180
190
            os.chdir(chdir)
181
191
 
182
192
        # Run the command
183
 
        os.execvp(self.args[0], self.args)
 
193
        if self.env is None:
 
194
            os.execvp(self.args[0], self.args)
 
195
        else:
 
196
            os.execvpe(self.args[0], self.args, self.env)
184
197
 
185
198
    def close(self):
186
199
        """Close the process and file descriptor.
230
243
                  % (type(self).__name__, name)
231
244
 
232
245
 
233
 
def run(args, stdin=None, stdout=None, stderr=None, chdir=None, okstatus=(0,)):
 
246
def run(args, stdin=None, stdout=None, stderr=None, chdir=None, okstatus=(0,),
 
247
        env=None):
234
248
    """Run a process without an input or output pipe.
235
249
 
236
250
    Shorthand for util.shell.Process(...) with mode fixed to 'x' and calls
237
251
    close immediately.
238
252
    """
239
253
    p = Process(args, "x", stdin=stdin, stdout=stdout, stderr=stderr,
240
 
                chdir=chdir, okstatus=okstatus)
 
254
                chdir=chdir, okstatus=okstatus, env=env)
241
255
    return p.close()
242
256
 
243
 
def get(args, stdin=None, stderr=None, chdir=None, okstatus=(0,), strip=True):
 
257
def get(args, stdin=None, stderr=None, chdir=None, okstatus=(0,), env=None,
 
258
        strip=True):
244
259
    """Get process output.
245
260
 
246
261
    Shorthand for util.shell.Process(...) with mode fixed to 'r' and
249
264
    If strip is True (the default) any final newlines will be stripped.
250
265
    """
251
266
    p = Process(args, "r", stdin=stdin, stderr=stderr, chdir=chdir,
252
 
                okstatus=okstatus)
 
267
                okstatus=okstatus, env=env)
253
268
    try:
254
269
        text = p.read()
255
270
        if strip: