prune_binaries.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. # Copyright (c) 2019 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. """Prune binaries from the source tree"""
  7. import argparse
  8. import sys
  9. import os
  10. import stat
  11. from pathlib import Path
  12. from _common import ENCODING, get_logger, add_common_params
  13. # List of paths to prune if they exist, excluded from domain_substitution and pruning lists
  14. # These allow the lists to be compatible between cloned and tarball sources
  15. CONTINGENT_PATHS = (
  16. # Sources
  17. 'third_party/angle/third_party/VK-GL-CTS/src/',
  18. 'third_party/llvm/',
  19. 'third_party/rust-src/',
  20. # Binaries
  21. 'buildtools/linux64/',
  22. 'buildtools/reclient/',
  23. 'third_party/android_rust_toolchain/',
  24. 'third_party/apache-linux/',
  25. 'third_party/checkstyle/',
  26. 'third_party/dawn/third_party/ninja/',
  27. 'third_party/dawn/tools/golang/',
  28. 'third_party/depot_tools/external_bin/',
  29. 'third_party/devtools-frontend/src/third_party/esbuild/',
  30. 'third_party/google-java-format/',
  31. 'third_party/libei/',
  32. 'third_party/llvm-build-tools/',
  33. 'third_party/ninja/',
  34. 'third_party/screen-ai/',
  35. 'third_party/siso/',
  36. 'third_party/updater/chrome_linux64/',
  37. 'third_party/updater/chromium_linux64/',
  38. 'tools/luci-go/',
  39. 'tools/resultdb/',
  40. 'tools/skia_goldctl/linux/',
  41. )
  42. def prune_files(unpack_root, prune_list):
  43. """
  44. Delete files under unpack_root listed in prune_list. Returns an iterable of unremovable files.
  45. unpack_root is a pathlib.Path to the directory to be pruned
  46. prune_list is an iterable of files to be removed.
  47. """
  48. unremovable_files = set()
  49. for relative_file in prune_list:
  50. file_path = unpack_root / relative_file
  51. try:
  52. file_path.unlink()
  53. # read-only files can't be deleted on Windows
  54. # so remove the flag and try again.
  55. except PermissionError:
  56. os.chmod(file_path, stat.S_IWRITE)
  57. file_path.unlink()
  58. except FileNotFoundError:
  59. unremovable_files.add(Path(relative_file).as_posix())
  60. return unremovable_files
  61. def _prune_path(path):
  62. """
  63. Delete all files and directories in path.
  64. path is a pathlib.Path to the directory to be pruned
  65. """
  66. for node in sorted(path.rglob('*'), key=lambda l: len(str(l)), reverse=True):
  67. if node.is_file() or node.is_symlink():
  68. try:
  69. node.unlink()
  70. except PermissionError:
  71. node.chmod(stat.S_IWRITE)
  72. node.unlink()
  73. elif node.is_dir() and not any(node.iterdir()):
  74. try:
  75. node.rmdir()
  76. except PermissionError:
  77. node.chmod(stat.S_IWRITE)
  78. node.rmdir()
  79. def prune_dirs(unpack_root):
  80. """
  81. Delete all files and directories in pycache and CONTINGENT_PATHS directories.
  82. unpack_root is a pathlib.Path to the source tree
  83. """
  84. for pycache in unpack_root.rglob('__pycache__'):
  85. _prune_path(pycache)
  86. get_logger().info('Removing Contingent Paths')
  87. for cpath in CONTINGENT_PATHS:
  88. get_logger().info('%s: %s', 'Exists' if Path(cpath).exists() else 'Absent', cpath)
  89. _prune_path(unpack_root / cpath)
  90. def _callback(args):
  91. if not args.directory.exists():
  92. get_logger().error('Specified directory does not exist: %s', args.directory)
  93. sys.exit(1)
  94. if not args.pruning_list.exists():
  95. get_logger().error('Could not find the pruning list: %s', args.pruning_list)
  96. prune_dirs(args.directory)
  97. prune_list = tuple(filter(len, args.pruning_list.read_text(encoding=ENCODING).splitlines()))
  98. unremovable_files = prune_files(args.directory, prune_list)
  99. if unremovable_files:
  100. get_logger().error('%d files could not be pruned.', len(unremovable_files))
  101. get_logger().debug('Files could not be pruned:\n%s',
  102. '\n'.join(f for f in unremovable_files))
  103. sys.exit(1)
  104. def main():
  105. """CLI Entrypoint"""
  106. parser = argparse.ArgumentParser()
  107. parser.add_argument('directory', type=Path, help='The directory to apply binary pruning.')
  108. parser.add_argument('pruning_list', type=Path, help='Path to pruning.list')
  109. add_common_params(parser)
  110. parser.set_defaults(callback=_callback)
  111. args = parser.parse_args()
  112. args.callback(args)
  113. if __name__ == '__main__':
  114. main()