-
Notifications
You must be signed in to change notification settings - Fork 4
/
update.py
executable file
·94 lines (73 loc) · 3.78 KB
/
update.py
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
#!/usr/bin/env python3
# This script checks if a newer version of the launcher is available, and if so, updates all relevant files.
#
# Dependencies that aren't in the standard library:
# - requests
# - lxml
# - ruamel.yaml
#
# This script was tested using Python 3.10.4; older versions may not work.
from functools import reduce
from hashlib import sha256
from typing import OrderedDict
import requests
from lxml import etree
from ruamel.yaml import YAML
import re
METADATA_FILE = 'com.gitlab.KRypt0n_.an-anime-game-launcher.metainfo.xml'
MANIFEST_FILE = 'com.gitlab.KRypt0n_.an-anime-game-launcher.yml'
flatpakMetadata = etree.parse(METADATA_FILE)
flatpakReleases = flatpakMetadata.xpath('/component/releases/release')
currentFlatpakRelease = reduce(lambda x, y: x if x.attrib['version'] > y.attrib['version'] else y, flatpakReleases)
print(f'Current flatpak release: {currentFlatpakRelease.attrib["version"]}')
releasesResponse = requests.get('https://gitlab.com/an-anime-team/an-anime-game-launcher/-/releases.json')
releasesResponse.json()
gitlabReleases: list[dict] = releasesResponse.json()
latestRelease = reduce(lambda x, y: x if x['released_at'] > y['released_at'] else y, gitlabReleases)
latestReleaseDate = latestRelease['released_at'].split('T')[0]
print(f'Latest release on GitLab: {latestRelease["tag"]}, released on {latestReleaseDate}')
if(latestRelease['tag'] > currentFlatpakRelease.attrib['version']):
print('New version available on GitLab!')
newReleaseElement = etree.Element('release', version=latestRelease['tag'], date=latestReleaseDate)
descriptionElement = etree.SubElement(newReleaseElement, 'description')
descriptionMarkdown: str = latestRelease['description']
changesMarkdown = descriptionMarkdown.split("## What's changed?\n\n")[1].split("\n\n")[0]
changesListItems = changesMarkdown.split('- ')
for item in changesListItems:
text = item.strip()
if text == '':
continue
element = etree.SubElement(descriptionElement, 'p')
element.text = text
print('Generated release element:')
print(str(etree.tostring(newReleaseElement, pretty_print=True), 'utf-8'))
flatpakMetadata.xpath('/component/releases')[0].insert(0, newReleaseElement)
yaml = YAML()
with open(MANIFEST_FILE, 'r+') as manifestFile:
data: OrderedDict = yaml.load(manifestFile)
for module in data['modules']:
if module['name'] == 'an-anime-game-launcher':
for source in module['sources']:
if 'url' in source:
aaglSource: OrderedDict = source
break
break
appimageRegex = re.compile('/uploads/[\da-f]+/An_Anime_Game_Launcher.AppImage', re.MULTILINE)
appimageUrl = appimageRegex.search(descriptionMarkdown).group(0)
appimageUrl = f'https://gitlab.com/an-anime-team/an-anime-game-launcher{appimageUrl}'
print(f'Calculating SHA256...', end='', flush=True)
appimageResponse = requests.get(appimageUrl)
appimageSha256 = sha256(appimageResponse.content).hexdigest()
print(f'\rDownload URL: {appimageUrl}')
print(f'SHA256: {appimageSha256}')
aaglSource['url'] = appimageUrl
aaglSource['sha256'] = appimageSha256
manifestFile.seek(0)
manifestFile.truncate()
# Update metadata and manifest
etree.indent(flatpakMetadata, space=' ')
flatpakMetadata.write(METADATA_FILE, xml_declaration=True, encoding='utf-8')
yaml.dump(data, manifestFile)
print(f'\nIf something looks wrong, run `git checkout {METADATA_FILE} {MANIFEST_FILE}` to revert the changes, slap krypton with a fish, and fix this script; he probably changed the format of the description.')
else:
print('Flatpak is already up to date.')