generated from hexmod/hash-code-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
136 lines (105 loc) · 3.84 KB
/
helper.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
import datetime
import importlib
import os
import pkgutil
import sys
import time
import zipfile
# Add the src folder to the path to allow the submission to import
# files in the src folder
sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
# Import the entry point for our submission
import main as hashCodeImp
DATA_LOCATION = ".\\data"
OUTPUT_LOCATION = ".\\output"
def run():
now = datetime.datetime.now()
print("Hash Code Runner - Welcome to Hash Code", now.year)
repl()
def repl():
while True:
print("Please choose an option:")
data_files = get_data_files()
(action, param) = get_action(data_files)
refresh_source()
if action == "singleFile":
run_for_file(get_file_path(param), os.path.join(OUTPUT_LOCATION, param))
elif action == "allFiles":
for a_file in data_files:
run_for_file(get_file_path(a_file), os.path.join(OUTPUT_LOCATION, a_file))
elif action == "zip":
zip_project(OUTPUT_LOCATION)
elif action == "allFilesAndZip":
for a_file in data_files:
run_for_file(get_file_path(a_file), os.path.join(OUTPUT_LOCATION, a_file))
zip_project(OUTPUT_LOCATION)
def get_file_path(a_file):
return os.path.join(DATA_LOCATION, a_file)
def refresh_source():
# Reload our source in case we have made any changes
importlib.reload(hashCodeImp)
for _, name, _ in pkgutil.iter_modules(['src']):
if name in sys.modules:
importlib.reload(sys.modules[name])
def get_data_files():
data_files = []
for file_name in os.listdir(DATA_LOCATION):
if file_name != "readme.txt" and os.path.isfile(os.path.join(DATA_LOCATION, file_name)):
data_files.append(file_name)
return data_files
def get_action(data_files):
options = [("doNothing", None)]
count = 1
for a_file in data_files:
print("[" + str(count) + "]", a_file)
options.append(("singleFile", a_file))
count += 1
print("[" + str(count) + "]", "Run for all Files")
options.append(("allFiles", None))
count += 1
print("[" + str(count) + "]", "Zip source")
options.append(("zip", None))
count += 1
print("[" + str(count) + "]", "Run for all files and zip source")
options.append(("allFilesAndZip", None))
user_choice = input()
return options[convert_input_to_option(user_choice, len(options))]
def convert_input_to_option(input, upper_bound):
try:
option_num = int(input)
if option_num > 0 and option_num < upper_bound:
return option_num
else:
return 0
except ValueError:
return 0 # Do Nothing
def run_for_file(file_location, output_location):
print("Running hash code entry against", file_location)
print("-----------------")
print()
start = time.time_ns()
hashCodeImp.run(file_location, output_location)
print()
print("-----------------")
end = time.time_ns()
print("Ran in:", (end - start) / 1000000000, "seconds")
print()
def zip_project(output_location):
print("Zipping project and saving to", output_location)
now = datetime.datetime.now()
zip_name = OUTPUT_LOCATION + "\\hashCode" + str(now.year) + ".zip"
zipf = zipfile.ZipFile(zip_name, "w", zipfile.ZIP_DEFLATED)
# Add our Pipfile, so any deps can be downloaded
zipf.write(".\\Pipfile")
zipf.write(".\\Pipfile.lock")
for root, dirs, files in os.walk(".\\src"):
for file in files:
filename = os.path.join(root, file)
# Ignore the top level init file, as this is only needed by the code runner
if filename != ".\\src\\__init__.py":
zipf.write(filename, filename.replace(".\\src\\", ".\\"))
zipf.close()
print()
# When run from the terminal
if __name__ == '__main__':
run()