home *** CD-ROM | disk | FTP | other *** search
- # module 'riscospath' -- pathname (or -related) operations for RiscOS
- # hacked from the mac version.
-
- import os
- import string
- import riscos
- from stat import *
-
-
- # Normalize the case of a pathname. Dummy in Posix, but string.lower here.
-
- normcase = string.lower
-
-
- # Return true if a path is absolute.
-
- def isabs(s):
- return '$' in s
-
-
- # Join two pathnames.
- # The result is equivalent to what the second pathname would refer to
- # if the first pathname were the current directory.
-
- def join(s, t):
- if (not s) or isabs(t): return t
- if s[-1] != '.': s=s+'.'
- return s + t
-
-
- # Split a pathname in two parts: the directory leading up to the final bit,
- # and the basename (the filename, without colons, in that directory).
- # The result (s, t) is such that join(s, t) yields the original argument.
-
- def split(s):
- if '.' not in s: return '', s
- stop = 0
- for i in range(len(s)):
- if s[i] == '.': stop = i+1
- return s[:stop-1], s[stop:]
-
-
- # Short interfaces to split()
-
- def dirname(s): return split(s)[0]
- def basename(s): return split(s)[1]
-
- # Split a path in root and extension.
- # The extension is everything starting at the first '/' in the last
- # pathname component; the root is everything before that.
- # It is always true that root + ext == p.
-
- def splitext(p):
- root, ext = '', ''
- for c in p:
- if c in '.':
- root, ext = root + ext + c, ''
- elif c == '/' or ext:
- ext = ext + c
- else:
- root = root + c
- return root, ext
-
- # Split into parts before and after a '$' ??
-
- def splitdrive(p):
- if '$' in p:
- n=string.index(p,'$')
- return p[:n],p[n:]
- else:
- return '',p
-
- # Return the longest prefix of all list elements.
-
- def commonprefix(m):
- if not m: return ''
- prefix = m[0]
- for item in m:
- for i in range(len(prefix)):
- if prefix[:i+1] <> item[:i+1]:
- prefix = prefix[:i]
- if i == 0: return ''
- break
- return prefix
-
- # Is a path a symbolic link?
- # This will always return false on systems where posix.lstat doesn't exist.
-
- def islink(path):
- return false
-
- # Return true if the pathname refers to an existing directory.
-
- def isdir(s):
- try:
- st = riscos.stat(s)
- except riscos.error:
- return 0
- return (st[ST_MODE]&S_IFDIR)!=0
-
-
- # Return true if the pathname refers to an existing regular file.
-
- def isfile(s):
- try:
- st = riscos.stat(s)
- except riscos.error:
- return 0
- return (st[ST_MODE]&S_IFREG)!=0
-
- # Return true if the pathname refers to an existing image file.
-
- def isimage(s):
- try:
- st = riscos.stat(s)
- except riscos.error:
- return 0
- return (st[ST_OBTYPE]==3)
-
- # Return true if the pathname refers to an existing file or directory.
-
- def exists(s):
- try:
- st = riscos.stat(s)
- except riscos.error:
- return 0
- return 1
-
-
- # Normalize path, removing things like ...:A:..:...
-
- def normpath(s):
- try:
- return string.lower(riscos.expand(s))
- except:
- return string.lower(s)
-
- def expandvars(s):
- try:
- return riscos.expand(s)
- except:
- return s
-
- # Directory tree walk.
- # For each directory under top (including top itself),
- # func(arg, dirname, filenames) is called, where
- # dirname is the name of the directory and filenames is the list
- # files files (and subdirectories etc.) in the directory.
- # The func may modify the filenames list, to implement a filter,
- # or to impose a different order of visiting.
-
- def walk(top, func, arg):
- try:
- names = os.listdir(top)
- except os.error:
- return
- func(arg, top, names)
- for name in names:
- name = join(top, name)
- if isdir(name):
- walk(name, func, arg)