root/galaxy-central/eggs/Paste-1.6-py2.6.egg/paste/util/subprocess24.py @ 3

リビジョン 3, 39.2 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1# subprocess - Subprocesses with accessible I/O streams
2#
3# For more information about this module, see PEP 324.
4#
5# This module should remain compatible with Python 2.2, see PEP 291.
6#
7# Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
8#
9# Licensed to PSF under a Contributor Agreement.
10# See http://www.python.org/2.4/license for licensing details.
11
12r"""subprocess - Subprocesses with accessible I/O streams
13
14This module allows you to spawn processes, connect to their
15input/output/error pipes, and obtain their return codes.  This module
16intends to replace several other, older modules and functions, like:
17
18os.system
19os.spawn*
20os.popen*
21popen2.*
22commands.*
23
24Information about how the subprocess module can be used to replace these
25modules and functions can be found below.
26
27
28
29Using the subprocess module
30===========================
31This module defines one class called Popen:
32
33class Popen(args, bufsize=0, executable=None,
34            stdin=None, stdout=None, stderr=None,
35            preexec_fn=None, close_fds=False, shell=False,
36            cwd=None, env=None, universal_newlines=False,
37            startupinfo=None, creationflags=0):
38
39
40Arguments are:
41
42args should be a string, or a sequence of program arguments.  The
43program to execute is normally the first item in the args sequence or
44string, but can be explicitly set by using the executable argument.
45
46On UNIX, with shell=False (default): In this case, the Popen class
47uses os.execvp() to execute the child program.  args should normally
48be a sequence.  A string will be treated as a sequence with the string
49as the only item (the program to execute).
50
51On UNIX, with shell=True: If args is a string, it specifies the
52command string to execute through the shell.  If args is a sequence,
53the first item specifies the command string, and any additional items
54will be treated as additional shell arguments.
55
56On Windows: the Popen class uses CreateProcess() to execute the child
57program, which operates on strings.  If args is a sequence, it will be
58converted to a string using the list2cmdline method.  Please note that
59not all MS Windows applications interpret the command line the same
60way: The list2cmdline is designed for applications using the same
61rules as the MS C runtime.
62
63bufsize, if given, has the same meaning as the corresponding argument
64to the built-in open() function: 0 means unbuffered, 1 means line
65buffered, any other positive value means use a buffer of
66(approximately) that size.  A negative bufsize means to use the system
67default, which usually means fully buffered.  The default value for
68bufsize is 0 (unbuffered).
69
70stdin, stdout and stderr specify the executed programs' standard
71input, standard output and standard error file handles, respectively.
72Valid values are PIPE, an existing file descriptor (a positive
73integer), an existing file object, and None.  PIPE indicates that a
74new pipe to the child should be created.  With None, no redirection
75will occur; the child's file handles will be inherited from the
76parent.  Additionally, stderr can be STDOUT, which indicates that the
77stderr data from the applications should be captured into the same
78file handle as for stdout.
79
80If preexec_fn is set to a callable object, this object will be called
81in the child process just before the child is executed.
82
83If close_fds is true, all file descriptors except 0, 1 and 2 will be
84closed before the child process is executed.
85
86if shell is true, the specified command will be executed through the
87shell.
88
89If cwd is not None, the current directory will be changed to cwd
90before the child is executed.
91
92If env is not None, it defines the environment variables for the new
93process.
94
95If universal_newlines is true, the file objects stdout and stderr are
96opened as a text files, but lines may be terminated by any of '\n',
97the Unix end-of-line convention, '\r', the Macintosh convention or
98'\r\n', the Windows convention.  All of these external representations
99are seen as '\n' by the Python program.  Note: This feature is only
100available if Python is built with universal newline support (the
101default).  Also, the newlines attribute of the file objects stdout,
102stdin and stderr are not updated by the communicate() method.
103
104The startupinfo and creationflags, if given, will be passed to the
105underlying CreateProcess() function.  They can specify things such as
106appearance of the main window and priority for the new process.
107(Windows only)
108
109
110This module also defines two shortcut functions:
111
112call(*args, **kwargs):
113    Run command with arguments.  Wait for command to complete, then
114    return the returncode attribute. The arguments are the same as for
115    the Popen constructor.  Example:
116
117    retcode = call(["ls", "-l"])
118
119
120Exceptions
121----------
122Exceptions raised in the child process, before the new program has
123started to execute, will be re-raised in the parent.  Additionally,
124the exception object will have one extra attribute called
125'child_traceback', which is a string containing traceback information
126from the childs point of view.
127
128The most common exception raised is OSError.  This occurs, for
129example, when trying to execute a non-existent file.  Applications
130should prepare for OSErrors.
131
132A ValueError will be raised if Popen is called with invalid arguments.
133
134
135Security
136--------
137Unlike some other popen functions, this implementation will never call
138/bin/sh implicitly.  This means that all characters, including shell
139metacharacters, can safely be passed to child processes.
140
141
142Popen objects
143=============
144Instances of the Popen class have the following methods:
145
146poll()
147    Check if child process has terminated.  Returns returncode
148    attribute.
149
150wait()
151    Wait for child process to terminate.  Returns returncode attribute.
152
153communicate(input=None)
154    Interact with process: Send data to stdin.  Read data from stdout
155    and stderr, until end-of-file is reached.  Wait for process to
156    terminate.  The optional stdin argument should be a string to be
157    sent to the child process, or None, if no data should be sent to
158    the child.
159
160    communicate() returns a tuple (stdout, stderr).
161
162    Note: The data read is buffered in memory, so do not use this
163    method if the data size is large or unlimited.
164
165The following attributes are also available:
166
167stdin
168    If the stdin argument is PIPE, this attribute is a file object
169    that provides input to the child process.  Otherwise, it is None.
170
171stdout
172    If the stdout argument is PIPE, this attribute is a file object
173    that provides output from the child process.  Otherwise, it is
174    None.
175
176stderr
177    If the stderr argument is PIPE, this attribute is file object that
178    provides error output from the child process.  Otherwise, it is
179    None.
180
181pid
182    The process ID of the child process.
183
184returncode
185    The child return code.  A None value indicates that the process
186    hasn't terminated yet.  A negative value -N indicates that the
187    child was terminated by signal N (UNIX only).
188
189
190Replacing older functions with the subprocess module
191====================================================
192In this section, "a ==> b" means that b can be used as a replacement
193for a.
194
195Note: All functions in this section fail (more or less) silently if
196the executed program cannot be found; this module raises an OSError
197exception.
198
199In the following examples, we assume that the subprocess module is
200imported with "from subprocess import *".
201
202
203Replacing /bin/sh shell backquote
204---------------------------------
205output=`mycmd myarg`
206==>
207output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
208
209
210Replacing shell pipe line
211-------------------------
212output=`dmesg | grep hda`
213==>
214p1 = Popen(["dmesg"], stdout=PIPE)
215p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
216output = p2.communicate()[0]
217
218
219Replacing os.system()
220---------------------
221sts = os.system("mycmd" + " myarg")
222==>
223p = Popen("mycmd" + " myarg", shell=True)
224sts = os.waitpid(p.pid, 0)
225
226Note:
227
228* Calling the program through the shell is usually not required.
229
230* It's easier to look at the returncode attribute than the
231  exitstatus.
232
233A more real-world example would look like this:
234
235try:
236    retcode = call("mycmd" + " myarg", shell=True)
237    if retcode < 0:
238        print >>sys.stderr, "Child was terminated by signal", -retcode
239    else:
240        print >>sys.stderr, "Child returned", retcode
241except OSError, e:
242    print >>sys.stderr, "Execution failed:", e
243
244
245Replacing os.spawn*
246-------------------
247P_NOWAIT example:
248
249pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
250==>
251pid = Popen(["/bin/mycmd", "myarg"]).pid
252
253
254P_WAIT example:
255
256retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
257==>
258retcode = call(["/bin/mycmd", "myarg"])
259
260
261Vector example:
262
263os.spawnvp(os.P_NOWAIT, path, args)
264==>
265Popen([path] + args[1:])
266
267
268Environment example:
269
270os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
271==>
272Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
273
274
275Replacing os.popen*
276-------------------
277pipe = os.popen(cmd, mode='r', bufsize)
278==>
279pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout
280
281pipe = os.popen(cmd, mode='w', bufsize)
282==>
283pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin
284
285
286(child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize)
287==>
288p = Popen(cmd, shell=True, bufsize=bufsize,
289          stdin=PIPE, stdout=PIPE, close_fds=True)
290(child_stdin, child_stdout) = (p.stdin, p.stdout)
291
292
293(child_stdin,
294 child_stdout,
295 child_stderr) = os.popen3(cmd, mode, bufsize)
296==>
297p = Popen(cmd, shell=True, bufsize=bufsize,
298          stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
299(child_stdin,
300 child_stdout,
301 child_stderr) = (p.stdin, p.stdout, p.stderr)
302
303
304(child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize)
305==>
306p = Popen(cmd, shell=True, bufsize=bufsize,
307          stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
308(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)
309
310
311Replacing popen2.*
312------------------
313Note: If the cmd argument to popen2 functions is a string, the command
314is executed through /bin/sh.  If it is a list, the command is directly
315executed.
316
317(child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
318==>
319p = Popen(["somestring"], shell=True, bufsize=bufsize
320          stdin=PIPE, stdout=PIPE, close_fds=True)
321(child_stdout, child_stdin) = (p.stdout, p.stdin)
322
323
324(child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode)
325==>
326p = Popen(["mycmd", "myarg"], bufsize=bufsize,
327          stdin=PIPE, stdout=PIPE, close_fds=True)
328(child_stdout, child_stdin) = (p.stdout, p.stdin)
329
330The popen2.Popen3 and popen3.Popen4 basically works as subprocess.Popen,
331except that:
332
333* subprocess.Popen raises an exception if the execution fails
334* the capturestderr argument is replaced with the stderr argument.
335* stdin=PIPE and stdout=PIPE must be specified.
336* popen2 closes all filedescriptors by default, but you have to specify
337  close_fds=True with subprocess.Popen.
338
339
340"""
341
342import sys
343mswindows = (sys.platform == "win32")
344
345import os
346import types
347import traceback
348
349if mswindows:
350    import threading
351    import msvcrt
352    ## @@: Changed in Paste
353    ## Since this module is only used on pre-python-2.4 systems, they probably
354    ## don't have _subprocess installed, but hopefully have the win32 stuff
355    ## installed.
356    if 1: # <-- change this to use pywin32 instead of the _subprocess driver
357        import pywintypes
358        from win32api import GetStdHandle, STD_INPUT_HANDLE, \
359                             STD_OUTPUT_HANDLE, STD_ERROR_HANDLE
360        from win32api import GetCurrentProcess, DuplicateHandle, \
361                             GetModuleFileName, GetVersion
362        from win32con import DUPLICATE_SAME_ACCESS, SW_HIDE
363        from win32pipe import CreatePipe
364        from win32process import CreateProcess, STARTUPINFO, \
365                                 GetExitCodeProcess, STARTF_USESTDHANDLES, \
366                                 STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE
367        from win32event import WaitForSingleObject, INFINITE, WAIT_OBJECT_0
368    else:
369        from _subprocess import *
370        class STARTUPINFO:
371            dwFlags = 0
372            hStdInput = None
373            hStdOutput = None
374            hStdError = None
375        class pywintypes:
376            error = IOError
377else:
378    import select
379    import errno
380    import fcntl
381    import pickle
382
383__all__ = ["Popen", "PIPE", "STDOUT", "call"]
384
385try:
386    MAXFD = os.sysconf("SC_OPEN_MAX")
387except:
388    MAXFD = 256
389
390# True/False does not exist on 2.2.0
391try:
392    False
393except NameError:
394    False = 0
395    True = 1
396
397_active = []
398
399def _cleanup():
400    for inst in _active[:]:
401        inst.poll()
402
403PIPE = -1
404STDOUT = -2
405
406
407def call(*args, **kwargs):
408    """Run command with arguments.  Wait for command to complete, then
409    return the returncode attribute.
410
411    The arguments are the same as for the Popen constructor.  Example:
412
413    retcode = call(["ls", "-l"])
414    """
415    return Popen(*args, **kwargs).wait()
416
417
418def list2cmdline(seq):
419    """
420    Translate a sequence of arguments into a command line
421    string, using the same rules as the MS C runtime:
422
423    1) Arguments are delimited by white space, which is either a
424       space or a tab.
425
426    2) A string surrounded by double quotation marks is
427       interpreted as a single argument, regardless of white space
428       contained within.  A quoted string can be embedded in an
429       argument.
430
431    3) A double quotation mark preceded by a backslash is
432       interpreted as a literal double quotation mark.
433
434    4) Backslashes are interpreted literally, unless they
435       immediately precede a double quotation mark.
436
437    5) If backslashes immediately precede a double quotation mark,
438       every pair of backslashes is interpreted as a literal
439       backslash.  If the number of backslashes is odd, the last
440       backslash escapes the next double quotation mark as
441       described in rule 3.
442    """
443
444    # See
445    # http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp
446    result = []
447    needquote = False
448    for arg in seq:
449        bs_buf = []
450
451        # Add a space to separate this argument from the others
452        if result:
453            result.append(' ')
454
455        needquote = (" " in arg) or ("\t" in arg)
456        if needquote:
457            result.append('"')
458
459        for c in arg:
460            if c == '\\':
461                # Don't know if we need to double yet.
462                bs_buf.append(c)
463            elif c == '"':
464                # Double backspaces.
465                result.append('\\' * len(bs_buf)*2)
466                bs_buf = []
467                result.append('\\"')
468            else:
469                # Normal char
470                if bs_buf:
471                    result.extend(bs_buf)
472                    bs_buf = []
473                result.append(c)
474
475        # Add remaining backspaces, if any.
476        if bs_buf:
477            result.extend(bs_buf)
478
479        if needquote:
480            result.extend(bs_buf)
481            result.append('"')
482
483    return ''.join(result)
484
485
486class Popen(object):
487    def __init__(self, args, bufsize=0, executable=None,
488                 stdin=None, stdout=None, stderr=None,
489                 preexec_fn=None, close_fds=False, shell=False,
490                 cwd=None, env=None, universal_newlines=False,
491                 startupinfo=None, creationflags=0):
492        """Create new Popen instance."""
493        _cleanup()
494
495        if not isinstance(bufsize, (int, long)):
496            raise TypeError("bufsize must be an integer")
497
498        if mswindows:
499            if preexec_fn is not None:
500                raise ValueError("preexec_fn is not supported on Windows "
501                                 "platforms")
502            if close_fds:
503                raise ValueError("close_fds is not supported on Windows "
504                                 "platforms")
505        else:
506            # POSIX
507            if startupinfo is not None:
508                raise ValueError("startupinfo is only supported on Windows "
509                                 "platforms")
510            if creationflags != 0:
511                raise ValueError("creationflags is only supported on Windows "
512                                 "platforms")
513
514        self.stdin = None
515        self.stdout = None
516        self.stderr = None
517        self.pid = None
518        self.returncode = None
519        self.universal_newlines = universal_newlines
520
521        # Input and output objects. The general principle is like
522        # this:
523        #
524        # Parent                   Child
525        # ------                   -----
526        # p2cwrite   ---stdin--->  p2cread
527        # c2pread    <--stdout---  c2pwrite
528        # errread    <--stderr---  errwrite
529        #
530        # On POSIX, the child objects are file descriptors.  On
531        # Windows, these are Windows file handles.  The parent objects
532        # are file descriptors on both platforms.  The parent objects
533        # are None when not using PIPEs. The child objects are None
534        # when not redirecting.
535
536        (p2cread, p2cwrite,
537         c2pread, c2pwrite,
538         errread, errwrite) = self._get_handles(stdin, stdout, stderr)
539
540        self._execute_child(args, executable, preexec_fn, close_fds,
541                            cwd, env, universal_newlines,
542                            startupinfo, creationflags, shell,
543                            p2cread, p2cwrite,
544                            c2pread, c2pwrite,
545                            errread, errwrite)
546
547        if p2cwrite:
548            self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
549        if c2pread:
550            if universal_newlines:
551                self.stdout = os.fdopen(c2pread, 'rU', bufsize)
552            else:
553                self.stdout = os.fdopen(c2pread, 'rb', bufsize)
554        if errread:
555            if universal_newlines:
556                self.stderr = os.fdopen(errread, 'rU', bufsize)
557            else:
558                self.stderr = os.fdopen(errread, 'rb', bufsize)
559
560        _active.append(self)
561
562
563    def _translate_newlines(self, data):
564        data = data.replace("\r\n", "\n")
565        data = data.replace("\r", "\n")
566        return data
567
568
569    if mswindows:
570        #
571        # Windows methods
572        #
573        def _get_handles(self, stdin, stdout, stderr):
574            """Construct and return tupel with IO objects:
575            p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
576            """
577            if stdin == None and stdout == None and stderr == None:
578                return (None, None, None, None, None, None)
579
580            p2cread, p2cwrite = None, None
581            c2pread, c2pwrite = None, None
582            errread, errwrite = None, None
583
584            if stdin == None:
585                p2cread = GetStdHandle(STD_INPUT_HANDLE)
586            elif stdin == PIPE:
587                p2cread, p2cwrite = CreatePipe(None, 0)
588                # Detach and turn into fd
589                p2cwrite = p2cwrite.Detach()
590                p2cwrite = msvcrt.open_osfhandle(p2cwrite, 0)
591            elif type(stdin) == types.IntType:
592                p2cread = msvcrt.get_osfhandle(stdin)
593            else:
594                # Assuming file-like object
595                p2cread = msvcrt.get_osfhandle(stdin.fileno())
596            p2cread = self._make_inheritable(p2cread)
597
598            if stdout == None:
599                c2pwrite = GetStdHandle(STD_OUTPUT_HANDLE)
600            elif stdout == PIPE:
601                c2pread, c2pwrite = CreatePipe(None, 0)
602                # Detach and turn into fd
603                c2pread = c2pread.Detach()
604                c2pread = msvcrt.open_osfhandle(c2pread, 0)
605            elif type(stdout) == types.IntType:
606                c2pwrite = msvcrt.get_osfhandle(stdout)
607            else:
608                # Assuming file-like object
609                c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
610            c2pwrite = self._make_inheritable(c2pwrite)
611
612            if stderr == None:
613                errwrite = GetStdHandle(STD_ERROR_HANDLE)
614            elif stderr == PIPE:
615                errread, errwrite = CreatePipe(None, 0)
616                # Detach and turn into fd
617                errread = errread.Detach()
618                errread = msvcrt.open_osfhandle(errread, 0)
619            elif stderr == STDOUT:
620                errwrite = c2pwrite
621            elif type(stderr) == types.IntType:
622                errwrite = msvcrt.get_osfhandle(stderr)
623            else:
624                # Assuming file-like object
625                errwrite = msvcrt.get_osfhandle(stderr.fileno())
626            errwrite = self._make_inheritable(errwrite)
627
628            return (p2cread, p2cwrite,
629                    c2pread, c2pwrite,
630                    errread, errwrite)
631
632
633        def _make_inheritable(self, handle):
634            """Return a duplicate of handle, which is inheritable"""
635            return DuplicateHandle(GetCurrentProcess(), handle,
636                                   GetCurrentProcess(), 0, 1,
637                                   DUPLICATE_SAME_ACCESS)
638
639
640        def _find_w9xpopen(self):
641            """Find and return absolut path to w9xpopen.exe"""
642            w9xpopen = os.path.join(os.path.dirname(GetModuleFileName(0)),
643                                    "w9xpopen.exe")
644            if not os.path.exists(w9xpopen):
645                # Eeek - file-not-found - possibly an embedding
646                # situation - see if we can locate it in sys.exec_prefix
647                w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix),
648                                        "w9xpopen.exe")
649                if not os.path.exists(w9xpopen):
650                    raise RuntimeError("Cannot locate w9xpopen.exe, which is "
651                                       "needed for Popen to work with your "
652                                       "shell or platform.")
653            return w9xpopen
654
655
656        def _execute_child(self, args, executable, preexec_fn, close_fds,
657                           cwd, env, universal_newlines,
658                           startupinfo, creationflags, shell,
659                           p2cread, p2cwrite,
660                           c2pread, c2pwrite,
661                           errread, errwrite):
662            """Execute program (MS Windows version)"""
663
664            if not isinstance(args, types.StringTypes):
665                args = list2cmdline(args)
666
667            # Process startup details
668            default_startupinfo = STARTUPINFO()
669            if startupinfo == None:
670                startupinfo = default_startupinfo
671            if not None in (p2cread, c2pwrite, errwrite):
672                startupinfo.dwFlags |= STARTF_USESTDHANDLES
673                startupinfo.hStdInput = p2cread
674                startupinfo.hStdOutput = c2pwrite
675                startupinfo.hStdError = errwrite
676
677            if shell:
678                default_startupinfo.dwFlags |= STARTF_USESHOWWINDOW
679                default_startupinfo.wShowWindow = SW_HIDE
680                comspec = os.environ.get("COMSPEC", "cmd.exe")
681                args = comspec + " /c " + args
682                if (GetVersion() >= 0x80000000L or
683                        os.path.basename(comspec).lower() == "command.com"):
684                    # Win9x, or using command.com on NT. We need to
685                    # use the w9xpopen intermediate program. For more
686                    # information, see KB Q150956
687                    # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp)
688                    w9xpopen = self._find_w9xpopen()
689                    args = '"%s" %s' % (w9xpopen, args)
690                    # Not passing CREATE_NEW_CONSOLE has been known to
691                    # cause random failures on win9x.  Specifically a
692                    # dialog: "Your program accessed mem currently in
693                    # use at xxx" and a hopeful warning about the
694                    # stability of your system.  Cost is Ctrl+C wont
695                    # kill children.
696                    creationflags |= CREATE_NEW_CONSOLE
697
698            # Start the process
699            try:
700                hp, ht, pid, tid = CreateProcess(executable, args,
701                                         # no special security
702                                         None, None,
703                                         # must inherit handles to pass std
704                                         # handles
705                                         1,
706                                         creationflags,
707                                         env,
708                                         cwd,
709                                         startupinfo)
710            except pywintypes.error, e:
711                # Translate pywintypes.error to WindowsError, which is
712                # a subclass of OSError.  FIXME: We should really
713                # translate errno using _sys_errlist (or simliar), but
714                # how can this be done from Python?
715                raise WindowsError(*e.args)
716
717            # Retain the process handle, but close the thread handle
718            self._handle = hp
719            self.pid = pid
720            ht.Close()
721
722            # Child is launched. Close the parent's copy of those pipe
723            # handles that only the child should have open.  You need
724            # to make sure that no handles to the write end of the
725            # output pipe are maintained in this process or else the
726            # pipe will not close when the child process exits and the
727            # ReadFile will hang.
728            if p2cread != None:
729                p2cread.Close()
730            if c2pwrite != None:
731                c2pwrite.Close()
732            if errwrite != None:
733                errwrite.Close()
734
735
736        def poll(self):
737            """Check if child process has terminated.  Returns returncode
738            attribute."""
739            if self.returncode == None:
740                if WaitForSingleObject(self._handle, 0) == WAIT_OBJECT_0:
741                    self.returncode = GetExitCodeProcess(self._handle)
742                    _active.remove(self)
743            return self.returncode
744
745
746        def wait(self):
747            """Wait for child process to terminate.  Returns returncode
748            attribute."""
749            if self.returncode == None:
750                obj = WaitForSingleObject(self._handle, INFINITE)
751                self.returncode = GetExitCodeProcess(self._handle)
752                _active.remove(self)
753            return self.returncode
754
755
756        def _readerthread(self, fh, buffer):
757            buffer.append(fh.read())
758
759
760        def communicate(self, input=None):
761            """Interact with process: Send data to stdin.  Read data from
762            stdout and stderr, until end-of-file is reached.  Wait for
763            process to terminate.  The optional input argument should be a
764            string to be sent to the child process, or None, if no data
765            should be sent to the child.
766
767            communicate() returns a tuple (stdout, stderr)."""
768            stdout = None # Return
769            stderr = None # Return
770
771            if self.stdout:
772                stdout = []
773                stdout_thread = threading.Thread(target=self._readerthread,
774                                                 args=(self.stdout, stdout))
775                stdout_thread.setDaemon(True)
776                stdout_thread.start()
777            if self.stderr:
778                stderr = []
779                stderr_thread = threading.Thread(target=self._readerthread,
780                                                 args=(self.stderr, stderr))
781                stderr_thread.setDaemon(True)
782                stderr_thread.start()
783
784            if self.stdin:
785                if input != None:
786                    self.stdin.write(input)
787                self.stdin.close()
788
789            if self.stdout:
790                stdout_thread.join()
791            if self.stderr:
792                stderr_thread.join()
793
794            # All data exchanged.  Translate lists into strings.
795            if stdout != None:
796                stdout = stdout[0]
797            if stderr != None:
798                stderr = stderr[0]
799
800            # Translate newlines, if requested.  We cannot let the file
801            # object do the translation: It is based on stdio, which is
802            # impossible to combine with select (unless forcing no
803            # buffering).
804            if self.universal_newlines and hasattr(open, 'newlines'):
805                if stdout:
806                    stdout = self._translate_newlines(stdout)
807                if stderr:
808                    stderr = self._translate_newlines(stderr)
809
810            self.wait()
811            return (stdout, stderr)
812
813    else:
814        #
815        # POSIX methods
816        #
817        def _get_handles(self, stdin, stdout, stderr):
818            """Construct and return tupel with IO objects:
819            p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
820            """
821            p2cread, p2cwrite = None, None
822            c2pread, c2pwrite = None, None
823            errread, errwrite = None, None
824
825            if stdin == None:
826                pass
827            elif stdin == PIPE:
828                p2cread, p2cwrite = os.pipe()
829            elif type(stdin) == types.IntType:
830                p2cread = stdin
831            else:
832                # Assuming file-like object
833                p2cread = stdin.fileno()
834
835            if stdout == None:
836                pass
837            elif stdout == PIPE:
838                c2pread, c2pwrite = os.pipe()
839            elif type(stdout) == types.IntType:
840                c2pwrite = stdout
841            else:
842                # Assuming file-like object
843                c2pwrite = stdout.fileno()
844
845            if stderr == None:
846                pass
847            elif stderr == PIPE:
848                errread, errwrite = os.pipe()
849            elif stderr == STDOUT:
850                errwrite = c2pwrite
851            elif type(stderr) == types.IntType:
852                errwrite = stderr
853            else:
854                # Assuming file-like object
855                errwrite = stderr.fileno()
856
857            return (p2cread, p2cwrite,
858                    c2pread, c2pwrite,
859                    errread, errwrite)
860
861
862        def _set_cloexec_flag(self, fd):
863            try:
864                cloexec_flag = fcntl.FD_CLOEXEC
865            except AttributeError:
866                cloexec_flag = 1
867
868            old = fcntl.fcntl(fd, fcntl.F_GETFD)
869            fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag)
870
871
872        def _close_fds(self, but):
873            for i in range(3, MAXFD):
874                if i == but:
875                    continue
876                try:
877                    os.close(i)
878                except:
879                    pass
880
881
882        def _execute_child(self, args, executable, preexec_fn, close_fds,
883                           cwd, env, universal_newlines,
884                           startupinfo, creationflags, shell,
885                           p2cread, p2cwrite,
886                           c2pread, c2pwrite,
887                           errread, errwrite):
888            """Execute program (POSIX version)"""
889
890            if isinstance(args, types.StringTypes):
891                args = [args]
892
893            if shell:
894                args = ["/bin/sh", "-c"] + args
895
896            if executable == None:
897                executable = args[0]
898
899            # For transferring possible exec failure from child to parent
900            # The first char specifies the exception type: 0 means
901            # OSError, 1 means some other error.
902            errpipe_read, errpipe_write = os.pipe()
903            self._set_cloexec_flag(errpipe_write)
904
905            self.pid = os.fork()
906            if self.pid == 0:
907                # Child
908                try:
909                    # Close parent's pipe ends
910                    if p2cwrite:
911                        os.close(p2cwrite)
912                    if c2pread:
913                        os.close(c2pread)
914                    if errread:
915                        os.close(errread)
916                    os.close(errpipe_read)
917
918                    # Dup fds for child
919                    if p2cread:
920                        os.dup2(p2cread, 0)
921                    if c2pwrite:
922                        os.dup2(c2pwrite, 1)
923                    if errwrite:
924                        os.dup2(errwrite, 2)
925
926                    # Close pipe fds.  Make sure we doesn't close the same
927                    # fd more than once.
928                    if p2cread:
929                        os.close(p2cread)
930                    if c2pwrite and c2pwrite not in (p2cread,):
931                        os.close(c2pwrite)
932                    if errwrite and errwrite not in (p2cread, c2pwrite):
933                        os.close(errwrite)
934
935                    # Close all other fds, if asked for
936                    if close_fds:
937                        self._close_fds(but=errpipe_write)
938
939                    if cwd != None:
940                        os.chdir(cwd)
941
942                    if preexec_fn:
943                        apply(preexec_fn)
944
945                    if env == None:
946                        os.execvp(executable, args)
947                    else:
948                        os.execvpe(executable, args, env)
949
950                except:
951                    exc_type, exc_value, tb = sys.exc_info()
952                    # Save the traceback and attach it to the exception object
953                    exc_lines = traceback.format_exception(exc_type,
954                                                           exc_value,
955                                                           tb)
956                    exc_value.child_traceback = ''.join(exc_lines)
957                    os.write(errpipe_write, pickle.dumps(exc_value))
958
959                # This exitcode won't be reported to applications, so it
960                # really doesn't matter what we return.
961                os._exit(255)
962
963            # Parent
964            os.close(errpipe_write)
965            if p2cread and p2cwrite:
966                os.close(p2cread)
967            if c2pwrite and c2pread:
968                os.close(c2pwrite)
969            if errwrite and errread:
970                os.close(errwrite)
971
972            # Wait for exec to fail or succeed; possibly raising exception
973            data = os.read(errpipe_read, 1048576) # Exceptions limited to 1 MB
974            os.close(errpipe_read)
975            if data != "":
976                os.waitpid(self.pid, 0)
977                child_exception = pickle.loads(data)
978                raise child_exception
979
980
981        def _handle_exitstatus(self, sts):
982            if os.WIFSIGNALED(sts):
983                self.returncode = -os.WTERMSIG(sts)
984            elif os.WIFEXITED(sts):
985                self.returncode = os.WEXITSTATUS(sts)
986            else:
987                # Should never happen
988                raise RuntimeError("Unknown child exit status!")
989
990            _active.remove(self)
991
992
993        def poll(self):
994            """Check if child process has terminated.  Returns returncode
995            attribute."""
996            if self.returncode == None:
997                try:
998                    pid, sts = os.waitpid(self.pid, os.WNOHANG)
999                    if pid == self.pid:
1000                        self._handle_exitstatus(sts)
1001                except os.error:
1002                    pass
1003            return self.returncode
1004
1005
1006        def wait(self):
1007            """Wait for child process to terminate.  Returns returncode
1008            attribute."""
1009            if self.returncode == None:
1010                pid, sts = os.waitpid(self.pid, 0)
1011                self._handle_exitstatus(sts)
1012            return self.returncode
1013
1014
1015        def communicate(self, input=None):
1016            """Interact with process: Send data to stdin.  Read data from
1017            stdout and stderr, until end-of-file is reached.  Wait for
1018            process to terminate.  The optional input argument should be a
1019            string to be sent to the child process, or None, if no data
1020            should be sent to the child.
1021
1022            communicate() returns a tuple (stdout, stderr)."""
1023            read_set = []
1024            write_set = []
1025            stdout = None # Return
1026            stderr = None # Return
1027
1028            if self.stdin:
1029                # Flush stdio buffer.  This might block, if the user has
1030                # been writing to .stdin in an uncontrolled fashion.
1031                self.stdin.flush()
1032                if input:
1033                    write_set.append(self.stdin)
1034                else:
1035                    self.stdin.close()
1036            if self.stdout:
1037                read_set.append(self.stdout)
1038                stdout = []
1039            if self.stderr:
1040                read_set.append(self.stderr)
1041                stderr = []
1042
1043            while read_set or write_set:
1044                rlist, wlist, xlist = select.select(read_set, write_set, [])
1045
1046                if self.stdin in wlist:
1047                    # When select has indicated that the file is writable,
1048                    # we can write up to PIPE_BUF bytes without risk
1049                    # blocking.  POSIX defines PIPE_BUF >= 512
1050                    bytes_written = os.write(self.stdin.fileno(), input[:512])
1051                    input = input[bytes_written:]
1052                    if not input:
1053                        self.stdin.close()
1054                        write_set.remove(self.stdin)
1055
1056                if self.stdout in rlist:
1057                    data = os.read(self.stdout.fileno(), 1024)
1058                    if data == "":
1059                        self.stdout.close()
1060                        read_set.remove(self.stdout)
1061                    stdout.append(data)
1062
1063                if self.stderr in rlist:
1064                    data = os.read(self.stderr.fileno(), 1024)
1065                    if data == "":
1066                        self.stderr.close()
1067                        read_set.remove(self.stderr)
1068                    stderr.append(data)
1069
1070            # All data exchanged.  Translate lists into strings.
1071            if stdout != None:
1072                stdout = ''.join(stdout)
1073            if stderr != None:
1074                stderr = ''.join(stderr)
1075
1076            # Translate newlines, if requested.  We cannot let the file
1077            # object do the translation: It is based on stdio, which is
1078            # impossible to combine with select (unless forcing no
1079            # buffering).
1080            if self.universal_newlines and hasattr(open, 'newlines'):
1081                if stdout:
1082                    stdout = self._translate_newlines(stdout)
1083                if stderr:
1084                    stderr = self._translate_newlines(stderr)
1085
1086            self.wait()
1087            return (stdout, stderr)
1088
1089
1090def _demo_posix():
1091    #
1092    # Example 1: Simple redirection: Get process list
1093    #
1094    plist = Popen(["ps"], stdout=PIPE).communicate()[0]
1095    print "Process list:"
1096    print plist
1097
1098    #
1099    # Example 2: Change uid before executing child
1100    #
1101    if os.getuid() == 0:
1102        p = Popen(["id"], preexec_fn=lambda: os.setuid(100))
1103        p.wait()
1104
1105    #
1106    # Example 3: Connecting several subprocesses
1107    #
1108    print "Looking for 'hda'..."
1109    p1 = Popen(["dmesg"], stdout=PIPE)
1110    p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
1111    print repr(p2.communicate()[0])
1112
1113    #
1114    # Example 4: Catch execution error
1115    #
1116    print
1117    print "Trying a weird file..."
1118    try:
1119        print Popen(["/this/path/does/not/exist"]).communicate()
1120    except OSError, e:
1121        if e.errno == errno.ENOENT:
1122            print "The file didn't exist.  I thought so..."
1123            print "Child traceback:"
1124            print e.child_traceback
1125        else:
1126            print "Error", e.errno
1127    else:
1128        print >>sys.stderr, "Gosh.  No error."
1129
1130
1131def _demo_windows():
1132    #
1133    # Example 1: Connecting several subprocesses
1134    #
1135    print "Looking for 'PROMPT' in set output..."
1136    p1 = Popen("set", stdout=PIPE, shell=True)
1137    p2 = Popen('find "PROMPT"', stdin=p1.stdout, stdout=PIPE)
1138    print repr(p2.communicate()[0])
1139
1140    #
1141    # Example 2: Simple execution of program
1142    #
1143    print "Executing calc..."
1144    p = Popen("calc")
1145    p.wait()
1146
1147
1148if __name__ == "__main__":
1149    if mswindows:
1150        _demo_windows()
1151    else:
1152        _demo_posix()
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。