home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / python2.6 / posixpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2013-01-10  |  10.9 KB  |  405 lines

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