-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathservices.py
364 lines (272 loc) · 11.2 KB
/
services.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
# SPDX-FileCopyrightText: (c) 2020 Western Digital Corporation or its affiliates,
# Arseniy Aharonov <[email protected]>
#
# SPDX-License-Identifier: MIT
from __future__ import print_function
import os
import platform
import subprocess
import sys
from fnmatch import filter as filterFileNames
from shutil import rmtree
from threading import Thread
from time import sleep
from json import dump as dumpJson
from shlex import split as splitCmdLine
import stat_attributes as attributes
def countCpuCores():
try:
from psutil import cpu_count
except ImportError:
try:
from multiprocessing import cpu_count
except ImportError:
return 1
try:
return cpu_count()
except NotImplementedError:
return 1
def isWindows():
return True if platform.system() == "Windows" else False
def getPlatform():
# 'Linux32/64', 'Darwin32/64', 'Java32/64', 'Windows32/64'
machine = "64" if platform.machine()[-2:] == "64" else "32"
platformMode = platform.system() + machine
return platformMode
def toWindowsPath(path):
return path.replace('/', '\\')
def toPosixPath(path):
return path.replace('\\', '/')
def toNativePath(path):
return toWindowsPath(path) if isWindows() else toPosixPath(path)
def nameExecutable(fileOnlyName):
return fileOnlyName + '.exe' if isWindows() else fileOnlyName
def mkdir(path, exist_ok=False):
if exist_ok and os.path.isdir(path):
return
os.makedirs(path)
def formatCommandLine(command):
if isWindows():
return " ".join(command) if isinstance(command, (list, tuple)) else command
else:
return command if isinstance(command, (list, tuple)) else splitCmdLine(command)
def execute(command, beSilent=False, **kwargs):
commandLine = formatCommandLine(command)
arguments = dict(bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
arguments.update(kwargs)
process = subprocess.Popen(commandLine, **arguments)
lines = []
thread = Thread(target=__captureOutputLines, args=(process, beSilent, lines))
thread.setDaemon(True)
thread.start()
thread.join()
process.communicate()
return process.returncode, lines
def __captureOutputLines(process, beSilent, lines):
for _line in iter(process.stdout.readline, b''):
if _line == '':
break
lines.append(_line)
if not beSilent:
print(_line, end='')
def executeForOutput(command, **kwargs):
commandLine = formatCommandLine(command)
arguments = dict(bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
arguments.update(kwargs)
process = subprocess.Popen(commandLine, **arguments)
output, _ = process.communicate()
return output.strip()
def remove(path):
doesExist, attemptRemoval, typeName = \
(os.path.isdir, rmtree, 'directory') if os.path.isdir(path) else (os.path.isfile, os.remove, 'file')
while doesExist(path):
try:
attemptRemoval(path)
except OSError:
print("Waiting for {type} '{path}' to find unlocked (probably by AntiVirus)!".format(type=typeName,
path=path))
sleep(1)
def writeJsonFile(filePath, data):
with open(filePath, 'w') as fp:
dumpJson(data, fp, indent=3)
def createLink(sourcePath, targetPath):
source = os.path.relpath(sourcePath, os.path.dirname(targetPath))
target = targetPath
if isWindows():
source = toWindowsPath(source)
target = toWindowsPath(target)
if os.path.isdir(source):
commandLine = 'cmd /c mklink /D "{target}" "{source}" >nul'
else:
commandLine = 'cmd /c mklink "{target}" "{source}" >nul'
subprocess.Popen(formatCommandLine(commandLine.format(target=target, source=source)), shell=True).wait()
else:
os.symlink(source, target) # pylint: disable=no-member
def findSubFolderOnPath(subFolder, path='.'):
currentPath = os.getcwd() if path == '.' else path
subFolderPath = os.path.join(currentPath, subFolder)
while not os.path.isdir(subFolderPath):
currentPath = os.path.dirname(currentPath)
if currentPath == os.path.dirname(currentPath):
return None
subFolderPath = os.path.join(currentPath, subFolder)
return subFolderPath
def getFileLocationThroughoutCurrentPath(fileName, currentPath='.'):
previousDirectory = None
currentDirectory = currentPath if currentPath != '.' else os.getcwd()
while previousDirectory != currentDirectory:
fileLocation = os.path.join(currentDirectory, fileName)
if os.path.isfile(fileLocation):
return fileLocation
previousDirectory = currentDirectory
currentDirectory = os.path.dirname(previousDirectory)
return None
def listFilenames(pathName, encompassingPattern, *patternsForSubsets):
files = filterFileNames(os.listdir(pathName), encompassingPattern)
return __selectFilesByPatterns(files, patternsForSubsets) if patternsForSubsets else set(files)
def listMakefiles(pathName, *patterns):
files = listFilenames(pathName, '*.mak', *patterns)
if len(patterns) != len(files) or set(patterns) != files:
ignored = __selectIgnoredFiles(files, pathName)
if ignored:
files.difference_update(ignored)
return sorted(files)
def readFileLines(filepath):
with open(filepath, 'r') as _file:
return _file.readlines()
def readNonEmptyLines(filepath):
for line in readFileLines(filepath):
line = line.strip()
if line:
yield line
def readTextFileAtOnce(filepath):
with open(filepath, 'r') as aFile:
text = aFile.read()
return text
def locateFile(filename, mode=None, path=None):
def _locateFileOnPath(_filename, _path):
_filepath = os.path.join(_path, _filename)
if isWindows() and mode and mode & os.X_OK:
_, _ext = os.path.splitext(_filepath)
exeExtensions = os.environ.get("PATHEXT", "").split(os.pathsep)
if not _ext:
return _matchExtensionToFile(_filepath, exeExtensions)
elif _ext not in exeExtensions:
return ""
return _filepath if _isFileAccessible(_filepath) else ""
def _matchExtensionToFile(_filepath, _extensions):
for _ext in _extensions:
if _isFileAccessible(_filepath + _ext):
return _filepath + _ext
else:
return ""
def _isFileAccessible(_filepath):
return os.path.isfile(_filepath) and (not mode or os.access(_filepath, mode))
if isinstance(path, str):
return _locateFileOnPath(filename, path)
else:
paths = [os.path.abspath(".")] + os.environ.get("PATH", "").split(os.pathsep) if not path else path
for _path in paths:
filepath = _locateFileOnPath(filename, _path)
if filepath:
return filepath
else:
return ""
def locateResource(filename):
resource = os.path.join(attributes.TOOL_PATH, attributes.RESOURCES_DIRECTORY, filename)
if not os.path.isfile(resource):
raise ServicesException(ServicesException.RESOURCE_NOT_FOUND.format(resource))
return resource
def formatMakeCommand(makefile, args=(), **variables):
makeTool = attributes.MAKE_TOOL.get(getPlatform(), "make")
command = [makeTool, "-f", makefile]
command.extend(args)
command.extend(['{0}="{1}"'.format(*pair) for pair in variables.items()])
return command
def __selectIgnoredFiles(makefiles, pathName):
ignoreFilePathname = os.path.join(pathName, attributes.IGNORE_FILENAME)
if os.path.isfile(ignoreFilePathname):
ignoreList = [line.strip() for line in readNonEmptyLines(ignoreFilePathname)]
if ignoreList:
return __selectFilesByPatterns(makefiles, ignoreList)
return set()
def __selectFilesByPatterns(makefiles, patterns):
filtered = []
for pattern in patterns:
filtered.extend(filterFileNames(makefiles, pattern))
return set(filtered)
def meta_class(mcs, *cls, **mewAttributes):
mangledName = "_".join(("_", mcs.__name__,) + tuple(item.__name__ for item in cls))
return mcs(mangledName, cls, mewAttributes)
def abstract_method(method):
return abstract_callable(method)
def abstract_callable(callableObject):
def call_wrapper(*args, **kwargs):
typeName = 'method' if args and hasattr(args[0], callableObject.__name__) else callableObject.__class__.__name__
raise NotImplementedError("The {0} '{1}' must be implemented!".format(typeName, callableObject.__name__))
return call_wrapper
class SingletonMeta(type):
""" A metaclass that creates a Singleton base class when called. """
__instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls.__instances:
cls.__instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
return cls.__instances[cls]
def clear(cls):
try:
del cls.__instances[cls] # pylint: disable=no-member
except KeyError:
pass
class FactoryByLegacy(type):
def __init__(cls, name, bases, spec):
super(FactoryByLegacy, cls).__init__(name, bases, spec)
if not hasattr(cls, '_announcedFactoryItems'):
cls._announcedFactoryItems = {}
cls.__registerIfAnnounced()
def __registerIfAnnounced(cls):
uidAttribute = getattr(cls, 'uidAttribute', 'UID')
uidValue = getattr(cls, uidAttribute, None)
if uidValue:
cls._announcedFactoryItems[uidValue] = cls
def __iter__(cls):
for _uid in cls._announcedFactoryItems:
yield _uid
def __contains__(cls, uid):
return uid in cls._announcedFactoryItems
def __len__(cls):
return len(cls._announcedFactoryItems)
def get(cls, uid, default=None):
return cls._announcedFactoryItems.get(uid, default)
def create(cls, uid, *args, **kwargs):
_cls = cls._announcedFactoryItems.get(uid, cls)
return _cls(*args, **kwargs)
class Configuration(object):
def __init__(self, **kwargs):
super(Configuration, self).__init__()
self.__configuration = kwargs
def __iter__(self):
for key in self.__configuration:
yield key
def __getitem__(self, key):
return self.__configuration.get(key, None)
def update(self, iterable=None, **kwargs):
if iterable:
self.__configuration.update({key: iterable[key] for key in iterable})
self.__configuration.update(kwargs)
def getInt(self, key, default=0):
try:
return int(self.__configuration.get(key, ''))
except ValueError:
return default
def get(self, key, default):
return self.__configuration.get(key, default)
class ServicesException(Exception):
"""
Custom exception for STAT services
"""
NO_VS_TOOLS_FOUND = "No VS TOOLS were found on this PC."
NO_NMAKE_FOUND = "No NMAKE was found on this PC."
RESOURCE_NOT_FOUND = "No resource by filename '{0}' was found."
if __name__ == '__main__':
pass