|
2 | 2 | import os |
3 | 3 | import logging |
4 | 4 | import importlib |
| 5 | +from pathlib import Path |
5 | 6 | from datetime import datetime |
6 | 7 |
|
7 | 8 | from threading import Thread, Lock, Semaphore |
|
23 | 24 | # |
24 | 25 | try: |
25 | 26 | import pkg_resources |
26 | | - scriptsDir = pkg_resources.resource_filename( |
27 | | - 'C2Client', |
28 | | - 'Scripts' |
29 | | - ) |
30 | | - |
| 27 | + scriptsDir = pkg_resources.resource_filename('C2Client', 'Scripts') |
31 | 28 | except ImportError: |
| 29 | + # Fallback: relative to this file (…/C2Client/Scripts) |
32 | 30 | scriptsDir = os.path.join(os.path.dirname(__file__), 'Scripts') |
33 | 31 |
|
34 | | -if not os.path.exists(scriptsDir): |
35 | | - os.makedirs(scriptsDir) |
| 32 | +scripts_path = Path(scriptsDir).resolve() |
| 33 | +scripts_path.mkdir(parents=True, exist_ok=True) |
| 34 | + |
| 35 | +# Ensure it's a real package |
| 36 | +(scripts_path / "__init__.py").touch(exist_ok=True) |
| 37 | + |
| 38 | +# Ensure the project root (parent of C2Client) is on sys.path so |
| 39 | +# `C2Client.Scripts` is importable as a package |
| 40 | +# e.g. /path/to/project_root/C2Client/Scripts |
| 41 | +project_root = scripts_path.parent.parent # .../project_root |
| 42 | +if str(project_root) not in sys.path: |
| 43 | + sys.path.insert(0, str(project_root)) |
36 | 44 |
|
| 45 | +package_name = "C2Client.Scripts" |
37 | 46 |
|
| 47 | +# ---------------------------- |
| 48 | +# Load all scripts as modules |
| 49 | +# ---------------------------- |
38 | 50 | LoadedScripts = [] |
39 | | -sys.path.insert(1, scriptsDir) |
40 | | -for scriptName in os.listdir(scriptsDir): |
41 | | - if scriptName.endswith(".py") and scriptName != "__init__.py": |
42 | | - module_name = scriptName[:-3] |
| 51 | +for entry in scripts_path.iterdir(): |
| 52 | + if entry.suffix == ".py" and entry.name != "__init__.py": |
| 53 | + modname = f"{package_name}.{entry.stem}" |
43 | 54 | try: |
44 | | - # Dynamically import the script |
45 | | - importedScript = importlib.import_module(module_name) |
46 | | - LoadedScripts.append(importedScript) |
47 | | - print(f"Successfully imported {scriptName}") |
48 | | - except ImportError as e: |
49 | | - print(f"Failed to import {scriptName}: {e}") |
| 55 | + m = importlib.import_module(modname) |
| 56 | + LoadedScripts.append(m) |
| 57 | + print(f"Successfully imported {modname}") |
| 58 | + except Exception as e: |
| 59 | + print(f"Failed to import {modname}: {e}") |
| 60 | + traceback.print_exc() |
50 | 61 |
|
51 | 62 |
|
52 | 63 | # |
|
0 commit comments