-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_runner.py
More file actions
171 lines (151 loc) · 6.96 KB
/
batch_runner.py
File metadata and controls
171 lines (151 loc) · 6.96 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
from __future__ import annotations
from datetime import datetime
from pathlib import Path, PurePosixPath
from ftp_scanner import FTPScanner
from rubi_processor import RubiProcessor
from rupi_processor import RupiProcessor
class BatchRunner:
def __init__(
self,
input_date,
parser_name,
*,
client_host,
client_port,
client_username,
client_password,
client_root_path,
server_host,
server_port,
server_username,
server_password,
server_root_path,
db_path,
work_dir="/tmp/ftp_work",
scale_percent=50,
passive=True,
):
self.input_date = input_date
self.date_str = self._normalize_date(input_date)
self.parser_name = self._normalize_parser(parser_name)
self.client_root_path = client_root_path.rstrip("/")
self.server_root_path = server_root_path.rstrip("/")
self.work_dir = Path(work_dir)
self.work_dir.mkdir(parents=True, exist_ok=True)
self.client_scanner = FTPScanner(
host=client_host,
port=client_port,
username=client_username,
password=client_password,
root_path=self.client_root_path,
passive=passive,
)
self.server_scanner = None
if self.parser_name == "rupi":
self.server_scanner = FTPScanner(
host=server_host,
port=server_port,
username=server_username,
password=server_password,
root_path=self.server_root_path,
passive=passive,
)
self.rubi_processor = RubiProcessor(db_path=db_path)
self.rupi_processor = RupiProcessor(scale_percent=scale_percent)
def _normalize_date(self, input_date):
try:
return datetime.strptime(input_date, "%Y-%m-%d").strftime("%Y%m%d")
except ValueError as exc:
raise ValueError(f"날짜 형식 오류: {input_date} (YYYY-MM-DD 필요)") from exc
def _normalize_parser(self, parser_name):
normalized = parser_name.lower().strip()
if normalized == "rubi":
return "rubi"
if normalized in {"rupi", "rubp"}:
return "rupi"
raise ValueError(f"지원하지 않는 parser_name: {parser_name}")
def build_relative_path(self, remote_path, root_path):
remote = PurePosixPath(remote_path)
root = PurePosixPath(root_path)
try:
return remote.relative_to(root).as_posix()
except ValueError:
return remote.as_posix().lstrip("/")
def build_local_path(self, remote_path):
relative = PurePosixPath(self.build_relative_path(remote_path, self.client_root_path))
return self.work_dir.joinpath(*relative.parts)
def build_server_remote_path(self, client_remote_path):
relative = PurePosixPath(self.build_relative_path(client_remote_path, self.client_root_path))
return (PurePosixPath(self.server_root_path) / relative).with_suffix(".png").as_posix()
def download_local_file(self, remote_file, local_path, idx, total):
print(f"[{idx}/{total}] CLIENT DOWNLOAD: {remote_file}")
self.client_scanner.download_file(remote_file, local_path)
def ensure_local_rupi_source_file(self, remote_file, local_path, idx, total):
if local_path.exists():
print(f"[{idx}/{total}] CLIENT DOWNLOAD SKIP (LOCAL TIF EXISTS): {local_path}")
return
self.download_local_file(remote_file, local_path, idx, total)
def upload_rupi_output(self, output_path, remote_output_path, idx, total):
print(f"[{idx}/{total}] SERVER UPLOAD: {remote_output_path}")
self.server_scanner.upload_file(output_path, remote_output_path)
def process_file(self, local_path, remote_file):
if self.parser_name == "rubi":
self.rubi_processor.process(local_path, source_file=remote_file)
return True, None
return True, self.rupi_processor.process(local_path)
def run(self):
processed = 0
skipped = 0
errors = 0
try:
remote_files = self.client_scanner.scan(self.date_str)
total = len(remote_files)
print(
f"[INFO] parser={self.parser_name}, input_date={self.input_date}, "
f"ftp_date={self.date_str}, client_total={total}, target_total={total}"
)
for idx, remote_file in enumerate(remote_files, start=1):
local_path = self.build_local_path(remote_file)
output_path = None
try:
if self.parser_name == "rupi":
output_path = self.rupi_processor.build_output_path(local_path)
remote_output_path = self.build_server_remote_path(remote_file)
if self.server_scanner.file_exists(remote_output_path):
print(f"[{idx}/{total}] PROCESS SKIP (REMOTE PNG EXISTS): {remote_output_path}")
skipped += 1
continue
if output_path.exists():
print(f"[{idx}/{total}] PROCESS SKIP (LOCAL PNG REUSE): {output_path}")
self.upload_rupi_output(output_path, remote_output_path, idx, total)
else:
self.ensure_local_rupi_source_file(remote_file, local_path, idx, total)
print(f"[{idx}/{total}] PROCESS")
success, output_path = self.process_file(local_path, remote_file)
if not success:
skipped += 1
continue
self.upload_rupi_output(output_path, remote_output_path, idx, total)
local_path.unlink(missing_ok=True)
processed += 1
continue
self.download_local_file(remote_file, local_path, idx, total)
print(f"[{idx}/{total}] PROCESS")
success, _ = self.process_file(local_path, remote_file)
if not success:
skipped += 1
continue
print(f"[{idx}/{total}] CLIENT DELETE: {remote_file}")
self.client_scanner.delete_file(remote_file)
local_path.unlink(missing_ok=True)
processed += 1
except Exception as exc:
errors += 1
print(f"[ERROR] {remote_file} / {exc}")
if self.parser_name == "rubi" and local_path.exists():
local_path.unlink()
finally:
self.client_scanner.close()
if self.server_scanner is not None:
self.server_scanner.close()
print(f"[SUMMARY] processed={processed}, skipped={skipped}, errors={errors}")