forked from canonical/ubuntu-image-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.py
More file actions
executable file
·200 lines (176 loc) · 6.76 KB
/
Copy pathrelease.py
File metadata and controls
executable file
·200 lines (176 loc) · 6.76 KB
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
#!/usr/bin/python3
import os
import re
import sys
from contextlib import ExitStack, contextmanager
from debian.changelog import Changelog
from git import Repo
from git.exc import GitCommandError
from subprocess import run
from tempfile import NamedTemporaryFile
@contextmanager
def chdir(path):
here = os.getcwd()
try:
os.chdir(path)
yield
finally:
os.chdir(here)
@contextmanager
def atomic(dst, encoding='utf-8'):
"""Open a temporary file for writing using the given encoding.
The context manager returns an open file object, into which you can write
text or bytes depending on the encoding it was opened with. Upon exit,
the temporary file is moved atomically to the destination. If an
exception occurs, the temporary file is removed.
:param dst: The path name of the target file.
:param encoding: The encoding to use for the open file. If None, then
file is opened in binary mode.
"""
directory = os.path.dirname(dst)
mode = 'wb' if encoding is None else 'wt'
with ExitStack() as resources:
fp = resources.enter_context(NamedTemporaryFile(
mode=mode, encoding=encoding, dir=directory, delete=False))
yield fp
os.rename(fp.name, dst)
def update_changelog(repo, series, version):
# Update d/changelog.
with ExitStack() as resources:
debian_changelog = os.path.join(
repo.working_dir, 'debian', 'changelog')
infp = resources.enter_context(
open(debian_changelog, 'r', encoding='utf-8'))
outfp = resources.enter_context(atomic(debian_changelog))
changelog = Changelog(infp)
# Currently, master is always Zesty.
changelog.distributions = series
series_version = {
'bionic': '18.04',
'artful': '17.10',
'zesty': '17.04',
'xenial': '16.04',
}[series]
new_version = '{}+{}ubuntu1'.format(version, series_version)
changelog.version = new_version
changelog.write_to_open_file(outfp)
return new_version
def sru_tracking_bug(repo, sru):
with ExitStack() as resources:
debian_changelog = os.path.join(
repo.working_dir, 'debian', 'changelog')
infp = resources.enter_context(
open(debian_changelog, 'r', encoding='utf-8'))
outfp = resources.enter_context(atomic(debian_changelog))
changelog = Changelog(infp)
changelog.add_change(' * SRU tracking number LP: #{}'.format(sru))
changelog.write_to_open_file(outfp)
def continue_abort(msg='Pausing'):
print(msg)
while True:
answer = input('[c]ontinue, [a]bort? ')
if answer == 'a':
print('Aborting! Fix things manually')
sys.exit(1)
elif answer == 'c':
break
def tag_or_skip(repo, version):
force = False
while True:
answer = input('[t]ag, [f]orce, or [s]kip? ')
if answer == 's':
return
if answer in 'tf':
if answer == 'f':
force = True
break
repo.create_tag(version, force=force)
def munge_lp_bug_numbers(repo):
debian_changelog = os.path.join(repo.working_dir, 'debian', 'changelog')
with ExitStack() as resources:
infp = resources.enter_context(
open(debian_changelog, 'r', encoding='utf-8'))
outfp = resources.enter_context(atomic(debian_changelog))
changelog = Changelog(infp)
# Iterate through every line in the top changelog block. Because we
# want to modify the existing LP bug numbers, and because the API
# doesn't give us direct access to those lines, we need to pop the
# hood, reach in, and manipulate them ourselves.
for i, line in enumerate(changelog[0]._changes):
munged = re.sub('LP: #([0-9]+)', 'LP:\\1', line)
changelog[0]._changes[i] = munged
changelog.write_to_open_file(outfp)
def make_source_package(working_dir):
with chdir(working_dir):
run(['gbp', 'buildpackage', '-S', '-us', '-uc', '--git-ignore-branch'])
def main():
try:
working_dir = sys.argv[1]
except IndexError:
working_dir = os.getcwd()
repo = Repo(working_dir)
assert not repo.bare
# Start by modifying the master branch.
print('Updating master...')
repo.heads.master.checkout()
# The version number.
version = input('version: ')
sru = input('SRU tracking bug: ')
# Modify the snapcraft.yaml.
snapcraft_yaml = os.path.join(working_dir, 'snapcraft.yaml')
with ExitStack() as resources:
infp = resources.enter_context(
open(snapcraft_yaml, 'r', encoding='utf-8'))
outfp = resources.enter_context(atomic(snapcraft_yaml))
for line in infp:
if line.startswith('version: '):
print('version:', '{}+snap1'.format(version), file=outfp)
else:
outfp.write(line)
new_version = update_changelog(repo, 'bionic', version)
continue_abort('Pausing for manual review and commit')
tag_or_skip(repo, new_version)
make_source_package(working_dir)
# Now do the Artful branch.
repo.git.checkout('artful')
# This will almost certainly cause merge conflicts.
try:
repo.git.merge('master', '--no-commit')
except GitCommandError:
continue_abort('Resolve merge master->artful conflicts manually')
munge_lp_bug_numbers(repo)
sru_tracking_bug(repo, sru)
new_version = update_changelog(repo, 'artful', version)
continue_abort('Pausing for manual review and commit')
tag_or_skip(repo, new_version)
make_source_package(working_dir)
# Now do the Zesty branch.
repo.git.checkout('zesty')
# This will almost certainly cause merge conflicts.
try:
repo.git.merge('master', '--no-commit')
except GitCommandError:
continue_abort('Resolve merge master->zesty conflicts manually')
munge_lp_bug_numbers(repo)
sru_tracking_bug(repo, sru)
new_version = update_changelog(repo, 'zesty', version)
continue_abort('Pausing for manual review and commit')
tag_or_skip(repo, new_version)
make_source_package(working_dir)
# Now do the Xenial branch.
repo.git.checkout('xenial')
# This will almost certainly cause merge conflicts.
try:
repo.git.merge('master', '--no-commit')
except GitCommandError:
continue_abort('Resolve merge master->xenial conflicts manually')
munge_lp_bug_numbers(repo)
sru_tracking_bug(repo, sru)
new_version = update_changelog(repo, 'xenial', version)
continue_abort('Pausing for manual review and commit')
tag_or_skip(repo, new_version)
make_source_package(working_dir)
# Back to master and create the snap.
repo.heads.master.checkout()
if __name__ == '__main__':
main()