forked from GraysonNocera/ece-461-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·191 lines (153 loc) · 5.7 KB
/
run
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
#!/usr/bin/env python3
import sys
import os
import subprocess
import json
import re
import pathlib
def sort_key(json_obj):
"""
Sort key for sorting json objects in descending order of NET_SCORE
"""
return -json_obj["NET_SCORE"]
def parse_test_results(test_dump: str):
"""
Parse results of running jest on tests
:param test_dump: stdout from running jest
:return: test_cases_passed, total_test_cases, percentage_coverage
"""
searches: list = [
r"All files\s*\|[^\|]*\|[^\|]*\|[^\|]*\|\s*",
r"\d*\.\d*",
r"Tests:\s*(\s*\d\s*failed,\s*\d\s*skipped,\s*|\d\s*failed,\s*|\d\s*skipped,\s*)?",
r"\d*",
r"\d*\s*passed,\s*",
r"\d*",
]
results: list = []
for i in range(0, len(searches), 2):
search_result: re.Match = re.search(searches[i], test_dump)
if not search_result:
return -1, -1, -1
_, end = search_result.span()
test_dump = test_dump[end:]
search_result: re.Match = re.search(searches[i + 1], test_dump)
if not search_result:
return -1, -1, -1
_, end = search_result.span()
results.append(test_dump[:end])
coverage, passed, total = results
return passed, total, coverage
def main():
write_to_file = False # set to true to write output to output.ndjson
if len(sys.argv) < 2:
print("Error: No mode specified.")
return 1
try:
with open(os.getenv("LOG_FILE"), "w") as f:
pass
except:
# No log file was provided or it couldn't be opened
# error_msg = "Warning: No log file provided.\n"
# error_msg += "To enable logging, set a path to a logging file in "
# error_msg += "environment variable LOG_FILE and set the log level "
# error_msg += "in environment variable LOG_LEVEL."
# print(error_msg)
pass
mode = sys.argv[1]
if mode == "install":
# INSTALL DEPENDENCIES
ret_val = subprocess.run("npm install --loglevel=error", shell=True, capture_output=True)
if ret_val.returncode != 0:
print("Error: NPM Install failed")
return 1
ret_val = ret_val.stdout.decode("utf-8")
match = re.search(r"up to date", ret_val)
if match:
print("0 dependencies installed: Up to date")
else:
num_packs = re.search(r"added (\d+) packages", ret_val)
if num_packs:
print(f"{num_packs.group(1)} dependencies installed... ")
elif mode == "build":
# BUILD PROJECT
ret_val = subprocess.run("tsc src/*.ts", shell=True)
if ret_val.returncode != 0:
print("Error: Build failed")
return 1
return 0
elif mode == "test":
# RUN TESTS
command = "npm test"
result: subprocess.CompletedProcess = subprocess.run(
command, shell=True, capture_output=True
)
passed, total, coverage = parse_test_results(
result.stdout.decode("utf-8") + result.stderr.decode("utf-8")
)
if passed == total == coverage == -1:
print("Testing data could not be acquired")
return 1
else:
print(f"Total: {total}")
print(f"Passed: {passed}")
print(f"Coverage: {coverage}%")
print(f"{passed}/{total} test cases passed. {coverage}% line coverage achieved.")
return 0
else:
# RANK MODULES
if len(sys.argv) < 2:
print("Arg 1 needs to be URL to input file or one of")
return 1
if not os.getenv("GITHUB_TOKEN"):
print("Error: Please specify GitHub token in environment variable GITHUB_TOKEN")
return 1
file_path = sys.argv[1]
try:
file = open(file_path, "r")
except FileNotFoundError:
print(f"Error: File not found at path: {file_path}")
return 1
node_ver = subprocess.run("node -v", shell=True, capture_output=True).stdout.decode("utf-8").strip()
with file:
output_list = []
for line in file:
# create the command string
line = line.strip()
if (node_ver == "v18.14.0"):
command = f"node src/main.js {line}"
else:
command = f"bin/bin/node src/main.js {line}"
# call main with each URL as argument
result = subprocess.run(command, shell=True, capture_output=True)
try:
json_output = json.loads(result.stdout.decode().strip())
output_list.append(json_output)
except:
print(f"Error when running command: {command}")
return 1
nan_list = []
good_list = []
# remove any modules that failed to be ranked
for item in output_list:
if (item['NET_SCORE'] is not None):
good_list.append(item)
else:
nan_list.append(item)
output_list = sorted(good_list, key=sort_key)
output_list.extend(nan_list)
output_str = "\n".join([json.dumps(item) for item in output_list])
if write_to_file:
root_dir = pathlib.PurePath(__file__).parent
with open(root_dir.joinpath("output.ndjson"), "w") as f:
f.write(output_str)
else:
print(output_str)
main()
## if __name__ == "__main__":
## main()
##
## else:
## print("Error: This file is not meant to be imported")
## print("Please run this file directly or edit line 158 of ece-461-project/run to allow it to be imported.")
## exit(1)