zip.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. import sys
  5. import zipfile
  6. EXTENSIONS_TO_SKIP = [
  7. '.pdb',
  8. '.mojom.js',
  9. '.mojom-lite.js',
  10. '.info',
  11. '.m.js',
  12. # These are only needed for Chromium tests we don't run. Listed in
  13. # 'extensions' because the mksnapshot zip has these under a subdirectory, and
  14. # the PATHS_TO_SKIP is checked with |startswith|.
  15. 'dbgcore.dll',
  16. 'dbghelp.dll',
  17. ]
  18. PATHS_TO_SKIP = [
  19. # Skip because it is an output of //ui/gl that we don't need.
  20. 'angledata',
  21. # Skip because these are outputs that we don't need.
  22. './libVkICD_mock_',
  23. # Skip because these are outputs that we don't need.
  24. './VkICD_mock_',
  25. # Skip because its an output of create_bundle from
  26. # //build/config/mac/rules.gni that we don't need
  27. 'Electron.dSYM',
  28. # Refs https://chromium-review.googlesource.com/c/angle/angle/+/2425197.
  29. # Remove this when Angle themselves remove the file:
  30. # https://issuetracker.google.com/issues/168736059
  31. 'gen/angle/angle_commit.h',
  32. # //chrome/browser:resources depends on this via
  33. # //chrome/browser/resources/ssl/ssl_error_assistant, but we don't need to
  34. # ship it.
  35. 'pyproto',
  36. # Skip because these are outputs that we don't need.
  37. 'resources/inspector',
  38. 'gen/third_party/devtools-frontend/src',
  39. 'gen/ui/webui',
  40. ]
  41. def skip_path(dep, dist_zip, target_cpu):
  42. # Skip specific paths and extensions as well as the following special case:
  43. # snapshot_blob.bin is a dependency of mksnapshot.zip because
  44. # v8_context_generator needs it, but this file does not get generated for arm
  45. # and arm 64 binaries of mksnapshot since they are built on x64 hardware.
  46. # Consumers of arm and arm64 mksnapshot can generate snapshot_blob.bin
  47. # themselves by running mksnapshot.
  48. should_skip = (
  49. any(dep.startswith(path) for path in PATHS_TO_SKIP) or
  50. any(dep.endswith(ext) for ext in EXTENSIONS_TO_SKIP) or
  51. (
  52. "arm" in target_cpu
  53. and dist_zip == "mksnapshot.zip"
  54. and dep == "snapshot_blob.bin"
  55. )
  56. )
  57. if should_skip:
  58. print("Skipping {}".format(dep))
  59. return should_skip
  60. def execute(argv):
  61. try:
  62. output = subprocess.check_output(argv, stderr=subprocess.STDOUT)
  63. return output
  64. except subprocess.CalledProcessError as e:
  65. print(e.output)
  66. raise e
  67. def main(argv):
  68. dist_zip, runtime_deps, target_cpu, _, flatten_val, flatten_relative_to = argv
  69. should_flatten = flatten_val == "true"
  70. dist_files = set()
  71. with open(runtime_deps) as f:
  72. for dep in f.readlines():
  73. dep = dep.strip()
  74. if not skip_path(dep, dist_zip, target_cpu):
  75. dist_files.add(dep)
  76. if sys.platform == 'darwin' and not should_flatten:
  77. execute(['zip', '-r', '-y', dist_zip] + list(dist_files))
  78. else:
  79. with zipfile.ZipFile(
  80. dist_zip, 'w', zipfile.ZIP_DEFLATED, allowZip64=True
  81. ) as z:
  82. for dep in dist_files:
  83. if os.path.isdir(dep):
  84. for root, _, files in os.walk(dep):
  85. for filename in files:
  86. z.write(os.path.join(root, filename))
  87. else:
  88. basename = os.path.basename(dep)
  89. dirname = os.path.dirname(dep)
  90. arcname = (
  91. os.path.join(dirname, 'chrome-sandbox')
  92. if basename == 'chrome_sandbox'
  93. else dep
  94. )
  95. name_to_write = arcname
  96. if should_flatten:
  97. if flatten_relative_to:
  98. if name_to_write.startswith(flatten_relative_to):
  99. name_to_write = name_to_write[len(flatten_relative_to):]
  100. else:
  101. name_to_write = os.path.basename(arcname)
  102. else:
  103. name_to_write = os.path.basename(arcname)
  104. z.write(
  105. dep,
  106. name_to_write,
  107. )
  108. if __name__ == '__main__':
  109. sys.exit(main(sys.argv[1:]))