summaryrefslogtreecommitdiffstats
path: root/tools/update-appdata.py
blob: b2960ee20ff957f784f522b602f8c5c2f0dae75d (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
#!/usr/bin/env python3
#
# update-appdata.py - Update the <releases/> section of resources/freedesktop/org.wireshark.Wireshark.metainfo.xml.
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
'''Update the <release> tag in resources/freedesktop/org.wireshark.Wireshark.metainfo.xml

According to https://www.freedesktop.org/software/appstream/docs/chap-Metadata.html
the <releases/> tag in resources/freedesktop/org.wireshark.Wireshark.metainfo.xml should contain release
information sorted newest to oldest.

As part of our release process, when we create release tag x.y.z, we tag
the next commit x.y.z+1rc0, e.g.

v3.0.0      2019-02-28 release tag
v3.0.1rc0   2019-02-28 next commit after v3.0.0
v3.0.1      2019-04-08 release tag
v3.0.2rc0   2019-04-08 next commit after v3.0.1

Find a list of release versions based on our most recent rc0 tag and
update the <releases/> section of resources/freedesktop/org.wireshark.Wireshark.metainfo.xml accordingly.
Assume that the tag for the most recent release doesn't exist and use
today's date for it.
'''

from datetime import date
import io
import os.path
import re
import subprocess
import sys
import time

def main():
    this_dir = os.path.dirname(__file__)
    appdata_xml = os.path.join(this_dir, '..', 'resources', 'freedesktop', 'org.wireshark.Wireshark.metainfo.xml')

    try:
        cur_rc0 = subprocess.run(
            ['git', 'describe', '--match', 'v*rc0'],
            check=True,
            encoding='UTF-8',
            stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout
    except Exception:
        print('Unable to fetch most recent rc0.')
        raise

    try:
        ver_m = re.match('v(\d+\.\d+)\.(\d+)rc0.*', cur_rc0)
        maj_min = ver_m.group(1)
        next_micro = ver_m.group(2)
    except Exception:
        print('Unable to fetch major.minor version.')
        raise

    # https://www.freedesktop.org/software/appstream/docs/chap-Metadata.html#tag-releases
    release_tag_fmt = '''\
        <release version="{0}.{1}" date="{2}">
            <url>https://www.wireshark.org/docs/relnotes/wireshark-{0}.{1}.html</url>
        </release>
'''
    release_tag_l = [
        f'        <!-- Automatically generated by tools/{os.path.basename(__file__)} -->\n',
        release_tag_fmt.format(maj_min, next_micro, date.fromtimestamp(time.time()).isoformat())
    ]
    for micro in range(int(next_micro) - 1, -1, -1):
        try:
            tag_date = subprocess.run(
                ['git', 'log', '-1', '--format=%cd', '--date=format:%F', 'v{}.{}'.format(maj_min, micro)],
                check=True,
                encoding='UTF-8',
                stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.strip()
            release_tag_l.append(release_tag_fmt.format(maj_min, micro, tag_date))
        except Exception:
            print('Unable to fetch release tag')
            raise

    ax_lines = []
    with io.open(appdata_xml, 'r', encoding='UTF-8') as ax_fd:
        in_releases = False
        for line in ax_fd:
            if '</releases>' in line:
                in_releases = False
            if in_releases:
                continue
            ax_lines.append(line)
            if '<releases>' in line:
                in_releases = True
                ax_lines.extend(release_tag_l)

    with io.open(appdata_xml, 'w', encoding='UTF-8') as ax_fd:
        ax_fd.write(''.join(ax_lines))

if __name__ == '__main__':
    main()