Welcome to mirror list, hosted at ThFree Co, Russian Federation.

deploy.py « .ci - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c4f3e96cd1c2c194f60314b6c21a650519fd7b4 (plain)
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
#!/usr/bin/env python3

from typing import Any
from typing import cast
from typing import Optional

import functools
import json
import logging
import os
import sys
from datetime import date
from ftplib import error_perm
from ftplib import FTP_TLS
from pathlib import Path

import requests

FTP_URL = 'panoramix.gajim.org'
FTP_USER = os.environ['FTP_USER']
FTP_PASS = os.environ['FTP_PASS']

API_KEY = os.environ['APPVEYOR_API_KEY']
HEADERS = {'Authorization': f'Bearer {API_KEY}'}

WINDOWS_NIGHTLY_FOLDER = 'downloads/snap/win'
LINUX_NIGHTLY_FOLDER = 'downloads/snap'

RELEASE_FOLDER_BASE = 'downloads'


logging.basicConfig(level='INFO', format='%(levelname)s: %(message)s')
log = logging.getLogger()


def ftp_connection(func: Any) -> Any:
    @functools.wraps(func)
    def func_wrapper(filedir: Path) -> None:
        ftp = FTP_TLS(FTP_URL, FTP_USER, FTP_PASS)
        log.info('Successfully connected to %s', FTP_URL)
        func(ftp, filedir)
        ftp.quit()
        log.info('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:
        sys.exit('No tag found')
    return tag


def find_linux_tarball(filedir: Path) -> Path:
    files = list(filedir.glob('gajim-*.tar.gz'))
    if len(files) != 1:
        sys.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)
    try:
        ftp.mkd(folder)
    except error_perm:
        pass
    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

    log.info('Upload file %s as %s', filepath.name, name)
    with open(filepath, 'rb') as f:
        ftp.storbinary('STOR ' + name, f)


def download_artifacts(path: Path) -> None:
    build_results = list(path.glob('*.json'))
    if not build_results:
        sys.exit('No build build_results found')

    responses = [json.loads(response.read_text()) for response in build_results]

    for response in responses:
        for artifact in response:
            filename = cast(str, artifact['fileName'])
            file_url = artifact['fileUrl']

            log.info('Download %s', filename)

            req = requests.get(file_url, headers=HEADERS)
            req.raise_for_status()
            with open(path / filename, 'wb') as file:
                file.write(req.content)

    for result in build_results:
        result.unlink()


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)
    download_artifacts(filedir)
    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)
    download_artifacts(filedir)
    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)