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
|
from typing import Any
from typing import Optional
import functools
import os
import sys
from datetime import date
from ftplib import FTP_TLS
from pathlib import Path
from rich.console import Console
FTP_URL = 'panoramix.gajim.org'
FTP_USER = os.environ['FTP_USER']
FTP_PASS = os.environ['FTP_PASS']
WINDOWS_NIGHTLY_FOLDER = 'downloads/snap/win'
LINUX_NIGHTLY_FOLDER = 'downloads/snap'
RELEASE_FOLDER_BASE = 'downloads'
console = Console()
def ftp_connection(func: Any) -> Any:
@functools.wraps(func)
def func_wrapper(filedir: Path) -> None:
ftp = FTP_TLS(FTP_URL, FTP_USER, FTP_PASS)
console.print('Successfully connected to', FTP_URL)
func(ftp, filedir)
ftp.quit()
console.print('Quit')
return
return func_wrapper
def get_release_folder_from_tag(tag: str) -> str:
numbers = tag.split('.')
return '.'.join(numbers[:2])
def get_gajim_tag() -> str:
tag = os.environ.get('CI_COMMIT_TAG')
if tag is None:
exit('No tag found')
return tag
def find_linux_tarball(filedir: Path) -> Path:
files = list(filedir.glob('gajim-*.tar.gz'))
if len(files) != 1:
exit('Unknown files found')
return files[0]
def get_dir_list(ftp: FTP_TLS) -> list[str]:
return [x[0] for x in ftp.mlsd()]
def create_release_folder(ftp: FTP_TLS, tag: str) -> None:
ftp.cwd(RELEASE_FOLDER_BASE)
folder = get_release_folder_from_tag(tag)
dir_list = get_dir_list(ftp)
if folder not in dir_list:
ftp.mkd(folder)
ftp.cwd(folder)
def upload_all_from_dir(ftp: FTP_TLS, filedir: Path) -> None:
for filepath in filedir.iterdir():
upload_file(ftp, filepath)
def upload_file(ftp: FTP_TLS,
filepath: Path,
name: Optional[str] = None) -> None:
if name is None:
name = filepath.name
console.print('Upload file', filepath.name, 'as', name)
with open(filepath, 'rb') as f:
ftp.storbinary('STOR ' + name, f)
def get_deploy_method() -> str:
deploy_type = os.environ['DEPLOY_TYPE']
is_nightly = bool(os.environ.get('GAJIM_NIGHTLY_BUILD'))
if is_nightly:
return f'deploy_{deploy_type}_nightly'
return f'deploy_{deploy_type}_release'
@ftp_connection
def deploy_windows_nightly(ftp: FTP_TLS, filedir: Path) -> None:
ftp.cwd(WINDOWS_NIGHTLY_FOLDER)
upload_all_from_dir(ftp, filedir)
@ftp_connection
def deploy_windows_release(ftp: FTP_TLS, filedir: Path) -> None:
tag = get_gajim_tag()
create_release_folder(ftp, tag)
upload_all_from_dir(ftp, filedir)
@ftp_connection
def deploy_linux_nightly(ftp: FTP_TLS, filedir: Path) -> None:
ftp.cwd(LINUX_NIGHTLY_FOLDER)
filepath = find_linux_tarball(filedir)
filename = f'gajim-{date.today().isoformat()}.tar.gz'
upload_file(ftp, filepath, name=filename)
@ftp_connection
def deploy_linux_release(ftp: FTP_TLS, file: Path) -> None:
tag = get_gajim_tag()
create_release_folder(ftp, tag)
filepath = find_linux_tarball(filedir)
filename = f'gajim-{tag}.tar.gz'
upload_file(ftp, filepath, name=filename)
if __name__ == '__main__':
filedir = Path(sys.argv[1])
current_module = sys.modules[__name__]
method = getattr(current_module, get_deploy_method())
method(filedir)
|