zip.py 3.7 KB

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