-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp_scanner.py
More file actions
119 lines (101 loc) · 4.1 KB
/
ftp_scanner.py
File metadata and controls
119 lines (101 loc) · 4.1 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
from __future__ import annotations
from io import BytesIO
from ftplib import FTP, all_errors
from pathlib import Path, PurePosixPath
class FTPScanner:
def __init__(self, host, port, username, password, root_path, *, passive=True, timeout=10):
self.root_path = root_path.rstrip("/")
self.ftp = FTP()
self.ftp.connect(host, port, timeout=timeout)
self.ftp.login(username, password)
self.ftp.set_pasv(passive)
def scan(self, date_str):
target_dir = f"{self.root_path}/{date_str}"
try:
self.ftp.cwd(target_dir)
except all_errors:
print(f"[SKIP] 날짜 폴더 없음: {target_dir}")
return []
try:
entries = list(self.ftp.mlsd(target_dir))
except all_errors as exc:
print(f"[WARN] 디렉토리 조회 실패: {target_dir} / {exc}")
return []
return [
f"{target_dir}/{name}"
for name, facts in entries
if facts.get("type") == "file"
]
def _download_with_validation(self, remote_path, write_chunk):
expected_size = self.get_file_size(remote_path)
downloaded_size = 0
def tracked_write(chunk):
nonlocal downloaded_size
downloaded_size += len(chunk)
write_chunk(chunk)
self.ftp.retrbinary(f"RETR {remote_path}", tracked_write)
if expected_size is not None and downloaded_size != expected_size:
raise IOError(
f"다운로드 크기 불일치: remote={expected_size}, downloaded={downloaded_size}, path={remote_path}"
)
def download_file(self, remote_path, local_path):
local_path = Path(local_path)
local_path.parent.mkdir(parents=True, exist_ok=True)
try:
with open(local_path, "wb") as file_obj:
self._download_with_validation(remote_path, file_obj.write)
except Exception:
try:
local_path.unlink()
except FileNotFoundError:
pass
raise
def download_bytes(self, remote_path):
buffer = BytesIO()
self._download_with_validation(remote_path, buffer.write)
return buffer.getvalue()
def read_text_file(self, remote_path, *, encoding="utf-8"):
try:
return self.download_bytes(remote_path).decode(encoding)
except all_errors:
return ""
def _ensure_remote_dir(self, remote_dir):
current = PurePosixPath("/")
for part in PurePosixPath(remote_dir).parts:
if part in {"", "/"}:
continue
current = current / part
try:
self.ftp.mkd(current.as_posix())
except all_errors:
pass
def upload_file(self, local_path, remote_path):
local_path = Path(local_path)
self._ensure_remote_dir(PurePosixPath(remote_path).parent.as_posix())
with local_path.open("rb") as file_obj:
self.ftp.storbinary(f"STOR {remote_path}", file_obj)
expected_size = local_path.stat().st_size
remote_size = self.get_file_size(remote_path)
if remote_size is None or remote_size != expected_size:
raise IOError(
f"업로드 크기 불일치: local={expected_size}, remote={remote_size}, path={remote_path}"
)
def get_file_size(self, remote_path):
try:
size = self.ftp.size(remote_path)
except all_errors:
return None
return int(size) if size is not None else None
def file_exists(self, remote_path):
return self.get_file_size(remote_path) is not None
def delete_file(self, remote_path):
self.ftp.delete(remote_path)
def append_text_line(self, remote_path, line, *, encoding="utf-8"):
self._ensure_remote_dir(PurePosixPath(remote_path).parent.as_posix())
payload = BytesIO((line.rstrip("\n") + "\n").encode(encoding))
self.ftp.storbinary(f"APPE {remote_path}", payload)
def close(self):
try:
self.ftp.quit()
except Exception:
pass