zip.py 3.5 KB

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