forked from LukeMeyer1/Model_Reuse_CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
112 lines (88 loc) · 3.69 KB
/
run
File metadata and controls
112 lines (88 loc) · 3.69 KB
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
#!/usr/bin/env python3
# from Logger import validate_log_file_or_die #new import
# validate_log_file_or_die()
import argparse, sys, os
def _early_log_file_check():
raw = os.getenv("LOG_FILE") or os.getenv("LOG_PATH") or os.getenv("LOGFILE")
if raw is None:
return # not set -> do nothing
p = raw.strip()
if not p:
sys.stderr.write("ERROR: LOG_FILE is empty or whitespace.\n"); sys.exit(1)
p = os.path.expanduser(os.path.expandvars(p))
# reject directories / trailing slash
if p.endswith(os.sep) or (os.altsep and p.endswith(os.altsep)) or os.path.isdir(p):
sys.stderr.write(f"ERROR: LOG_FILE points to a directory: {raw}\n"); sys.exit(1)
parent = os.path.dirname(p) or "."
if not os.path.isdir(parent):
sys.stderr.write(f"ERROR: LOG_FILE parent dir does not exist: {parent}\n"); sys.exit(1)
# must ALREADY exist (do not create)
if not os.path.isfile(p):
sys.stderr.write(f"ERROR: LOG_FILE does not exist: {p}\n"); sys.exit(1)
# must be writable without creating/truncating
try:
with open(p, "r+", encoding="utf-8"):
pass
except Exception as e:
sys.stderr.write(f"ERROR: LOG_FILE not writable: {p}: {e}\n"); sys.exit(1)
_early_log_file_check()
# --- END EARLY CHECK ---
from pathlib import Path
import Orchestrator
def validate_environment_variables():
"""Validate required environment variables at startup."""
# Validate GitHub token only if it's set
github_token = os.getenv("GITHUB_TOKEN")
if github_token and len(github_token.strip()) > 0:
# Check if GitHub token is valid by making a simple API call
try:
import requests
response = requests.get(
"https://api.github.com/user",
headers={"Authorization": f"token {github_token}"},
timeout=10
)
if response.status_code != 200:
sys.exit(1)
except requests.exceptions.RequestException as e:
sys.exit(1)
except ImportError:
# If requests is not available, skip validation but warn
pass
# Validate log file path only if LOG_FILE is explicitly set
log_file = os.getenv("LOG_FILE")
if log_file:
try:
log_path = Path(log_file)
# Check if parent directory exists and is writable
if not log_path.parent.exists():
sys.exit(1)
if not os.access(log_path.parent, os.W_OK):
sys.exit(1)
except (OSError, PermissionError) as e:
sys.exit(1)
def main(argv=None) -> int:
# Validate environment variables at startup
validate_environment_variables()
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description="CLI for Trustworthy Model Re-use")
parser.add_argument("command", help="install | test | URL_FILE")
# Parse-known so we can forward extras ONLY for 'test'
args, extra = parser.parse_known_args(argv)
if args.command == "test":
# Forward any remaining args (e.g., --cov, -k ...) to pytest
return Orchestrator.run_tests(pytest_args=extra)
# For every other command, unknown args are an error (strict mode)
if extra:
parser.error(f"unrecognized arguments: {' '.join(extra)}") # exit code 2
if args.command == "install":
return Orchestrator.install_dependencies()
# Treat anything else as a file-first workflow
file_path = Path(args.command)
if not file_path.exists():
print(f"Error: file {file_path} not found.", file=sys.stderr)
return 1
return Orchestrator.process_urls(file_path)
if __name__ == "__main__":
raise SystemExit(main())