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

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