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

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