home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / andere sprachen / python-1.3 / lib / amigapath.py < prev    next >
Encoding:
Python Source  |  1996-07-16  |  6.9 KB  |  292 lines

  1. # Module 'amigapath' -- common operations on AMIGA pathnames
  2. # Irmen de Jong, april 1st, 1996 (no joke)
  3. # (adapted from `posixpath.py')
  4. #
  5. # 26-mar-96: fixed split, islink. Improved expanduser.
  6. # 1-apr-96: fixed expanduser (works 100% now)
  7. #
  8.  
  9. import amiga
  10. import stat
  11. import string
  12.  
  13. normcase = string.lower        # normalize the case of a pathname
  14.  
  15.  
  16. # Return wheter a path is absolute. On the Amiga: when a ':' occurs in it.
  17.  
  18. def isabs(s):
  19.     return ':' in s
  20.  
  21.  
  22. # Join two pathnames.
  23. # Ignore the first part if the second part is absolute.
  24. # Insert a '/' unless the first part is empty or already ends in '/' or ':'.
  25.  
  26. def join(a, b):
  27.     if ':' in b: return b
  28.     if a == '' or a[-1:] == '/': return a + b
  29.     # Note: join('x', '') returns 'x/'; is this what we want?
  30.     if a[-1:]==':': return a+b
  31.     return a + '/' + b
  32.  
  33.  
  34. # Split a path in head (everything up to the last '/' or ':') and tail (the
  35. # rest).  If the path ends in '/' or ':', tail will be empty.  If there is no
  36. # '/' or ':' in the path, head  will be empty.
  37. # DIFFERENCE WITH posixpath: only ONE trailing '/' will be stripped from head!
  38. # (on the Amiga a double slash means "parent dir"! ) This means that if
  39. # head ends in a '/', you MUST add a '/' to it when reconstructing the path,
  40. # or you will lose the "parent dir" slash.
  41. # Functions that depend on this function are also affected!
  42. #  (basename, dirname)
  43. #
  44. # Suggestion by Kent Polk, kent@eaenki.nde.swri.edu
  45.  
  46. def split(p):
  47.     i = string.rfind(p, '/')
  48.     j = string.rfind(p, ':')
  49.     if (i>j) and p[-1]=='/':
  50.         p=p[:-1]
  51.         i = string.rfind(p, '/')
  52.     if (j>i) or ((j>=0) and (i<0)): i=j
  53.     head, tail = p[:i+1], p[i+1:]
  54.     if head:
  55.         if head[-1]=='/': head=head[:-1]
  56.     return head, tail
  57.  
  58.  
  59. # Split a path in root and extension.
  60. # The extension is everything starting at the first dot in the last
  61. # pathname component; the root is everything before that.
  62. # It is always true that root + ext == p.
  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.     return root, ext
  79.  
  80.  
  81. # Split a pathname into a drive specification and the rest of the
  82. # path.  Useful on DOS/Windows/NT/Amiga; on Unix, the drive is always empty.
  83.  
  84. def splitdrive(p):
  85.     i = string.rfind(p,':') + 1
  86.     if i<=0: return '', p
  87.     return p[:i],p[i:]
  88.  
  89.  
  90. # Return the tail (basename) part of a path.
  91.  
  92. def basename(p):
  93.     return split(p)[1]
  94.  
  95.  
  96. # Return the head (dirname) part of a path.
  97.  
  98. def dirname(p):
  99.     return split(p)[0]
  100.  
  101.  
  102. # Get the full pathname of a file/directory (i.e. expand assigns).
  103.  
  104. fullpath = amiga.fullpath
  105.  
  106.  
  107. # Return the longest prefix of all list elements.
  108.  
  109. def commonprefix(m):
  110.     if not m: return ''
  111.     prefix = m[0]
  112.     for item in m:
  113.         for i in range(len(prefix)):
  114.             if prefix[:i+1] <> item[:i+1]:
  115.                 prefix = prefix[:i]
  116.                 if i == 0: return ''
  117.                 break
  118.     return prefix
  119.  
  120.  
  121. # Is a path a symbolic link?
  122. # This will always return false on systems where amiga.lstat doesn't exist.
  123. # (or where S_ISLNK isn't defined)
  124. def islink(path):
  125.     try:
  126.         st = amiga.lstat(path)
  127.         return stat.S_ISLNK(st[stat.ST_MODE])
  128.     except (amiga.error, AttributeError):
  129.         return 0
  130.  
  131.  
  132. # Does a path exist?
  133. # This is false for dangling symbolic links.
  134.  
  135. def exists(path):
  136.     try:
  137.         st = amiga.stat(path)
  138.     except amiga.error:
  139.         return 0
  140.     return 1
  141.  
  142.  
  143. # Is a path an amiga directory?
  144. # This follows symbolic links, so both islink() and isdir() can be true
  145. # for the same path.
  146.  
  147. def isdir(path):
  148.     try:
  149.         st = amiga.stat(path)
  150.     except amiga.error:
  151.         return 0
  152.     return stat.S_ISDIR(st[stat.ST_MODE])
  153.  
  154.  
  155. # Is a path a regular file?
  156. # This follows symbolic links, so both islink() and isfile() can be true
  157. # for the same path.
  158.  
  159. def isfile(path):
  160.     try:
  161.         st = amiga.stat(path)
  162.     except amiga.error:
  163.         return 0
  164.     return stat.S_ISREG(st[stat.ST_MODE])
  165.  
  166.  
  167. # Are two filenames really pointing to the same file?
  168.  
  169. def samefile(f1, f2):
  170.     s1 = amiga.stat(f1)
  171.     s2 = amiga.stat(f2)
  172.     return samestat(s1, s2)
  173.  
  174.  
  175. # Are two open files really referencing the same file?
  176. # (Not necessarily the same file descriptor!)
  177.  
  178. def sameopenfile(fp1, fp2):
  179.     s1 = amiga.fstat(fp1)
  180.     s2 = amiga.fstat(fp2)
  181.     return samestat(s1, s2)
  182.  
  183.  
  184. # Are two stat buffers (obtained from stat, fstat or lstat)
  185. # describing the same file?
  186.  
  187. def samestat(s1, s2):
  188.     return s1[stat.ST_INO] == s2[stat.ST_INO] and \
  189.         s1[stat.ST_DEV] == s2[stat.ST_DEV]
  190.  
  191.  
  192. # Is a path a mount point? AMIGA: Is it a device name?
  193.  
  194. def ismount(path):
  195.     drive,rest = splitdrive(path)
  196.     if rest=='':
  197.         return 1
  198.     else:
  199.         return 0
  200.  
  201.  
  202. # Directory tree walk.
  203. # For each directory under top (including top itself, but excluding
  204. # '.' and '..'), func(arg, dirname, filenames) is called, where
  205. # dirname is the name of the directory and filenames is the list
  206. # files files (and subdirectories etc.) in the directory.
  207. # The func may modify the filenames list, to implement a filter,
  208. # or to impose a different order of visiting.
  209.  
  210. def walk(top, func, arg):
  211.     try:
  212.         names = amiga.listdir(top)
  213.     except amiga.error:
  214.         return
  215.     func(arg, top, names)
  216.     for name in names:
  217.         name = join(top, name)
  218.         if isdir(name) and not islink(name):
  219.             walk(name, func, arg)
  220.  
  221.  
  222. # Expand paths beginning with '~' or '~user'.
  223. # '~' means $HOME; '~user' means that user's home directory.
  224. # If the path doesn't begin with '~', or if the user or $HOME is unknown,
  225. # the path is returned unchanged (leaving error reporting to whatever
  226. # function is called with the expanded path as argument).
  227. # See also module 'glob' for expansion of *, ? and [...] in pathnames.
  228. # (A function should also be defined to do full *sh-style environment
  229. # variable expansion.)
  230.  
  231. def expanduser(path):
  232.     if path[:1] <> '~':
  233.         return path
  234.     i, n = 1, len(path)
  235.     while i < n and path[i] <> '/':
  236.         i = i+1
  237.     if i == 1:
  238.         if not amiga.environ.has_key('HOME'):
  239.             return path
  240.         userhome = amiga.environ['HOME']
  241.     else:
  242.         try:
  243.             import pwd
  244.             pwent = pwd.getpwnam(path[1:i])
  245.         except (KeyError,ImportError,SystemError):
  246.             return path            # ~user only works if pwd module works
  247.         userhome = pwent[5]
  248.     return userhome + path[i:]
  249.  
  250.  
  251. # Expand paths containing shell variable substitutions.
  252. # This expands the forms $variable and ${variable} only.
  253. # Non-existant variables are left unchanged.
  254.  
  255. _varprog = None
  256.  
  257. def expandvars(path):
  258.     global _varprog
  259.     if '$' not in path:
  260.         return path
  261.     if not _varprog:
  262.         import regex
  263.         _varprog = regex.compile('$\([a-zA-Z0-9_]+\|{[^}]*}\)')
  264.     i = 0
  265.     while 1:
  266.         i = _varprog.search(path, i)
  267.         if i < 0:
  268.             break
  269.         name = _varprog.group(1)
  270.         j = i + len(_varprog.group(0))
  271.         if name[:1] == '{' and name[-1:] == '}':
  272.             name = name[1:-1]
  273.         if amiga.environ.has_key(name):
  274.             tail = path[j:]
  275.             path = path[:i] + amiga.environ[name]
  276.             i = len(path)
  277.             path = path + tail
  278.         else:
  279.             i = j
  280.     return path
  281.  
  282.  
  283. # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
  284. # It should be understood that this may change the meaning of the path
  285. # if it contains symbolic links!
  286. # XXX: Currently not implemented on Amiga. Probably not useful anyway
  287. #      because x/y//z is different than x/y/z on the Amiga.
  288.  
  289. def normpath(path):
  290.     return path
  291.  
  292.