home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / ntpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-29  |  11.8 KB  |  478 lines

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