home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / iria107a.lzh / script / os.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-11-17  |  18.2 KB  |  594 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.0)
  3.  
  4. """OS routines for Mac, DOS, NT, or Posix depending on what system we're on.
  5.  
  6. This exports:
  7.   - all functions from posix, nt, dos, os2, mac, or ce, e.g. unlink, stat, etc.
  8.   - os.path is one of the modules posixpath, ntpath, macpath, or dospath
  9.   - os.name is 'posix', 'nt', 'dos', 'os2', 'mac', or 'ce'
  10.   - os.curdir is a string representing the current directory ('.' or ':')
  11.   - os.pardir is a string representing the parent directory ('..' or '::')
  12.   - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
  13.   - os.altsep is the alternate pathname separator (None or '/')
  14.   - os.pathsep is the component separator used in $PATH etc
  15.   - os.linesep is the line separator in text files ('\r' or '
  16. ' or '\r
  17. ')
  18.   - os.defpath is the default search path for executables
  19.  
  20. Programs that import and use 'os' stand a better chance of being
  21. portable between different platforms.  Of course, they must then
  22. only use functions that are defined by all platforms (e.g., unlink
  23. and opendir), and leave all pathname manipulation to os.path
  24. (e.g., split and join).
  25. """
  26. import sys
  27. _names = sys.builtin_module_names
  28. altsep = None
  29. if 'posix' in _names:
  30.     name = 'posix'
  31.     linesep = '\n'
  32.     curdir = '.'
  33.     pardir = '..'
  34.     sep = '/'
  35.     pathsep = ':'
  36.     defpath = ':/bin:/usr/bin'
  37.     from posix import *
  38.     
  39.     try:
  40.         from posix import _exit
  41.     except ImportError:
  42.         pass
  43.  
  44.     import posixpath
  45.     path = posixpath
  46.     del posixpath
  47. elif 'nt' in _names:
  48.     name = 'nt'
  49.     linesep = '\r\n'
  50.     curdir = '.'
  51.     pardir = '..'
  52.     sep = '\\'
  53.     pathsep = ';'
  54.     defpath = '.;C:\\bin'
  55.     from nt import *
  56.     for i in [
  57.         '_exit']:
  58.         
  59.         try:
  60.             exec 'from nt import ' + i
  61.         except ImportError:
  62.             0
  63.             0
  64.             [
  65.                 '_exit']
  66.         except:
  67.             0
  68.  
  69.     
  70.     import ntpath
  71.     path = ntpath
  72.     del ntpath
  73. elif 'dos' in _names:
  74.     name = 'dos'
  75.     linesep = '\r\n'
  76.     curdir = '.'
  77.     pardir = '..'
  78.     sep = '\\'
  79.     pathsep = ';'
  80.     defpath = '.;C:\\bin'
  81.     from dos import *
  82.     
  83.     try:
  84.         from dos import _exit
  85.     except ImportError:
  86.         [
  87.             '_exit']
  88.         [
  89.             '_exit']
  90.     except:
  91.         [
  92.             '_exit']
  93.  
  94.     import dospath
  95.     path = dospath
  96.     del dospath
  97. elif 'os2' in _names:
  98.     name = 'os2'
  99.     linesep = '\r\n'
  100.     curdir = '.'
  101.     pardir = '..'
  102.     sep = '\\'
  103.     pathsep = ';'
  104.     defpath = '.;C:\\bin'
  105.     from os2 import *
  106.     
  107.     try:
  108.         from os2 import _exit
  109.     except ImportError:
  110.         pass
  111.  
  112.     import ntpath
  113.     path = ntpath
  114.     del ntpath
  115. elif 'mac' in _names:
  116.     name = 'mac'
  117.     linesep = '\r'
  118.     curdir = ':'
  119.     pardir = '::'
  120.     sep = ':'
  121.     pathsep = '\n'
  122.     defpath = ':'
  123.     from mac import *
  124.     
  125.     try:
  126.         from mac import _exit
  127.     except ImportError:
  128.         pass
  129.  
  130.     import macpath
  131.     path = macpath
  132.     del macpath
  133. elif 'ce' in _names:
  134.     name = 'ce'
  135.     linesep = '\r\n'
  136.     curdir = '.'
  137.     pardir = '..'
  138.     sep = '\\'
  139.     pathsep = ';'
  140.     defpath = '\\Windows'
  141.     from ce import *
  142.     for i in [
  143.         '_exit']:
  144.         
  145.         try:
  146.             exec 'from ce import ' + i
  147.         except ImportError:
  148.             0
  149.             0
  150.             [
  151.                 '_exit']
  152.         except:
  153.             0
  154.  
  155.     
  156.     import ntpath
  157.     path = ntpath
  158.     del ntpath
  159. else:
  160.     raise ImportError, 'no os specific module found'
  161. del _names
  162. sys.modules['os.path'] = path
  163.  
  164. def makedirs(name, mode = 511):
  165.     '''makedirs(path [, mode=0777]) -> None
  166.  
  167.     Super-mkdir; create a leaf directory and all intermediate ones.
  168.     Works like mkdir, except that any intermediate path segment (not
  169.     just the rightmost) will be created if it does not exist.  This is
  170.     recursive.
  171.  
  172.     '''
  173.     (head, tail) = path.split(name)
  174.     if not tail:
  175.         (head, tail) = path.split(head)
  176.     
  177.     if head and tail and not path.exists(head):
  178.         makedirs(head, mode)
  179.     
  180.     mkdir(name, mode)
  181.  
  182.  
  183. def removedirs(name):
  184.     '''removedirs(path) -> None
  185.  
  186.     Super-rmdir; remove a leaf directory and empty all intermediate
  187.     ones.  Works like rmdir except that, if the leaf directory is
  188.     successfully removed, directories corresponding to rightmost path
  189.     segments will be pruned way until either the whole path is
  190.     consumed or an error occurs.  Errors during this latter phase are
  191.     ignored -- they generally mean that a directory was not empty.
  192.  
  193.     '''
  194.     rmdir(name)
  195.     (head, tail) = path.split(name)
  196.     if not tail:
  197.         (head, tail) = path.split(head)
  198.     
  199.     while head and tail:
  200.         
  201.         try:
  202.             rmdir(head)
  203.         except error:
  204.             break
  205.  
  206.         (head, tail) = path.split(head)
  207.  
  208.  
  209. def renames(old, new):
  210.     '''renames(old, new) -> None
  211.  
  212.     Super-rename; create directories as necessary and delete any left
  213.     empty.  Works like rename, except creation of any intermediate
  214.     directories needed to make the new pathname good is attempted
  215.     first.  After the rename, directories corresponding to rightmost
  216.     path segments of the old name will be pruned way until either the
  217.     whole path is consumed or a nonempty directory is found.
  218.  
  219.     Note: this function can fail with the new directory structure made
  220.     if you lack permissions needed to unlink the leaf directory or
  221.     file.
  222.  
  223.     '''
  224.     (head, tail) = path.split(new)
  225.     if head and tail and not path.exists(head):
  226.         makedirs(head)
  227.     
  228.     rename(old, new)
  229.     (head, tail) = path.split(old)
  230.     if head and tail:
  231.         
  232.         try:
  233.             removedirs(head)
  234.         except error:
  235.             pass
  236.  
  237.     
  238.  
  239.  
  240. try:
  241.     environ
  242. except NameError:
  243.     [
  244.         '_exit']
  245.     [
  246.         '_exit']
  247.     environ = { }
  248. except:
  249.     [
  250.         '_exit']
  251.  
  252.  
  253. def execl(file, *args):
  254.     '''execl(file, *args)
  255.  
  256.     Execute the executable file with argument list args, replacing the
  257.     current process. '''
  258.     execv(file, args)
  259.  
  260.  
  261. def execle(file, *args):
  262.     '''execle(file, *args, env)
  263.  
  264.     Execute the executable file with argument list args and
  265.     environment env, replacing the current process. '''
  266.     env = args[-1]
  267.     execve(file, args[:-1], env)
  268.  
  269.  
  270. def execlp(file, *args):
  271.     '''execlp(file, *args)
  272.  
  273.     Execute the executable file (which is searched for along $PATH)
  274.     with argument list args, replacing the current process. '''
  275.     execvp(file, args)
  276.  
  277.  
  278. def execlpe(file, *args):
  279.     '''execlpe(file, *args, env)
  280.  
  281.     Execute the executable file (which is searched for along $PATH)
  282.     with argument list args and environment env, replacing the current
  283.     process. '''
  284.     env = args[-1]
  285.     execvpe(file, args[:-1], env)
  286.  
  287.  
  288. def execvp(file, args):
  289.     '''execp(file, args)
  290.  
  291.     Execute the executable file (which is searched for along $PATH)
  292.     with argument list args, replacing the current process.
  293.     args may be a list or tuple of strings. '''
  294.     _execvpe(file, args)
  295.  
  296.  
  297. def execvpe(file, args, env):
  298.     '''execv(file, args, env)
  299.  
  300.     Execute the executable file (which is searched for along $PATH)
  301.     with argument list args and environment env , replacing the
  302.     current process.
  303.     args may be a list or tuple of strings. '''
  304.     _execvpe(file, args, env)
  305.  
  306. _notfound = None
  307.  
  308. def _execvpe(file, args, env = None):
  309.     global _notfound
  310.     if env is not None:
  311.         func = execve
  312.         argrest = (args, env)
  313.     else:
  314.         func = execv
  315.         argrest = (args,)
  316.         env = environ
  317.     (head, tail) = path.split(file)
  318.     if head:
  319.         apply(func, (file,) + argrest)
  320.         return None
  321.     
  322.     if env.has_key('PATH'):
  323.         envpath = env['PATH']
  324.     else:
  325.         envpath = defpath
  326.     PATH = envpath.split(pathsep)
  327.     if not _notfound:
  328.         import tempfile
  329.         
  330.         try:
  331.             execv(tempfile.mktemp(), ('blah',))
  332.         except error:
  333.             _notfound = None
  334.  
  335.     
  336.     (exc, arg) = (error, _notfound)
  337.     for dir in PATH:
  338.         fullname = path.join(dir, file)
  339.         
  340.         try:
  341.             apply(func, (fullname,) + argrest)
  342.         except error:
  343.             0
  344.             (errno, msg) = 0
  345.             PATH
  346.             if errno != arg[0]:
  347.                 (exc, arg) = (error, (errno, msg))
  348.             
  349.         except:
  350.             errno != arg[0]
  351.  
  352.     
  353.     raise exc, arg
  354.  
  355.  
  356. try:
  357.     putenv
  358. except NameError:
  359.     [
  360.         '_exit']
  361.     [
  362.         '_exit']
  363. except:
  364.     [
  365.         '_exit']
  366.  
  367. import UserDict
  368. if name in ('os2', 'nt', 'dos'):
  369.     
  370.     class _Environ(UserDict.UserDict):
  371.         
  372.         def __init__(self, environ):
  373.             UserDict.UserDict.__init__(self)
  374.             data = self.data
  375.             for k, v in environ.items():
  376.                 data[k.upper()] = v
  377.             
  378.  
  379.         
  380.         def __setitem__(self, key, item):
  381.             putenv(key, item)
  382.             self.data[key.upper()] = item
  383.  
  384.         
  385.         def __getitem__(self, key):
  386.             return self.data[key.upper()]
  387.  
  388.         
  389.         def __delitem__(self, key):
  390.             del self.data[key.upper()]
  391.  
  392.         
  393.         def has_key(self, key):
  394.             return self.data.has_key(key.upper())
  395.  
  396.         
  397.         def get(self, key, failobj = None):
  398.             return self.data.get(key.upper(), failobj)
  399.  
  400.         
  401.         def update(self, dict):
  402.             for k, v in dict.items():
  403.                 self[k] = v
  404.             
  405.  
  406.  
  407. else:
  408.     
  409.     class _Environ(UserDict.UserDict):
  410.         
  411.         def __init__(self, environ):
  412.             UserDict.UserDict.__init__(self)
  413.             self.data = environ
  414.  
  415.         
  416.         def __setitem__(self, key, item):
  417.             putenv(key, item)
  418.             self.data[key] = item
  419.  
  420.         
  421.         def update(self, dict):
  422.             for k, v in dict.items():
  423.                 self[k] = v
  424.             
  425.  
  426.  
  427. environ = _Environ(environ)
  428.  
  429. def getenv(key, default = None):
  430.     """Get an environment variable, return None if it doesn't exist.
  431.  
  432.     The optional second argument can specify an alternate default."""
  433.     return environ.get(key, default)
  434.  
  435.  
  436. def _exists(name):
  437.     
  438.     try:
  439.         eval(name)
  440.         return 1
  441.     except NameError:
  442.         return 0
  443.  
  444.  
  445. if _exists('fork') and not _exists('spawnv') and _exists('execv'):
  446.     P_WAIT = 0
  447.     P_NOWAIT = P_NOWAITO = 1
  448.     
  449.     def _spawnvef(mode, file, args, env, func):
  450.         pid = fork()
  451.         if not pid:
  452.             
  453.             try:
  454.                 if env is None:
  455.                     func(file, args)
  456.                 else:
  457.                     func(file, args, env)
  458.             except:
  459.                 _exit(127)
  460.  
  461.         elif mode == P_NOWAIT:
  462.             return pid
  463.         
  464.         while 1:
  465.             (wpid, sts) = waitpid(pid, 0)
  466.             if WIFSTOPPED(sts):
  467.                 continue
  468.             elif WIFSIGNALED(sts):
  469.                 return -WTERMSIG(sts)
  470.             elif WIFEXITED(sts):
  471.                 return WEXITSTATUS(sts)
  472.             else:
  473.                 raise error, 'Not stopped, signaled or exited???'
  474.  
  475.     
  476.     def spawnv(mode, file, args):
  477.         """spawnv(mode, file, args) -> integer
  478.  
  479. Execute file with arguments from args in a subprocess.
  480. If mode == P_NOWAIT return the pid of the process.
  481. If mode == P_WAIT return the process's exit code if it exits normally;
  482. otherwise return -SIG, where SIG is the signal that killed it. """
  483.         return _spawnvef(mode, file, args, None, execv)
  484.  
  485.     
  486.     def spawnve(mode, file, args, env):
  487.         """spawnve(mode, file, args, env) -> integer
  488.  
  489. Execute file with arguments from args in a subprocess with the
  490. specified environment.
  491. If mode == P_NOWAIT return the pid of the process.
  492. If mode == P_WAIT return the process's exit code if it exits normally;
  493. otherwise return -SIG, where SIG is the signal that killed it. """
  494.         return _spawnvef(mode, file, args, env, execve)
  495.  
  496.     
  497.     def spawnvp(mode, file, args):
  498.         """spawnvp(mode, file, args) -> integer
  499.  
  500. Execute file (which is looked for along $PATH) with arguments from
  501. args in a subprocess.
  502. If mode == P_NOWAIT return the pid of the process.
  503. If mode == P_WAIT return the process's exit code if it exits normally;
  504. otherwise return -SIG, where SIG is the signal that killed it. """
  505.         return _spawnvef(mode, file, args, None, execvp)
  506.  
  507.     
  508.     def spawnvpe(mode, file, args, env):
  509.         """spawnvpe(mode, file, args, env) -> integer
  510.  
  511. Execute file (which is looked for along $PATH) with arguments from
  512. args in a subprocess with the supplied environment.
  513. If mode == P_NOWAIT return the pid of the process.
  514. If mode == P_WAIT return the process's exit code if it exits normally;
  515. otherwise return -SIG, where SIG is the signal that killed it. """
  516.         return _spawnvef(mode, file, args, env, execvpe)
  517.  
  518.  
  519. if _exists('spawnv'):
  520.     
  521.     def spawnl(mode, file, *args):
  522.         """spawnl(mode, file, *args) -> integer
  523.  
  524. Execute file with arguments from args in a subprocess.
  525. If mode == P_NOWAIT return the pid of the process.
  526. If mode == P_WAIT return the process's exit code if it exits normally;
  527. otherwise return -SIG, where SIG is the signal that killed it. """
  528.         return spawnv(mode, file, args)
  529.  
  530.     
  531.     def spawnle(mode, file, *args):
  532.         """spawnle(mode, file, *args, env) -> integer
  533.  
  534. Execute file with arguments from args in a subprocess with the
  535. supplied environment.
  536. If mode == P_NOWAIT return the pid of the process.
  537. If mode == P_WAIT return the process's exit code if it exits normally;
  538. otherwise return -SIG, where SIG is the signal that killed it. """
  539.         env = args[-1]
  540.         return spawnve(mode, file, args[:-1], env)
  541.  
  542.  
  543. if _exists('spawnvp'):
  544.     
  545.     def spawnlp(mode, file, *args):
  546.         """spawnlp(mode, file, *args, env) -> integer
  547.  
  548. Execute file (which is looked for along $PATH) with arguments from
  549. args in a subprocess with the supplied environment.
  550. If mode == P_NOWAIT return the pid of the process.
  551. If mode == P_WAIT return the process's exit code if it exits normally;
  552. otherwise return -SIG, where SIG is the signal that killed it. """
  553.         return spawnvp(mode, file, args)
  554.  
  555.     
  556.     def spawnlpe(mode, file, *args):
  557.         """spawnlpe(mode, file, *args, env) -> integer
  558.  
  559. Execute file (which is looked for along $PATH) with arguments from
  560. args in a subprocess with the supplied environment.
  561. If mode == P_NOWAIT return the pid of the process.
  562. If mode == P_WAIT return the process's exit code if it exits normally;
  563. otherwise return -SIG, where SIG is the signal that killed it. """
  564.         env = args[-1]
  565.         return spawnvpe(mode, file, args[:-1], env)
  566.  
  567.  
  568. if _exists('fork'):
  569.     if not _exists('popen2'):
  570.         
  571.         def popen2(cmd, mode = 't', bufsize = -1):
  572.             import popen2
  573.             (stdout, stdin) = popen2.popen2(cmd, bufsize)
  574.             return (stdin, stdout)
  575.  
  576.     
  577.     if not _exists('popen3'):
  578.         
  579.         def popen3(cmd, mode = 't', bufsize = -1):
  580.             import popen2
  581.             (stdout, stdin, stderr) = popen2.popen3(cmd, bufsize)
  582.             return (stdin, stdout, stderr)
  583.  
  584.     
  585.     if not _exists('popen4'):
  586.         
  587.         def popen4(cmd, mode = 't', bufsize = -1):
  588.             import popen2
  589.             (stdout, stdin) = popen2.popen4(cmd, bufsize)
  590.             return (stdin, stdout)
  591.  
  592.     
  593.  
  594.