-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfppy.py
100 lines (82 loc) · 3 KB
/
sfppy.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
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
"""
===============================================================================
SFPPy Command Line Interface (CLI)
===============================================================================
Provides a command-line tool (`sfppy`) to run SFPPy scripts from anywhere
without manually modifying `sys.path`. It automatically adds the SFPPy root
directory (which contains `patankar/` and example scripts) to `sys.path`.
**Features:**
- Detects and adds `SFPPy/` (the parent of `patankar/`) to `sys.path`.
- Runs Python scripts in the correct directory to handle relative imports.
- Enables running SFPPy scripts globally without needing to be in the project folder.
**Usage:**
```sh
sfppy example1.py
```
or with arguments:
```sh
sfppy example2.py --some-option value
```
**Installation:**
1. Ensure `cli.py` is placed inside `SFPPy/` (not inside `patankar/`).
2. Modify `setup.py` to register `sfppy` as a console script:
```python
entry_points={
"console_scripts": [
"sfppy=cli:main",
],
},
```
3. Install SFPPy:
```sh
pip install -e .
```
4. Now you can run SFPPy scripts from anywhere using `sfppy <script.py>`.
-------------------------------------------------------------------------------
@project: SFPPy - SafeFoodPackaging Portal in Python initiative
@author: Olivier Vitrac
@license: MIT
@date: 2025-03-07
===============================================================================
"""
import sys
import os
def main():
"""
Entry point for the SFPPy CLI.
This function:
1. Checks if a script path is provided as an argument.
2. Identifies `SFPPy/` (the parent of `patankar/`) as the project root.
3. Adds `SFPPy/` to `sys.path` to allow imports like `from patankar.module import X`.
4. Changes the working directory to the script's location for proper relative paths.
5. Executes the script in a controlled namespace.
Usage:
sfppy <script.py> [args]
Raises:
SystemExit: If no script is provided.
"""
if len(sys.argv) < 2:
print("Usage: sfppy <script.py> [args]")
sys.exit(1)
script_path = os.path.abspath(sys.argv[1]) # Absolute path of the script
if not os.path.isfile(script_path):
print(f"Error: Script '{script_path}' not found.")
sys.exit(1)
# Locate SFPPy root directory (parent of patankar/)
sfppy_root = os.path.dirname(os.path.abspath(__file__)) # This is SFPPy/
# Ensure SFPPy contains patankar/
if not os.path.isdir(os.path.join(sfppy_root, "patankar")):
print(f"Error: The detected SFPPy root '{sfppy_root}' does not contain 'patankar/'.")
sys.exit(1)
# Ensure SFPPy is in sys.path
if sfppy_root not in sys.path:
sys.path.insert(0, sfppy_root)
# Change working directory to the script's location
script_dir = os.path.dirname(script_path)
os.chdir(script_dir)
# Execute the script
script_globals = {}
with open(script_path, "r", encoding="utf-8") as f:
exec(f.read(), script_globals)
if __name__ == "__main__":
main()