108 lines
3.0 KiB
Python
108 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Program do przetwarzania wielu rejestracji COMTRADE
|
|
Przeszukuje rekurencyjnie podkatalogi i uruchamia tester dla kazdej rejestracji
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import shutil
|
|
import subprocess
|
|
|
|
def find_comtrade_files(root_dir):
|
|
"""Znajduje wszystkie pliki .CFG w podkatalogach"""
|
|
cfg_files = []
|
|
|
|
for root, dirs, files in os.walk(root_dir):
|
|
for file in files:
|
|
if file.upper().endswith('.CFG'):
|
|
cfg_path = os.path.join(root, file)
|
|
dat_file = cfg_path[:-4] + '.DAT'
|
|
if os.path.exists(dat_file):
|
|
cfg_files.append(cfg_path)
|
|
|
|
return cfg_files
|
|
|
|
def process_recording(cfg_file, tester_script):
|
|
"""Przetwarza pojedyncza rejestracje"""
|
|
dir_name = os.path.dirname(cfg_file)
|
|
file_name = os.path.basename(cfg_file)[:-4]
|
|
|
|
print(f"\n{'='*60}")
|
|
print(f"Przetwarzanie: {file_name}")
|
|
print(f"Katalog: {dir_name}")
|
|
print('='*60)
|
|
|
|
original_dir = os.getcwd()
|
|
os.chdir(dir_name)
|
|
|
|
try:
|
|
result = subprocess.run(
|
|
[sys.executable, tester_script, file_name],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=120
|
|
)
|
|
|
|
if result.returncode == 0:
|
|
print(f"OK - wynik zapisany w {dir_name}")
|
|
|
|
# Przenies wynik PNG
|
|
if os.path.exists('test_result.png'):
|
|
output_name = f'{file_name}_result.png'
|
|
if os.path.exists(output_name):
|
|
os.remove(output_name)
|
|
shutil.move('test_result.png', output_name)
|
|
print(f"Zapisano: {output_name}")
|
|
|
|
# Przenies rezultat.md
|
|
if os.path.exists('rezultat.md'):
|
|
output_md = f'{file_name}_rezultat.md'
|
|
if os.path.exists(output_md):
|
|
os.remove(output_md)
|
|
shutil.move('rezultat.md', output_md)
|
|
print(f"Zapisano: {output_md}")
|
|
else:
|
|
print(f"BLED przetwarzania:")
|
|
print(result.stderr)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
print(f"BLED - przekroczony czas wykonania")
|
|
except Exception as e:
|
|
print(f"BLED - {str(e)}")
|
|
finally:
|
|
os.chdir(original_dir)
|
|
|
|
def main():
|
|
if len(sys.argv) > 1:
|
|
root_dir = sys.argv[1]
|
|
else:
|
|
root_dir = os.getcwd()
|
|
|
|
tester_script = os.path.join(os.getcwd(), 'tester.py')
|
|
|
|
if not os.path.exists(tester_script):
|
|
print(f"BLED: Nie znaleziono {tester_script}")
|
|
sys.exit(1)
|
|
|
|
print(f"Szukanie rejestracji COMTRADE w: {root_dir}")
|
|
|
|
cfg_files = find_comtrade_files(root_dir)
|
|
|
|
if not cfg_files:
|
|
print("NIE ZNALEZIONO zadnych rejestracji COMTRADE")
|
|
sys.exit(0)
|
|
|
|
print(f"ZNALEZIONO {len(cfg_files)} rejestracji")
|
|
|
|
for i, cfg_file in enumerate(cfg_files, 1):
|
|
print(f"\n[{i}/{len(cfg_files)}]")
|
|
process_recording(cfg_file, tester_script)
|
|
|
|
print(f"\n{'='*60}")
|
|
print("ZAKONCZONO przetwarzanie wszystkich rejestracji")
|
|
print('='*60)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|