-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.py
251 lines (221 loc) · 7.7 KB
/
Functions.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
#!/usr/bin/env python3
import os, sys
import toml
import subprocess
import zipfile
import requests
import shutil
# FUNCTIONS
def coloredText(message='', style='1', background_color='49m', text_color='39'):
# http://ozzmaker.com/add-colour-to-text-in-python/
escape = '\033['
reset = '0m'
return f'{escape}{style};{text_color};{background_color}{message}{escape}{reset}'
def printFailMessage(message, exception=None):
bright_red = '31'
extended_message = f'- Failed to {message}'
if exception:
extended_message += os.linesep
extended_message += str(exception)
report = coloredText(message=extended_message, text_color=bright_red)
print(report)
def printSuccessMessage(message):
bright_green = '32'
extended_message = f'+ Succeeded to {message}'
report = coloredText(message=extended_message, text_color=bright_green)
print(report)
def printNeutralMessage(message):
bright_blue = '34'
extended_message = f'* {message}'
report = coloredText(message=extended_message, text_color=bright_blue)
print(report)
def run(*args):
subprocess.run(
args,
capture_output=False,
universal_newlines=True, # converts the output to a string instead of a byte array.
#check=True # forces the Python method to throw an exception if the underlying process encounters errors
)
def downloadFile(url, destination):
if os.path.exists(destination):
printNeutralMessage(f'File already exists {destination}')
return
try:
message = f'download from {url}'
file = requests.get(url, allow_redirects=True)
open(destination, 'wb').write(file.content)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def attachDmg(file):
try:
message = f'attach {file}'
run('hdiutil', 'attach', file)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def installSilently(installer, silent_script):
try:
message = f'install {installer}'
run(
installer,
'--script', silent_script,
'--no-force-installations'
)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def config():
return toml.load(os.path.join(os.getcwd(), 'pyproject.toml'))
def osName():
platform = sys.platform
if platform.startswith('darwin'):
return 'macos'
elif platform.startswith('lin'):
return 'linux'
elif platform.startswith('win'):
return 'windows'
else:
print("- Unsupported platform '{0}'".format(platform))
return None
def environmentVariable(name, default=None):
value = os.getenv(name)
if value is not None:
return value
else:
printFailMessage(f'find environment variable {name}, using default value {default}')
return default
def setEnvironmentVariable(name, value):
try:
message = f'set environment variable {name} to {value}'
os.environ[name] = value
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def addReadPermission(file):
try:
message = f'add read permissions to {file}'
run('chmod', 'a+x', file)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def createFile(path, content):
if os.path.exists(path):
printNeutralMessage(f'File already exists {path}')
return
try:
message = f'create file {path}'
with open(path, "w") as file:
file.write(content)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def createDir(path):
if os.path.exists(path):
printNeutralMessage(f'Directory already exists {path}')
return
try:
message = f'create dir {path}'
os.mkdir(path)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def copyFile(source, destination):
path = os.path.join(destination, os.path.basename(source))
if os.path.exists(path):
printNeutralMessage(f'File already exists {path}')
return
try:
message = f'copy file {source} to {destination}'
shutil.copy2(source, destination, follow_symlinks=True)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def moveDir(source, destination):
path = os.path.join(destination, os.path.basename(source))
if os.path.exists(path):
printNeutralMessage(f'Directory already exists {path}')
return
try:
message = f'move dir {source} to {destination}'
shutil.move(source, destination)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)
def dict2xml(d, root_node=None, add_xml_version=True):
wrap = False if root_node is None or isinstance(d, list) else True
root = 'root' if root_node is None else root_node
root_singular = root[:-1] if root[-1] == 's' else root
xml = ''
attr = ''
children = []
if add_xml_version:
xml += '<?xml version="1.0" ?>'
if isinstance(d, dict):
for key, value in dict.items(d):
if isinstance(value, (dict, list)):
children.append(dict2xml(value, root_node=key, add_xml_version=False))
elif key[0] == '@':
attr = attr + ' ' + key[1::] + '="' + str(value) + '"'
else:
xml = '<' + key + ">" + str(value) + '</' + key + '>'
children.append(xml)
elif isinstance(d, list):
for value in d:
children.append(dict2xml(value, root_node=root_singular, add_xml_version=False))
else:
raise TypeError(f"Type {type(d)} is not supported")
end_tag = '>' if children else '/>'
if wrap:
xml = '<' + root + attr + end_tag
if children:
xml += "".join(children)
if wrap:
xml += '</' + root + '>'
return xml
def zipDir(source, destination):
# https://thispointer.com/python-how-to-create-a-zip-archive-from-multiple-files-or-directory/
# https://stackoverflow.com/questions/27991745/zip-file-and-avoid-directory-structure
# https://stackoverflow.com/questions/32640053/compressing-directory-using-shutil-make-archive-while-preserving-directory-str
# Zip all the files from given directory
"""
Compress a directory (ZIP file).
"""
# Check if src exists
if not os.path.exists(source):
printFailMessage(f"zip directory (it doesn't exist) {source}")
sys.exit()
return
# create a ZipFile object
try:
message = f'zip dir {source} to {destination}'
with zipfile.ZipFile(destination, 'w') as zf:
rootdirname = os.path.basename(source)
for dirpath, _, filenames in os.walk(source):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
arcpath = os.path.relpath(filepath, rootdirname)
zf.write(filepath, arcpath)
except Exception as exception:
printFailMessage(message, exception)
sys.exit()
else:
printSuccessMessage(message)