| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
import os |
| 5 |
import re |
| 6 |
import subprocess |
| 7 |
|
| 8 |
from debian_linux.debian import Changelog, VersionLinux |
| 9 |
|
| 10 |
|
| 11 |
def base_version(ver): |
| 12 |
# Assume base version is at least 3.0, thus only 2 components wanted |
| 13 |
match = re.match(r'^(\d+\.\d+)', ver) |
| 14 |
assert match |
| 15 |
return match.group(1) |
| 16 |
|
| 17 |
|
| 18 |
def add_update(ver, inc): |
| 19 |
base = base_version(ver) |
| 20 |
if base == ver: |
| 21 |
update = 0 |
| 22 |
else: |
| 23 |
update = int(ver[len(base)+1:]) |
| 24 |
update += inc |
| 25 |
if update == 0: |
| 26 |
return base |
| 27 |
else: |
| 28 |
return '{}.{}'.format(base, update) |
| 29 |
|
| 30 |
|
| 31 |
def next_update(ver): |
| 32 |
return add_update(ver, 1) |
| 33 |
|
| 34 |
|
| 35 |
def print_stable_log(log, cur_ver, new_ver): |
| 36 |
major_ver = re.sub(r'^(\d+)\..*', r'\1', cur_ver) |
| 37 |
while cur_ver != new_ver: |
| 38 |
next_ver = next_update(cur_ver) |
| 39 |
print(' https://www.kernel.org/pub/linux/kernel/v{}.x/ChangeLog-{}' |
| 40 |
.format(major_ver, next_ver), |
| 41 |
file=log) |
| 42 |
log.flush() # serialise our output with git's |
| 43 |
subprocess.check_call(['git', 'log', '--reverse', |
| 44 |
'--pretty= - %s', |
| 45 |
'v{}..v{}^'.format(cur_ver, next_ver)], |
| 46 |
stdout=log) |
| 47 |
cur_ver = next_ver |
| 48 |
|
| 49 |
|
| 50 |
def main(repo, new_ver): |
| 51 |
if os.path.exists(os.path.join(repo, '.git')): |
| 52 |
os.environ['GIT_DIR'] = os.path.join(repo, '.git') |
| 53 |
else: |
| 54 |
os.environ['GIT_DIR'] = repo |
| 55 |
|
| 56 |
changelog = Changelog(version=VersionLinux) |
| 57 |
cur_pkg_ver = changelog[0].version |
| 58 |
cur_ver = cur_pkg_ver.linux_version_full |
| 59 |
|
| 60 |
if base_version(new_ver) != base_version(cur_ver): |
| 61 |
print('{} is not on the same stable series as {}' |
| 62 |
.format(new_ver, cur_ver), |
| 63 |
file=sys.stderr) |
| 64 |
sys.exit(2) |
| 65 |
|
| 66 |
new_pkg_ver = new_ver + '-1' |
| 67 |
if '~exp' in cur_pkg_ver.revision: |
| 68 |
new_pkg_ver += '~exp1' |
| 69 |
|
| 70 |
# Three possible cases: |
| 71 |
# 1. The current version has been released so we need to add a new |
| 72 |
# version to the changelog. |
| 73 |
# 2. The current version has not been released so we're changing its |
| 74 |
# version string. |
| 75 |
# (a) There are no stable updates included in the current version, |
| 76 |
# so we need to insert an introductory line, the URL(s) and |
| 77 |
# git log(s) and a blank line at the top. |
| 78 |
# (b) One or more stable updates are already included in the current |
| 79 |
# version, so we need to insert the URL(s) and git log(s) after |
| 80 |
# them. |
| 81 |
|
| 82 |
changelog_intro = 'New upstream stable update:' |
| 83 |
|
| 84 |
# Case 1 |
| 85 |
if changelog[0].distribution != 'UNRELEASED': |
| 86 |
subprocess.check_call(['dch', '-v', new_pkg_ver, '-D', 'UNRELEASED', |
| 87 |
changelog_intro]) |
| 88 |
|
| 89 |
with open('debian/changelog', 'r') as old_log: |
| 90 |
with open('debian/changelog.new', 'w') as new_log: |
| 91 |
line_no = 0 |
| 92 |
inserted = False |
| 93 |
intro_line = ' * {}\n'.format(changelog_intro) |
| 94 |
|
| 95 |
for line in old_log: |
| 96 |
line_no += 1 |
| 97 |
|
| 98 |
# Case 2 |
| 99 |
if changelog[0].distribution == 'UNRELEASED' and line_no == 1: |
| 100 |
print('{} ({}) UNRELEASED; urgency={}' |
| 101 |
.format(changelog[0].source, new_pkg_ver, |
| 102 |
changelog[0].urgency), |
| 103 |
file=new_log) |
| 104 |
continue |
| 105 |
|
| 106 |
if not inserted: |
| 107 |
# Case 2(a) |
| 108 |
if line_no == 3 and line != intro_line: |
| 109 |
new_log.write(intro_line) |
| 110 |
print_stable_log(new_log, cur_ver, new_ver) |
| 111 |
new_log.write('\n') |
| 112 |
inserted = True |
| 113 |
# Case 1 or 2(b) |
| 114 |
elif line_no > 3 and line == '\n': |
| 115 |
print_stable_log(new_log, cur_ver, new_ver) |
| 116 |
inserted = True |
| 117 |
|
| 118 |
# Check that we inserted before hitting the end of the |
| 119 |
# first version entry |
| 120 |
assert not (line.startswith(' -- ') and not inserted) |
| 121 |
|
| 122 |
new_log.write(line) |
| 123 |
|
| 124 |
os.rename('debian/changelog.new', 'debian/changelog') |
| 125 |
|
| 126 |
|
| 127 |
if __name__ == '__main__': |
| 128 |
if len(sys.argv) != 3: |
| 129 |
print('''\ |
| 130 |
Usage: {} REPO VERSION |
| 131 |
REPO is the git repository to generate a changelog from |
| 132 |
VERSION is the stable version (without leading v)'''.format(sys.argv[0]), |
| 133 |
file=sys.stderr) |
| 134 |
sys.exit(2) |
| 135 |
main(*sys.argv[1:]) |