home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / posixpath.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2004-02-22  |  14KB  |  411 lines

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