blob: be8fdf7772751964620d0c8441937dbc46ff3f2d [file] [log] [blame]
David Scherer7aced172000-08-15 01:13:23 +00001# spawn - This is ugly, OS-specific code to spawn a separate process. It
2# also defines a function for getting the version of a path most
3# likely to work with cranky API functions.
4
5import os
6
7def hardpath(path):
8 path = os.path.normcase(os.path.abspath(path))
9 try:
10 import win32api
11 path = win32api.GetShortPathName( path )
12 except:
13 pass
14 return path
15
Nicholas Riley21afd012000-09-24 06:28:47 +000016if hasattr(os, 'fork'):
David Scherer7aced172000-08-15 01:13:23 +000017
18 # UNIX-ish operating system: we fork() and exec(), and we have to track
19 # the pids of our children and call waitpid() on them to avoid leaving
20 # zombies in the process table. kill_zombies() does the dirty work, and
21 # should be called periodically.
22
23 zombies = []
24
25 def spawn(bin, *args):
26 pid = os.fork()
27 if pid:
28 zombies.append(pid)
29 else:
30 os.execv( bin, (bin, ) + args )
31
32 def kill_zombies():
33 for z in zombies[:]:
34 stat = os.waitpid(z, os.WNOHANG)
35 if stat[0]==z:
36 zombies.remove(z)
Nicholas Riley21afd012000-09-24 06:28:47 +000037elif hasattr(os, 'spawnv'):
38
39 # Windows-ish OS: we use spawnv(), and stick quotes around arguments
40 # in case they contains spaces, since Windows will jam all the
41 # arguments to spawn() or exec() together into one string. The
42 # kill_zombies function is a noop.
43
44 def spawn(bin, *args):
Steven M. Gava4eb28682001-10-07 11:26:48 +000045 nargs = ['"'+bin+'"']
Nicholas Riley21afd012000-09-24 06:28:47 +000046 for arg in args:
47 nargs.append( '"'+arg+'"' )
48 os.spawnv( os.P_NOWAIT, bin, nargs )
49
50 def kill_zombies(): pass
David Scherer7aced172000-08-15 01:13:23 +000051
52else:
53 # If you get here, you may be able to write an alternative implementation
54 # of these functions for your OS.
55
56 def kill_zombies(): pass
57
58 raise OSError, 'This OS does not support fork() or spawnv().'