home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / posixpath.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  9.1 KB  |  343 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import os
  5. import stat
  6. __all__ = [
  7.     'normcase',
  8.     'isabs',
  9.     'join',
  10.     'splitdrive',
  11.     'split',
  12.     'splitext',
  13.     'basename',
  14.     'dirname',
  15.     'commonprefix',
  16.     'getsize',
  17.     'getmtime',
  18.     'getatime',
  19.     'islink',
  20.     'exists',
  21.     'isdir',
  22.     'isfile',
  23.     'ismount',
  24.     'walk',
  25.     'expanduser',
  26.     'expandvars',
  27.     'normpath',
  28.     'abspath',
  29.     'samefile',
  30.     'sameopenfile',
  31.     'samestat']
  32.  
  33. def normcase(s):
  34.     return s
  35.  
  36.  
  37. def isabs(s):
  38.     return s[:1] == '/'
  39.  
  40.  
  41. def join(a, *p):
  42.     path = a
  43.     for b in p:
  44.         if b[:1] == '/':
  45.             path = b
  46.         elif path == '' or path[-1:] == '/':
  47.             path = path + b
  48.         else:
  49.             path = path + '/' + b
  50.     
  51.     return path
  52.  
  53.  
  54. def split(p):
  55.     i = p.rfind('/') + 1
  56.     (head, tail) = (p[:i], p[i:])
  57.     if head and head != '/' * len(head):
  58.         while head[-1] == '/':
  59.             head = head[:-1]
  60.     
  61.     return (head, tail)
  62.  
  63.  
  64. def splitext(p):
  65.     (root, ext) = ('', '')
  66.     for c in p:
  67.         if c == '/':
  68.             (root, ext) = (root + ext + c, '')
  69.         elif c == '.':
  70.             if ext:
  71.                 (root, ext) = (root + ext, c)
  72.             else:
  73.                 ext = c
  74.         elif ext:
  75.             ext = ext + c
  76.         else:
  77.             root = root + c
  78.     
  79.     return (root, ext)
  80.  
  81.  
  82. def splitdrive(p):
  83.     return ('', p)
  84.  
  85.  
  86. def basename(p):
  87.     return split(p)[1]
  88.  
  89.  
  90. def dirname(p):
  91.     return split(p)[0]
  92.  
  93.  
  94. def commonprefix(m):
  95.     if not m:
  96.         return ''
  97.     
  98.     prefix = m[0]
  99.     for item in m:
  100.         for i in range(len(prefix)):
  101.             if prefix[:i + 1] != item[:i + 1]:
  102.                 prefix = prefix[:i]
  103.                 if i == 0:
  104.                     return ''
  105.                 
  106.                 break
  107.             
  108.         
  109.     
  110.     return prefix
  111.  
  112.  
  113. def getsize(filename):
  114.     st = os.stat(filename)
  115.     return st[stat.ST_SIZE]
  116.  
  117.  
  118. def getmtime(filename):
  119.     st = os.stat(filename)
  120.     return st[stat.ST_MTIME]
  121.  
  122.  
  123. def getatime(filename):
  124.     st = os.stat(filename)
  125.     return st[stat.ST_ATIME]
  126.  
  127.  
  128. def islink(path):
  129.     
  130.     try:
  131.         st = os.lstat(path)
  132.     except (os.error, AttributeError):
  133.         return 0
  134.  
  135.     return stat.S_ISLNK(st[stat.ST_MODE])
  136.  
  137.  
  138. def exists(path):
  139.     
  140.     try:
  141.         st = os.stat(path)
  142.     except os.error:
  143.         return 0
  144.  
  145.     return 1
  146.  
  147.  
  148. def isdir(path):
  149.     
  150.     try:
  151.         st = os.stat(path)
  152.     except os.error:
  153.         return 0
  154.  
  155.     return stat.S_ISDIR(st[stat.ST_MODE])
  156.  
  157.  
  158. def isfile(path):
  159.     
  160.     try:
  161.         st = os.stat(path)
  162.     except os.error:
  163.         return 0
  164.  
  165.     return stat.S_ISREG(st[stat.ST_MODE])
  166.  
  167.  
  168. def samefile(f1, f2):
  169.     s1 = os.stat(f1)
  170.     s2 = os.stat(f2)
  171.     return samestat(s1, s2)
  172.  
  173.  
  174. def sameopenfile(fp1, fp2):
  175.     s1 = os.fstat(fp1)
  176.     s2 = os.fstat(fp2)
  177.     return samestat(s1, s2)
  178.  
  179.  
  180. def samestat(s1, s2):
  181.     if s1[stat.ST_INO] == s2[stat.ST_INO]:
  182.         pass
  183.     return s1[stat.ST_DEV] == s2[stat.ST_DEV]
  184.  
  185.  
  186. def ismount(path):
  187.     
  188.     try:
  189.         s1 = os.stat(path)
  190.         s2 = os.stat(join(path, '..'))
  191.     except os.error:
  192.         return 0
  193.  
  194.     dev1 = s1[stat.ST_DEV]
  195.     dev2 = s2[stat.ST_DEV]
  196.     if dev1 != dev2:
  197.         return 1
  198.     
  199.     ino1 = s1[stat.ST_INO]
  200.     ino2 = s2[stat.ST_INO]
  201.     if ino1 == ino2:
  202.         return 1
  203.     
  204.     return 0
  205.  
  206.  
  207. def walk(top, func, arg):
  208.     
  209.     try:
  210.         names = os.listdir(top)
  211.     except os.error:
  212.         return None
  213.  
  214.     func(arg, top, names)
  215.     for name in names:
  216.         name = join(top, name)
  217.         
  218.         try:
  219.             st = os.lstat(name)
  220.         except os.error:
  221.             continue
  222.  
  223.         if stat.S_ISDIR(st[stat.ST_MODE]):
  224.             walk(name, func, arg)
  225.         
  226.     
  227.  
  228.  
  229. def expanduser(path):
  230.     if path[:1] != '~':
  231.         return path
  232.     
  233.     (i, n) = (1, len(path))
  234.     while i < n and path[i] != '/':
  235.         i = i + 1
  236.     if i == 1:
  237.         if not os.environ.has_key('HOME'):
  238.             import pwd
  239.             userhome = pwd.getpwuid(os.getuid())[5]
  240.         else:
  241.             userhome = os.environ['HOME']
  242.     else:
  243.         import pwd
  244.         
  245.         try:
  246.             pwent = pwd.getpwnam(path[1:i])
  247.         except KeyError:
  248.             return path
  249.  
  250.         userhome = pwent[5]
  251.     if userhome[-1:] == '/':
  252.         i = i + 1
  253.     
  254.     return userhome + path[i:]
  255.  
  256. _varprog = None
  257.  
  258. def expandvars(path):
  259.     global _varprog
  260.     if '$' not in path:
  261.         return path
  262.     
  263.     if not _varprog:
  264.         import re
  265.         _varprog = re.compile('\\$(\\w+|\\{[^}]*\\})')
  266.     
  267.     i = 0
  268.     while 1:
  269.         m = _varprog.search(path, i)
  270.         if not m:
  271.             break
  272.         
  273.         (i, j) = m.span(0)
  274.         name = m.group(1)
  275.         if name[:1] == '{' and name[-1:] == '}':
  276.             name = name[1:-1]
  277.         
  278.         if os.environ.has_key(name):
  279.             tail = path[j:]
  280.             path = path[:i] + os.environ[name]
  281.             i = len(path)
  282.             path = path + tail
  283.         else:
  284.             i = j
  285.     return path
  286.  
  287.  
  288. def normpath(path):
  289.     if path == '':
  290.         return '.'
  291.     
  292.     initial_slashes = path.startswith('/')
  293.     if initial_slashes and path.startswith('//') and not path.startswith('///'):
  294.         initial_slashes = 2
  295.     
  296.     comps = path.split('/')
  297.     new_comps = []
  298.     for comp in comps:
  299.         if comp in ('', '.'):
  300.             continue
  301.         
  302.         if not comp != '..':
  303.             if not initial_slashes and not new_comps and new_comps and new_comps[-1] == '..':
  304.                 new_comps.append(comp)
  305.             elif new_comps:
  306.                 new_comps.pop()
  307.             
  308.     
  309.     comps = new_comps
  310.     path = '/'.join(comps)
  311.     if initial_slashes:
  312.         path = '/' * initial_slashes + path
  313.     
  314.     if not path:
  315.         pass
  316.     return '.'
  317.  
  318.  
  319. def abspath(path):
  320.     if not isabs(path):
  321.         path = join(os.getcwd(), path)
  322.     
  323.     return normpath(path)
  324.  
  325.  
  326. def realpath(filename):
  327.     filename = abspath(filename)
  328.     bits = [
  329.         '/'] + filename.split('/')[1:]
  330.     for i in range(2, len(bits) + 1):
  331.         component = join(*bits[0:i])
  332.         if islink(component):
  333.             resolved = os.readlink(component)
  334.             (dir, file) = split(component)
  335.             resolved = normpath(join(dir, resolved))
  336.             newpath = join(*[
  337.                 resolved] + bits[i:])
  338.             return realpath(newpath)
  339.         
  340.     
  341.     return filename
  342.  
  343.