David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | # 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 | |
| 5 | import os |
| 6 | |
| 7 | def 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 Riley | 21afd01 | 2000-09-24 06:28:47 +0000 | [diff] [blame] | 16 | if hasattr(os, 'fork'): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 17 | |
| 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 Riley | 21afd01 | 2000-09-24 06:28:47 +0000 | [diff] [blame] | 37 | elif 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): |
| 45 | nargs = [bin] |
| 46 | for arg in args: |
| 47 | nargs.append( '"'+arg+'"' ) |
| 48 | os.spawnv( os.P_NOWAIT, bin, nargs ) |
| 49 | |
| 50 | def kill_zombies(): pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 51 | |
| 52 | else: |
| 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().' |