|
1 | 1 | import json |
2 | 2 | import sys |
| 3 | +import os |
3 | 4 | import requests |
4 | 5 | from jsonschema import validate, ValidationError |
5 | 6 | from concurrent.futures import ThreadPoolExecutor, as_completed |
6 | 7 | from termcolor import colored |
7 | 8 |
|
| 9 | +def get_data_file_path(filename): |
| 10 | + """Get the path to data files.""" |
| 11 | + # Try current directory first |
| 12 | + if os.path.exists(filename): |
| 13 | + return filename |
| 14 | + |
| 15 | + # Try script directory |
| 16 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 17 | + data_path = os.path.join(script_dir, filename) |
| 18 | + if os.path.exists(data_path): |
| 19 | + return data_path |
| 20 | + |
| 21 | + # Try installation directory |
| 22 | + try: |
| 23 | + import site |
| 24 | + for site_dir in site.getsitepackages(): |
| 25 | + data_path = os.path.join(site_dir, filename) |
| 26 | + if os.path.exists(data_path): |
| 27 | + return data_path |
| 28 | + except: |
| 29 | + pass |
| 30 | + |
| 31 | + raise FileNotFoundError(f"Cannot find {filename}") |
| 32 | + |
| 33 | + |
8 | 34 | def load_targets(json_file, schema_file): |
9 | 35 | """Load and validate the target platforms from a JSON file.""" |
10 | | - with open(json_file, 'r') as file: |
| 36 | + try: |
| 37 | + json_path = get_data_file_path(json_file) |
| 38 | + schema_path = get_data_file_path(schema_file) |
| 39 | + except FileNotFoundError as e: |
| 40 | + print(f"Error: {e}") |
| 41 | + print("Make sure data.json and data.schema.json are in the same directory.") |
| 42 | + exit(1) |
| 43 | + |
| 44 | + with open(json_path, 'r') as file: |
11 | 45 | data = json.load(file) |
12 | 46 |
|
13 | | - |
14 | | - with open(schema_file, 'r') as schema: |
| 47 | + with open(schema_path, 'r') as schema: |
15 | 48 | schema_data = json.load(schema) |
16 | 49 | try: |
17 | 50 | validate(instance=data, schema=schema_data) |
@@ -152,4 +185,4 @@ def main(): |
152 | 185 |
|
153 | 186 |
|
154 | 187 | if __name__ == "__main__": |
155 | | - main() |
| 188 | + main() |
0 commit comments