-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (39 loc) · 1.87 KB
/
main.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
import json
import argparse
import importlib.util
import sys
def main():
try:
parser = argparse.ArgumentParser(description='Applies a NAPE Control Action Test to a given evidence file.')
parser.add_argument('--evidence', help='The evidence file to evaluate.')
parser.add_argument('--test', help='The Control Acton test file.')
parser.add_argument('--check-install', action='store_true', help='Check if the CLI is installed and working.')
args = parser.parse_args()
# Custom validation
if (args.evidence and not args.test) or (args.test and not args.evidence):
parser.error("--evidence and --test must be provided together.")
if args.check_install:
print("NAPE Evaluator CLI is installed and working.")
sys.exit(0)
# Read the text file
with open(args.evidence, 'r') as f:
text = f.readlines()
# Dynamically import the Control Action file
spec = importlib.util.spec_from_file_location("module.name", args.test)
action_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(action_module)
# Call the evaluate function and print the result
outcome, reason = action_module.evaluate(text)
result = {"outcome": outcome, "reason": reason}
print(json.dumps(result))
except FileNotFoundError as e:
result = {"outcome": "error", "reason": "Unable to find the file(s) for evaluation. " + str(e)}
print(json.dumps(result))
except ImportError as e:
result = {"outcome": "error", "reason": "Failed to import the necessary files. " + str(e)}
print(json.dumps(result))
except Exception as e:
result = {"outcome": "error", "reason": "Failed to execute the evidence evaluation. " + str(e)}
print(json.dumps(result))
if __name__ == '__main__':
main()