-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
152 lines (130 loc) · 4.9 KB
/
run.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
"""CLI runner for TradingView Copier."""
import argparse
import asyncio
import signal
import subprocess
import sys
from pathlib import Path
class Runner:
"""Runner class for managing TradingView Copier operations."""
def run_proxy(self):
"""Start the TradingView proxy server."""
try:
subprocess.run(
[sys.executable, "src/scripts/start_proxy.py"],
check=True
)
except KeyboardInterrupt:
pass
except subprocess.CalledProcessError:
sys.exit(1)
def run_worker(self):
"""Start the MT5 worker."""
subprocess.run(["python", "src/scripts/start_worker.py"])
def update_requirements(self):
"""Update requirements.txt."""
subprocess.run(["python", "src/scripts/generate_requirements.py"])
def list_symbols(self):
"""List MT5 symbols."""
subprocess.run(["python", "src/scripts/manage_symbols.py", "--mt5-symbols"])
def manage_symbols(self):
"""Show symbol management help."""
print("\nSymbol Management Commands:")
print("---------------------------")
print("List symbols: python run.py symbols")
print("Add mapping: python run.py symbols-add BTCUSD BTCUSD.r")
print("Remove mapping: python run.py symbols-remove BTCUSD")
print("Update suffix: python run.py symbols-suffix .r")
def test_db(self):
"""Test database connection."""
subprocess.run([sys.executable, "-m", "tests.infrastructure.test_db"])
def test_redis(self):
"""Test Redis connection."""
subprocess.run([sys.executable, "-m", "tests.infrastructure.test_redis"])
def test_mt5(self):
"""Test MT5 connection."""
try:
from tests.infrastructure.test_mt5 import test_mt5_connection
asyncio.run(test_mt5_connection())
except Exception as e:
print(f"Error running MT5 test: {e}")
sys.exit(1)
def test_tv(self):
"""Test TradingView service."""
try:
from tests.infrastructure.test_tv import run_test
success = asyncio.run(run_test())
if not success:
sys.exit(1)
except Exception as e:
print(f"Error running TV test: {e}")
sys.exit(1)
def test_all(self):
"""Run all infrastructure tests."""
try:
from tests.infrastructure.test_all import main
main() # This already handles asyncio.run internally
except Exception as e:
print(f"Error running tests: {e}")
sys.exit(1)
def clean_redis(self):
"""Clean Redis data."""
subprocess.run(["python", "src/scripts/clean_redis.py"])
def show_help(self):
"""Show help message."""
print("\nAvailable commands:")
print("-" * 50)
commands = {
"proxy": "Start the TradingView proxy server",
"worker": "Start the MT5 worker",
"update-reqs": "Update requirements.txt",
"symbols": "List all MT5 symbols",
"symbols-help": "Show symbol management commands",
"test-db": "Test database connection",
"test-redis": "Test Redis connection",
"test-mt5": "Test MT5 connection",
"test-tv": "Test TradingView service",
"test-all": "Run all infrastructure tests",
"clean-redis": "Clean Redis data",
"help": "Show this help message"
}
for cmd, desc in commands.items():
print(f"python run.py {cmd:<15} - {desc}")
def main():
"""Main entry point for the CLI."""
parser = argparse.ArgumentParser(description="TradingView Copier CLI")
parser.add_argument('command', nargs='?', default='help',
help="Command to execute")
parser.add_argument('args', nargs=argparse.REMAINDER,
help="Additional arguments for the command")
args = parser.parse_args()
runner = Runner()
# Command mapping
commands = {
'proxy': runner.run_proxy,
'worker': runner.run_worker,
'update-reqs': runner.update_requirements,
'symbols': runner.list_symbols,
'symbols-help': runner.manage_symbols,
'test-db': runner.test_db,
'test-redis': runner.test_redis,
'test-mt5': runner.test_mt5,
'test-tv': runner.test_tv,
'test-all': runner.test_all,
'clean-redis': runner.clean_redis,
'help': runner.show_help
}
if args.command in commands:
try:
commands[args.command]()
except KeyboardInterrupt:
print("\nOperation cancelled by user")
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
else:
print(f"Unknown command: {args.command}")
runner.show_help()
sys.exit(1)
if __name__ == "__main__":
main()