Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 1 | # 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 | """ |
| 11 | Instead of importing this module directly, import os and refer to this module |
| 12 | as os.path. |
| 13 | """ |
| 14 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 15 | # strings representing various path-related bits and pieces |
| 16 | curdir = '@' |
| 17 | pardir = '^' |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 18 | extsep = '/' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 19 | sep = '.' |
| 20 | pathsep = ',' |
| 21 | defpath = '<Run$Dir>' |
| 22 | altsep = None |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 23 | |
| 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 | |
| 27 | import os, stat, string |
| 28 | |
| 29 | try: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 30 | import swi |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 31 | except ImportError: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 32 | class _swi: |
| 33 | def swi(*a): |
| 34 | raise AttributeError, 'This function only available under RISC OS' |
| 35 | block= swi |
| 36 | swi= _swi() |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 37 | |
| 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 | |
| 55 | def _split(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 56 | """ |
| 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 59 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 60 | dash= _allowMOSFSNames and p[:1]=='-' |
| 61 | if dash: |
| 62 | q= string.find(p, '-', 1)+1 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 63 | else: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 64 | if p[:1]==':': |
| 65 | q= 0 |
| 66 | else: |
| 67 | q= string.find(p, ':')+1 # q= index of start of non-FS portion of path |
| 68 | s= string.find(p, '#') |
| 69 | 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 Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 73 | if c not in string.ascii_letters: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 74 | q= 0 |
| 75 | break # disallow invalid non-special-field characters in FS name |
| 76 | r= q |
| 77 | if p[q:q+1]==':': |
| 78 | r= string.find(p, '.', q+1)+1 |
| 79 | 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | def normcase(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 85 | """ |
| 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 89 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 90 | return string.lower(p) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | def isabs(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 94 | """ |
| 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 101 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 102 | (fs, drive, path)= _split(p) |
| 103 | return drive!='' or path[:1] in _roots |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def join(a, *p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 107 | """ |
| 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 110 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 111 | j= a |
| 112 | for b in p: |
| 113 | (fs, drive, path)= _split(b) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 114 | if j=='' or fs!='' or drive!='' or path[:1] in _roots: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 115 | j= b |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 116 | elif j[-1]==':': |
| 117 | j= j+b |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 118 | else: |
| 119 | j= j+'.'+b |
| 120 | return j |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | def split(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 124 | """ |
| 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 127 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 128 | (fs, drive, path)= _split(p) |
| 129 | q= string.rfind(path, '.') |
| 130 | if q!=-1: |
| 131 | return (fs+drive+path[:q], path[q+1:]) |
| 132 | return ('', p) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | def splitext(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 136 | """ |
| 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 139 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 140 | (tail, head)= split(p) |
| 141 | if '/' in head: |
| 142 | q= len(head)-string.rfind(head, '/') |
| 143 | return (p[:-q], p[-q:]) |
| 144 | return (p, '') |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | def splitdrive(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 148 | """ |
| 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 Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 152 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 153 | (fs, drive, path)= _split(p) |
| 154 | return (fs+drive, p) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | def basename(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 158 | """ |
| 159 | Return the tail (basename) part of a path. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 160 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 161 | return split(p)[1] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | def dirname(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 165 | """ |
| 166 | Return the head (dirname) part of a path. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 167 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 168 | return split(p)[0] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | def commonprefix(ps): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 172 | """ |
| 173 | Return the longest prefix of all list elements. Purely string-based; does not |
| 174 | separate any path parts. Why am I in os.path? |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 175 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 176 | if len(ps)==0: |
| 177 | return '' |
| 178 | prefix= ps[0] |
| 179 | for p in ps[1:]: |
| 180 | prefix= prefix[:len(p)] |
| 181 | for i in range(len(prefix)): |
| 182 | if prefix[i] <> p[i]: |
| 183 | prefix= prefix[:i] |
| 184 | if i==0: |
| 185 | return '' |
| 186 | break |
| 187 | return prefix |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | ## File access functions. Why are we in os.path? |
| 191 | |
| 192 | def getsize(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 193 | """ |
| 194 | Return the size of a file, reported by os.stat(). |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 195 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 196 | st= os.stat(p) |
| 197 | return st[stat.ST_SIZE] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | def getmtime(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 201 | """ |
| 202 | Return the last modification time of a file, reported by os.stat(). |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 203 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 204 | st = os.stat(p) |
| 205 | return st[stat.ST_MTIME] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 206 | |
| 207 | getatime= getmtime |
| 208 | |
| 209 | |
| 210 | # RISC OS-specific file access functions |
| 211 | |
| 212 | def exists(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 213 | """ |
| 214 | Test whether a path exists. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 215 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 216 | try: |
| 217 | return swi.swi('OS_File', '5s;i', p)!=0 |
| 218 | except swi.error: |
| 219 | return 0 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 220 | |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 221 | lexists = exists |
| 222 | |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 223 | |
| 224 | def isdir(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 225 | """ |
| 226 | Is a path a directory? Includes image files. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 227 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 228 | try: |
| 229 | return swi.swi('OS_File', '5s;i', p) in [2, 3] |
| 230 | except swi.error: |
| 231 | return 0 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 232 | |
| 233 | |
| 234 | def isfile(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 235 | """ |
| 236 | Test whether a path is a file, including image files. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 237 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 238 | try: |
| 239 | return swi.swi('OS_File', '5s;i', p) in [1, 3] |
| 240 | except swi.error: |
| 241 | return 0 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 242 | |
| 243 | |
| 244 | def islink(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 245 | """ |
| 246 | RISC OS has no links or mounts. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 247 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 248 | return _false |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 249 | |
| 250 | ismount= islink |
| 251 | |
| 252 | |
| 253 | # Same-file testing. |
| 254 | |
| 255 | # samefile works on filename comparison since there is no ST_DEV and ST_INO is |
| 256 | # not reliably unique (esp. directories). First it has to normalise the |
| 257 | # pathnames, which it can do 'properly' using OS_FSControl since samefile can |
| 258 | # assume it's running on RISC OS (unlike normpath). |
| 259 | |
| 260 | def samefile(fa, fb): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 261 | """ |
| 262 | Test whether two pathnames reference the same actual file. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 263 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 264 | l= 512 |
| 265 | b= swi.block(l) |
| 266 | swi.swi('OS_FSControl', 'isb..i', 37, fa, b, l) |
| 267 | fa= b.ctrlstring() |
| 268 | swi.swi('OS_FSControl', 'isb..i', 37, fb, b, l) |
| 269 | fb= b.ctrlstring() |
| 270 | return fa==fb |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | def sameopenfile(a, b): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 274 | """ |
| 275 | Test whether two open file objects reference the same file. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 276 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 277 | return os.fstat(a)[stat.ST_INO]==os.fstat(b)[stat.ST_INO] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 278 | |
| 279 | |
| 280 | ## Path canonicalisation |
| 281 | |
| 282 | # 'user directory' is taken as meaning the User Root Directory, which is in |
| 283 | # practice never used, for anything. |
| 284 | |
| 285 | def expanduser(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 286 | (fs, drive, path)= _split(p) |
| 287 | l= 512 |
| 288 | b= swi.block(l) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 289 | |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 290 | if path[:1]!='@': |
| 291 | return p |
| 292 | if fs=='': |
| 293 | fsno= swi.swi('OS_Args', '00;i') |
| 294 | swi.swi('OS_FSControl', 'iibi', 33, fsno, b, l) |
| 295 | fsname= b.ctrlstring() |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 296 | else: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 297 | if fs[:1]=='-': |
| 298 | fsname= fs[1:-1] |
| 299 | else: |
| 300 | fsname= fs[:-1] |
| 301 | fsname= string.split(fsname, '#', 1)[0] # remove special field from fs |
| 302 | x= swi.swi('OS_FSControl', 'ib2s.i;.....i', 54, b, fsname, l) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 303 | if x<l: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 304 | urd= b.tostring(0, l-x-1) |
| 305 | else: # no URD! try CSD |
| 306 | x= swi.swi('OS_FSControl', 'ib0s.i;.....i', 54, b, fsname, l) |
| 307 | if x<l: |
| 308 | urd= b.tostring(0, l-x-1) |
| 309 | else: # no CSD! use root |
| 310 | urd= '$' |
| 311 | return fsname+':'+urd+path[1:] |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 312 | |
| 313 | # Environment variables are in angle brackets. |
| 314 | |
| 315 | def expandvars(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 316 | """ |
| 317 | Expand environment variables using OS_GSTrans. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 318 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 319 | l= 512 |
| 320 | b= swi.block(l) |
| 321 | return b.tostring(0, swi.swi('OS_GSTrans', 'sbi;..i', p, b, l)) |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 322 | |
| 323 | |
Tim Peters | 1422e9d | 2001-12-15 22:12:47 +0000 | [diff] [blame] | 324 | # Return an absolute path. RISC OS' osfscontrol_canonicalise_path does this among others |
| 325 | abspath = os.expand |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 326 | |
| 327 | |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 328 | # realpath is a no-op on systems without islink support |
| 329 | realpath = abspath |
| 330 | |
| 331 | |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 332 | # Normalize a path. Only special path element under RISC OS is "^" for "..". |
| 333 | |
| 334 | def normpath(p): |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 335 | """ |
| 336 | Normalize path, eliminating up-directory ^s. |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 337 | """ |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 338 | (fs, drive, path)= _split(p) |
| 339 | rhs= '' |
| 340 | ups= 0 |
| 341 | while path!='': |
| 342 | (path, el)= split(path) |
| 343 | if el=='^': |
| 344 | ups= ups+1 |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 345 | else: |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 346 | if ups>0: |
| 347 | ups= ups-1 |
| 348 | else: |
| 349 | if rhs=='': |
| 350 | rhs= el |
| 351 | else: |
| 352 | rhs= el+'.'+rhs |
| 353 | while ups>0: |
| 354 | ups= ups-1 |
| 355 | rhs= '^.'+rhs |
| 356 | return fs+drive+rhs |
Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 357 | |
| 358 | |
| 359 | # Directory tree walk. |
| 360 | # Independent of host system. Why am I in os.path? |
| 361 | |
| 362 | def walk(top, func, arg): |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 363 | """Directory tree walk with callback function. |
| 364 | |
| 365 | For each directory in the directory tree rooted at top (including top |
| 366 | itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
| 367 | dirname is the name of the directory, and fnames a list of the names of |
| 368 | the files and subdirectories in dirname (excluding '.' and '..'). func |
| 369 | may modify the fnames list in-place (e.g. via del or slice assignment), |
| 370 | and walk will only recurse into the subdirectories whose names remain in |
| 371 | fnames; this can be used to implement a filter, or to impose a specific |
| 372 | order of visiting. No semantics are defined for, or required of, arg, |
| 373 | beyond that arg is always passed to func. It can be used, e.g., to pass |
| 374 | a filename pattern, or a mutable object designed to accumulate |
| 375 | statistics. Passing None for arg is common.""" |
| 376 | |
Tim Peters | 683ecc7 | 2001-07-02 04:59:35 +0000 | [diff] [blame] | 377 | try: |
| 378 | names= os.listdir(top) |
| 379 | except os.error: |
| 380 | return |
| 381 | func(arg, top, names) |
| 382 | for name in names: |
| 383 | name= join(top, name) |
| 384 | if isdir(name) and not islink(name): |
| 385 | walk(name, func, arg) |