File: popen.py

package info (click to toggle)
viewcvs 0.9.2%2Bcvs.1.0.dev.2004.07.28-4.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,452 kB
  • ctags: 1,355
  • sloc: python: 10,100; cpp: 840; ansic: 763; yacc: 526; sh: 163; makefile: 115
file content (363 lines) | stat: -rw-r--r-- 10,135 bytes parent folder | download | duplicates (3)
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
#
# Copyright (C) 2000-2002 The ViewCVS Group. All Rights Reserved.
#
# By using this file, you agree to the terms and conditions set forth in
# the LICENSE.html file which can be found at the top level of the ViewCVS
# distribution or at http://viewcvs.sourceforge.net/license-1.html.
#
# Contact information:
#   Greg Stein, PO Box 760, Palo Alto, CA, 94302
#   gstein@lyra.org, http://viewcvs.sourceforge.net/
#
# -----------------------------------------------------------------------
#
# popen.py: a replacement for os.popen()
#
# This implementation of popen() provides a cmd + args calling sequence,
# rather than a system() type of convention. The shell facilities are not
# available, but that implies we can avoid worrying about shell hacks in
# the arguments.
#
# -----------------------------------------------------------------------
#

import os
import sys
import sapi
import threading
import string

if sys.platform == "win32":
  import win32popen
  import win32event
  import win32process
  import debug
  import StringIO

def popen(cmd, args, mode, capture_err=1):
  if sys.platform == "win32":
    command = win32popen.CommandLine(cmd, args)

    #sapi.server.header()
    #debug.PrintStackTrace(command)

    if mode.find('r') >= 0:
      hStdIn = None

      if debug.SHOW_CHILD_PROCESSES:
        dbgIn, dbgOut = None, StringIO.StringIO()

        handle, hStdOut = win32popen.MakeSpyPipe(0, 1, (dbgOut,))

        if capture_err:
          hStdErr = hStdOut
          dbgErr = dbgOut
        else:
          dbgErr = StringIO.StringIO()
          x, hStdErr = win32popen.MakeSpyPipe(None, 1, (dbgErr,))
      else:
        handle, hStdOut = win32popen.CreatePipe(0, 1)
        if capture_err:
          hStdErr = hStdOut
        else:
          hStdErr = win32popen.NullFile(1)

    else:
      if debug.SHOW_CHILD_PROCESSES:
        dbgIn, dbgOut, dbgErr = StringIO.StringIO(), StringIO.StringIO(), StringIO.StringIO()
        hStdIn, handle = win32popen.MakeSpyPipe(1, 0, (dbgIn,))
        x, hStdOut = win32popen.MakeSpyPipe(None, 1, (dbgOut,))
        x, hStdErr = win32popen.MakeSpyPipe(None, 1, (dbgErr,))
      else:
        hStdIn, handle = win32popen.CreatePipe(0, 1)
        hStdOut = None
        hStdErr = None

    phandle, pid, thandle, tid = win32popen.CreateProcess(command, hStdIn, hStdOut, hStdErr)

    if debug.SHOW_CHILD_PROCESSES:
      debug.Process(command, dbgIn, dbgOut, dbgErr)

    return _pipe(win32popen.File2FileObject(handle, mode), phandle)

  # flush the stdio buffers since we are about to change the FD under them
  sys.stdout.flush()
  sys.stderr.flush()

  r, w = os.pipe()
  pid = os.fork()
  if pid:
    # in the parent

    # close the descriptor that we don't need and return the other one.
    if string.find(mode, 'r') >= 0:
      os.close(w)
      return _pipe(os.fdopen(r, mode), pid)
    os.close(r)
    return _pipe(os.fdopen(w, mode), pid)

  # in the child

  # we'll need /dev/null for the discarded I/O
  null = os.open('/dev/null', os.O_RDWR)

  if string.find(mode, 'r') >= 0:
    # hook stdout/stderr to the "write" channel
    os.dup2(w, 1)
    # "close" stdin; the child shouldn't use it
    ### this isn't quite right... we may want the child to read from stdin
    os.dup2(null, 0)
    # what to do with errors?
    if capture_err:
      os.dup2(w, 2)
    else:
      os.dup2(null, 2)
  else:
    # hook stdin to the "read" channel
    os.dup2(r, 0)
    # "close" stdout/stderr; the child shouldn't use them
    ### this isn't quite right... we may want the child to write to these
    os.dup2(null, 1)
    os.dup2(null, 2)

  # don't need these FDs any more
  os.close(null)
  os.close(r)
  os.close(w)

  # the stdin/stdout/stderr are all set up. exec the target
  try:
    os.execvp(cmd, (cmd,) + tuple(args))
  except:
    # aid debugging, if the os.execvp above fails for some reason:
    print "<h2>exec failed:</h2><pre>", cmd, string.join(args), "</pre>"
    raise

  # crap. shouldn't be here.
  sys.exit(127)

