zip.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. ]
  12. PATHS_TO_SKIP = [
  13. 'angledata', #Skipping because it is an output of //ui/gl that we don't need
  14. './libVkICD_mock_', #Skipping because these are outputs that we don't need
  15. './VkICD_mock_', #Skipping because these are outputs that we don't need
  16. # //chrome/browser:resources depends on this via
  17. # //chrome/browser/resources/ssl/ssl_error_assistant, but we don't need to
  18. # ship it.
  19. 'pyproto',
  20. ]
  21. def skip_path(dep, dist_zip, target_cpu):
  22. # Skip specific paths and extensions as well as the following special case:
  23. # snapshot_blob.bin is a dependency of mksnapshot.zip because
  24. # v8_context_generator needs it, but this file does not get generated for arm
  25. # and arm 64 binaries of mksnapshot since they are built on x64 hardware.
  26. # Consumers of arm and arm64 mksnapshot can generate snapshot_blob.bin
  27. # themselves by running mksnapshot.
  28. should_skip = (
  29. any(dep.startswith(path) for path in PATHS_TO_SKIP) or
  30. any(dep.endswith(ext) for ext in EXTENSIONS_TO_SKIP) or
  31. ('arm' in target_cpu and dist_zip == 'mksnapshot.zip' and dep == 'snapshot_blob.bin'))
  32. if should_skip:
  33. print("Skipping {}".format(dep))
  34. return should_skip
  35. def execute(argv):
  36. try:
  37. output = subprocess.check_output(argv, stderr=subprocess.STDOUT)
  38. return output
  39. except subprocess.CalledProcessError as e:
  40. print(e.output)
  41. raise e
  42. def main(argv):
  43. dist_zip, runtime_deps, target_cpu, target_os, flatten_val = argv
  44. should_flatten = flatten_val == "true"
  45. dist_files = set()
  46. with open(runtime_deps) as f:
  47. for dep in f.readlines():
  48. dep = dep.strip()
  49. dist_files.add(dep)
  50. if sys.platform == 'darwin' and not should_flatten:
  51. execute(['zip', '-r', '-y', dist_zip] + list(dist_files))
  52. else:
  53. with zipfile.ZipFile(dist_zip, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) as z:
  54. for dep in dist_files:
  55. if skip_path(dep, dist_zip, target_cpu):
  56. continue
  57. if os.path.isdir(dep):
  58. for root, dirs, files in os.walk(dep):
  59. for file in files:
  60. z.write(os.path.join(root, file))
  61. else:
  62. basename = os.path.basename(dep)
  63. dirname = os.path.dirname(dep)
  64. arcname = os.path.join(dirname, 'chrome-sandbox') if basename == 'chrome_sandbox' else dep
  65. z.write(dep, os.path.basename(arcname) if should_flatten else arcname)
  66. if __name__ == '__main__':
  67. sys.exit(main(sys.argv[1:]))