-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_and_upload.py
77 lines (56 loc) · 2.21 KB
/
pull_and_upload.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
import ftplib
import os
from dotenv import load_dotenv
from loguru import logger
from pathlib import Path
from convert import main as convert_main
from files import generate_upload_map, get_relative_remote_path, get_relative_local_path
load_dotenv()
FTP_SERVER_HOST = os.getenv("FTP_SERVER_HOST")
FTP_SERVER_USER = os.getenv("FTP_SERVER_USER")
FTP_SERVER_PASS = os.getenv("FTP_SERVER_PASS")
FTP_SERVER_MEDIAWIKI_DIR = os.getenv("FTP_SERVER_MEDIAWIKI_DIR")
LOCAL_DIR = os.getenv("LOCAL_DIR")
TARGET_LANGUAGES = (os.getenv("TARGET_LANGUAGES") or "").split(",")
if (
not FTP_SERVER_HOST
or not FTP_SERVER_USER
or not FTP_SERVER_PASS
or not FTP_SERVER_MEDIAWIKI_DIR
or not LOCAL_DIR
or not TARGET_LANGUAGES
):
raise ValueError("Missing environment variables")
REMOTE_I18N_DIR = Path(FTP_SERVER_MEDIAWIKI_DIR, "languages", "i18n")
LOCAL_EXTENSIONS_DIR = Path(LOCAL_DIR, "extensions")
REMOTE_EXTENSIONS_DIR = Path(FTP_SERVER_MEDIAWIKI_DIR, "extensions")
def upload_file(ftp: ftplib.FTP, local_file: Path, remote_file: Path) -> None:
"""
Uploads a single file to the FTP server.
:param ftp: An active FTP connection object.
:param local_file: Path to the local file to upload.
:param remote_file: The target path on the FTP server.
"""
remote_dir = Path(remote_file).parent
ftp.cwd("/")
ftp.cwd(remote_dir.as_posix())
already_exists = remote_file.name in ftp.nlst()
relative_local_path = get_relative_local_path(local_file)
relative_remote_path = get_relative_remote_path(remote_file)
with open(local_file, "rb") as file:
logger.info(f"Uploading '{relative_local_path}' to '{relative_remote_path}'...")
ftp.storbinary(f"STOR {Path(remote_file).name}", file)
logger.info(f"{'Overwritten' if already_exists else 'Created' } '{remote_file}'")
def main():
# git pull
os.system("git pull")
# convert
convert_main()
# get files
files = generate_upload_map(LOCAL_DIR, FTP_SERVER_MEDIAWIKI_DIR)
with ftplib.FTP(FTP_SERVER_HOST) as ftp:
ftp.login(FTP_SERVER_USER, FTP_SERVER_PASS)
for local_file, remote_file in files.items():
upload_file(ftp, local_file, remote_file)
if __name__ == "__main__":
main()