home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / spawn.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  5.5 KB  |  179 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.spawn
  5.  
  6. Provides the 'spawn()' function, a front-end to various platform-
  7. specific functions for launching another program in a sub-process.
  8. Also provides the 'find_executable()' to search the path for a given
  9. executable name.
  10. """
  11. __revision__ = '$Id: spawn.py 37828 2004-11-10 22:23:15Z loewis $'
  12. import sys
  13. import os
  14. import string
  15. from distutils.errors import *
  16. from distutils import log
  17.  
  18. def spawn(cmd, search_path = 1, verbose = 0, dry_run = 0):
  19.     """Run another program, specified as a command list 'cmd', in a new
  20.     process.  'cmd' is just the argument list for the new process, ie.
  21.     cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
  22.     There is no way to run a program with a name different from that of its
  23.     executable.
  24.  
  25.     If 'search_path' is true (the default), the system's executable
  26.     search path will be used to find the program; otherwise, cmd[0]
  27.     must be the exact path to the executable.  If 'dry_run' is true,
  28.     the command will not actually be run.
  29.  
  30.     Raise DistutilsExecError if running the program fails in any way; just
  31.     return on success.
  32.     """
  33.     if os.name == 'posix':
  34.         _spawn_posix(cmd, search_path, dry_run = dry_run)
  35.     elif os.name == 'nt':
  36.         _spawn_nt(cmd, search_path, dry_run = dry_run)
  37.     elif os.name == 'os2':
  38.         _spawn_os2(cmd, search_path, dry_run = dry_run)
  39.     else:
  40.         raise DistutilsPlatformError, "don't know how to spawn programs on platform '%s'" % os.name
  41.     return os.name == 'posix'
  42.  
  43.  
  44. def _nt_quote_args(args):
  45.     '''Quote command-line arguments for DOS/Windows conventions: just
  46.     wraps every argument which contains blanks in double quotes, and
  47.     returns a new argument list.
  48.     '''
  49.     for i in range(len(args)):
  50.         if string.find(args[i], ' ') != -1:
  51.             args[i] = '"%s"' % args[i]
  52.             continue
  53.     
  54.     return args
  55.  
  56.  
  57. def _spawn_nt(cmd, search_path = 1, verbose = 0, dry_run = 0):
  58.     executable = cmd[0]
  59.     cmd = _nt_quote_args(cmd)
  60.     if search_path:
  61.         if not find_executable(executable):
  62.             pass
  63.         executable = executable
  64.     
  65.     log.info(string.join([
  66.         executable] + cmd[1:], ' '))
  67.     if not dry_run:
  68.         
  69.         try:
  70.             rc = os.spawnv(os.P_WAIT, executable, cmd)
  71.         except OSError:
  72.             exc = None
  73.             raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1])
  74.  
  75.         if rc != 0:
  76.             raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], rc)
  77.         rc != 0
  78.     
  79.  
  80.  
  81. def _spawn_os2(cmd, search_path = 1, verbose = 0, dry_run = 0):
  82.     executable = cmd[0]
  83.     if search_path:
  84.         if not find_executable(executable):
  85.             pass
  86.         executable = executable
  87.     
  88.     log.info(string.join([
  89.         executable] + cmd[1:], ' '))
  90.     if not dry_run:
  91.         
  92.         try:
  93.             rc = os.spawnv(os.P_WAIT, executable, cmd)
  94.         except OSError:
  95.             exc = None
  96.             raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1])
  97.  
  98.         if rc != 0:
  99.             print "command '%s' failed with exit status %d" % (cmd[0], rc)
  100.             raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], rc)
  101.         rc != 0
  102.     
  103.  
  104.  
  105. def _spawn_posix(cmd, search_path = 1, verbose = 0, dry_run = 0):
  106.     log.info(string.join(cmd, ' '))
  107.     if dry_run:
  108.         return None
  109.     if not search_path or os.execvp:
  110.         pass
  111.     exec_fn = os.execv
  112.     pid = os.fork()
  113.     if pid == 0:
  114.         
  115.         try:
  116.             exec_fn(cmd[0], cmd)
  117.         except OSError:
  118.             dry_run
  119.             e = dry_run
  120.             sys.stderr.write('unable to execute %s: %s\n' % (cmd[0], e.strerror))
  121.             os._exit(1)
  122.         except:
  123.             dry_run
  124.  
  125.         sys.stderr.write('unable to execute %s for unknown reasons' % cmd[0])
  126.         os._exit(1)
  127.     else:
  128.         while None:
  129.             
  130.             try:
  131.                 (pid, status) = os.waitpid(pid, 0)
  132.             except OSError:
  133.                 exc = None
  134.                 import errno
  135.                 if exc.errno == errno.EINTR:
  136.                     continue
  137.                 
  138.                 raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1])
  139.  
  140.             if os.WIFSIGNALED(status):
  141.                 raise DistutilsExecError, "command '%s' terminated by signal %d" % (cmd[0], os.WTERMSIG(status))
  142.             if os.WIFEXITED(status):
  143.                 exit_status = os.WEXITSTATUS(status)
  144.                 if exit_status == 0:
  145.                     return None
  146.                 raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], exit_status)
  147.             os.WIFEXITED(status)
  148.             if os.WIFSTOPPED(status):
  149.                 continue
  150.                 continue
  151.             raise DistutilsExecError, "unknown error executing '%s': termination status %d" % (cmd[0], status)
  152.             continue
  153.             return None
  154.  
  155.  
  156. def find_executable(executable, path = None):
  157.     """Try to find 'executable' in the directories listed in 'path' (a
  158.     string listing directories separated by 'os.pathsep'; defaults to
  159.     os.environ['PATH']).  Returns the complete filename or None if not
  160.     found.
  161.     """
  162.     if path is None:
  163.         path = os.environ['PATH']
  164.     
  165.     paths = string.split(path, os.pathsep)
  166.     (base, ext) = os.path.splitext(executable)
  167.     if (sys.platform == 'win32' or os.name == 'os2') and ext != '.exe':
  168.         executable = executable + '.exe'
  169.     
  170.     if not os.path.isfile(executable):
  171.         for p in paths:
  172.             f = os.path.join(p, executable)
  173.             if os.path.isfile(f):
  174.                 return f
  175.         
  176.         return None
  177.     return executable
  178.  
  179.