1#!/usr/bin/env python3
2#
3# update-appdata.py - Update the <releases/> section of org.wireshark.Wireshark.metainfo.xml.
4#
5# Wireshark - Network traffic analyzer
6# By Gerald Combs <gerald@wireshark.org>
7# Copyright 1998 Gerald Combs
8#
9# SPDX-License-Identifier: GPL-2.0-or-later
10'''Update the <release> tag in org.wireshark.Wireshark.metainfo.xml
11
12According to https://www.freedesktop.org/software/appstream/docs/chap-Metadata.html
13the <releases/> tag in org.wireshark.Wireshark.metainfo.xml should contain release
14information sorted newest to oldest.
15
16As part of our release process, when we create release tag x.y.z, we tag
17the next commit x.y.z+1rc0, e.g.
18
19v3.0.0      2019-02-28 release tag
20v3.0.1rc0   2019-02-28 next commit after v3.0.0
21v3.0.1      2019-04-08 release tag
22v3.0.2rc0   2019-04-08 next commit after v3.0.1
23
24Find a list of release versions based on our most recent rc0 tag and
25update the <releases/> section of org.wireshark.Wireshark.metainfo.xml accordingly.
26Assume that the tag for the most recent release doesn't exist and use
27today's date for it.
28'''
29
30from datetime import date
31import io
32import os.path
33import re
34import subprocess
35import sys
36import time
37
38def main():
39    if sys.version_info[0] < 3:
40        print("This requires Python 3")
41        sys.exit(2)
42
43    this_dir = os.path.dirname(__file__)
44    appdata_xml = os.path.join(this_dir, '..', 'org.wireshark.Wireshark.metainfo.xml')
45
46    try:
47        tag_cp = subprocess.run(
48            ['git', 'tag', '-l', 'wireshark-*'],
49            encoding='UTF-8',
50            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
51        if not 'wireshark-' in tag_cp.stdout:
52            print('Wireshark release tag not found')
53            sys.exit(1)
54    except Exception:
55        print('`git tag` returned {}:'.format(tag_cp.returncode))
56        raise
57
58    try:
59        cur_rc0 = subprocess.run(
60            ['git', 'describe', '--match', 'v*rc0'],
61            check=True,
62            encoding='UTF-8',
63            stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout
64    except Exception:
65        print('Unable to fetch most recent rc0.')
66        raise
67
68    try:
69        ver_m = re.match('v(\d+\.\d+)\.(\d+)rc0.*', cur_rc0)
70        maj_min = ver_m.group(1)
71        next_micro = ver_m.group(2)
72    except Exception:
73        print('Unable to fetch major.minor version.')
74        raise
75
76    # https://www.freedesktop.org/software/appstream/docs/chap-Metadata.html#tag-releases
77    release_tag_fmt = '''\
78        <release version="{0}.{1}" date="{2}">
79            <url>https://www.wireshark.org/docs/relnotes/wireshark-{0}.{1}.html</url>
80        </release>
81'''
82    release_tag_l = [
83        release_tag_fmt.format(maj_min, next_micro, date.fromtimestamp(time.time()).isoformat())
84    ]
85    for micro in range(int(next_micro) - 1, -1, -1):
86        try:
87            tag_date = subprocess.run(
88                ['git', 'log', '-1', '--format=%cd', '--date=format:%F', 'v{}.{}'.format(maj_min, micro)],
89                check=True,
90                encoding='UTF-8',
91                stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.strip()
92            release_tag_l.append(release_tag_fmt.format(maj_min, micro, tag_date))
93        except Exception:
94            print('Unable to fetch release tag')
95            raise
96
97    ax_lines = []
98    with io.open(appdata_xml, 'r', encoding='UTF-8') as ax_fd:
99        in_releases = False
100        for line in ax_fd:
101            if '</releases>' in line:
102                in_releases = False
103            if in_releases:
104                continue
105            ax_lines.append(line)
106            if '<releases>' in line:
107                in_releases = True
108                ax_lines.extend(release_tag_l)
109
110    with io.open(appdata_xml, 'w', encoding='UTF-8') as ax_fd:
111        ax_fd.write(''.join(ax_lines))
112
113if __name__ == '__main__':
114    main()
115