summarylogtreecommitdiffstats
path: root/gen_pkgbuild.py
blob: e7ebfaf36fda67edbfb2240285fae9f5814bed71 (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
#!/usr/bin/env python
import io
import logging
import re
import sys

from lxml import etree
import requests
import tornado.template


logger = logging.getLogger(__name__)


def main(pkgver):
    all_languages = {}

    mozilla_translations_page = requests.get(
        'https://pontoon.mozilla.org/projects/thunderbird/ajax/',
        headers={'X-Requested-With': 'XMLHttpRequest'},
    ).text

    parser = etree.HTMLParser()
    tree = etree.parse(io.StringIO(mozilla_translations_page), parser)
    for lang_td in tree.findall('.//td[@class="name"]'):
        lang_iso639 = lang_td.attrib['data-slug']
        lang_name = lang_td.find('.//a').text
        all_languages[lang_iso639] = lang_name
    all_languages['en-US'] = 'English'

    sha256sums_data = requests.get(
        f'https://ftp.mozilla.org/pub/thunderbird/releases/{pkgver}/SHA256SUMS'
    ).text

    sha256sums = []
    available_languages = []
    for line in sha256sums_data.strip().split('\n'):
        sha256sum, filename = line.split(maxsplit=1)
        if not filename.startswith('linux-x86_64/xpi/'):
            continue
        sha256sums.append(sha256sum.strip("b'").strip("'"))
        available_languages.append(
            filename[len('linux-x86_64/xpi/'):][:-len('.xpi')])
    available_languages = sorted(available_languages)

    logger.info('Collected languages: %s', available_languages)

    languages = {}
    for lang_iso639 in available_languages:
        languages[lang_iso639] = all_languages[lang_iso639]
    loader = tornado.template.Loader('.')
    content = loader.load('PKGBUILD.tmpl').generate(
        pkgver=pkgver,
        min_pkgver=re.sub(r'b\d', 'b0', pkgver),
        languages=languages,
        sha256sums=sha256sums,
    )

    with open('PKGBUILD', 'wb') as f:
        f.write(content)


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main(sys.argv[1])