blob: b9d1062d3037e966989f41ebd89e85e231263f0f [file] [log] [blame]
Guido van Rossum228d8072001-03-02 05:58:11 +00001# Module 'riscospath' -- common operations on RISC OS pathnames.
2
3# contributed by Andrew Clover ( andrew@oaktree.co.uk )
4
5# The "os.path" name is an alias for this module on RISC OS systems;
6# on other systems (e.g. Mac, Windows), os.path provides the same
7# operations in a manner specific to that platform, and is an alias
8# to another module (e.g. macpath, ntpath).
9
10"""
11Instead of importing this module directly, import os and refer to this module
12as os.path.
13"""
14
Skip Montanaro117910d2003-02-14 19:35:31 +000015# strings representing various path-related bits and pieces
16curdir = '@'
17pardir = '^'
Martin v. Löwisa94568a2003-05-10 07:36:56 +000018extsep = '/'
Skip Montanaro117910d2003-02-14 19:35:31 +000019sep = '.'
20pathsep = ','
21defpath = '<Run$Dir>'
22altsep = None
Guido van Rossum228d8072001-03-02 05:58:11 +000023
24# Imports - make an error-generating swi object if the swi module is not
25# available (ie. we are not running on RISC OS Python)
26
27import os, stat, string
28
29try:
Tim Peters683ecc72001-07-02 04:59:35 +000030 import swi
Guido van Rossum228d8072001-03-02 05:58:11 +000031except ImportError:
Tim Peters683ecc72001-07-02 04:59:35 +000032 class _swi:
33 def swi(*a):
34 raise AttributeError, 'This function only available under RISC OS'
35 block= swi
36 swi= _swi()
Guido van Rossum228d8072001-03-02 05:58:11 +000037
38[_false, _true]= range(2)
39
40_roots= ['$', '&', '%', '@', '\\']
41
42
43# _allowMOSFSNames
44# After importing riscospath, set _allowMOSFSNames true if you want the module
45# to understand the "-SomeFS-" notation left over from the old BBC Master MOS,
46# as well as the standard "SomeFS:" notation. Set this to be fully backwards
47# compatible but remember that "-SomeFS-" can also be a perfectly valid file
48# name so care must be taken when splitting and joining paths.
49
50_allowMOSFSNames= _false
51
52
53## Path manipulation, RISC OS stylee.
54
55def _split(p):
Tim Peters683ecc72001-07-02 04:59:35 +000056 """
57 split filing system name (including special field) and drive specifier from rest
58 of path. This is needed by many riscospath functions.
Guido van Rossum228d8072001-03-02 05:58:11 +000059 """
Tim Peters683ecc72001-07-02 04:59:35 +000060 dash= _allowMOSFSNames and p[:1]=='-'
61 if dash:
Neal Norwitz9d72bb42007-04-17 08:48:32 +000062 q= p.find('-', 1)+1
Guido van Rossum228d8072001-03-02 05:58:11 +000063 else:
Tim Peters683ecc72001-07-02 04:59:35 +000064 if p[:1]==':':
65 q= 0
66 else:
Neal Norwitz9d72bb42007-04-17 08:48:32 +000067 q= p.find(':')+1 # q= index of start of non-FS portion of path
68 s= p.find('#')
Tim Peters683ecc72001-07-02 04:59:35 +000069 if s==-1 or s>q:
70 s= q # find end of main FS name, not including special field
71 else:
72 for c in p[dash:s]:
Fred Drake79e75e12001-07-20 19:05:50 +000073 if c not in string.ascii_letters:
Tim Peters683ecc72001-07-02 04:59:35 +000074 q= 0
75 break # disallow invalid non-special-field characters in FS name
76 r= q
77 if p[q:q+1]==':':
Neal Norwitz9d72bb42007-04-17 08:48:32 +000078 r= p.find('.', q+1)+1
Tim Peters683ecc72001-07-02 04:59:35 +000079 if r==0:
80 r= len(p) # find end of drive name (if any) following FS name (if any)
81 return (p[:q], p[q:r], p[r:])
Guido van Rossum228d8072001-03-02 05:58:11 +000082
83
84def normcase(p):
Tim Peters683ecc72001-07-02 04:59:35 +000085 """
86 Normalize the case of a pathname. This converts to lowercase as the native RISC
87 OS filesystems are case-insensitive. However, not all filesystems have to be,
88 and there's no simple way to find out what type an FS is argh.
Guido van Rossum228d8072001-03-02 05:58:11 +000089 """
Neal Norwitz9d72bb42007-04-17 08:48:32 +000090 return p.lower()
Guido van Rossum228d8072001-03-02 05:58:11 +000091
92
93def isabs(p):
Tim Peters683ecc72001-07-02 04:59:35 +000094 """
95 Return whether a path is absolute. Under RISC OS, a file system specifier does
96 not make a path absolute, but a drive name or number does, and so does using the
97 symbol for root, URD, library, CSD or PSD. This means it is perfectly possible
98 to have an "absolute" URL dependent on the current working directory, and
99 equally you can have a "relative" URL that's on a completely different device to
100 the current one argh.
Guido van Rossum228d8072001-03-02 05:58:11 +0000101 """
Tim Peters683ecc72001-07-02 04:59:35 +0000102 (fs, drive, path)= _split(p)
103 return drive!='' or path[:1] in _roots
Guido van Rossum228d8072001-03-02 05:58:11 +0000104
105
106def join(a, *p):
Tim Peters683ecc72001-07-02 04:59:35 +0000107 """
108 Join path elements with the directory separator, replacing the entire path when
109 an absolute or FS-changing path part is found.
Guido van Rossum228d8072001-03-02 05:58:11 +0000110 """
Tim Peters683ecc72001-07-02 04:59:35 +0000111 j= a
112 for b in p:
113 (fs, drive, path)= _split(b)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000114 if j=='' or fs!='' or drive!='' or path[:1] in _roots:
Tim Peters683ecc72001-07-02 04:59:35 +0000115 j= b
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000116 elif j[-1]==':':
117 j= j+b
Tim Peters683ecc72001-07-02 04:59:35 +0000118 else:
119 j= j+'.'+b
120 return j
Guido van Rossum228d8072001-03-02 05:58:11 +0000121
122
123def split(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000124 """
125 Split a path in head (everything up to the last '.') and tail (the rest). FS
126 name must still be dealt with separately since special field may contain '.'.
Guido van Rossum228d8072001-03-02 05:58:11 +0000127 """
Tim Peters683ecc72001-07-02 04:59:35 +0000128 (fs, drive, path)= _split(p)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000129 q= path.rfind('.')
Tim Peters683ecc72001-07-02 04:59:35 +0000130 if q!=-1:
131 return (fs+drive+path[:q], path[q+1:])
132 return ('', p)
Guido van Rossum228d8072001-03-02 05:58:11 +0000133
134
135def splitext(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000136 """
137 Split a path in root and extension. This assumes the 'using slash for dot and
138 dot for slash with foreign files' convention common in RISC OS is in force.
Guido van Rossum228d8072001-03-02 05:58:11 +0000139 """
Tim Peters683ecc72001-07-02 04:59:35 +0000140 (tail, head)= split(p)
141 if '/' in head:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000142 q= len(head)-head.rfind('/')
Tim Peters683ecc72001-07-02 04:59:35 +0000143 return (p[:-q], p[-q:])
144 return (p, '')
Guido van Rossum228d8072001-03-02 05:58:11 +0000145
146
147def splitdrive(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000148 """
149 Split a pathname into a drive specification (including FS name) and the rest of
150 the path. The terminating dot of the drive name is included in the drive
151 specification.
Guido van Rossum228d8072001-03-02 05:58:11 +0000152 """
Tim Peters683ecc72001-07-02 04:59:35 +0000153 (fs, drive, path)= _split(p)
154 return (fs+drive, p)
Guido van Rossum228d8072001-03-02 05:58:11 +0000155
156
157def basename(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000158 """
159 Return the tail (basename) part of a path.
Guido van Rossum228d8072001-03-02 05:58:11 +0000160 """
Tim Peters683ecc72001-07-02 04:59:35 +0000161 return split(p)[1]
Guido van Rossum228d8072001-03-02 05:58:11 +0000162
163
164def dirname(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000165 """
166 Return the head (dirname) part of a path.
Guido van Rossum228d8072001-03-02 05:58:11 +0000167 """
Tim Peters683ecc72001-07-02 04:59:35 +0000168 return split(p)[0]
Guido van Rossum228d8072001-03-02 05:58:11 +0000169
170
Georg Brandl649f8e72005-08-03 07:30:12 +0000171def commonprefix(m):
172 "Given a list of pathnames, returns the longest common leading component"
173 if not m: return ''
174 s1 = min(m)
175 s2 = max(m)
176 n = min(len(s1), len(s2))
Guido van Rossum805365e2007-05-07 22:24:25 +0000177 for i in range(n):
Georg Brandl649f8e72005-08-03 07:30:12 +0000178 if s1[i] != s2[i]:
179 return s1[:i]
180 return s1[:n]
Guido van Rossum228d8072001-03-02 05:58:11 +0000181
182
183## File access functions. Why are we in os.path?
184
185def getsize(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000186 """
187 Return the size of a file, reported by os.stat().
Guido van Rossum228d8072001-03-02 05:58:11 +0000188 """
Tim Peters683ecc72001-07-02 04:59:35 +0000189 st= os.stat(p)
190 return st[stat.ST_SIZE]
Guido van Rossum228d8072001-03-02 05:58:11 +0000191
192
193def getmtime(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000194 """
195 Return the last modification time of a file, reported by os.stat().
Guido van Rossum228d8072001-03-02 05:58:11 +0000196 """
Tim Peters683ecc72001-07-02 04:59:35 +0000197 st = os.stat(p)
198 return st[stat.ST_MTIME]
Guido van Rossum228d8072001-03-02 05:58:11 +0000199
200getatime= getmtime
201
202
203# RISC OS-specific file access functions
204
205def exists(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000206 """
207 Test whether a path exists.
Guido van Rossum228d8072001-03-02 05:58:11 +0000208 """
Tim Peters683ecc72001-07-02 04:59:35 +0000209 try:
210 return swi.swi('OS_File', '5s;i', p)!=0
211 except swi.error:
212 return 0
Guido van Rossum228d8072001-03-02 05:58:11 +0000213
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000214lexists = exists
215
Guido van Rossum228d8072001-03-02 05:58:11 +0000216
217def isdir(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000218 """
219 Is a path a directory? Includes image files.
Guido van Rossum228d8072001-03-02 05:58:11 +0000220 """
Tim Peters683ecc72001-07-02 04:59:35 +0000221 try:
222 return swi.swi('OS_File', '5s;i', p) in [2, 3]
223 except swi.error:
224 return 0
Guido van Rossum228d8072001-03-02 05:58:11 +0000225
226
227def isfile(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000228 """
229 Test whether a path is a file, including image files.
Guido van Rossum228d8072001-03-02 05:58:11 +0000230 """
Tim Peters683ecc72001-07-02 04:59:35 +0000231 try:
232 return swi.swi('OS_File', '5s;i', p) in [1, 3]
233 except swi.error:
234 return 0
Guido van Rossum228d8072001-03-02 05:58:11 +0000235
236
237def islink(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000238 """
239 RISC OS has no links or mounts.
Guido van Rossum228d8072001-03-02 05:58:11 +0000240 """
Tim Peters683ecc72001-07-02 04:59:35 +0000241 return _false
Guido van Rossum228d8072001-03-02 05:58:11 +0000242
243ismount= islink
244
245
246# Same-file testing.
247
248# samefile works on filename comparison since there is no ST_DEV and ST_INO is
249# not reliably unique (esp. directories). First it has to normalise the
250# pathnames, which it can do 'properly' using OS_FSControl since samefile can
251# assume it's running on RISC OS (unlike normpath).
252
253def samefile(fa, fb):
Tim Peters683ecc72001-07-02 04:59:35 +0000254 """
255 Test whether two pathnames reference the same actual file.
Guido van Rossum228d8072001-03-02 05:58:11 +0000256 """
Tim Peters683ecc72001-07-02 04:59:35 +0000257 l= 512
258 b= swi.block(l)
259 swi.swi('OS_FSControl', 'isb..i', 37, fa, b, l)
260 fa= b.ctrlstring()
261 swi.swi('OS_FSControl', 'isb..i', 37, fb, b, l)
262 fb= b.ctrlstring()
263 return fa==fb
Guido van Rossum228d8072001-03-02 05:58:11 +0000264
265
266def sameopenfile(a, b):
Tim Peters683ecc72001-07-02 04:59:35 +0000267 """
268 Test whether two open file objects reference the same file.
Guido van Rossum228d8072001-03-02 05:58:11 +0000269 """
Tim Peters683ecc72001-07-02 04:59:35 +0000270 return os.fstat(a)[stat.ST_INO]==os.fstat(b)[stat.ST_INO]
Guido van Rossum228d8072001-03-02 05:58:11 +0000271
272
273## Path canonicalisation
274
275# 'user directory' is taken as meaning the User Root Directory, which is in
276# practice never used, for anything.
277
278def expanduser(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000279 (fs, drive, path)= _split(p)
280 l= 512
281 b= swi.block(l)
Guido van Rossum228d8072001-03-02 05:58:11 +0000282
Tim Peters683ecc72001-07-02 04:59:35 +0000283 if path[:1]!='@':
284 return p
285 if fs=='':
286 fsno= swi.swi('OS_Args', '00;i')
287 swi.swi('OS_FSControl', 'iibi', 33, fsno, b, l)
288 fsname= b.ctrlstring()
Guido van Rossum228d8072001-03-02 05:58:11 +0000289 else:
Tim Peters683ecc72001-07-02 04:59:35 +0000290 if fs[:1]=='-':
291 fsname= fs[1:-1]
292 else:
293 fsname= fs[:-1]
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000294 fsname= fsname.split('#', 1)[0] # remove special field from fs
Tim Peters683ecc72001-07-02 04:59:35 +0000295 x= swi.swi('OS_FSControl', 'ib2s.i;.....i', 54, b, fsname, l)
Guido van Rossum228d8072001-03-02 05:58:11 +0000296 if x<l:
Tim Peters683ecc72001-07-02 04:59:35 +0000297 urd= b.tostring(0, l-x-1)
298 else: # no URD! try CSD
299 x= swi.swi('OS_FSControl', 'ib0s.i;.....i', 54, b, fsname, l)
300 if x<l:
301 urd= b.tostring(0, l-x-1)
302 else: # no CSD! use root
303 urd= '$'
304 return fsname+':'+urd+path[1:]
Guido van Rossum228d8072001-03-02 05:58:11 +0000305
306# Environment variables are in angle brackets.
307
308def expandvars(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000309 """
310 Expand environment variables using OS_GSTrans.
Guido van Rossum228d8072001-03-02 05:58:11 +0000311 """
Tim Peters683ecc72001-07-02 04:59:35 +0000312 l= 512
313 b= swi.block(l)
314 return b.tostring(0, swi.swi('OS_GSTrans', 'sbi;..i', p, b, l))
Guido van Rossum228d8072001-03-02 05:58:11 +0000315
316
Tim Peters1422e9d2001-12-15 22:12:47 +0000317# Return an absolute path. RISC OS' osfscontrol_canonicalise_path does this among others
318abspath = os.expand
Guido van Rossum228d8072001-03-02 05:58:11 +0000319
320
Guido van Rossum83eeef42001-09-17 15:16:09 +0000321# realpath is a no-op on systems without islink support
322realpath = abspath
323
324
Guido van Rossum228d8072001-03-02 05:58:11 +0000325# Normalize a path. Only special path element under RISC OS is "^" for "..".
326
327def normpath(p):
Tim Peters683ecc72001-07-02 04:59:35 +0000328 """
329 Normalize path, eliminating up-directory ^s.
Guido van Rossum228d8072001-03-02 05:58:11 +0000330 """
Tim Peters683ecc72001-07-02 04:59:35 +0000331 (fs, drive, path)= _split(p)
332 rhs= ''
333 ups= 0
334 while path!='':
335 (path, el)= split(path)
336 if el=='^':
337 ups= ups+1
Guido van Rossum228d8072001-03-02 05:58:11 +0000338 else:
Tim Peters683ecc72001-07-02 04:59:35 +0000339 if ups>0:
340 ups= ups-1
341 else:
342 if rhs=='':
343 rhs= el
344 else:
345 rhs= el+'.'+rhs
346 while ups>0:
347 ups= ups-1
348 rhs= '^.'+rhs
349 return fs+drive+rhs
Guido van Rossum228d8072001-03-02 05:58:11 +0000350
351
352# Directory tree walk.
353# Independent of host system. Why am I in os.path?
354
355def walk(top, func, arg):
Tim Peterscf5e6a42001-10-10 04:16:20 +0000356 """Directory tree walk with callback function.
357
358 For each directory in the directory tree rooted at top (including top
359 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
360 dirname is the name of the directory, and fnames a list of the names of
361 the files and subdirectories in dirname (excluding '.' and '..'). func
362 may modify the fnames list in-place (e.g. via del or slice assignment),
363 and walk will only recurse into the subdirectories whose names remain in
364 fnames; this can be used to implement a filter, or to impose a specific
365 order of visiting. No semantics are defined for, or required of, arg,
366 beyond that arg is always passed to func. It can be used, e.g., to pass
367 a filename pattern, or a mutable object designed to accumulate
368 statistics. Passing None for arg is common."""
369
Tim Peters683ecc72001-07-02 04:59:35 +0000370 try:
371 names= os.listdir(top)
372 except os.error:
373 return
374 func(arg, top, names)
375 for name in names:
376 name= join(top, name)
377 if isdir(name) and not islink(name):
378 walk(name, func, arg)