| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import io |
| 4 |
import os |
| 5 |
import os.path |
| 6 |
import re |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
|
| 10 |
|
| 11 |
def main(repo, range='torvalds/master..dhowells/efi-lock-down'): |
| 12 |
patch_dir = 'debian/patches' |
| 13 |
lockdown_patch_dir = 'features/all/lockdown' |
| 14 |
series_name = 'series' |
| 15 |
|
| 16 |
# Only replace patches in this subdirectory and starting with a digit |
| 17 |
# - the others are presumably Debian-specific for now |
| 18 |
lockdown_patch_name_re = re.compile( |
| 19 |
r'^' + re.escape(lockdown_patch_dir) + r'/\d') |
| 20 |
series_before = [] |
| 21 |
series_after = [] |
| 22 |
|
| 23 |
old_series = set() |
| 24 |
new_series = set() |
| 25 |
|
| 26 |
try: |
| 27 |
with open(os.path.join(patch_dir, series_name), 'r') as series_fh: |
| 28 |
for line in series_fh: |
| 29 |
name = line.strip() |
| 30 |
if lockdown_patch_name_re.match(name): |
| 31 |
old_series.add(name) |
| 32 |
elif len(old_series) == 0: |
| 33 |
series_before.append(line) |
| 34 |
else: |
| 35 |
series_after.append(line) |
| 36 |
except FileNotFoundError: |
| 37 |
pass |
| 38 |
|
| 39 |
with open(os.path.join(patch_dir, series_name), 'w') as series_fh: |
| 40 |
for line in series_before: |
| 41 |
series_fh.write(line) |
| 42 |
|
| 43 |
# Add directory prefix to all filenames. |
| 44 |
# Add Origin to all patch headers. |
| 45 |
def add_patch(name, source_patch, origin): |
| 46 |
name = os.path.join(lockdown_patch_dir, name) |
| 47 |
path = os.path.join(patch_dir, name) |
| 48 |
try: |
| 49 |
os.unlink(path) |
| 50 |
except FileNotFoundError: |
| 51 |
pass |
| 52 |
with open(path, 'w') as patch: |
| 53 |
in_header = True |
| 54 |
for line in source_patch: |
| 55 |
if in_header and re.match(r'^(\n|[^\w\s]|Index:)', line): |
| 56 |
patch.write('Origin: %s\n' % origin) |
| 57 |
if line != '\n': |
| 58 |
patch.write('\n') |
| 59 |
in_header = False |
| 60 |
patch.write(line) |
| 61 |
series_fh.write(name) |
| 62 |
series_fh.write('\n') |
| 63 |
new_series.add(name) |
| 64 |
|
| 65 |
# XXX No signature to verify |
| 66 |
|
| 67 |
env = os.environ.copy() |
| 68 |
env['GIT_DIR'] = os.path.join(repo, '.git') |
| 69 |
args = ['git', 'format-patch', '--subject-prefix=', range] |
| 70 |
format_proc = subprocess.Popen(args, |
| 71 |
cwd=os.path.join(patch_dir, |
| 72 |
lockdown_patch_dir), |
| 73 |
env=env, stdout=subprocess.PIPE) |
| 74 |
with io.open(format_proc.stdout.fileno(), encoding='utf-8') as pipe: |
| 75 |
for line in pipe: |
| 76 |
name = line.strip('\n') |
| 77 |
with open(os.path.join(patch_dir, lockdown_patch_dir, name)) \ |
| 78 |
as source_patch: |
| 79 |
patch_from = source_patch.readline() |
| 80 |
match = re.match(r'From ([0-9a-f]{40}) ', patch_from) |
| 81 |
assert match |
| 82 |
origin = ('https://git.kernel.org/pub/scm/linux/kernel/' |
| 83 |
'git/dhowells/linux-fs.git/commit?id=%s' % |
| 84 |
match.group(1)) |
| 85 |
add_patch(name, source_patch, origin) |
| 86 |
|
| 87 |
for line in series_after: |
| 88 |
series_fh.write(line) |
| 89 |
|
| 90 |
for name in new_series: |
| 91 |
if name in old_series: |
| 92 |
old_series.remove(name) |
| 93 |
else: |
| 94 |
print('Added patch', os.path.join(patch_dir, name)) |
| 95 |
|
| 96 |
for name in old_series: |
| 97 |
print('Obsoleted patch', os.path.join(patch_dir, name)) |
| 98 |
|
| 99 |
|
| 100 |
if __name__ == '__main__': |
| 101 |
if not (2 <= len(sys.argv) <= 3): |
| 102 |
sys.stderr.write('''\ |
| 103 |
Usage: %s REPO [REVISION-RANGE] |
| 104 |
REPO is a git repo containing the REVISION-RANGE. The default range is |
| 105 |
torvalds/master..dhowells/efi-lock-down. |
| 106 |
''' % sys.argv[0]) |
| 107 |
print('BASE is the base branch (default: torvalds/master).') |
| 108 |
sys.exit(2) |
| 109 |
main(*sys.argv[1:]) |