src/eric7/SystemUtilities/FileSystemUtilities.py

branch
eric7
changeset 10393
434e1b0bc49e
parent 10388
a34ce7f42e8b
child 10396
2f72e9330af2
equal deleted inserted replaced
10392:ef315a1fdd4a 10393:434e1b0bc49e
44 def normcasepath(path): 44 def normcasepath(path):
45 """ 45 """
46 Function returning a path, that is normalized with respect to its case 46 Function returning a path, that is normalized with respect to its case
47 and references. 47 and references.
48 48
49 @param path file path (string) 49 @param path file path
50 @return case normalized path (string) 50 @type str
51 @return case normalized path
52 @rtype str
51 """ 53 """
52 return os.path.normcase(os.path.normpath(path)) 54 return os.path.normcase(os.path.normpath(path))
53 55
54 56
55 def normcaseabspath(path): 57 def normcaseabspath(path):
56 """ 58 """
57 Function returning an absolute path, that is normalized with respect to 59 Function returning an absolute path, that is normalized with respect to
58 its case and references. 60 its case and references.
59 61
60 @param path file path (string) 62 @param path file path
61 @return absolute, normalized path (string) 63 @type str
64 @return absolute, normalized path
65 @rtype str
62 """ 66 """
63 return os.path.normcase(os.path.abspath(path)) 67 return os.path.normcase(os.path.abspath(path))
64 68
65 69
66 def normjoinpath(a, *p): 70 def normjoinpath(a, *p):
67 """ 71 """
68 Function returning a normalized path of the joined parts passed into it. 72 Function returning a normalized path of the joined parts passed into it.
69 73
70 @param a first path to be joined (string) 74 @param a first path to be joined
71 @param p variable number of path parts to be joined (string) 75 @type str
72 @return normalized path (string) 76 @param p variable number of path parts to be joined
77 @type str
78 @return normalized path
79 @rtype str
73 """ 80 """
74 return os.path.normpath(os.path.join(a, *p)) 81 return os.path.normpath(os.path.join(a, *p))
75 82
76 83
77 def normabsjoinpath(a, *p): 84 def normabsjoinpath(a, *p):
78 """ 85 """
79 Function returning a normalized, absolute path of the joined parts passed 86 Function returning a normalized, absolute path of the joined parts passed
80 into it. 87 into it.
81 88
82 @param a first path to be joined (string) 89 @param a first path to be joined
83 @param p variable number of path parts to be joind (string) 90 @type str
84 @return absolute, normalized path (string) 91 @param p variable number of path parts to be joined
92 @type str
93 @return absolute, normalized path
94 @rtype str
85 """ 95 """
86 return os.path.abspath(os.path.join(a, *p)) 96 return os.path.abspath(os.path.join(a, *p))
87 97
88 98
89 def isinpath(file): 99 def isinpath(file):
90 """ 100 """
91 Function to check for an executable file. 101 Function to check for an executable file.
92 102
93 @param file filename of the executable to check (string) 103 @param file filename of the executable to check
94 @return flag to indicate, if the executable file is accessible 104 @type str
95 via the searchpath defined by the PATH environment variable. 105 @return flag indicating, if the executable file is accessible via the executable
106 search path defined by the PATH environment variable.
107 @rtype bool
96 """ 108 """
97 if os.path.isabs(file): 109 if os.path.isabs(file):
98 return os.access(file, os.X_OK) 110 return os.access(file, os.X_OK)
99 111
100 if os.path.exists(os.path.join(os.curdir, file)): 112 if os.path.exists(os.path.join(os.curdir, file)):
130 def relativeUniversalPath(path, start): 142 def relativeUniversalPath(path, start):
131 """ 143 """
132 Function to convert a file path to a path relative to a start path 144 Function to convert a file path to a path relative to a start path
133 with universal separators. 145 with universal separators.
134 146
135 @param path file or directory name to convert (string) 147 @param path file or directory name to convert
136 @param start start path (string) 148 @type str
149 @param start start path
150 @type str
137 @return relative path or unchanged path, if path does not start with 151 @return relative path or unchanged path, if path does not start with
138 the start path with universal separators (string) 152 the start path with universal separators
153 @rtype str
139 """ 154 """
140 return fromNativeSeparators(os.path.relpath(path, start)) 155 return fromNativeSeparators(os.path.relpath(path, start))
141 156
142 157
143 def absolutePath(path, start): 158 def absolutePath(path, start):
144 """ 159 """
145 Public method to convert a path relative to a start path to an 160 Public method to convert a path relative to a start path to an
146 absolute path. 161 absolute path.
147 162
148 @param path file or directory name to convert (string) 163 @param path file or directory name to convert
149 @param start start path (string) 164 @type str
150 @return absolute path (string) 165 @param start start path
166 @type str
167 @return absolute path
168 @rtype str
151 """ 169 """
152 if not os.path.isabs(path): 170 if not os.path.isabs(path):
153 path = os.path.normpath(os.path.join(start, path)) 171 path = os.path.normpath(os.path.join(start, path))
154 return path 172 return path
155 173
157 def absoluteUniversalPath(path, start): 175 def absoluteUniversalPath(path, start):
158 """ 176 """
159 Public method to convert a path relative to a start path with 177 Public method to convert a path relative to a start path with
160 universal separators to an absolute path. 178 universal separators to an absolute path.
161 179
162 @param path file or directory name to convert (string) 180 @param path file or directory name to convert
163 @param start start path (string) 181 @type str
164 @return absolute path with native separators (string) 182 @param start start path
183 @type str
184 @return absolute path with native separators
185 @rtype str
165 """ 186 """
166 if not os.path.isabs(path): 187 if not os.path.isabs(path):
167 path = toNativeSeparators(os.path.normpath(os.path.join(start, path))) 188 path = toNativeSeparators(os.path.normpath(os.path.join(start, path)))
168 return path 189 return path
169 190
170 191
171 def getExecutablePath(file): 192 def getExecutablePath(file):
172 """ 193 """
173 Function to build the full path of an executable file from the environment. 194 Function to build the full path of an executable file from the environment.
174 195
175 @param file filename of the executable to check (string) 196 @param file filename of the executable to check
197 @type str
176 @return full executable name, if the executable file is accessible 198 @return full executable name, if the executable file is accessible
177 via the searchpath defined by the PATH environment variable, or an 199 via the executable search path defined by the PATH environment variable, or an
178 empty string otherwise. 200 empty string otherwise.
201 @rtype str
179 """ 202 """
180 if os.path.isabs(file): 203 if os.path.isabs(file):
181 if os.access(file, os.X_OK): 204 if os.access(file, os.X_OK):
182 return file 205 return file
183 else: 206 else:
204 227
205 def getExecutablePaths(file): 228 def getExecutablePaths(file):
206 """ 229 """
207 Function to build all full path of an executable file from the environment. 230 Function to build all full path of an executable file from the environment.
208 231
209 @param file filename of the executable (string) 232 @param file filename of the executable
210 @return list of full executable names (list of strings), if the executable 233 @type str
211 file is accessible via the searchpath defined by the PATH environment 234 @return list of full executable names, if the executable file is accessible via
212 variable, or an empty list otherwise. 235 the executable search path defined by the PATH environment variable, or an
236 empty list otherwise.
237 @rtype list of str
213 """ 238 """
214 paths = [] 239 paths = []
215 240
216 if os.path.isabs(file): 241 if os.path.isabs(file):
217 if os.access(file, os.X_OK): 242 if os.access(file, os.X_OK):
243 268
244 First an executable with the extension .exe is searched for, thereafter 269 First an executable with the extension .exe is searched for, thereafter
245 such with the extensions .cmd or .bat and finally the given file name as 270 such with the extensions .cmd or .bat and finally the given file name as
246 is. The first match is returned. 271 is. The first match is returned.
247 272
248 @param file filename of the executable to check (string) 273 @param file filename of the executable to check
249 @return full executable name, if the executable file is accessible 274 @type str
250 via the searchpath defined by the PATH environment variable, or an 275 @return full executable name, if the executable file is accessible via the
276 executable search path defined by the PATH environment variable, or an
251 empty string otherwise. 277 empty string otherwise.
278 @rtype str
252 """ 279 """
253 if os.path.isabs(file): 280 if os.path.isabs(file):
254 if os.access(file, os.X_OK): 281 if os.access(file, os.X_OK):
255 return file 282 return file
256 else: 283 else:
281 308
282 def isExecutable(exe): 309 def isExecutable(exe):
283 """ 310 """
284 Function to check, if a file is executable. 311 Function to check, if a file is executable.
285 312
286 @param exe filename of the executable to check (string) 313 @param exe filename of the executable to check
287 @return flag indicating executable status (boolean) 314 @type str
315 @return flag indicating executable status
316 @rtype bool
288 """ 317 """
289 return os.access(exe, os.X_OK) 318 return os.access(exe, os.X_OK)
290 319
291 320
292 def isDrive(path): 321 def isDrive(path):
309 isWindowsDrive = True 338 isWindowsDrive = True
310 339
311 return isWindowsDrive 340 return isWindowsDrive
312 341
313 342
314 def samepath(f1, f2): 343 def samepath(f1, f2, followSymlinks=True):
315 """ 344 """
316 Function to compare two paths. 345 Function to compare two paths.
317 346
318 @param f1 first path for the compare (string) 347 @param f1 first filepath for the compare
319 @param f2 second path for the compare (string) 348 @type str
349 @param f2 second filepath for the compare
350 @type str
351 @param followSymlinks flag indicating to respect symbolic links for the comparison
352 (i.e. compare the real paths) (defaults to True)
353 @type bool (optional)
320 @return flag indicating whether the two paths represent the 354 @return flag indicating whether the two paths represent the
321 same path on disk. 355 same path on disk
356 @rtype bool
322 """ 357 """
323 if f1 is None or f2 is None: 358 if f1 is None or f2 is None:
324 return False 359 return False
325 360
326 if normcaseabspath(os.path.realpath(f1)) == normcaseabspath(os.path.realpath(f2)): 361 if followSymlinks:
327 return True 362 if normcaseabspath(os.path.realpath(f1)) == normcaseabspath(
363 os.path.realpath(f2)
364 ):
365 return True
366 else:
367 if normcaseabspath(f1) == normcaseabspath(f2):
368 return True
328 369
329 return False 370 return False
330 371
331 372
332 def samefilepath(f1, f2): 373 def samefilepath(f1, f2, followSymlinks=True):
333 """ 374 """
334 Function to compare two paths. Strips the filename. 375 Function to compare two paths. Strips the filename.
335 376
336 @param f1 first filepath for the compare (string) 377 @param f1 first filepath for the compare
337 @param f2 second filepath for the compare (string) 378 @type str
379 @param f2 second filepath for the compare
380 @type str
381 @param followSymlinks flag indicating to respect symbolic links for the comparison
382 (i.e. compare the real paths) (defaults to True)
383 @type bool (optional)
338 @return flag indicating whether the two paths represent the 384 @return flag indicating whether the two paths represent the
339 same path on disk. 385 same path on disk
386 @rtype bool
340 """ 387 """
341 if f1 is None or f2 is None: 388 if f1 is None or f2 is None:
342 return False 389 return False
343 390
344 if normcaseabspath(os.path.dirname(os.path.realpath(f1))) == normcaseabspath( 391 if followSymlinks:
345 os.path.dirname(os.path.realpath(f2)) 392 if normcaseabspath(os.path.dirname(os.path.realpath(f1))) == normcaseabspath(
346 ): 393 os.path.dirname(os.path.realpath(f2))
347 return True 394 ):
395 return True
396 else:
397 if normcaseabspath(os.path.dirname(f1)) == normcaseabspath(os.path.dirname(f2)):
398 return True
348 399
349 return False 400 return False
350 401
351 402
352 try: 403 try:
357 408
358 def splitPath(name): 409 def splitPath(name):
359 """ 410 """
360 Function to split a pathname into a directory part and a file part. 411 Function to split a pathname into a directory part and a file part.
361 412
362 @param name path name (string) 413 @param name path name
363 @return a tuple of 2 strings (dirname, filename). 414 @type str
415 @return tuple containing directory name and file name
416 @rtype tuple of (str, str)
364 """ 417 """
365 if os.path.isdir(name): 418 if os.path.isdir(name):
366 dn = os.path.abspath(name) 419 dn = os.path.abspath(name)
367 fn = "." 420 fn = "."
368 else: 421 else:
375 Function to join a file extension to a path. 428 Function to join a file extension to a path.
376 429
377 The leading "." of ext is replaced by a platform specific extension 430 The leading "." of ext is replaced by a platform specific extension
378 separator if necessary. 431 separator if necessary.
379 432
380 @param prefix the basepart of the filename (string) 433 @param prefix the basepart of the filename
381 @param ext the extension part (string) 434 @type str
382 @return the complete filename (string) 435 @param ext the extension part
436 @type str
437 @return the complete filename
438 @rtype str
383 """ 439 """
384 if ext[0] != ".": 440 if ext[0] != ".":
385 ext = ".{0}".format(ext) 441 ext = ".{0}".format(ext)
386 # require leading separator to match os.path.splitext 442 # require leading separator to match os.path.splitext
387 return prefix + EXTSEP + ext[1:] 443 return prefix + EXTSEP + ext[1:]
389 445
390 def compactPath(path, width, measure=len): 446 def compactPath(path, width, measure=len):
391 """ 447 """
392 Function to return a compacted path fitting inside the given width. 448 Function to return a compacted path fitting inside the given width.
393 449
394 @param path path to be compacted (string) 450 @param path path to be compacted
395 @param width width for the compacted path (integer) 451 @type str
452 @param width width for the compacted path
453 @type int
396 @param measure reference to a function used to measure the length of the 454 @param measure reference to a function used to measure the length of the
397 string 455 string
398 @return compacted path (string) 456 @type function(str)
457 @return compacted path
458 @rtype str
399 """ 459 """
400 if measure(path) <= width: 460 if measure(path) <= width:
401 return path 461 return path
402 462
403 ellipsis = "..." 463 ellipsis = "..."
449 @type function 509 @type function
450 @param ignore list of entries to be ignored 510 @param ignore list of entries to be ignored
451 @type list of str 511 @type list of str
452 @return list of all files and directories in the tree rooted 512 @return list of all files and directories in the tree rooted
453 at path. The names are expanded to start with path. 513 at path. The names are expanded to start with path.
454 @rtype list of strs 514 @rtype list of str
455 """ 515 """
456 patterns = pattern if isinstance(pattern, list) else [pattern] 516 patterns = pattern if isinstance(pattern, list) else [pattern]
457 files = [] if filesonly else [path] 517 files = [] if filesonly else [path]
458 ignoreList = [ 518 ignoreList = [
459 ".svn", 519 ".svn",
506 def getDirs(path, excludeDirs): 566 def getDirs(path, excludeDirs):
507 """ 567 """
508 Function returning a list of all directories below path. 568 Function returning a list of all directories below path.
509 569
510 @param path root of the tree to check 570 @param path root of the tree to check
511 @param excludeDirs basename of directories to ignore 571 @type str
572 @param excludeDirs base name of directories to ignore
573 @type list of str
512 @return list of all directories found 574 @return list of all directories found
575 @rtype list of str
513 """ 576 """
514 try: 577 try:
515 dirs = [] 578 dirs = []
516 with os.scandir(path) as dirEntriesIterator: 579 with os.scandir(path) as dirEntriesIterator:
517 for dirEntry in dirEntriesIterator: 580 for dirEntry in dirEntriesIterator:

eric ide

mercurial