|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import requests |
| 5 | +import tempfile |
| 6 | +import os |
| 7 | +from subprocess import call, check_call |
| 8 | + |
| 9 | +session = requests.Session() |
| 10 | + |
| 11 | +arg = sys.argv[1] |
| 12 | + |
| 13 | +# See if the argument we got is a patch id (an int) |
| 14 | +try: |
| 15 | + pid = int(arg) |
| 16 | +except ValueError: |
| 17 | + # No, assume it's a message id |
| 18 | + msgid = arg |
| 19 | +else: |
| 20 | + # Lookup the msgid using the patch id |
| 21 | + url = f'https://patchwork.ozlabs.org/api/patches/{pid}/' |
| 22 | + json = session.get(url).json() |
| 23 | + msgid = json['msgid'] |
| 24 | + |
| 25 | +# When copying from URL bar these sometimes come along and should be removed |
| 26 | +msgid = msgid.rstrip('/') |
| 27 | + |
| 28 | +temp_dir = tempfile.mkdtemp() |
| 29 | + |
| 30 | +cmd = f'b4 am -s -l -t -o {temp_dir} %s {msgid}' % ' '.join(sys.argv[2:]) |
| 31 | +print(f'Running: {cmd}') |
| 32 | + |
| 33 | +check_call(cmd.split()) |
| 34 | +print("b4 completed OK") |
| 35 | + |
| 36 | +mbx = None |
| 37 | +cover = None |
| 38 | +for fname in os.listdir(temp_dir): |
| 39 | + if fname.endswith('.mbx'): |
| 40 | + mbx = fname |
| 41 | + if fname.endswith('.cover'): |
| 42 | + cover = fname |
| 43 | + |
| 44 | +if mbx is None: |
| 45 | + print("Error: couldn't find mbox!") |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | +cmd = f'git am {temp_dir}/{mbx}' |
| 49 | +print(f'Running: {cmd}') |
| 50 | +rc = call(cmd.split()) |
| 51 | +if rc == 0: |
| 52 | + print("git am completed OK") |
| 53 | +else: |
| 54 | + print('============================================================') |
| 55 | + print('git am failed, fix up manually.') |
| 56 | + print("When finished 'exit 0' to continue or 'exit 1' to abort.") |
| 57 | + print('============================================================') |
| 58 | + rc = call('bash') |
| 59 | + if rc != 0: |
| 60 | + print(f"Aborting as requested, series left in {temp_dir}") |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | +os.unlink(f'{temp_dir}/{mbx}') |
| 64 | +if cover: |
| 65 | + os.unlink(f'{temp_dir}/{cover}') |
| 66 | + |
| 67 | +os.rmdir(temp_dir) |
| 68 | +print("OK") |
0 commit comments