js2asar.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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, folder_name)
  15. call_asar(archive, os.path.join(output_dir, folder_name))
  16. shutil.rmtree(output_dir)
  17. def copy_files(source_files, output_dir, folder_name):
  18. for source_file in source_files:
  19. output_path = os.path.join(output_dir, source_file)
  20. # Files that aren't in the default_app folder need to be put inside
  21. # the temp one we are making so they end up in the ASAR
  22. if not os.path.normpath(source_file).startswith(folder_name + os.sep):
  23. output_path = os.path.join(output_dir, folder_name, source_file)
  24. safe_mkdir(os.path.dirname(output_path))
  25. shutil.copy2(source_file, output_path)
  26. def call_asar(archive, output_dir):
  27. asar = os.path.join(SOURCE_ROOT, 'node_modules', '.bin', 'asar')
  28. if sys.platform in ['win32', 'cygwin']:
  29. asar += '.cmd'
  30. subprocess.check_call([asar, 'pack', output_dir, archive])
  31. def safe_mkdir(path):
  32. try:
  33. os.makedirs(path)
  34. except OSError as e:
  35. if e.errno != errno.EEXIST:
  36. raise
  37. if __name__ == '__main__':
  38. sys.exit(main())