src/eric7/SystemUtilities/FileSystemUtilities.py

Sun, 07 Apr 2024 17:40:03 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 07 Apr 2024 17:40:03 +0200
branch
eric7
changeset 10668
2be6fadcac40
parent 10521
a51b382e47d7
child 10680
306373ccf8fd
permissions
-rw-r--r--

Fixed a little issue in the file system utilities.

9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
10439
21c28b0f9e41 Updated copyright for 2024.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10397
diff changeset
3 # Copyright (c) 2022 - 2024 Detlev Offenbach <detlev@die-offenbachs.de>
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing file system related utility functions.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import contextlib
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import ctypes
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import fnmatch
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import os
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import pathlib
10170
6cf1ee737d8f Corrected some more code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10108
diff changeset
15 import subprocess # secok
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from eric7.SystemUtilities import OSUtilities
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 def toNativeSeparators(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Function returning a path, that is using native separator characters.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @param path path to be converted
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 @return path with converted separator characters
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @rtype str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 return str(pathlib.PurePath(path)) if bool(path) else ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 def fromNativeSeparators(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 Function returning a path, that is using "/" separator characters.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @param path path to be converted
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 @return path with converted separator characters
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 @rtype str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 return pathlib.PurePath(path).as_posix() if bool(path) else ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 def normcasepath(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 Function returning a path, that is normalized with respect to its case
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 and references.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
49 @param path file path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
50 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
51 @return case normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
52 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 return os.path.normcase(os.path.normpath(path))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 def normcaseabspath(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 Function returning an absolute path, that is normalized with respect to
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 its case and references.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
62 @param path file path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
63 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
64 @return absolute, normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
65 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 return os.path.normcase(os.path.abspath(path))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 def normjoinpath(a, *p):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 Function returning a normalized path of the joined parts passed into it.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
74 @param a first path to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
75 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
76 @param p variable number of path parts to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
77 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
78 @return normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
79 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 return os.path.normpath(os.path.join(a, *p))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 def normabsjoinpath(a, *p):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 Function returning a normalized, absolute path of the joined parts passed
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 into it.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
89 @param a first path to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
90 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
91 @param p variable number of path parts to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
92 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
93 @return absolute, normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
94 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 return os.path.abspath(os.path.join(a, *p))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 def isinpath(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 Function to check for an executable file.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
103 @param file filename of the executable to check
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
104 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
105 @return flag indicating, if the executable file is accessible via the executable
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
106 search path defined by the PATH environment variable.
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
107 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 if os.path.isabs(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 return os.access(file, os.X_OK)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 if os.path.exists(os.path.join(os.curdir, file)):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 return os.access(os.path.join(os.curdir, file), os.X_OK)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 path = OSUtilities.getEnvironmentEntry("PATH")
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 # environment variable not defined
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 if path is None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 dirs = path.split(os.pathsep)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 return any(os.access(os.path.join(directory, file), os.X_OK) for directory in dirs)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
10668
2be6fadcac40 Fixed a little issue in the file system utilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
125 def startsWithPath(path, start):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 Function to check, if a path starts with a given start path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 @param path path to be checked
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 @param start start path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 @return flag indicating that the path starts with the given start
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 @rtype bool
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 """
10668
2be6fadcac40 Fixed a little issue in the file system utilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
137 start1 = start if start.endswith(os.sep) else f"{start}{os.sep}"
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 return bool(start) and (
10668
2be6fadcac40 Fixed a little issue in the file system utilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
139 path == start or normcasepath(path).startswith(normcasepath(start1))
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 )
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 def relativeUniversalPath(path, start):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 Function to convert a file path to a path relative to a start path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 with universal separators.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
148 @param path file or directory name to convert
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
149 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
150 @param start start path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
151 @type str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 @return relative path or unchanged path, if path does not start with
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
153 the start path with universal separators
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
154 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 return fromNativeSeparators(os.path.relpath(path, start))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 def absolutePath(path, start):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 Public method to convert a path relative to a start path to an
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 absolute path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
164 @param path file or directory name to convert
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
165 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
166 @param start start path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
167 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
168 @return absolute path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
169 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 if not os.path.isabs(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 path = os.path.normpath(os.path.join(start, path))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 def absoluteUniversalPath(path, start):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 Public method to convert a path relative to a start path with
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 universal separators to an absolute path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
181 @param path file or directory name to convert
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
182 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
183 @param start start path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
184 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
185 @return absolute path with native separators
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
186 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 if not os.path.isabs(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 path = toNativeSeparators(os.path.normpath(os.path.join(start, path)))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 def getExecutablePath(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 Function to build the full path of an executable file from the environment.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
197 @param file filename of the executable to check
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
198 @type str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 @return full executable name, if the executable file is accessible
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
200 via the executable search path defined by the PATH environment variable, or an
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 empty string otherwise.
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
202 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 if os.path.isabs(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 if os.access(file, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 return file
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 cur_path = os.path.join(os.curdir, file)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 return cur_path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 path = os.getenv("PATH")
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 # environment variable not defined
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 if path is None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 dirs = path.split(os.pathsep)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 for directory in dirs:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 exe = os.path.join(directory, file)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 if os.access(exe, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 return exe
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 def getExecutablePaths(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 Function to build all full path of an executable file from the environment.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
233 @param file filename of the executable
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
234 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
235 @return list of full executable names, if the executable file is accessible via
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
236 the executable search path defined by the PATH environment variable, or an
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
237 empty list otherwise.
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
238 @rtype list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 paths = []
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 if os.path.isabs(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 if os.access(file, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 return [file]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 return []
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 cur_path = os.path.join(os.curdir, file)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 paths.append(cur_path)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 path = os.getenv("PATH")
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 # environment variable not defined
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 if path is not None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 dirs = path.split(os.pathsep)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 for directory in dirs:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 exe = os.path.join(directory, file)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 if os.access(exe, os.X_OK) and exe not in paths:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 paths.append(exe)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 return paths
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 def getWindowsExecutablePath(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 Function to build the full path of an executable file from the environment
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 on Windows platforms.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 First an executable with the extension .exe is searched for, thereafter
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 such with the extensions .cmd or .bat and finally the given file name as
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 is. The first match is returned.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
274 @param file filename of the executable to check
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
275 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
276 @return full executable name, if the executable file is accessible via the
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
277 executable search path defined by the PATH environment variable, or an
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 empty string otherwise.
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
279 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 if os.path.isabs(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 if os.access(file, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 return file
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 filenames = [file + ".exe", file + ".cmd", file + ".bat", file]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 for filename in filenames:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 cur_path = os.path.join(os.curdir, filename)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 return os.path.abspath(cur_path)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 path = os.getenv("PATH")
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 # environment variable not defined
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 if path is None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 dirs = path.split(os.pathsep)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 for directory in dirs:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 for filename in filenames:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 exe = os.path.join(directory, filename)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 if os.access(exe, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 return exe
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 def isExecutable(exe):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 Function to check, if a file is executable.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
314 @param exe filename of the executable to check
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
315 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
316 @return flag indicating executable status
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
317 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 return os.access(exe, os.X_OK)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 def isDrive(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 Function to check, if a path is a Windows drive.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 @param path path name to be checked
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 @return flag indicating a Windows drive
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 @rtype bool
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 isWindowsDrive = False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 drive, directory = os.path.splitdrive(path)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 if (
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 drive
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 and len(drive) == 2
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 and drive.endswith(":")
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 and directory in ["", "\\", "/"]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 ):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 isWindowsDrive = True
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 return isWindowsDrive
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
344 def samepath(f1, f2, followSymlinks=True):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 Function to compare two paths.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
348 @param f1 first filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
349 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
350 @param f2 second filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
351 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
352 @param followSymlinks flag indicating to respect symbolic links for the comparison
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
353 (i.e. compare the real paths) (defaults to True)
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
354 @type bool (optional)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 @return flag indicating whether the two paths represent the
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
356 same path on disk
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
357 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 if f1 is None or f2 is None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
362 if followSymlinks:
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
363 if normcaseabspath(os.path.realpath(f1)) == normcaseabspath(
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
364 os.path.realpath(f2)
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
365 ):
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
366 return True
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
367 else:
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
368 if normcaseabspath(f1) == normcaseabspath(f2):
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
369 return True
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
374 def samefilepath(f1, f2, followSymlinks=True):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 Function to compare two paths. Strips the filename.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
378 @param f1 first filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
379 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
380 @param f2 second filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
381 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
382 @param followSymlinks flag indicating to respect symbolic links for the comparison
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
383 (i.e. compare the real paths) (defaults to True)
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
384 @type bool (optional)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 @return flag indicating whether the two paths represent the
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
386 same path on disk
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
387 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 if f1 is None or f2 is None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
392 if followSymlinks:
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
393 if normcaseabspath(os.path.dirname(os.path.realpath(f1))) == normcaseabspath(
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
394 os.path.dirname(os.path.realpath(f2))
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
395 ):
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
396 return True
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
397 else:
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
398 if normcaseabspath(os.path.dirname(f1)) == normcaseabspath(os.path.dirname(f2)):
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
399 return True
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 try:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 EXTSEP = os.extsep
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 except AttributeError:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 EXTSEP = "."
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 def splitPath(name):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 Function to split a pathname into a directory part and a file part.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
414 @param name path name
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
415 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
416 @return tuple containing directory name and file name
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
417 @rtype tuple of (str, str)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 if os.path.isdir(name):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 dn = os.path.abspath(name)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 fn = "."
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 dn, fn = os.path.split(name)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 return (dn, fn)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 def joinext(prefix, ext):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 Function to join a file extension to a path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 The leading "." of ext is replaced by a platform specific extension
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 separator if necessary.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
434 @param prefix the basepart of the filename
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
435 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
436 @param ext the extension part
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
437 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
438 @return the complete filename
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
439 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 if ext[0] != ".":
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 ext = ".{0}".format(ext)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 # require leading separator to match os.path.splitext
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 return prefix + EXTSEP + ext[1:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 def compactPath(path, width, measure=len):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 Function to return a compacted path fitting inside the given width.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
451 @param path path to be compacted
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
452 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
453 @param width width for the compacted path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
454 @type int
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 @param measure reference to a function used to measure the length of the
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 string
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
457 @type function(str)
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
458 @return compacted path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
459 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 ellipsis = "..."
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 head, tail = os.path.split(path)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 mid = len(head) // 2
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 head1 = head[:mid]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 head2 = head[mid:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 while head1:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 # head1 is same size as head2 or one shorter
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 path = os.path.join("{0}{1}{2}".format(head1, ellipsis, head2), tail)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 head1 = head1[:-1]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 head2 = head2[1:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 path = os.path.join(ellipsis, tail)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 while tail:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 path = "{0}{1}".format(ellipsis, tail)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 tail = tail[1:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 def direntries(
9639
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
489 path,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
490 filesonly=False,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
491 pattern=None,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
492 followsymlinks=True,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
493 checkStop=None,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
494 ignore=None,
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 ):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 Function returning a list of all files and directories.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 @param path root of the tree to check
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 @param filesonly flag indicating that only files are wanted
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 @type bool
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 @param pattern a filename pattern or list of filename patterns to check
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 against
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 @type str or list of str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 @param followsymlinks flag indicating whether symbolic links
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 should be followed
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 @type bool
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 @param checkStop function to be called to check for a stop
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 @type function
9639
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
511 @param ignore list of entries to be ignored
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
512 @type list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 @return list of all files and directories in the tree rooted
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 at path. The names are expanded to start with path.
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
515 @rtype list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 patterns = pattern if isinstance(pattern, list) else [pattern]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 files = [] if filesonly else [path]
9639
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
519 ignoreList = [
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
520 ".svn",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
521 ".hg",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
522 ".git",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
523 ".ropeproject",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
524 ".eric7project",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
525 ".jedi",
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
526 "__pycache__",
9639
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
527 ]
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
528 if ignore is not None:
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
529 ignoreList.extend(ignore)
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
530
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
531 with contextlib.suppress(OSError, UnicodeDecodeError), os.scandir(
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
532 path
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
533 ) as dirEntriesIterator:
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
534 for dirEntry in dirEntriesIterator:
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 if checkStop and checkStop():
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 break
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
538 if dirEntry.name in ignoreList:
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 continue
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 if (
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 pattern
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
543 and not dirEntry.is_dir()
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
544 and not any(fnmatch.fnmatch(dirEntry.name, p) for p in patterns)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 ):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 # entry doesn't fit the given pattern
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 continue
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
549 if dirEntry.is_dir():
10108
0f4194acf75e Find File
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9972
diff changeset
550 if dirEntry.path in ignoreList:
0f4194acf75e Find File
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9972
diff changeset
551 continue
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
552 if dirEntry.is_symlink() and not followsymlinks:
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 continue
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 files += direntries(
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
555 dirEntry.path,
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
556 filesonly=filesonly,
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
557 pattern=pattern,
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
558 followsymlinks=followsymlinks,
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
559 checkStop=checkStop,
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
560 ignore=ignore,
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 )
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 else:
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
563 files.append(dirEntry.path)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 return files
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 def getDirs(path, excludeDirs):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 Function returning a list of all directories below path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 @param path root of the tree to check
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
572 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
573 @param excludeDirs base name of directories to ignore
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
574 @type list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 @return list of all directories found
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
576 @rtype list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 try:
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
579 dirs = []
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
580 with os.scandir(path) as dirEntriesIterator:
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
581 for dirEntry in dirEntriesIterator:
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
582 if (
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
583 dirEntry.is_dir()
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
584 and not dirEntry.is_symlink()
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
585 and dirEntry.name not in excludeDirs
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
586 ):
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
587 dirs.append(dirEntry.path)
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
588 dirs.extend(getDirs(dirEntry.path, excludeDirs))
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
589 return dirs
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 except OSError:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 return []
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 def findVolume(volumeName, findAll=False):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 Function to find the directory belonging to a given volume name.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 @param volumeName name of the volume to search for
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 @param findAll flag indicating to get the directories for all volumes
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 starting with the given name (defaults to False)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602 @type bool (optional)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 @return directory path or list of directory paths for the given volume
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 name
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 @rtype str or list of str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 volumeDirectories = []
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 volumeDirectory = None
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 if OSUtilities.isWindowsPlatform():
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 # we are on a Windows platform
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 def getVolumeName(diskName):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614 Local function to determine the volume of a disk or device.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 Each disk or external device connected to windows has an
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 attribute called "volume name". This function returns the
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 volume name for the given disk/device.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 Code from http://stackoverflow.com/a/12056414
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 volumeNameBuffer = ctypes.create_unicode_buffer(1024)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 ctypes.windll.kernel32.GetVolumeInformationW(
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 ctypes.c_wchar_p(diskName),
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 volumeNameBuffer,
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 ctypes.sizeof(volumeNameBuffer),
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 None,
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628 None,
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629 None,
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 None,
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 0,
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 )
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 return volumeNameBuffer.value
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 #
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 # In certain circumstances, volumes are allocated to USB
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 # storage devices which cause a Windows popup to raise if their
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 # volume contains no media. Wrapping the check in SetErrorMode
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639 # with SEM_FAILCRITICALERRORS (1) prevents this popup.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 #
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 oldMode = ctypes.windll.kernel32.SetErrorMode(1)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 try:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643 for disk in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 dirpath = "{0}:\\".format(disk)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 if os.path.exists(dirpath):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 if findAll:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 if getVolumeName(dirpath).startswith(volumeName):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 volumeDirectories.append(dirpath)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 if getVolumeName(dirpath) == volumeName:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 volumeDirectory = dirpath
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 break
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 finally:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654 ctypes.windll.kernel32.SetErrorMode(oldMode)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 else:
10338
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
656 # we are on a Linux, FreeBSD or macOS platform
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 for mountCommand in ["mount", "/sbin/mount", "/usr/sbin/mount"]:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 with contextlib.suppress(FileNotFoundError):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 mountOutput = subprocess.run( # secok
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 mountCommand, check=True, capture_output=True, text=True
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 ).stdout.splitlines()
9923
f706be800097 Refined the 'findVolume()' solution for Linux and macOS to work on both.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9920
diff changeset
662 mountedVolumes = [
f706be800097 Refined the 'findVolume()' solution for Linux and macOS to work on both.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9920
diff changeset
663 x.split(" type", 1)[0].split(" (", 1)[0].split(maxsplit=2)[-1]
f706be800097 Refined the 'findVolume()' solution for Linux and macOS to work on both.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9920
diff changeset
664 for x in mountOutput
f706be800097 Refined the 'findVolume()' solution for Linux and macOS to work on both.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9920
diff changeset
665 ]
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 if findAll:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 for volume in mountedVolumes:
9903
52132654939b MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9653
diff changeset
668 if os.path.basename(volume).startswith(volumeName):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 volumeDirectories.append(volume)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 if volumeDirectories:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 break
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 for volume in mountedVolumes:
9903
52132654939b MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9653
diff changeset
674 if os.path.basename(volume) == volumeName:
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 volumeDirectory = volume
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 break
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 if volumeDirectory:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 break
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 if findAll:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 return volumeDirectories
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 return volumeDirectory
9944
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
684
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
685
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
686 def getUserMounts():
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
687 """
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
688 Function to determine all available user mounts.
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
689
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
690 Note: On Windows platforms all available drives are returned.
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
691
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
692 @return list of user mounts or drives
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
693 @rtype list of str
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
694 """
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
695 mountedPaths = []
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
696
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
697 if OSUtilities.isWindowsPlatform():
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
698 # we are on a Windows platform
9972
68ac01294544 Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9944
diff changeset
699 for disk in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
68ac01294544 Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9944
diff changeset
700 dirpath = "{0}:\\".format(disk)
68ac01294544 Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9944
diff changeset
701 if os.path.exists(dirpath):
68ac01294544 Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9944
diff changeset
702 mountedPaths.append(dirpath)
9944
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
703 else:
10338
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
704 # we are on a Linux, FreeBSD or macOS platform
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
705 if OSUtilities.isMacPlatform():
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
706 # macOS
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
707 mountPathStart = "/Volumes/"
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
708 elif OSUtilities.isLinuxPlatform():
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
709 # Linux
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
710 mountPathStart = "/media/{0}/".format(OSUtilities.getUserName())
10339
446d22fa1aea Renamed "isBsdPlatform()" to "isFreeBsdPlatform()" to express its real test.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10338
diff changeset
711 elif OSUtilities.isFreeBsdPlatform():
10338
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
712 # FreeBSD
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
713 mountPathStart = "/media/"
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
714 else:
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
715 # unsupported platform
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
716 return []
9944
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
717
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
718 for mountCommand in ["mount", "/sbin/mount", "/usr/sbin/mount"]:
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
719 with contextlib.suppress(FileNotFoundError):
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
720 mountOutput = subprocess.run( # secok
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
721 mountCommand, check=True, capture_output=True, text=True
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
722 ).stdout.splitlines()
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
723 mounts = [
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
724 x.split(" type", 1)[0].split(" (", 1)[0].split(maxsplit=2)[-1]
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
725 for x in mountOutput
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
726 ]
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
727 mountedPaths = [x for x in mounts if x.startswith(mountPathStart)]
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
728 break
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
729
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
730 return mountedPaths
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
731
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
732
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
733 def startfile(filePath):
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
734 """
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
735 Function to open the given file path with the system default application.
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
736
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
737 @param filePath file path to be opened
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
738 @type str or Path
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
739 @return flag indicating a successful start of the associated application
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
740 @rtype bool
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
741 """
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
742 filePath = str(filePath)
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
743
10397
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
744 with contextlib.suppress(OSError):
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
745 if OSUtilities.isWindowsPlatform():
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
746 os.startfile(filePath) # secok
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
747 return True
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
748
10397
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
749 elif OSUtilities.isMacPlatform():
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
750 return subprocess.call(("open", filePath)) == 0 # secok
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
751
10397
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
752 elif OSUtilities.isLinuxPlatform() or OSUtilities.isFreeBsdPlatform():
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
753 return subprocess.call(("xdg-open", filePath)) == 0 # secok
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
754
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
755 # unsupported platform or OSError
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
756 return False
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
757
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
758
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
759 ################################################################################
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
760 ## Functions below handle (MicroPython) device and remote file names.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
761 ################################################################################
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
762
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
763
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
764 _DeviceFileMarker = "device:"
10521
a51b382e47d7 Did a little code reorganization to improve readability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10439
diff changeset
765 _RemoteFileMarker = "remote:"
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
766
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
767
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
768 def deviceFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
769 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
770 Function to create a device (MicroPython) file name given a plain file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
771
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
772 @param fileName plain file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
773 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
774 @return device file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
775 @rtype str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
776 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
777 return f"{_DeviceFileMarker}{fileName}"
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
778
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
779
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
780 def isDeviceFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
781 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
782 Function to check, if the given file name is a device file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
783
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
784 @param fileName file name to be checked
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
785 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
786 @return flag indicating a device file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
787 @rtype bool
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
788 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
789 return fileName.startswith(_DeviceFileMarker)
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
790
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
791
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
792 def remoteFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
793 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
794 Function to create a remote file name given a plain file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
795
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
796 @param fileName plain file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
797 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
798 @return remote file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
799 @rtype str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
800 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
801 return f"{_RemoteFileMarker}{fileName}"
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
802
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
803
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
804 def isRemoteFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
805 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
806 Function to check, if the given file name is a remote file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
807
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
808 @param fileName file name to be checked
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
809 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
810 @return flag indicating a remote file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
811 @rtype bool
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
812 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
813 return fileName.startswith(_RemoteFileMarker)
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
814
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
815
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
816 def isPlainFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
817 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
818 Function to check, if the given file name is a plain (i.e. local) file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
819
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
820 @param fileName file name to be checked
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
821 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
822 @return flag indicating a local file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
823 @rtype bool
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
824 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
825 return not fileName.startswith((_DeviceFileMarker, _RemoteFileMarker))
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
826
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
827
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
828 def plainFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
829 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
830 Function to create a plain file name given a device or remote file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
831
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
832 @param fileName device or remote file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
833 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
834 @return plain file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
835 @rtype str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
836 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
837 return fileName.replace(_DeviceFileMarker, "").replace(_RemoteFileMarker, "")

eric ide

mercurial