home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-python-addon-1.4.9-installer.exe / ntpath.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-10-01  |  13.5 KB  |  472 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. '''Common pathname manipulations, WindowsNT/95 version.
  5.  
  6. Instead of importing this module directly, import os and refer to this
  7. module as os.path.
  8. '''
  9. import os
  10. import stat
  11. import sys
  12. __all__ = [
  13.     'normcase',
  14.     'isabs',
  15.     'join',
  16.     'splitdrive',
  17.     'split',
  18.     'splitext',
  19.     'basename',
  20.     'dirname',
  21.     'commonprefix',
  22.     'getsize',
  23.     'getmtime',
  24.     'getatime',
  25.     'getctime',
  26.     'islink',
  27.     'exists',
  28.     'isdir',
  29.     'isfile',
  30.     'ismount',
  31.     'walk',
  32.     'expanduser',
  33.     'expandvars',
  34.     'normpath',
  35.     'abspath',
  36.     'splitunc',
  37.     'curdir',
  38.     'pardir',
  39.     'sep',
  40.     'pathsep',
  41.     'defpath',
  42.     'altsep',
  43.     'extsep',
  44.     'realpath',
  45.     'supports_unicode_filenames']
  46. curdir = '.'
  47. pardir = '..'
  48. extsep = '.'
  49. sep = '\\'
  50. pathsep = ';'
  51. altsep = '/'
  52. defpath = '.;C:\\bin'
  53. if 'ce' in sys.builtin_module_names:
  54.     defpath = '\\Windows'
  55. elif 'os2' in sys.builtin_module_names:
  56.     altsep = '/'
  57.  
  58.  
  59. def normcase(s):
  60.     '''Normalize case of pathname.
  61.  
  62.     Makes all characters lowercase and all slashes into backslashes.'''
  63.     return s.replace('/', '\\').lower()
  64.  
  65.  
  66. def isabs(s):
  67.     '''Test whether a path is absolute'''
  68.     s = splitdrive(s)[1]
  69.     if s != '':
  70.         pass
  71.     return s[:1] in '/\\'
  72.  
  73.  
  74. def join(a, *p):
  75.     '''Join two or more pathname components, inserting "\\" as needed'''
  76.     path = a
  77.     for b in p:
  78.         b_wins = 0
  79.         if path == '':
  80.             b_wins = 1
  81.         elif isabs(b):
  82.             if path[1:2] != ':' or b[1:2] == ':':
  83.                 b_wins = 1
  84.             elif len(path) > 3 and len(path) == 3 and path[-1] not in '/\\':
  85.                 b_wins = 1
  86.             
  87.         
  88.         if b_wins:
  89.             path = b
  90.             continue
  91.         if path[-1] in '/\\':
  92.             if b and b[0] in '/\\':
  93.                 path += b[1:]
  94.             else:
  95.                 path += b
  96.         b[0] in '/\\'
  97.         if path[-1] == ':':
  98.             path += b
  99.             continue
  100.         if b:
  101.             if b[0] in '/\\':
  102.                 path += b
  103.             else:
  104.                 path += '\\' + b
  105.         b[0] in '/\\'
  106.         path += '\\'
  107.     
  108.     return path
  109.  
  110.  
  111. def splitdrive(p):
  112.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  113. "(drive,path)";  either part may be empty'''
  114.     if p[1:2] == ':':
  115.         return (p[0:2], p[2:])
  116.     
  117.     return ('', p)
  118.  
  119.  
  120. def splitunc(p):
  121.     """Split a pathname into UNC mount point and relative path specifiers.
  122.  
  123.     Return a 2-tuple (unc, rest); either part may be empty.
  124.     If unc is not empty, it has the form '//host/mount' (or similar
  125.     using backslashes).  unc+rest is always the input path.
  126.     Paths containing drive letters never have an UNC part.
  127.     """
  128.     if p[1:2] == ':':
  129.         return ('', p)
  130.     
  131.     firstTwo = p[0:2]
  132.     if firstTwo == '//' or firstTwo == '\\\\':
  133.         normp = normcase(p)
  134.         index = normp.find('\\', 2)
  135.         if index == -1:
  136.             return ('', p)
  137.         
  138.         index = normp.find('\\', index + 1)
  139.         if index == -1:
  140.             index = len(p)
  141.         
  142.         return (p[:index], p[index:])
  143.     
  144.     return ('', p)
  145.  
  146.  
  147. def split(p):
  148.     '''Split a pathname.
  149.  
  150.     Return tuple (head, tail) where tail is everything after the final slash.
  151.     Either part may be empty.'''
  152.     (d, p) = splitdrive(p)
  153.     i = len(p)
  154.     while i and p[i - 1] not in '/\\':
  155.         i = i - 1
  156.     (head, tail) = (p[:i], p[i:])
  157.     head2 = head
  158.     while head2 and head2[-1] in '/\\':
  159.         head2 = head2[:-1]
  160.     if not head2:
  161.         pass
  162.     head = head
  163.     return (d + head, tail)
  164.  
  165.  
  166. def splitext(p):
  167.     '''Split the extension from a pathname.
  168.  
  169.     Extension is everything from the last dot to the end.
  170.     Return (root, ext), either part may be empty.'''
  171.     i = p.rfind('.')
  172.     if i <= max(p.rfind('/'), p.rfind('\\')):
  173.         return (p, '')
  174.     else:
  175.         return (p[:i], p[i:])
  176.  
  177.  
  178. def basename(p):
  179.     '''Returns the final component of a pathname'''
  180.     return split(p)[1]
  181.  
  182.  
  183. def dirname(p):
  184.     '''Returns the directory component of a pathname'''
  185.     return split(p)[0]
  186.  
  187.  
  188. def commonprefix(m):
  189.     '''Given a list of pathnames, returns the longest common leading component'''
  190.     if not m:
  191.         return ''
  192.     
  193.     prefix = m[0]
  194.     for item in m:
  195.         for i in range(len(prefix)):
  196.             if prefix[:i + 1] != item[:i + 1]:
  197.                 prefix = prefix[:i]
  198.                 if i == 0:
  199.                     return ''
  200.                 
  201.                 break
  202.                 continue
  203.         
  204.     
  205.     return prefix
  206.  
  207.  
  208. def getsize(filename):
  209.     '''Return the size of a file, reported by os.stat()'''
  210.     return os.stat(filename).st_size
  211.  
  212.  
  213. def getmtime(filename):
  214.     '''Return the last modification time of a file, reported by os.stat()'''
  215.     return os.stat(filename).st_mtime
  216.  
  217.  
  218. def getatime(filename):
  219.     '''Return the last access time of a file, reported by os.stat()'''
  220.     return os.stat(filename).st_atime
  221.  
  222.  
  223. def getctime(filename):
  224.     '''Return the creation time of a file, reported by os.stat().'''
  225.     return os.stat(filename).st_ctime
  226.  
  227.  
  228. def islink(path):
  229.     '''Test for symbolic link.  On WindowsNT/95 always returns false'''
  230.     return False
  231.  
  232.  
  233. def exists(path):
  234.     '''Test whether a path exists'''
  235.     
  236.     try:
  237.         st = os.stat(path)
  238.     except os.error:
  239.         return False
  240.  
  241.     return True
  242.  
  243.  
  244. def isdir(path):
  245.     '''Test whether a path is a directory'''
  246.     
  247.     try:
  248.         st = os.stat(path)
  249.     except os.error:
  250.         return False
  251.  
  252.     return stat.S_ISDIR(st.st_mode)
  253.  
  254.  
  255. def isfile(path):
  256.     '''Test whether a path is a regular file'''
  257.     
  258.     try:
  259.         st = os.stat(path)
  260.     except os.error:
  261.         return False
  262.  
  263.     return stat.S_ISREG(st.st_mode)
  264.  
  265.  
  266. def ismount(path):
  267.     '''Test whether a path is a mount point (defined as root of drive)'''
  268.     (unc, rest) = splitunc(path)
  269.     if unc:
  270.         return rest in ('', '/', '\\')
  271.     
  272.     p = splitdrive(path)[1]
  273.     if len(p) == 1:
  274.         pass
  275.     return p[0] in '/\\'
  276.  
  277.  
  278. def walk(top, func, arg):
  279.     """Directory tree walk with callback function.
  280.  
  281.     For each directory in the directory tree rooted at top (including top
  282.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  283.     dirname is the name of the directory, and fnames a list of the names of
  284.     the files and subdirectories in dirname (excluding '.' and '..').  func
  285.     may modify the fnames list in-place (e.g. via del or slice assignment),
  286.     and walk will only recurse into the subdirectories whose names remain in
  287.     fnames; this can be used to implement a filter, or to impose a specific
  288.     order of visiting.  No semantics are defined for, or required of, arg,
  289.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  290.     a filename pattern, or a mutable object designed to accumulate
  291.     statistics.  Passing None for arg is common."""
  292.     
  293.     try:
  294.         names = os.listdir(top)
  295.     except os.error:
  296.         return None
  297.  
  298.     func(arg, top, names)
  299.     exceptions = ('.', '..')
  300.     for name in names:
  301.         if name not in exceptions:
  302.             name = join(top, name)
  303.             if isdir(name):
  304.                 walk(name, func, arg)
  305.             
  306.         isdir(name)
  307.     
  308.  
  309.  
  310. def expanduser(path):
  311.     '''Expand ~ and ~user constructs.
  312.  
  313.     If user or $HOME is unknown, do nothing.'''
  314.     if path[:1] != '~':
  315.         return path
  316.     
  317.     (i, n) = (1, len(path))
  318.     while i < n and path[i] not in '/\\':
  319.         i = i + 1
  320.     if i == 1:
  321.         if 'HOME' in os.environ:
  322.             userhome = os.environ['HOME']
  323.         elif not ('HOMEPATH' in os.environ):
  324.             return path
  325.         else:
  326.             
  327.             try:
  328.                 drive = os.environ['HOMEDRIVE']
  329.             except KeyError:
  330.                 drive = ''
  331.  
  332.             userhome = join(drive, os.environ['HOMEPATH'])
  333.     else:
  334.         return path
  335.     return userhome + path[i:]
  336.  
  337.  
  338. def expandvars(path):
  339.     '''Expand shell variables of form $var and ${var}.
  340.  
  341.     Unknown variables are left unchanged.'''
  342.     if '$' not in path:
  343.         return path
  344.     
  345.     import string
  346.     varchars = string.ascii_letters + string.digits + '_-'
  347.     res = ''
  348.     index = 0
  349.     pathlen = len(path)
  350.     while index < pathlen:
  351.         c = path[index]
  352.         if c == "'":
  353.             path = path[index + 1:]
  354.             pathlen = len(path)
  355.             
  356.             try:
  357.                 index = path.index("'")
  358.                 res = res + "'" + path[:index + 1]
  359.             except ValueError:
  360.                 res = res + path
  361.                 index = pathlen - 1
  362.             except:
  363.                 None<EXCEPTION MATCH>ValueError
  364.             
  365.  
  366.         None<EXCEPTION MATCH>ValueError
  367.         if c == '$':
  368.             if path[index + 1:index + 2] == '$':
  369.                 res = res + c
  370.                 index = index + 1
  371.             elif path[index + 1:index + 2] == '{':
  372.                 path = path[index + 2:]
  373.                 pathlen = len(path)
  374.                 
  375.                 try:
  376.                     index = path.index('}')
  377.                     var = path[:index]
  378.                     if var in os.environ:
  379.                         res = res + os.environ[var]
  380.                 except ValueError:
  381.                     res = res + path
  382.                     index = pathlen - 1
  383.                 except:
  384.                     None<EXCEPTION MATCH>ValueError
  385.                 
  386.  
  387.             None<EXCEPTION MATCH>ValueError
  388.             var = ''
  389.             index = index + 1
  390.             c = path[index:index + 1]
  391.             while c != '' and c in varchars:
  392.                 var = var + c
  393.                 index = index + 1
  394.                 c = path[index:index + 1]
  395.             if var in os.environ:
  396.                 res = res + os.environ[var]
  397.             
  398.             if c != '':
  399.                 res = res + c
  400.             
  401.         else:
  402.             res = res + c
  403.         index = index + 1
  404.     return res
  405.  
  406.  
  407. def normpath(path):
  408.     '''Normalize path, eliminating double slashes, etc.'''
  409.     path = path.replace('/', '\\')
  410.     (prefix, path) = splitdrive(path)
  411.     while path[:1] == '\\':
  412.         prefix = prefix + '\\'
  413.         path = path[1:]
  414.     comps = path.split('\\')
  415.     i = 0
  416.     while i < len(comps):
  417.         if comps[i] in ('.', ''):
  418.             del comps[i]
  419.             continue
  420.         if comps[i] == '..':
  421.             if i > 0 and comps[i - 1] != '..':
  422.                 del comps[i - 1:i + 1]
  423.                 i -= 1
  424.             elif i == 0 and prefix.endswith('\\'):
  425.                 del comps[i]
  426.             else:
  427.                 i += 1
  428.         prefix.endswith('\\')
  429.         i += 1
  430.     if not prefix and not comps:
  431.         comps.append('.')
  432.     
  433.     return prefix + '\\'.join(comps)
  434.  
  435.  
  436. def abspath(path):
  437.     '''Return the absolute version of a path'''
  438.     global abspath
  439.     
  440.     try:
  441.         _getfullpathname = _getfullpathname
  442.         import nt
  443.     except ImportError:
  444.         
  445.         def _abspath(path):
  446.             if not isabs(path):
  447.                 path = join(os.getcwd(), path)
  448.             
  449.             return normpath(path)
  450.  
  451.         abspath = _abspath
  452.         return _abspath(path)
  453.  
  454.     if path:
  455.         
  456.         try:
  457.             path = _getfullpathname(path)
  458.         except WindowsError:
  459.             pass
  460.         except:
  461.             None<EXCEPTION MATCH>WindowsError
  462.         
  463.  
  464.     None<EXCEPTION MATCH>WindowsError
  465.     path = os.getcwd()
  466.     return normpath(path)
  467.  
  468. realpath = abspath
  469. if hasattr(sys, 'getwindowsversion'):
  470.     pass
  471. supports_unicode_filenames = sys.getwindowsversion()[3] >= 2
  472.