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 / os2emxpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-29  |  10.3 KB  |  423 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Common pathname manipulations, OS/2 EMX 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.     'getctime',
  25.     'islink',
  26.     'exists',
  27.     'lexists',
  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.     'devnull',
  45.     'realpath',
  46.     'supports_unicode_filenames']
  47. curdir = '.'
  48. pardir = '..'
  49. extsep = '.'
  50. sep = '/'
  51. altsep = '\\'
  52. pathsep = ';'
  53. defpath = '.;C:\\bin'
  54. devnull = 'nul'
  55.  
  56. def normcase(s):
  57.     '''Normalize case of pathname.
  58.  
  59.     Makes all characters lowercase and all altseps into seps.'''
  60.     return s.replace('\\', '/').lower()
  61.  
  62.  
  63. def isabs(s):
  64.     '''Test whether a path is absolute'''
  65.     s = splitdrive(s)[1]
  66.     if s != '':
  67.         pass
  68.     return s[:1] in '/\\'
  69.  
  70.  
  71. def join(a, *p):
  72.     '''Join two or more pathname components, inserting sep as needed'''
  73.     path = a
  74.     for b in p:
  75.         if isabs(b):
  76.             path = b
  77.             continue
  78.         if path == '' or path[-1:] in '/\\:':
  79.             path = path + b
  80.             continue
  81.         path = path + '/' + b
  82.     
  83.     return path
  84.  
  85.  
  86. def splitdrive(p):
  87.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  88. "(drive,path)";  either part may be empty'''
  89.     if p[1:2] == ':':
  90.         return (p[0:2], p[2:])
  91.     
  92.     return ('', p)
  93.  
  94.  
  95. def splitunc(p):
  96.     """Split a pathname into UNC mount point and relative path specifiers.
  97.  
  98.     Return a 2-tuple (unc, rest); either part may be empty.
  99.     If unc is not empty, it has the form '//host/mount' (or similar
  100.     using backslashes).  unc+rest is always the input path.
  101.     Paths containing drive letters never have an UNC part.
  102.     """
  103.     if p[1:2] == ':':
  104.         return ('', p)
  105.     
  106.     firstTwo = p[0:2]
  107.     if firstTwo == '//' or firstTwo == '\\\\':
  108.         normp = normcase(p)
  109.         index = normp.find('/', 2)
  110.         if index == -1:
  111.             return ('', p)
  112.         
  113.         index = normp.find('/', index + 1)
  114.         if index == -1:
  115.             index = len(p)
  116.         
  117.         return (p[:index], p[index:])
  118.     
  119.     return ('', p)
  120.  
  121.  
  122. def split(p):
  123.     '''Split a pathname.
  124.  
  125.     Return tuple (head, tail) where tail is everything after the final slash.
  126.     Either part may be empty.'''
  127.     (d, p) = splitdrive(p)
  128.     i = len(p)
  129.     while i and p[i - 1] not in '/\\':
  130.         i = i - 1
  131.     head = p[:i]
  132.     tail = 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.             root = root + ext + c
  151.             ext = ''
  152.             continue
  153.         if c == '.':
  154.             if ext:
  155.                 root = root + ext
  156.                 ext = c
  157.             else:
  158.                 ext = c
  159.         ext
  160.         if ext:
  161.             ext = ext + c
  162.             continue
  163.         root = root + c
  164.     
  165.     return (root, ext)
  166.  
  167.  
  168. def basename(p):
  169.     '''Returns the final component of a pathname'''
  170.     return split(p)[1]
  171.  
  172.  
  173. def dirname(p):
  174.     '''Returns the directory component of a pathname'''
  175.     return split(p)[0]
  176.  
  177.  
  178. def commonprefix(m):
  179.     '''Given a list of pathnames, returns the longest common leading component'''
  180.     if not m:
  181.         return ''
  182.     
  183.     s1 = min(m)
  184.     s2 = max(m)
  185.     n = min(len(s1), len(s2))
  186.     for i in xrange(n):
  187.         if s1[i] != s2[i]:
  188.             return s1[:i]
  189.             continue
  190.     
  191.     return s1[:n]
  192.  
  193.  
  194. def getsize(filename):
  195.     '''Return the size of a file, reported by os.stat()'''
  196.     return os.stat(filename).st_size
  197.  
  198.  
  199. def getmtime(filename):
  200.     '''Return the last modification time of a file, reported by os.stat()'''
  201.     return os.stat(filename).st_mtime
  202.  
  203.  
  204. def getatime(filename):
  205.     '''Return the last access time of a file, reported by os.stat()'''
  206.     return os.stat(filename).st_atime
  207.  
  208.  
  209. def getctime(filename):
  210.     '''Return the creation time of a file, reported by os.stat().'''
  211.     return os.stat(filename).st_ctime
  212.  
  213.  
  214. def islink(path):
  215.     '''Test for symbolic link.  On OS/2 always returns false'''
  216.     return False
  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 False
  226.  
  227.     return True
  228.  
  229. lexists = exists
  230.  
  231. def isdir(path):
  232.     '''Test whether a path is a directory'''
  233.     
  234.     try:
  235.         st = os.stat(path)
  236.     except os.error:
  237.         return False
  238.  
  239.     return stat.S_ISDIR(st.st_mode)
  240.  
  241.  
  242. def isfile(path):
  243.     '''Test whether a path is a regular file'''
  244.     
  245.     try:
  246.         st = os.stat(path)
  247.     except os.error:
  248.         return False
  249.  
  250.     return stat.S_ISREG(st.st_mode)
  251.  
  252.  
  253. def ismount(path):
  254.     '''Test whether a path is a mount point (defined as root of drive)'''
  255.     (unc, rest) = splitunc(path)
  256.     if unc:
  257.         return rest in ('', '/', '\\')
  258.     
  259.     p = splitdrive(path)[1]
  260.     if len(p) == 1:
  261.         pass
  262.     return p[0] in '/\\'
  263.  
  264.  
  265. def walk(top, func, arg):
  266.     '''Directory tree walk whth callback function.
  267.  
  268.     walk(top, func, arg) calls func(arg, d, files) for each directory d
  269.     in the tree rooted at top (including top itself); files is a list
  270.     of all the files and subdirs in directory d.'''
  271.     
  272.     try:
  273.         names = os.listdir(top)
  274.     except os.error:
  275.         return None
  276.  
  277.     func(arg, top, names)
  278.     exceptions = ('.', '..')
  279.     for name in names:
  280.         if name not in exceptions:
  281.             name = join(top, name)
  282.             if isdir(name):
  283.                 walk(name, func, arg)
  284.             
  285.         isdir(name)
  286.     
  287.  
  288.  
  289. def expanduser(path):
  290.     '''Expand ~ and ~user constructs.
  291.  
  292.     If user or $HOME is unknown, do nothing.'''
  293.     if path[:1] != '~':
  294.         return path
  295.     
  296.     i = 1
  297.     n = len(path)
  298.     while i < n and path[i] not in '/\\':
  299.         i = i + 1
  300.     if i == 1:
  301.         if 'HOME' in os.environ:
  302.             userhome = os.environ['HOME']
  303.         elif 'HOMEPATH' not in os.environ:
  304.             return path
  305.         else:
  306.             
  307.             try:
  308.                 drive = os.environ['HOMEDRIVE']
  309.             except KeyError:
  310.                 drive = ''
  311.  
  312.             userhome = join(drive, os.environ['HOMEPATH'])
  313.     else:
  314.         return path
  315.     return userhome + path[i:]
  316.  
  317.  
  318. def expandvars(path):
  319.     '''Expand shell variables of form $var and ${var}.
  320.  
  321.     Unknown variables are left unchanged.'''
  322.     if '$' not in path:
  323.         return path
  324.     
  325.     import string
  326.     varchars = string.letters + string.digits + '_-'
  327.     res = ''
  328.     index = 0
  329.     pathlen = len(path)
  330.     while index < pathlen:
  331.         c = path[index]
  332.         if c == "'":
  333.             path = path[index + 1:]
  334.             pathlen = len(path)
  335.             
  336.             try:
  337.                 index = path.index("'")
  338.                 res = res + "'" + path[:index + 1]
  339.             except ValueError:
  340.                 res = res + path
  341.                 index = pathlen - 1
  342.             except:
  343.                 None<EXCEPTION MATCH>ValueError
  344.             
  345.  
  346.         None<EXCEPTION MATCH>ValueError
  347.         if c == '$':
  348.             if path[index + 1:index + 2] == '$':
  349.                 res = res + c
  350.                 index = index + 1
  351.             elif path[index + 1:index + 2] == '{':
  352.                 path = path[index + 2:]
  353.                 pathlen = len(path)
  354.                 
  355.                 try:
  356.                     index = path.index('}')
  357.                     var = path[:index]
  358.                     if var in os.environ:
  359.                         res = res + os.environ[var]
  360.                 except ValueError:
  361.                     res = res + path
  362.                     index = pathlen - 1
  363.                 except:
  364.                     None<EXCEPTION MATCH>ValueError
  365.                 
  366.  
  367.             None<EXCEPTION MATCH>ValueError
  368.             var = ''
  369.             index = index + 1
  370.             c = path[index:index + 1]
  371.             while c != '' and c in varchars:
  372.                 var = var + c
  373.                 index = index + 1
  374.                 c = path[index:index + 1]
  375.             if var in os.environ:
  376.                 res = res + os.environ[var]
  377.             
  378.             if c != '':
  379.                 res = res + c
  380.             
  381.         else:
  382.             res = res + c
  383.         index = index + 1
  384.     return res
  385.  
  386.  
  387. def normpath(path):
  388.     '''Normalize path, eliminating double slashes, etc.'''
  389.     path = path.replace('\\', '/')
  390.     (prefix, path) = splitdrive(path)
  391.     while path[:1] == '/':
  392.         prefix = prefix + '/'
  393.         path = path[1:]
  394.     comps = path.split('/')
  395.     i = 0
  396.     while i < len(comps):
  397.         if comps[i] == '.':
  398.             del comps[i]
  399.             continue
  400.         if comps[i] == '..' and i > 0 and comps[i - 1] not in ('', '..'):
  401.             del comps[i - 1:i + 1]
  402.             i = i - 1
  403.             continue
  404.         if comps[i] == '' and i > 0 and comps[i - 1] != '':
  405.             del comps[i]
  406.             continue
  407.         i = 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 not isabs(path):
  417.         path = join(os.getcwd(), path)
  418.     
  419.     return normpath(path)
  420.  
  421. realpath = abspath
  422. supports_unicode_filenames = False
  423.