js2asar.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. import errno
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. import tempfile
  8. SOURCE_ROOT = os.path.dirname(os.path.dirname(__file__))
  9. def main():
  10. archive = sys.argv[1]
  11. folder_name = sys.argv[2]
  12. source_files = sys.argv[3:]
  13. output_dir = tempfile.mkdtemp()
  14. copy_files(source_files, output_dir)
  15. call_asar(archive, os.path.join(output_dir, folder_name))
  16. shutil.rmtree(output_dir)
  17. def copy_files(source_files, output_dir):
  18. for source_file in source_files:
  19. output_path = os.path.join(output_dir, source_file)
  20. safe_mkdir(os.path.dirname(output_path))
  21. shutil.copy2(source_file, output_path)
  22. def call_asar(archive, output_dir):
  23. asar = os.path.join(SOURCE_ROOT, 'node_modules', '.bin', 'asar')
  24. if sys.platform in ['win32', 'cygwin']:
  25. asar += '.cmd'
  26. subprocess.check_call([asar, 'pack', output_dir, archive])
  27. def safe_mkdir(path):
  28. try:
  29. os.makedirs(path)
  30. except OSError as e:
  31. if e.errno != errno.EEXIST:
  32. raise
  33. if __name__ == '__main__':
  34. sys.exit(main())