src/eric7/SystemUtilities/FileSystemUtilities.py

branch
eric7
changeset 10791
ca9ece290f71
parent 10680
306373ccf8fd
child 10971
b9d883f73dff
equal deleted inserted replaced
10790:f0fd0c1592e2 10791:ca9ece290f71
10 import contextlib 10 import contextlib
11 import ctypes 11 import ctypes
12 import fnmatch 12 import fnmatch
13 import os 13 import os
14 import pathlib 14 import pathlib
15 import shutil
15 import subprocess # secok 16 import subprocess # secok
16 17
17 from eric7.SystemUtilities import OSUtilities 18 from eric7.SystemUtilities import OSUtilities
18 19
19 20
104 @type str 105 @type str
105 @return flag indicating, if the executable file is accessible via the executable 106 @return flag indicating, if the executable file is accessible via the executable
106 search path defined by the PATH environment variable. 107 search path defined by the PATH environment variable.
107 @rtype bool 108 @rtype bool
108 """ 109 """
109 if os.path.isabs(file): 110 return bool(shutil.which(file))
110 return os.access(file, os.X_OK)
111
112 if os.path.exists(os.path.join(os.curdir, file)):
113 return os.access(os.path.join(os.curdir, file), os.X_OK)
114
115 path = OSUtilities.getEnvironmentEntry("PATH")
116
117 # environment variable not defined
118 if path is None:
119 return False
120
121 dirs = path.split(os.pathsep)
122 return any(os.access(os.path.join(directory, file), os.X_OK) for directory in dirs)
123 111
124 112
125 def startsWithPath(path, start): 113 def startsWithPath(path, start):
126 """ 114 """
127 Function to check, if a path starts with a given start path. 115 Function to check, if a path starts with a given start path.
199 @return full executable name, if the executable file is accessible 187 @return full executable name, if the executable file is accessible
200 via the executable search path defined by the PATH environment variable, or an 188 via the executable search path defined by the PATH environment variable, or an
201 empty string otherwise. 189 empty string otherwise.
202 @rtype str 190 @rtype str
203 """ 191 """
204 if os.path.isabs(file): 192 exe = shutil.which(file)
205 if os.access(file, os.X_OK): 193 return exe if bool(exe) else ""
206 return file
207 else:
208 return ""
209
210 cur_path = os.path.join(os.curdir, file)
211 if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
212 return cur_path
213
214 path = os.getenv("PATH")
215
216 # environment variable not defined
217 if path is None:
218 return ""
219
220 dirs = path.split(os.pathsep)
221 for directory in dirs:
222 exe = os.path.join(directory, file)
223 if os.access(exe, os.X_OK):
224 return exe
225
226 return ""
227 194
228 195
229 def getExecutablePaths(file): 196 def getExecutablePaths(file):
230 """ 197 """
231 Function to build all full path of an executable file from the environment. 198 Function to build all full path of an executable file from the environment.
258 exe = os.path.join(directory, file) 225 exe = os.path.join(directory, file)
259 if os.access(exe, os.X_OK) and exe not in paths: 226 if os.access(exe, os.X_OK) and exe not in paths:
260 paths.append(exe) 227 paths.append(exe)
261 228
262 return paths 229 return paths
263
264
265 def getWindowsExecutablePath(file):
266 """
267 Function to build the full path of an executable file from the environment
268 on Windows platforms.
269
270 First an executable with the extension .exe is searched for, thereafter
271 such with the extensions .cmd or .bat and finally the given file name as
272 is. The first match is returned.
273
274 @param file filename of the executable to check
275 @type str
276 @return full executable name, if the executable file is accessible via the
277 executable search path defined by the PATH environment variable, or an
278 empty string otherwise.
279 @rtype str
280 """
281 if os.path.isabs(file):
282 if os.access(file, os.X_OK):
283 return file
284 else:
285 return ""
286
287 filenames = [file + ".exe", file + ".cmd", file + ".bat", file]
288
289 for filename in filenames:
290 cur_path = os.path.join(os.curdir, filename)
291 if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
292 return os.path.abspath(cur_path)
293
294 path = os.getenv("PATH")
295
296 # environment variable not defined
297 if path is None:
298 return ""
299
300 dirs = path.split(os.pathsep)
301 for directory in dirs:
302 for filename in filenames:
303 exe = os.path.join(directory, filename)
304 if os.access(exe, os.X_OK):
305 return exe
306
307 return ""
308 230
309 231
310 def isExecutable(exe): 232 def isExecutable(exe):
311 """ 233 """
312 Function to check, if a file is executable. 234 Function to check, if a file is executable.

eric ide

mercurial