-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
329 lines (255 loc) · 11.1 KB
/
setup.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
'''
Check Setup -
Verify or Install Text Files + Log Process
'''
import os
import sys
import time
from datetime import datetime
import shutil
# you can make more functions or global read-only variables here if you please!
def logging(logs: list, date: str, time: str) -> None:
filename = f"/home/logs/{date}/{time}.txt"
directory = f"/home/logs/{date}"
# Check if logs directory exists; if not, create it
if not os.path.isdir("/home/logs"):
os.mkdir("/home/logs")
# Check today's log directory exists; if not, create it
if not os.path.isdir(directory):
os.mkdir(directory)
# Create log file
log_file = open(filename, 'w')
i = 0
while i < len(logs):
log_file.write(logs[i] + '\n')
i += 1
log_file.close()
def verification(master: str, timestamp: str) -> list:
output = []
output.append(f"{timestamp} Start verification process.")
output.append(f"{timestamp} Extracting paths in configuration file.")
# Retrieve config.txt file ('with' and 'as' keywords ensure file is closed afterwards)
config_file = master + 'config.txt'
with open(config_file) as conf:
conf = conf.readlines()
# Retrive three lists:
# - mk_dirs = contains the directories from config.txt that need to be made (e.g. /home/files/)
# - mk_files = contains the files from config.txt that need to be made (e.g. /home/files/animals.txt)
# - master_files = contains the files from config.txt with absolute path to master directory (e.g. /home/master/files/animals.txt)
mk_dirs, mk_files, master_files = analyse_conf(conf, master)
total_dirs = len(mk_dirs)
total_files = len(mk_files)
output.append(f"Total directories to check: {total_dirs}")
output.append(f"{timestamp} Checking if directories exists.")
# Check if directories exist, if not, notify user
i = 0
while i < total_dirs:
if os.path.isdir(mk_dirs[i]):
output.append(f"{mk_dirs[i]} is found!")
else:
output.append(f"{mk_dirs[i]} NOT found!")
i += 1
output.append(f"{timestamp} Extracting files in configuration file.")
i = 0
while i < total_files:
output.append(f"File to check: {mk_files[i]}")
i += 1
output.append(f"Total files to check: {i}")
output.append(f"{timestamp} Checking if files exists.")
i = 0
while i < total_files:
if os.path.isfile(mk_files[i]):
output.append(f"{mk_files[i]} found!")
else:
output.append(f"{mk_files[i]} NOT found!")
i += 1
output.append(f"{timestamp} Check contents with master copy.")
i = 0
# Compare master files with user files
while i < total_files:
if os.path.isfile(mk_files[i]) and os.path.isfile(master_files[i]):
with open(mk_files[i]) as file1:
with open(master_files[i]) as file2:
file1_list = file1.readlines()
file2_list = file2.readlines()
if file1_list == file2_list:
output.append(f"{mk_files[i]} is same as {master_files[i]}: True")
else:
j = 0
while j < len(file2_list):
# Assign elements to variables for comparison since f-string expressions cannot include backslashes (causes SyntaxError)
first = file1_list[j].strip('\n')
second = file2_list[j].strip('\n')
output.append(f"File name: {mk_files[i]}, {first}, {second}")
if file1_list[j] != file2_list[j]:
output.append("Abnormalities detected...")
return output
j += 1
i += 1
output.append(f"{timestamp} Verification complete.")
return output
def installation(master: str, timestamp: str) -> list:
output = []
output.append(f"{timestamp} Start installation process.")
output.append(f"{timestamp} Extracting paths in configuration file.")
# Retrieve config.txt file ('with' and 'as' keywords ensure file is closed afterwards)
config_file = master + 'config.txt'
with open(config_file) as conf:
conf = conf.readlines()
# Retrive three lists:
# - mk_dirs = contains the directories from config.txt that need to be made (e.g. /home/files/)
# - mk_files = contains the files from config.txt that need to be made (e.g. /home/files/animals.txt)
# - master_files = contains the files from config.txt with absolute path to master directory (e.g. /home/master/files/animals.txt)
mk_dirs, mk_files, master_files = analyse_conf(conf, master)
total_dirs = len(mk_dirs)
total_files = len(mk_files)
output.append(f"Total directories to create: {total_dirs}")
output.append(f"{timestamp} Create new directories.")
# Check if directories exist, if not, create them
i = 0
while i < total_dirs:
if not os.path.isdir(mk_dirs[i]):
os.mkdir(mk_dirs[i])
output.append(f"{mk_dirs[i]} is created successfully.")
else:
output.append(f"{mk_dirs[i]} exists. Skip directory creation.")
i += 1
output.append(f"{timestamp} Extracting paths of all files in {master}.")
# Get list of files in master directories and sub-directories
master_list = list_files(master)
# Remove config.txt from the list
master_list.remove(master+'config.txt')
i = 0
while i < len(master_list):
output.append(f"Found: {master_list[i]}")
i += 1
output.append(f"{timestamp} Create new files.")
i = 0
while i < total_files:
output.append(f"Creating file: {mk_files[i]}")
i += 1
output.append(f"{timestamp} Copying files.")
i = 0
while i < len(master_files):
output.append(f"Locating: {master_files[i].split('/')[-1]}")
if os.path.isfile(master_files[i]):
output.append(f"Original path: {master_files[i]}")
output.append(f"Destination path: {mk_files[i]}")
shutil.copy(master_files[i], mk_files[i])
else:
output.append(f"Original path: {master_files[i]} is not found.")
output.append("Installation error...")
return output
i += 1
output.append(f"{timestamp} Installation complete.")
return output
def analyse_conf(conf, master):
# Count the number of directories to create and store them in a list (mk_dirs)
# Also create a list of files that need to be created (mk_files)
i = 0
mk_dirs = []
mk_files = []
master_files = []
current_dir = master
while i < len(conf):
# If element is a directory
if conf[i][0] == "/":
mk_dirs.append(conf[i].strip('\n'))
current_dir = conf[i].strip('\n').strip()
# Else if it's a file
else:
mk_files.append(current_dir+conf[i][2:].strip('\n').strip())
master_files.append(current_dir.strip('/home/')+conf[i][1:].strip('\n').strip())
i += 1
return mk_dirs, mk_files, master_files
def list_files(directory):
# Get files and folders in directory
abs_dir = os.listdir(directory)
if abs_dir:
direct = directory + abs_dir[0] + '/'
file_names = []
# Iterate through files and sub-directories to find other files and create list of absolute file paths
i = 0
while i < len(abs_dir):
# Create absolute path for file
abs_file = os.path.join(directory, abs_dir[i])
# If directory, parse it to list_files() function and add the resulting list it to our file_names list
if os.path.isdir(abs_file):
file_names += list_files(abs_file)
# If just a file, append it to our file_names list
elif abs_file.endswith('.txt'):
file_names.append(abs_file)
i += 1
return sorted(file_names)
def main(master: str, flags: str, timestamp: str):
if len(flags) == 2:
if flags[1] == "i":
output = installation(master, timestamp)
elif flags[1] == "v":
output = verification(master, timestamp)
elif flags[1] == "i" or flags[2] == "i":
output = installation(master, timestamp)
elif flags[1] == "v" or flags[2] == "v":
output = verification(master, timestamp)
i = 0
while i < len(output):
print(output[i])
i += 1
if len(flags) == 3:
if flags[1] == "l" or flags[2] == "l":
# Generate current date and time from timestamp
timesplit = timestamp.split()
cur_date = (f"{timesplit[2]}-{timesplit[1]}-{timesplit[0]}")
cur_time = timesplit[3].replace(':', '_')
logging(output, cur_date, cur_time)
if __name__ == "__main__":
# If insufficient command lines arguments provided (no master directory given)
if len(sys.argv) < 2:
print("Insufficient arguments.", file=sys.stderr)
exit(1)
# If master directory exists
elif not os.path.isdir(sys.argv[1]):
print("Invalid master directory.", file=sys.stderr)
exit(1)
# If "/" missing at end of master directory
elif sys.argv[1][-1] != "/":
print("Invalid master directory.", file=sys.stderr)
exit(1)
# If flag not in command line arguments assign it default value
elif len(sys.argv) < 3:
flags = "-il"
# If flag doesn't start with "-"
elif sys.argv[2][0] != "-":
print("Invalid flag. Flag must start with '-'.", file=sys.stderr)
exit(1)
# If flag has only log command
elif sys.argv[2] == "-l":
print("Invalid flag. Log can only run with install or verify.", file=sys.stderr)
exit(1)
# If flag has both verify and install commands
elif sys.argv[2] == "-vi" or sys.argv[2] == "-iv":
print("Invalid flag. Choose verify or install process not both.", file=sys.stderr)
exit(1)
# If flag has the same command repeated twice
elif len(sys.argv[2]) == 3 and sys.argv[2][1] == sys.argv[2][2]:
print("Invalid flag. Each character must be unique.", file=sys.stderr)
exit(1)
# If flag argument is too long
elif len(sys.argv[2]) > 3:
print("Invalid flag. Too many characters.")
exit(1)
# If flag has neither log, verify or install commands (l, v or i)
elif len(sys.argv[2]) == 2 and sys.argv[2][1] != "l" and sys.argv[2][1] != "v" and sys.argv[2][1] != "i":
print("Invalid flag. Character must be a combination of 'v' or 'i' and 'l'.", file=sys.stderr)
exit(1)
elif len(sys.argv[2]) == 3 and ((sys.argv[2][1] != "l" and sys.argv[2][1] != "v" and sys.argv[2][1] != "i") or
(sys.argv[2][2] != "l" and sys.argv[2][2] != "v" and sys.argv[2][2] != "i")):
print("Invalid flag. Character must be a combination of 'v' or 'i' and 'l'.", file=sys.stderr)
exit(1)
# Assign command line arguments to variables
else:
flags = sys.argv[2]
master = sys.argv[1]
# Get current date and time
timestamp = datetime.fromtimestamp(time.time()).strftime("%d %b %Y %H:%M:%S")
main(master, flags, timestamp)