-
Notifications
You must be signed in to change notification settings - Fork 13
/
make-release.py
executable file
·202 lines (162 loc) · 5.99 KB
/
make-release.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python3
# hidpi-daemon: Universal driver for System76 computers
# Copyright (C) 2005-2016 System76, Inc.
#
# This file is part of `hidpi-daemon`.
#
# `hidpi-daemon` is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# `hidpi-daemon` is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with `hidpi-daemon`; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Make a stable hidpi-daemon release.
"""
import sys
import os
from os import path
import re
import time
from subprocess import check_call, check_output, call
from hidpidaemon import __version__
from hidpidaemon.tests.helpers import TempDir
DISTROS = ('trusty', 'xenial', 'yakkety', 'zesty', 'artful', 'bionic')
ALPHA = '~~alpha'
TREE = path.dirname(path.abspath(__file__))
assert TREE == sys.path[0]
assert os.getcwd() == TREE
CHANGELOG = path.join(TREE, 'debian', 'changelog')
SETUP = path.join(TREE, 'setup.py')
INIT = path.join(TREE, 'hidpidaemon', '__init__.py')
DSC_NAME = 'hidpi-daemon_{}.dsc'.format(__version__)
assert path.isfile(CHANGELOG)
assert path.isfile(SETUP)
assert path.isfile(INIT)
def confirm():
while True:
response = input(' Okay? yes/NO: ').lower()
if response == 'yes':
return True
if response == 'no':
return False
print("Please enter 'yes' or 'no'")
def check_for_uncommitted_changes():
if check_output(['git', 'diff']).decode() != '':
sys.exit('ERROR: unstaged changes!')
if check_output(['git', 'diff', '--cached']).decode() != '':
sys.exit('ERROR: uncommited changes!')
def iter_input_lines(fp):
yield fp.readline()
line = fp.readline()
if line != '\n':
raise ValueError('bad empty line[1]:\n{!r}'.format(line))
yield line
line = fp.readline()
if not line.startswith(' * Daily WIP for '):
raise ValueError('bad first item line[2]:\n{!r}'.format(line))
line = fp.readline()
if line[:4] != ' * ':
raise ValueError('bad second item line[3]:\n{!r}'.format(line))
yield line
i = 4
while True:
line = fp.readline()
if line[:4] not in (' * ', ' ', '\n'):
raise ValueError('bad item line[{}]:\n{!r}'.format(i, line))
yield line
i += 1
if line == '\n':
break
line = fp.readline()
if line[:4] != ' -- ':
raise ValueError('bad author line[{}]:\n{!r}'.format(i, line))
yield line
def parse_version_line(line):
if ALPHA not in line:
raise ValueError('Missing {!r} in version:\n{!r}'.format(ALPHA, line))
m = re.match(
'^hidpi-daemon \(([\.0-9]+)' + ALPHA + '\) ([a-z]+); urgency=low$', line
)
if m is None:
raise ValueError('bad version line[0]:\n{!r}'.format(line))
ver = m.group(1)
if ver != __version__:
raise ValueError(
'changelog != __version: {!r} != {!r}'.format(ver, __version__)
)
distro = m.group(2)
if distro not in DISTROS:
raise ValueError('bad distro {!r} not in {!r}'.format(distro, DISTROS))
return (ver, distro)
def build_version_line(line):
parse_version_line(line)
return line.replace(ALPHA, '')
def build_author_line():
user = check_output(['git', 'config', '--get', 'user.name']).decode().strip()
email = check_output(['git', 'config', '--get', 'user.email']).decode().strip()
author = ' '.join((user, '<' + email + '>'))
ts = time.strftime('%a, %d %b %Y %H:%M:%S %z', time.localtime())
return ' -- {} {}\n'.format(author, ts)
def iter_output_lines(input_lines):
yield build_version_line(input_lines[0])
yield from input_lines[1:-1]
yield build_author_line()
# Make sure there are no uncommited changes in the tree:
check_for_uncommitted_changes()
# Read lines from current debian/changelog file:
with open(CHANGELOG, 'r') as fp:
input_lines = list(iter_input_lines(fp))
remaining_lines = fp.readlines()
# Parse and validate, then build lines for new changelog files:
(version, distro) = parse_version_line(input_lines[0])
assert version == __version__
output_lines = list(iter_output_lines(input_lines))
assert len(output_lines) == len(input_lines)
assert output_lines[1:-1] == input_lines[1:-1]
# Again, make sure there are no uncommited changes in the tree:
check_for_uncommitted_changes()
# Write the new debian/changelog file:
with open(CHANGELOG, 'w') as fp:
fp.writelines(output_lines + remaining_lines)
# Make sure the unit tests pass in-tree:
check_call([SETUP, 'test'])
# Make sure package builds okay locally using pbuilder-dist:
check_call(['pbuilder-dist', distro, 'update'])
tmp = TempDir()
os.mkdir(tmp.join('result'))
check_call(['dpkg-source', '-b', TREE], cwd=tmp.join('result'))
check_call(['pbuilder-dist', distro, 'build', tmp.join('result', DSC_NAME)])
del tmp
def abort(msg=None):
if msg is not None:
print('\nERROR: ' + msg)
print('')
print('Release not made, reverting changes...')
check_call(['git', 'checkout', '--', CHANGELOG, INIT])
print('Goodbye.')
status = (0 if msg is None else 2)
sys.exit(status)
# Confirm before we make the commit:
print('-' * 80)
call(['git', 'diff'])
print('-' * 80)
print('Source tree is {!r}'.format(TREE))
print('Will release {!r} for {!r}'.format(version, distro))
if not confirm():
abort()
# Commit and tag:
check_call(['git', 'commit', CHANGELOG, '-m', 'Release {}'.format(version)])
check_call(['git', 'push'])
check_call(['git', 'tag', version])
check_call(['git', 'push', 'origin', 'tag', version])
# We're done:
print('-' * 80)
print('Released {!r} for {!r}'.format(version, distro))