|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# pylint: disable=invalid-name |
| 3 | + |
| 4 | +from argparse import ArgumentParser |
| 5 | +from pathlib import Path |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from tempfile import TemporaryDirectory |
| 10 | + |
| 11 | +parser = ArgumentParser( |
| 12 | + description='Check that patches apply to their tree before running CI') |
| 13 | +parser.add_argument( |
| 14 | + '-p', |
| 15 | + '--patches-dir', |
| 16 | + help='Path to patches directory (can be relative or absolute)', |
| 17 | + required=True, |
| 18 | + type=Path) |
| 19 | +parser.add_argument('-r', |
| 20 | + '--repo', |
| 21 | + help='URL to git repository', |
| 22 | + required=True) |
| 23 | +parser.add_argument('-R', |
| 24 | + '--ref', |
| 25 | + help='Git reference to apply patches upon', |
| 26 | + required=True) |
| 27 | +args = parser.parse_args() |
| 28 | + |
| 29 | +# If patches directory does not exist, there is nothing to check |
| 30 | +if not (patches_dir := args.patches_dir.resolve()).exists(): |
| 31 | + print(f"{patches_dir} does not exist, exiting 0...") |
| 32 | + sys.exit(0) |
| 33 | + |
| 34 | +# There should be patches in there due to check-patches.py but we should double |
| 35 | +# check and fail if not |
| 36 | +if not list(patches_dir.glob('*.patch')): |
| 37 | + print(f"{patches_dir} does not contain any patches?") |
| 38 | + sys.exit(1) |
| 39 | + |
| 40 | +# Rather that invoke 'git clone', which can be expensive for servers depending |
| 41 | +# on the frequency and duration of requests, we fetch a tarball and 'git init' |
| 42 | +# that. |
| 43 | +with TemporaryDirectory() as workdir: |
| 44 | + # Fetch the tarball from the repository. This is different for each type of |
| 45 | + # tree that we support. |
| 46 | + if args.repo.startswith( |
| 47 | + 'https://git.kernel.org/pub/scm/linux/kernel/git/'): |
| 48 | + # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git -> 'linux' |
| 49 | + if (base_repo := args.repo.rsplit('/', 1)[1]).endswith('.git'): |
| 50 | + base_repo = base_repo[:-len('.git')] |
| 51 | + |
| 52 | + # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git -> |
| 53 | + # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/snapshot/linux-master.tar.gz |
| 54 | + tarball_url = f"{args.repo}/snapshot/{base_repo}-{args.ref}.tar.gz" |
| 55 | + strip = 1 |
| 56 | + elif 'googlesource.com' in args.repo: |
| 57 | + tarball_url = f"{args.repo}/+archive/refs/heads/{args.ref}.tar.gz" |
| 58 | + strip = 0 |
| 59 | + else: |
| 60 | + raise RuntimeError(f"Do not know how to download {args.repo}?") |
| 61 | + |
| 62 | + try: |
| 63 | + print(f"Downloading {tarball_url}...") |
| 64 | + tarball = subprocess.run(['curl', '-LSs', tarball_url], |
| 65 | + capture_output=True, |
| 66 | + check=True).stdout |
| 67 | + |
| 68 | + print(f"Extracting {tarball_url}...") |
| 69 | + subprocess.run( |
| 70 | + ['tar', '-C', workdir, f"--strip-components={strip}", '-xzf-'], |
| 71 | + check=True, |
| 72 | + input=tarball) |
| 73 | + except subprocess.CalledProcessError: |
| 74 | + print( |
| 75 | + 'Downloading or extracting tarball failed! As this may be flakiness on the server end, exiting 0 to have TuxSuite fail later...' |
| 76 | + ) |
| 77 | + sys.exit(0) |
| 78 | + |
| 79 | + # Ensure that we can always commit regardless of whether user.name or |
| 80 | + # user.email are set in whatever environment we are running in, as this is |
| 81 | + # a temporary tree. |
| 82 | + git_name = 'check-patch-apply.py' |
| 83 | + git_email = f"{git_name}@{os.uname().nodename}.local" |
| 84 | + git_commit_env_vars = { |
| 85 | + **os.environ, # clone the environment, as subprocess may need it |
| 86 | + 'GIT_AUTHOR_NAME': git_name, |
| 87 | + 'GIT_AUTHOR_EMAIL': git_email, |
| 88 | + 'GIT_COMMITTER_NAME': git_name, |
| 89 | + 'GIT_COMMITTER_EMAIL': git_email, |
| 90 | + } # yapf: disable |
| 91 | + |
| 92 | + print(f"Creating initial git repository in {workdir}...") |
| 93 | + subprocess.run(['git', 'init', '-q'], check=True, cwd=workdir) |
| 94 | + |
| 95 | + print(f"Adding files in {workdir}...") |
| 96 | + subprocess.run(['git', 'add', '.'], check=True, cwd=workdir) |
| 97 | + |
| 98 | + print( |
| 99 | + f"Creating initial commit '{args.repo} @ {args.ref}' in {workdir}...") |
| 100 | + subprocess.run(['git', 'commit', '-m', f"{args.repo} @ {args.ref}", '-q'], |
| 101 | + check=True, |
| 102 | + cwd=workdir, |
| 103 | + env=git_commit_env_vars) |
| 104 | + |
| 105 | + print(f"Applying patches in {workdir}...") |
| 106 | + subprocess.run(['git', 'quiltimport', '--patches', patches_dir], |
| 107 | + check=True, |
| 108 | + cwd=workdir, |
| 109 | + env=git_commit_env_vars) |
0 commit comments