-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
289 lines (244 loc) · 10.3 KB
/
main.py
File metadata and controls
289 lines (244 loc) · 10.3 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python3
"""
RevivaX - Sistema de Recuperação de Arquivos
Ponto de entrada principal da aplicação
Uso:
python main.py # Inicia servidor web
python main.py --cli # Modo CLI (futuro)
python main.py --version # Mostra versão
"""
import sys
import socket
import argparse
import logging
import webbrowser
from pathlib import Path
from threading import Thread
try:
from waitress import serve
except ImportError:
serve = None
# Opcional: Tenta importar pywebview para experiência desktop nativa
try:
import webview
WEBVIEW_AVAILABLE = True
except ImportError:
WEBVIEW_AVAILABLE = False
def find_available_port(host: str, start_port: int) -> int:
"""Encontra uma porta disponível a partir da porta fornecida"""
port = start_port
max_attempts = 100
while port <= 65535 and (port - start_port) < max_attempts:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
return port
except OSError:
port += 1
raise RuntimeError(f"Nenhuma porta disponível encontrada a partir de {start_port}.")
# Configuração do path
BASE_DIR = Path(__file__).parent.absolute()
sys.path.insert(0, str(BASE_DIR))
# Imports
from web.app import app, init_managers
from core.runtime_paths import get_logs_dir, get_recovered_dir, get_runtime_root
# Configuração de logging
def setup_logging():
"""Configura logging do sistema"""
log_dir = get_logs_dir()
log_dir.mkdir(exist_ok=True)
log_file = log_dir / 'revivax.log'
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_file, encoding='utf-8'),
logging.StreamHandler(sys.stdout)
]
)
return logging.getLogger(__name__)
def print_banner():
"""Imprime banner do sistema"""
banner = """
╔══════════════════════════════════════════════════════════════╗
║ ║
║ ██████╗ ███████╗██╗ ██╗██╗██╗ ██╗ █████╗ ██╗ ██╗ ║
║ ██╔══██╗██╔════╝██║ ██║██║██║ ██║██╔══██╗╚██╗██╔╝ ║
║ ██████╔╝█████╗ ██║ ██║██║██║ ██║███████║ ╚███╔╝ ║
║ ██╔══██╗██╔══╝ ╚██╗ ██╔╝██║╚██╗ ██╔╝██╔══██║ ██╔██╗ ║
║ ██║ ██║███████╗ ╚████╔╝ ██║ ╚████╔╝ ██║ ██║██╔╝ ██╗ ║
║ ╚═╝ ╚═╝╚══════╝ ╚═══╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ║
║ ║
║ Sistema Avançado de Recuperação de Arquivos ║
║ ║
║ Versão: 1.0.0 Modo: Somente Leitura ║
║ ║
╚══════════════════════════════════════════════════════════════╝
"""
print(banner)
def check_requirements():
"""Verifica requisitos do sistema"""
logger = logging.getLogger(__name__)
# Verifica Python version
if sys.version_info < (3, 8):
logger.error("Python 3.8+ é necessário")
return False
# Verifica diretórios graváveis
for dir_path in (get_runtime_root(), get_logs_dir(), get_recovered_dir()):
dir_path.mkdir(parents=True, exist_ok=True)
logger.info("Requisitos verificados com sucesso")
return True
def open_browser(url: str, delay: float = 2.5):
"""Abre navegador após delay"""
import threading
import time
def _open():
time.sleep(delay)
try:
# Trailing slash auxilia navegadores a forçarem a atualização da URL bar (evita about:blank)
final_url = url if url.endswith('/') else url + '/'
webbrowser.open_new_tab(final_url)
except Exception as e:
logging.getLogger(__name__).warning(f"Não foi possível abrir navegador: {e}")
thread = threading.Thread(target=_open)
thread.daemon = True
thread.start()
def main():
"""Função principal"""
# Configura argumentos
parser = argparse.ArgumentParser(
description='RevivaX - Sistema de Recuperação de Arquivos',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Exemplos:
python main.py # Inicia servidor web na porta 5000
python main.py --port 8080 # Inicia na porta 8080
python main.py --no-browser # Não abre navegador automaticamente
"""
)
parser.add_argument(
'--version',
action='version',
version='%(prog)s 1.0.0'
)
# O serviço é restrito a localhost para não expor a interface web na rede.
# Este é o requisito interno SEC-02: o painel só deve ser acessível pela
# própria máquina, salvo quando houver um mecanismo externo controlado.
host = '127.0.0.1'
parser.add_argument(
'--port',
type=int,
default=5000,
help='Porta do servidor (padrão: 5000)'
)
parser.add_argument(
'--no-browser',
action='store_true',
help='Não abre navegador automaticamente'
)
parser.add_argument(
'--debug',
action='store_true',
help='Modo debug (não use em produção)'
)
parser.add_argument(
'--no-webview',
action='store_true',
help='Força a abertura no navegador padrão em vez do WebView nativo'
)
args = parser.parse_args()
# Setup
logger = setup_logging()
print_banner()
if not check_requirements():
sys.exit(1)
# Resolve porta disponível
try:
actual_port = find_available_port(host, args.port)
if actual_port != args.port:
logger.info(f"Porta {args.port} em uso. Utilizando porta disponível: {actual_port}")
except Exception as e:
logger.error(f"Erro ao checar porta: {e}")
sys.exit(1)
# Garante smartctl disponível (baixa se necessário) ANTES de iniciar o Flask
logger.info("Verificando disponibilidade do smartctl...")
try:
from core.smartctl_setup import ensure_smartctl
sc_path = ensure_smartctl()
if sc_path:
logger.info("smartctl pronto: %s", sc_path)
print(f" [SMART] smartctl disponível: {sc_path}")
else:
logger.warning("smartctl indisponível — SMART usará PowerShell nativo.")
print(" [SMART] smartctl não encontrado — usando PowerShell como fallback.")
except Exception as e:
logger.debug("Pré-verificação do smartctl falhou (não crítico): %s", e)
# Inicializa managers
logger.info("Inicializando RevivaX...")
try:
init_managers()
logger.info("Managers inicializados com sucesso")
except Exception as e:
logger.error("Erro ao inicializar: %s", e)
sys.exit(1)
# URL do servidor
url = f"http://{host}:{actual_port}"
print(f"\n{'='*60}")
print(f" Servidor iniciado em: {url}")
print(f" Modo: {'DEBUG' if args.debug else 'PRODUÇÃO'}")
print(f"{'='*60}\n")
print(" Pressione Ctrl+C para encerrar\n")
# Inicia servidor Flask
try:
# Decide como exibir a UI
if not args.no_browser:
if WEBVIEW_AVAILABLE and not args.no_webview:
# Inicia Flask numa thread separada
def run_flask():
app.run(
host=host,
port=actual_port,
debug=args.debug,
threaded=True,
use_reloader=False,
)
flask_thread = Thread(target=run_flask)
flask_thread.daemon = True
flask_thread.start()
# Inicia WebView nativo (bloqueante)
logger.info("Iniciando interface nativa com PyWebView...")
webview.create_window(
"RevivaX - Data Recovery",
url,
width=1200,
height=800,
min_size=(800, 600)
)
webview.start()
# Quando a janela fecha, a thread principal encerra
return
else:
# Comportamento antigo: abre navegador padrão
logger.info(f"O servidor está rodando em {url}")
open_browser(url, delay=2.5)
if not args.debug and serve:
logger.info("Iniciando servidor WSGI de Produção (Waitress)...")
serve(app, host=host, port=actual_port)
else:
app.run(host=host, port=actual_port, debug=args.debug, threaded=True, use_reloader=False)
else:
# Apenas servidor
logger.info(f"O servidor está rodando em {url}")
if not args.debug and serve:
logger.info("Iniciando servidor WSGI de Produção (Waitress)...")
serve(app, host=host, port=actual_port)
else:
app.run(host=host, port=actual_port, debug=args.debug, threaded=True, use_reloader=False)
except KeyboardInterrupt:
print("\n\n Encerrando RevivaX...")
logger.info("Servidor encerrado pelo usuário")
except Exception as e:
logger.error(f"Erro no servidor: {e}")
sys.exit(1)
if __name__ == '__main__':
main()