def pipe_cmds(cmds):
  """Executes a sequence of commands. The output of each command is directed to
  the input of the next command. A _pipe object is returned for writing to the
  first command's input. The output of the last command is directed to the
  standard out. On windows, if sys.stdout is not an inheritable file handle
  (i.e. it is not possible to direct the standard out of a child process to
  it), then a separate thread will be spawned to spool output to
  sys.stdout.write(). In all cases, the pipe_cmds() caller should refrain
  from writing to the standard out until the last process has terminated.
  """
  if sys.platform == "win32":

    if debug.SHOW_CHILD_PROCESSES:
      dbgIn = StringIO.StringIO()
      hStdIn, handle = win32popen.MakeSpyPipe(1, 0, (dbgIn,))

      i = 0
      for cmd in cmds:
        i = i + 1

        dbgOut, dbgErr = StringIO.StringIO(), StringIO.StringIO()

        if i < len(cmds):
          nextStdIn, hStdOut = win32popen.MakeSpyPipe(1, 1, (dbgOut,))
          x, hStdErr = win32popen.MakeSpyPipe(None, 1, (dbgErr,))
        else:
          ehandle = win32event.CreateEvent(None, 1, 0, None)
          nextStdIn, hStdOut = win32popen.MakeSpyPipe(None, 1, (dbgOut, sapi.server.file()), ehandle)
          x, hStdErr = win32popen.MakeSpyPipe(None, 1, (dbgErr,))

        command = win32popen.CommandLine(cmd[0], cmd[1:])
        phandle, pid, thandle, tid = win32popen.CreateProcess(command, hStdIn, hStdOut, hStdErr)
        if debug.SHOW_CHILD_PROCESSES:
          debug.Process(command, dbgIn, dbgOut, dbgErr)

        dbgIn = dbgOut
        hStdIn = nextStdIn


    else:

      hStdIn, handle = win32popen.CreatePipe(1, 0)
      spool = None

      i = 0
      for cmd in cmds:
        i = i + 1
        if i < len(cmds):
          nextStdIn, hStdOut = win32popen.CreatePipe(1, 1)
        else:
          # very last process
          nextStdIn = None

          if sapi.server.inheritableOut:
            # send child output to standard out
            hStdOut = win32popen.MakeInheritedHandle(win32popen.FileObject2File(sys.stdout),0)
            ehandle = None
          else:
            ehandle = win32event.CreateEvent(None, 1, 0, None)
            x, hStdOut = win32popen.MakeSpyPipe(None, 1, (sapi.server.file(),), ehandle)

        command = win32popen.CommandLine(cmd[0], cmd[1:])
        phandle, pid, thandle, tid = win32popen.CreateProcess(command, hStdIn, hStdOut, None)
        hStdIn = nextStdIn

    return _pipe(win32popen.File2FileObject(handle, 'wb'), phandle, ehandle)

  # flush the stdio buffers since we are about to change the FD under them
  sys.stdout.flush()
  sys.stderr.flush()

  prev_r, parent_w = os.pipe()

  null = os.open('/dev/null', os.O_RDWR)

  for cmd in cmds[:-1]:
    r, w = os.pipe()
    pid = os.fork()
    if not pid:
      # in the child

      # hook up stdin to the "read" channel
      os.dup2(prev_r, 0)

      # hook up stdout to the output channel
      os.dup2(w, 1)

      # toss errors
      os.dup2(null, 2)

      # close these extra descriptors
      os.close(prev_r)
      os.close(parent_w)
      os.close(null)
      os.close(r)
      os.close(w)

      # time to run the command
      try:
        os.execvp(cmd[0], cmd)
      except:
        pass

      sys.exit(127)

    # in the parent

    # we don't need these any more
    os.close(prev_r)
    os.close(w)

    # the read channel of this pipe will feed into to the next command
    prev_r = r

  # no longer needed
  os.close(null)

  # done with most of the commands. set up the last command to write to stdout
  if not sapi.server.inheritableOut:
    r, w = os.pipe()

  pid = os.fork()
  if not pid:
    # in the child (the last command)

    # hook up stdin to the "read" channel
    os.dup2(prev_r, 0)

    if not sapi.server.inheritableOut:
      os.dup2(w, 1)
      os.close(r)
      os.close(w)

    # close these extra descriptors
    os.close(prev_r)
    os.close(parent_w)

    # run the last command
    try:
      os.execvp(cmds[-1][0], cmds[-1])
    except:
      pass

    sys.exit(127)

  # not needed any more
  os.close(prev_r)

  if not sapi.server.inheritableOut:
    os.close(w)
    thread = _copy(r, sapi.server.file())
    thread.start()
  else:
    thread = None

  # write into the first pipe, wait on the final process
  return _pipe(os.fdopen(parent_w, 'w'), pid, thread=thread)

class _copy(threading.Thread):
  def __init__(self, srcfd, destfile):
    self.srcfd = srcfd
    self.destfile = destfile
    threading.Thread.__init__(self)

  def run(self):
    try:
      while 1:
        s = os.read(self.srcfd, 1024)
        if not s:
          break
        self.destfile.write(s)
    finally:
      os.close(self.srcfd)

class _pipe:
  "Wrapper for a file which can wait() on a child process at close time."

  def __init__(self, file, child_pid, done_event = None, thread = None):
    self.file = file
    self.child_pid = child_pid
    if sys.platform == "win32":
      if done_event:
        self.wait_for = (child_pid, done_event)
      else:
        self.wait_for = (child_pid,)
    else:
      self.thread = thread

  def eof(self):
    if sys.platform == "win32":
      r = win32event.WaitForMultipleObjects(self.wait_for, 1, 0)
      if r == win32event.WAIT_OBJECT_0:
        self.file.close()
        self.file = None
        return win32process.GetExitCodeProcess(self.child_pid)
      return None

    if self.thread and self.thread.isAlive():
      return None

    pid, status = os.waitpid(self.child_pid, os.WNOHANG)
    if pid:
      self.file.close()
      self.file = None
      return status
    return None

  def close(self):
    if self.file:
      self.file.close()
      self.file = None
      if sys.platform == "win32":
        win32event.WaitForMultipleObjects(self.wait_for, 1, win32event.INFINITE)
        return win32process.GetExitCodeProcess(self.child_pid)
      else:
        if self.thread:
          self.thread.join()
        return os.waitpid(self.child_pid, 0)[1]
    return None

  def __getattr__(self, name):
    return getattr(self.file, name)

  def __del__(self):
    self.close()