home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / os.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  21.8 KB  |  685 lines

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