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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Common operations on Posix pathnames.
  5.  
  6. Instead of importing this module directly, import os and refer to
  7. this module as os.path.  The "os.path" name is an alias for this
  8. module on Posix systems; on other systems (e.g. Mac, Windows),
  9. os.path provides the same operations in a manner specific to that
  10. platform, and is an alias to another module (e.g. macpath, ntpath).
  11.  
  12. Some of this can actually be useful on non-Posix systems too, e.g.
  13. for manipulation of the pathname component of URLs.
  14. '''
  15. import os
  16. import stat
  17. __all__ = [
  18.     'normcase',
  19.     'isabs',
  20.     'join',
  21.     'splitdrive',
  22.     'split',
  23.     'splitext',
  24.     'basename',
  25.     'dirname',
  26.     'commonprefix',
  27.     'getsize',
  28.     'getmtime',
  29.     'getatime',
  30.     'islink',
  31.     'exists',
  32.     'isdir',
  33.     'isfile',
  34.     'ismount',
  35.     'walk',
  36.     'expanduser',
  37.     'expandvars',
  38.     'normpath',
  39.     'abspath',
  40.     'samefile',
  41.     'sameopenfile',
  42.     'samestat']
  43.  
  44. def normcase(s):
  45.     '''Normalize case of pathname.  Has no effect under Posix'''
  46.     return s
  47.  
  48.  
  49. def isabs(s):
  50.     '''Test whether a path is absolute'''
  51.     return s[:1] == '/'
  52.  
  53.  
  54. def join(a, *p):
  55.     """Join two or more pathname components, inserting '/' as needed"""
  56.     path = a
  57.     for b in p:
  58.         if b[:1] == '/':
  59.             path = b
  60.         elif path == '' or path[-1:] == '/':
  61.             path = path + b
  62.         else:
  63.             path = path + '/' + b
  64.     
  65.     return path
  66.  
  67.  
  68. def split(p):
  69.     '''Split a pathname.  Returns tuple "(head, tail)" where "tail" is
  70.     everything after the final slash.  Either part may be empty.'''
  71.     i = p.rfind('/') + 1
  72.     (head, tail) = (p[:i], p[i:])
  73.     if head and head != '/' * len(head):
  74.         while head[-1] == '/':
  75.             head = head[:-1]
  76.     
  77.     return (head, tail)
  78.  
  79.  
  80. def splitext(p):
  81.     '''Split the extension from a pathname.  Extension is everything from the
  82.     last dot to the end.  Returns "(root, ext)", either part may be empty.'''
  83.     (root, ext) = ('', '')
  84.     for c in p:
  85.         if c == '/':
  86.             (root, ext) = (root + ext + c, '')
  87.         elif c == '.':
  88.             if ext:
  89.                 (root, ext) = (root + ext, c)
  90.             else:
  91.                 ext = c
  92.         elif ext:
  93.             ext = ext + c
  94.         else:
  95.             root = root + c
  96.     
  97.     return (root, ext)
  98.  
  99.  
  100. def splitdrive(p):
  101.     '''Split a pathname into drive and path. On Posix, drive is always
  102.     empty.'''
  103.     return ('', p)
  104.  
  105.  
  106. def basename(p):
  107.     '''Returns the final component of a pathname'''
  108.     return split(p)[1]
  109.  
  110.  
  111. def dirname(p):
  112.     '''Returns the directory component of a pathname'''
  113.     return split(p)[0]
  114.  
  115.  
  116. def commonprefix(m):
  117.     '''Given a list of pathnames, returns the longest common leading component'''
  118.     if not m:
  119.         return ''
  120.     
  121.     prefix = m[0]
  122.     for item in m:
  123.         for i in range(len(prefix)):
  124.             if prefix[:i + 1] != item[:i + 1]:
  125.                 prefix = prefix[:i]
  126.                 if i == 0:
  127.                     return ''
  128.                 
  129.                 break
  130.             
  131.         
  132.     
  133.     return prefix
  134.  
  135.  
  136. def getsize(filename):
  137.     '''Return the size of a file, reported by os.stat().'''
  138.     st = os.stat(filename)
  139.     return st[stat.ST_SIZE]
  140.  
  141.  
  142. def getmtime(filename):
  143.     '''Return the last modification time of a file, reported by os.stat().'''
  144.     st = os.stat(filename)
  145.     return st[stat.ST_MTIME]
  146.  
  147.  
  148. def getatime(filename):
  149.     '''Return the last access time of a file, reported by os.stat().'''
  150.     st = os.stat(filename)
  151.     return st[stat.ST_ATIME]
  152.  
  153.  
  154. def islink(path):
  155.     '''Test whether a path is a symbolic link'''
  156.     
  157.     try:
  158.         st = os.lstat(path)
  159.     except (os.error, AttributeError):
  160.         return 0
  161.  
  162.     return stat.S_ISLNK(st[stat.ST_MODE])
  163.  
  164.  
  165. def exists(path):
  166.     '''Test whether a path exists.  Returns false for broken symbolic links'''
  167.     
  168.     try:
  169.         st = os.stat(path)
  170.     except os.error:
  171.         return 0
  172.  
  173.     return 1
  174.  
  175.  
  176. def isdir(path):
  177.     '''Test whether a path is a directory'''
  178.     
  179.     try:
  180.         st = os.stat(path)
  181.     except os.error:
  182.         return 0
  183.  
  184.     return stat.S_ISDIR(st[stat.ST_MODE])
  185.  
  186.  
  187. def isfile(path):
  188.     '''Test whether a path is a regular file'''
  189.     
  190.     try:
  191.         st = os.stat(path)
  192.     except os.error:
  193.         return 0
  194.  
  195.     return stat.S_ISREG(st[stat.ST_MODE])
  196.  
  197.  
  198. def samefile(f1, f2):
  199.     '''Test whether two pathnames reference the same actual file'''
  200.     s1 = os.stat(f1)
  201.     s2 = os.stat(f2)
  202.     return samestat(s1, s2)
  203.  
  204.  
  205. def sameopenfile(fp1, fp2):
  206.     '''Test whether two open file objects reference the same file'''
  207.     s1 = os.fstat(fp1)
  208.     s2 = os.fstat(fp2)
  209.     return samestat(s1, s2)
  210.  
  211.  
  212. def samestat(s1, s2):
  213.     '''Test whether two stat buffers reference the same file'''
  214.     if s1[stat.ST_INO] == s2[stat.ST_INO]:
  215.         pass
  216.     return s1[stat.ST_DEV] == s2[stat.ST_DEV]
  217.  
  218.  
  219. def ismount(path):
  220.     '''Test whether a path is a mount point'''
  221.     
  222.     try:
  223.         s1 = os.stat(path)
  224.         s2 = os.stat(join(path, '..'))
  225.     except os.error:
  226.         return 0
  227.  
  228.     dev1 = s1[stat.ST_DEV]
  229.     dev2 = s2[stat.ST_DEV]
  230.     if dev1 != dev2:
  231.         return 1
  232.     
  233.     ino1 = s1[stat.ST_INO]
  234.     ino2 = s2[stat.ST_INO]
  235.     if ino1 == ino2:
  236.         return 1
  237.     
  238.     return 0
  239.  
  240.  
  241. def walk(top, func, arg):
  242.     """Directory tree walk with callback function.
  243.  
  244.     For each directory in the directory tree rooted at top (including top
  245.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  246.     dirname is the name of the directory, and fnames a list of the names of
  247.     the files and subdirectories in dirname (excluding '.' and '..').  func
  248.     may modify the fnames list in-place (e.g. via del or slice assignment),
  249.     and walk will only recurse into the subdirectories whose names remain in
  250.     fnames; this can be used to implement a filter, or to impose a specific
  251.     order of visiting.  No semantics are defined for, or required of, arg,
  252.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  253.     a filename pattern, or a mutable object designed to accumulate
  254.     statistics.  Passing None for arg is common."""
  255.     
  256.     try:
  257.         names = os.listdir(top)
  258.     except os.error:
  259.         return None
  260.  
  261.     func(arg, top, names)
  262.     for name in names:
  263.         name = join(top, name)
  264.         
  265.         try:
  266.             st = os.lstat(name)
  267.         except os.error:
  268.             continue
  269.  
  270.         if stat.S_ISDIR(st[stat.ST_MODE]):
  271.             walk(name, func, arg)
  272.         
  273.     
  274.  
  275.  
  276. def expanduser(path):
  277.     '''Expand ~ and ~user constructions.  If user or $HOME is unknown,
  278.     do nothing.'''
  279.     if path[:1] != '~':
  280.         return path
  281.     
  282.     (i, n) = (1, len(path))
  283.     while i < n and path[i] != '/':
  284.         i = i + 1
  285.     if i == 1:
  286.         if not os.environ.has_key('HOME'):
  287.             return path
  288.         
  289.         userhome = os.environ['HOME']
  290.     else:
  291.         import pwd
  292.         
  293.         try:
  294.             pwent = pwd.getpwnam(path[1:i])
  295.         except KeyError:
  296.             return path
  297.  
  298.         userhome = pwent[5]
  299.     if userhome[-1:] == '/':
  300.         i = i + 1
  301.     
  302.     return userhome + path[i:]
  303.  
  304. _varprog = None
  305.  
  306. def expandvars(path):
  307.     '''Expand shell variables of form $var and ${var}.  Unknown variables
  308.     are left unchanged.'''
  309.     global _varprog
  310.     if '$' not in path:
  311.         return path
  312.     
  313.     if not _varprog:
  314.         import re
  315.         _varprog = re.compile('\\$(\\w+|\\{[^}]*\\})')
  316.     
  317.     i = 0
  318.     while 1:
  319.         m = _varprog.search(path, i)
  320.         if not m:
  321.             break
  322.         
  323.         (i, j) = m.span(0)
  324.         name = m.group(1)
  325.         if name[:1] == '{' and name[-1:] == '}':
  326.             name = name[1:-1]
  327.         
  328.         if os.environ.has_key(name):
  329.             tail = path[j:]
  330.             path = path[:i] + os.environ[name]
  331.             i = len(path)
  332.             path = path + tail
  333.         else:
  334.             i = j
  335.     return path
  336.  
  337.  
  338. def normpath(path):
  339.     '''Normalize path, eliminating double slashes, etc.'''
  340.     if path == '':
  341.         return '.'
  342.     
  343.     initial_slashes = path.startswith('/')
  344.     if initial_slashes and path.startswith('//') and not path.startswith('///'):
  345.         initial_slashes = 2
  346.     
  347.     comps = path.split('/')
  348.     new_comps = []
  349.     for comp in comps:
  350.         if comp in ('', '.'):
  351.             continue
  352.         
  353.         if not comp != '..':
  354.             if not initial_slashes and not new_comps and new_comps and new_comps[-1] == '..':
  355.                 new_comps.append(comp)
  356.             elif new_comps:
  357.                 new_comps.pop()
  358.             
  359.     
  360.     comps = new_comps
  361.     path = '/'.join(comps)
  362.     if initial_slashes:
  363.         path = '/' * initial_slashes + path
  364.     
  365.     if not path:
  366.         pass
  367.     return '.'
  368.  
  369.  
  370. def abspath(path):
  371.     '''Return an absolute path.'''
  372.     if not isabs(path):
  373.         path = join(os.getcwd(), path)
  374.     
  375.     return normpath(path)
  376.  
  377.  
  378. def realpath(filename):
  379.     '''Return the canonical path of the specified filename, eliminating any
  380. symbolic links encountered in the path.'''
  381.     filename = abspath(filename)
  382.     bits = [
  383.         '/'] + filename.split('/')[1:]
  384.     for i in range(2, len(bits) + 1):
  385.         component = join(*bits[0:i])
  386.         if islink(component):
  387.             resolved = os.readlink(component)
  388.             (dir, file) = split(component)
  389.             resolved = normpath(join(dir, resolved))
  390.             newpath = join(*[
  391.                 resolved] + bits[i:])
  392.             return realpath(newpath)
  393.         
  394.     
  395.     return filename
  396.  
  397.