patches.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. # Copyright (c) 2020 The ungoogled-chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Applies unified diff patches"""
  7. import argparse
  8. import os
  9. import shutil
  10. import subprocess
  11. from pathlib import Path
  12. from _common import get_logger, parse_series, add_common_params
  13. def _find_patch_from_env():
  14. patch_bin_path = None
  15. patch_bin_env = os.environ.get('PATCH_BIN')
  16. if patch_bin_env:
  17. patch_bin_path = Path(patch_bin_env)
  18. if patch_bin_path.exists():
  19. get_logger().debug('Found PATCH_BIN with path "%s"', patch_bin_path)
  20. else:
  21. patch_which = shutil.which(patch_bin_env)
  22. if patch_which:
  23. get_logger().debug('Found PATCH_BIN for command with path "%s"', patch_which)
  24. patch_bin_path = Path(patch_which)
  25. else:
  26. get_logger().debug('PATCH_BIN env variable is not set')
  27. return patch_bin_path
  28. def _find_patch_from_which():
  29. patch_which = shutil.which('patch')
  30. if not patch_which:
  31. get_logger().debug('Did not find "patch" in PATH environment variable')
  32. return None
  33. return Path(patch_which)
  34. def find_and_check_patch(patch_bin_path=None):
  35. """
  36. Find and/or check the patch binary is working. It finds a path to patch in this order:
  37. 1. Use patch_bin_path if it is not None
  38. 2. See if "PATCH_BIN" environment variable is set
  39. 3. Do "which patch" to find GNU patch
  40. Then it does some sanity checks to see if the patch command is valid.
  41. Returns the path to the patch binary found.
  42. """
  43. if patch_bin_path is None:
  44. patch_bin_path = _find_patch_from_env()
  45. if patch_bin_path is None:
  46. patch_bin_path = _find_patch_from_which()
  47. if not patch_bin_path:
  48. raise ValueError('Could not find patch from PATCH_BIN env var or "which patch"')
  49. if not patch_bin_path.exists():
  50. raise ValueError('Could not find the patch binary: {}'.format(patch_bin_path))
  51. # Ensure patch actually runs
  52. cmd = [str(patch_bin_path), '--version']
  53. result = subprocess.run(cmd,
  54. stdout=subprocess.PIPE,
  55. stderr=subprocess.PIPE,
  56. check=False,
  57. universal_newlines=True)
  58. if result.returncode:
  59. get_logger().error('"%s" returned non-zero exit code', ' '.join(cmd))
  60. get_logger().error('stdout:\n%s', result.stdout)
  61. get_logger().error('stderr:\n%s', result.stderr)
  62. raise RuntimeError('Got non-zero exit code running "{}"'.format(' '.join(cmd)))
  63. return patch_bin_path
  64. def dry_run_check(patch_path, tree_path, patch_bin_path=None):
  65. """
  66. Run patch --dry-run on a patch
  67. tree_path is the pathlib.Path of the source tree to patch
  68. patch_path is a pathlib.Path to check
  69. reverse is whether the patches should be reversed
  70. patch_bin_path is the pathlib.Path of the patch binary, or None to find it automatically
  71. See find_and_check_patch() for logic to find "patch"
  72. Returns the status code, stdout, and stderr of patch --dry-run
  73. """
  74. cmd = [
  75. str(find_and_check_patch(patch_bin_path)), '-p1', '--ignore-whitespace', '-i',
  76. str(patch_path), '-d',
  77. str(tree_path), '--no-backup-if-mismatch', '--dry-run'
  78. ]
  79. result = subprocess.run(cmd,
  80. stdout=subprocess.PIPE,
  81. stderr=subprocess.PIPE,
  82. check=False,
  83. universal_newlines=True)
  84. return result.returncode, result.stdout, result.stderr
  85. def apply_patches(patch_path_iter, tree_path, reverse=False, patch_bin_path=None):
  86. """
  87. Applies or reverses a list of patches
  88. tree_path is the pathlib.Path of the source tree to patch
  89. patch_path_iter is a list or tuple of pathlib.Path to patch files to apply
  90. reverse is whether the patches should be reversed
  91. patch_bin_path is the pathlib.Path of the patch binary, or None to find it automatically
  92. See find_and_check_patch() for logic to find "patch"
  93. Raises ValueError if the patch binary could not be found.
  94. """
  95. patch_paths = list(patch_path_iter)
  96. patch_bin_path = find_and_check_patch(patch_bin_path=patch_bin_path)
  97. if reverse:
  98. patch_paths.reverse()
  99. logger = get_logger()
  100. for patch_path, patch_num in zip(patch_paths, range(1, len(patch_paths) + 1)):
  101. cmd = [
  102. str(patch_bin_path), '-p1', '--ignore-whitespace', '-i',
  103. str(patch_path), '-d',
  104. str(tree_path), '--no-backup-if-mismatch'
  105. ]
  106. if reverse:
  107. cmd.append('--reverse')
  108. log_word = 'Reversing'
  109. else:
  110. cmd.append('--forward')
  111. log_word = 'Applying'
  112. logger.info('* %s %s (%s/%s)', log_word, patch_path.name, patch_num, len(patch_paths))
  113. logger.debug(' '.join(cmd))
  114. subprocess.run(cmd, check=True)
  115. def generate_patches_from_series(patches_dir, resolve=False):
  116. """Generates pathlib.Path for patches from a directory in GNU Quilt format"""
  117. for patch_path in parse_series(patches_dir / 'series'):
  118. if resolve:
  119. yield (patches_dir / patch_path).resolve()
  120. else:
  121. yield patch_path
  122. def _copy_files(path_iter, source, destination):
  123. """Copy files from source to destination with relative paths from path_iter"""
  124. for path in path_iter:
  125. (destination / path).parent.mkdir(parents=True, exist_ok=True)
  126. shutil.copy2(str(source / path), str(destination / path))
  127. def merge_patches(source_iter, destination, prepend=False):
  128. """
  129. Merges GNU quilt-formatted patches directories from sources into destination
  130. destination must not already exist, unless prepend is True. If prepend is True, then
  131. the source patches will be prepended to the destination.
  132. """
  133. series = []
  134. known_paths = set()
  135. if destination.exists():
  136. if prepend:
  137. if not (destination / 'series').exists():
  138. raise FileNotFoundError(
  139. 'Could not find series file in existing destination: {}'.format(destination /
  140. 'series'))
  141. known_paths.update(generate_patches_from_series(destination))
  142. else:
  143. raise FileExistsError('destination already exists: {}'.format(destination))
  144. for source_dir in source_iter:
  145. patch_paths = tuple(generate_patches_from_series(source_dir))
  146. patch_intersection = known_paths.intersection(patch_paths)
  147. if patch_intersection:
  148. raise FileExistsError(
  149. 'Patches from {} have conflicting paths with other sources: {}'.format(
  150. source_dir, patch_intersection))
  151. series.extend(patch_paths)
  152. _copy_files(patch_paths, source_dir, destination)
  153. if prepend and (destination / 'series').exists():
  154. series.extend(generate_patches_from_series(destination))
  155. with (destination / 'series').open('w') as series_file:
  156. series_file.write('\n'.join(map(str, series)))
  157. def _apply_callback(args, parser_error):
  158. logger = get_logger()
  159. patch_bin_path = None
  160. if args.patch_bin is not None:
  161. patch_bin_path = Path(args.patch_bin)
  162. if not patch_bin_path.exists():
  163. patch_bin_path = shutil.which(args.patch_bin)
  164. if patch_bin_path:
  165. patch_bin_path = Path(patch_bin_path)
  166. else:
  167. parser_error(
  168. f'--patch-bin "{args.patch_bin}" is not a command or path to executable.')
  169. for patch_dir in args.patches:
  170. logger.info('Applying patches from %s', patch_dir)
  171. apply_patches(generate_patches_from_series(patch_dir, resolve=True),
  172. args.target,
  173. patch_bin_path=patch_bin_path)
  174. def _merge_callback(args, _):
  175. merge_patches(args.source, args.destination, args.prepend)
  176. def main():
  177. """CLI Entrypoint"""
  178. parser = argparse.ArgumentParser()
  179. add_common_params(parser)
  180. subparsers = parser.add_subparsers()
  181. apply_parser = subparsers.add_parser(
  182. 'apply', help='Applies patches (in GNU Quilt format) to the specified source tree')
  183. apply_parser.add_argument('--patch-bin',
  184. help='The GNU patch command to use. Omit to find it automatically.')
  185. apply_parser.add_argument('target', type=Path, help='The directory tree to apply patches onto.')
  186. apply_parser.add_argument(
  187. 'patches',
  188. type=Path,
  189. nargs='+',
  190. help='The directories containing patches to apply. They must be in GNU quilt format')
  191. apply_parser.set_defaults(callback=_apply_callback)
  192. merge_parser = subparsers.add_parser('merge',
  193. help='Merges patches directories in GNU quilt format')
  194. merge_parser.add_argument(
  195. '--prepend',
  196. '-p',
  197. action='store_true',
  198. help=('If "destination" exists, prepend patches from sources into it.'
  199. ' By default, merging will fail if the destination already exists.'))
  200. merge_parser.add_argument(
  201. 'destination',
  202. type=Path,
  203. help=('The directory to write the merged patches to. '
  204. 'The destination must not exist unless --prepend is specified.'))
  205. merge_parser.add_argument('source',
  206. type=Path,
  207. nargs='+',
  208. help='The GNU quilt patches to merge.')
  209. merge_parser.set_defaults(callback=_merge_callback)
  210. args = parser.parse_args()
  211. if 'callback' not in args:
  212. parser.error('Must specify subcommand apply or merge')
  213. args.callback(args, parser.error)
  214. if __name__ == '__main__':
  215. main()