-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto-update.py
executable file
·107 lines (82 loc) · 3.04 KB
/
auto-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
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
# Adapted from https://github.com/flathub/org.vim.Vim/blob/master/auto-update.py
#
# See: https://discourse.lathub.org/t/tools-for-automatically-updating-flatpak-packaging/736/
import argparse
import os
import re
import subprocess
import textwrap
import ruamel.yaml
HERE = os.path.abspath(os.path.dirname(__file__))
CLONE = os.path.join(HERE, 'purr-data')
MANIFEST = os.path.join(HERE, 'net.purrdata.PurrData.yml')
APPDATA = os.path.join(HERE, 'net.purrdata.PurrData.metainfo.xml')
def run(args, **kwargs):
print('$', ' '.join(args))
return subprocess.run(args, check=True, **kwargs)
def dry_run(args, **kwargs):
print('would run $', ' '.join(args))
def run_and_read(args):
result = run(args, stdout=subprocess.PIPE)
return result.stdout.decode('ascii').strip()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--remote', default='origin')
parser.add_argument('--dry-run', action='store_true')
args = parser.parse_args()
yaml = ruamel.yaml.YAML()
with open(MANIFEST, 'r') as f:
manifest = yaml.load(f)
os.chdir(CLONE)
run(('git', 'pull'))
sorted_tags = run_and_read(('git', 'tag', '-l', '--sort=-creatordate'))
tag = sorted_tags.split('\n')[0]
sha = run_and_read(('git', 'show-ref', '--hash', tag))
date = run_and_read(('git', 'log', '-1', '--date=short',
'--pretty=format:%cd', tag))
os.chdir(HERE)
# Patch manifest
old_tag = None
source = None
pd_modules = ['purr-data', 'pd-externals', 'pd-abstractions', 'purr-data-integration']
for module in manifest['modules']:
if issubclass(type(module), dict) and module['name'] in pd_modules:
source = module['sources'][0]
old_tag = source['tag']
source['tag'] = tag
source['commit'] = sha
module['sources'][0] = source
with open(MANIFEST, 'w') as f:
yaml.dump(manifest, f)
# Patch appdata. Sorry, I can't bring myself to use an XML parser.
with open(APPDATA, 'r') as f:
xml = f.read()
xml = re.sub(r'<release version="(.+?)" date="(.+?)">',
'<release version="{}" date="{}">'.format(tag, date),
xml)
with open(APPDATA, 'w') as f:
f.write(xml)
try:
run(('git', 'diff-index', '--quiet', 'HEAD', '--'))
except subprocess.CalledProcessError:
pass
else:
print("Manifest is up-to-date")
return
branch = 'update-to-{}'.format(tag)
f = dry_run if args.dry_run else run
f(('git', 'checkout', '-b', branch))
f(('git', 'commit', '-am', 'Update to {}'.format(tag)))
f(('git', 'push', '-u', args.remote, branch))
f(('hub', 'pull-request', '--no-edit', '-m', textwrap.dedent('''
Update to {tag}
Upstream changes: {url}/compare/{old_tag}...{tag}
<i>(This pull request was automatically generated.)</i>
''').strip().format(
tag=tag,
old_tag=old_tag,
url=source['url'],
)))
if __name__ == '__main__':
main()