-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsync-releases-with-github.py
executable file
·111 lines (97 loc) · 3.69 KB
/
sync-releases-with-github.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
108
109
110
111
#!/usr/bin/env python3
import os
import github3
from pprint import pprint
import tempfile
import subprocess
def parse_lektor(lr):
d = dict()
for a in lr.strip().split('---\n'):
lns = a.strip().split('\n')
if len(lns) == 1:
lnsl = lns[0].split(': ')
key = lnsl[0]
value = ': '.join(lnsl[1:]) if len(lnsl) > 1 else ''
else:
key = lns[0][:-1]
value = '\n'.join(lns[2:])
d[key.strip()] = value.strip()
return d
def new_release(tag):
global repo
dn = 'content/releases/getdns-' + tag[1:].replace('.', '-')
with open(dn + '/contents.lr') as f:
contents = parse_lektor(f.read())
version = contents['version']
dversion = contents['version'].replace('.', '-')
sha256 = contents['sha256']
announcement = """ | <div align="left">Please do not use the github generated Source code (zip) and (tar.gz) files, but our own tarball instead:</div>
------- | ------------------------
tarball | <https://getdnsapi.net/dist/getdns-%s.tar.gz>
pgp sig | <https://getdnsapi.net/dist/getdns-%s.tar.gz.asc>
sha256 | %s
""" % ( version, version, sha256.strip() )
if 'image' in contents and contents['image'].strip():
img_parts = contents['image'].split('.')
image180 = '.'.join(img_parts[:-1]) + '@180' + '.' + img_parts[-1]
announcement += '\n<img align="right" ' \
+ 'src="https://getdnsapi.net/releases/getdns-%s/%s">\n\n' \
% (dversion, image180)
with tempfile.NamedTemporaryFile(mode = 'w') as f:
f.write(contents['announcement']
.replace(']: /', ']: https://getdnsapi.net/')
.replace('](/' , '](https://getdnsapi.net/')
.replace('\n * ', '\n - ')
)
tmpfn = f.name
f.flush()
f.seek(0)
pandoc_out = subprocess.check_output(['pandoc', '-f', 'markdown', '-t', 'markdown', '--wrap', 'preserve'], stdin=f)
pandoc_out = pandoc_out.replace(b' > ', b'\n > ')
announcement += pandoc_out.decode('utf-8')
announcement += "\n### ChangeLog\n\n```\n%s\n```\n" % contents['changelog']
if 'stubbychangelog' in contents and contents['stubbychangelog'].strip():
announcement += "\n### Stubby ChangeLog\n\n```\n%s\n```\n" % contents['stubbychangelog']
if 'title' in contents:
title = contents['title']
elif 'rc' not in version and 'a' not in version and 'b' not in version:
title = 'getdns-%s release' % version
else:
raise(Exception('No title!'))
print(tag, title)
print(announcement)
rel = repo.create_release( tag_name = tag
, name = title
, body = announcement
, draft = True
, prerelease = 'rc' in version
)
with open(dn + '/getdns-%s.tar.gz' % version) as f:
rel.upload_asset( content_type = 'application/gzip'
, name = 'getdns-%s.tar.gz' % version
, asset = f
)
with open(dn + '/getdns-%s.tar.gz.asc' % version) as f:
rel.upload_asset( content_type = 'test/plain'
, name = 'getdns-%s.tar.gz.asc' % version
, asset = f
)
# using username and password
with open(os.environ.get('HOME', '~') + '/.github/auth') as f:
g = github3.login(*[x.strip() for x in f.read().strip().split()[:2]])
repo = g.repository('getdnsapi', 'getdns')
rels = dict()
for rel in repo.releases():
rels[rel.tag_name] = rel
for tag in repo.tags():
if not tag.name.startswith('v'):
continue
if tag.name in rels:
print('update', rels[tag.name])
elif not os.path.exists('content/releases/getdns-%s/contents.lr' % tag.name[1:].replace('.', '-')):
print('skipping content/releases/getdns-%s/contents.lr' % tag.name[1:].replace('.', '-'))
continue
else:
print('new release for', tag.name)
new_release(tag.name)
break