-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindows_wrapper.py
executable file
·165 lines (142 loc) · 5.75 KB
/
windows_wrapper.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import sys
import multiprocessing
import logging
import traceback
import runpy
import time
import appdirs
# Set up logging to write to a file
log_file = os.path.join(
os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.getcwd(),
'cli_debrid.log'
)
logging.basicConfig(
filename=log_file,
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Also print to console
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
def setup_environment():
if sys.platform.startswith('win'):
app_name = "cli_debrid"
app_author = "cli_debrid"
base_path = appdirs.user_data_dir(app_name, app_author)
config_dir = os.path.join(base_path, 'config')
logs_dir = os.path.join(base_path, 'logs')
db_content_dir = os.path.join(base_path, 'db_content')
os.environ['USER_CONFIG'] = config_dir
os.environ['USER_LOGS'] = logs_dir
os.environ['USER_DB_CONTENT'] = db_content_dir
else:
os.environ.setdefault('USER_CONFIG', '/user/config')
os.environ.setdefault('USER_LOGS', '/user/logs')
os.environ.setdefault('USER_DB_CONTENT', '/user/db_content')
# Ensure directories exist
for dir_path in [os.environ['USER_CONFIG'], os.environ['USER_LOGS'], os.environ['USER_DB_CONTENT']]:
os.makedirs(dir_path, exist_ok=True)
# Create empty lock file if it doesn't exist
lock_file = os.path.join(os.environ['USER_CONFIG'], '.config.lock')
if not os.path.exists(lock_file):
open(lock_file, 'w').close()
def adjust_sys_path():
if getattr(sys, 'frozen', False):
base_dir = os.path.dirname(sys.executable)
else:
base_dir = os.path.dirname(os.path.abspath(__file__))
# Add base directory to sys.path
if base_dir not in sys.path:
sys.path.insert(0, base_dir)
# Add cli_battery directory to sys.path
cli_battery_dir = os.path.join(base_dir, 'cli_battery')
if cli_battery_dir not in sys.path:
sys.path.insert(0, cli_battery_dir)
def get_script_path(script_name):
if getattr(sys, 'frozen', False):
# When frozen, look for scripts in the same directory as the executable
base_dir = os.path.dirname(sys.executable)
# Try to find the script in the _MEIPASS directory first (PyInstaller temp directory)
if hasattr(sys, '_MEIPASS'):
meipass_path = os.path.join(sys._MEIPASS, script_name)
if os.path.exists(meipass_path):
return meipass_path
else:
base_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base_dir, script_name)
def run_script(script_name, port=None, battery_port=None):
script_path = get_script_path(script_name)
logging.info(f"Running script: {script_path}")
try:
# Add the directory containing the script to sys.path
script_dir = os.path.dirname(script_path)
if script_dir not in sys.path:
sys.path.insert(0, script_dir)
# Set up environment before running the script
setup_environment()
# Set environment variables for ports
if port:
os.environ['CLI_DEBRID_PORT'] = str(port)
if battery_port:
os.environ['CLI_DEBRID_BATTERY_PORT'] = str(battery_port)
runpy.run_path(script_path, run_name='__main__')
except Exception as e:
logging.error(f"Error running script {script_name}: {str(e)}")
logging.error(traceback.format_exc())
def run_main():
logging.info("Starting run_main()")
# Parse command line arguments
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, help='Port for main web server')
parser.add_argument('--battery-port', type=int, help='Port for battery web server')
args = parser.parse_args()
# Set the battery port in the environment before starting any processes
if args.battery_port:
os.environ['CLI_DEBRID_BATTERY_PORT'] = str(args.battery_port)
script_names = ['main.py', os.path.join('cli_battery', 'main.py')]
processes = []
# Start both processes in parallel with appropriate ports
for script_name in script_names:
if 'cli_battery' in script_name:
# For battery process, pass the battery port
process = multiprocessing.Process(
target=run_script,
args=(script_name,),
kwargs={'battery_port': args.battery_port}
)
logging.info(f"Starting battery process on port {args.battery_port}")
else:
# For main process, pass the main port
process = multiprocessing.Process(
target=run_script,
args=(script_name,),
kwargs={'port': args.port}
)
logging.info(f"Starting main process on port {args.port}")
processes.append(process)
process.start()
try:
# Wait for both processes to complete
for process in processes:
process.join()
except KeyboardInterrupt:
logging.info("Keyboard interrupt received. Terminating scripts.")
for process in processes:
if process.is_alive():
process.terminate()
process.join()
if __name__ == "__main__":
logging.info("Starting cli_debrid.exe")
try:
multiprocessing.freeze_support()
adjust_sys_path()
run_main()
except Exception as e:
logging.error(f"Unhandled exception: {str(e)}")
logging.error(traceback.format_exc())
logging.info("cli_debrid.exe execution completed")
# Keep console window open
input("Press Enter to exit...")