verify-mksnapshot.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python
  2. import argparse
  3. import glob
  4. import os
  5. import shutil
  6. import subprocess
  7. import sys
  8. from lib.util import get_electron_branding, rm_rf, scoped_cwd
  9. PROJECT_NAME = get_electron_branding()['project_name']
  10. PRODUCT_NAME = get_electron_branding()['product_name']
  11. SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  12. SNAPSHOT_SOURCE = os.path.join(SOURCE_ROOT, 'spec', 'fixtures', 'testsnap.js')
  13. def main():
  14. args = parse_args()
  15. source_root = os.path.abspath(args.source_root)
  16. initial_app_path = os.path.join(source_root, args.build_dir)
  17. app_path = create_app_copy(initial_app_path)
  18. returncode = 0
  19. try:
  20. with scoped_cwd(app_path):
  21. mkargs = [ get_binary_path('mksnapshot', app_path), \
  22. SNAPSHOT_SOURCE, '--startup_blob', 'snapshot_blob.bin', \
  23. '--turbo_instruction_scheduling' ]
  24. subprocess.check_call(mkargs)
  25. print 'ok mksnapshot successfully created snapshot_blob.bin.'
  26. context_snapshot = 'v8_context_snapshot.bin'
  27. context_snapshot_path = os.path.join(app_path, context_snapshot)
  28. gen_binary = get_binary_path('v8_context_snapshot_generator', \
  29. app_path)
  30. genargs = [ gen_binary, \
  31. '--output_file={0}'.format(context_snapshot_path) ]
  32. subprocess.check_call(genargs)
  33. print 'ok v8_context_snapshot_generator successfully created ' \
  34. + context_snapshot
  35. test_path = os.path.join(SOURCE_ROOT, 'spec', 'fixtures', \
  36. 'snapshot-items-available')
  37. if sys.platform == 'darwin':
  38. bin_files = glob.glob(os.path.join(app_path, '*.bin'))
  39. app_dir = os.path.join(app_path, '{0}.app'.format(PRODUCT_NAME))
  40. electron = os.path.join(app_dir, 'Contents', 'MacOS', PRODUCT_NAME)
  41. bin_out_path = os.path.join(app_dir, 'Contents', 'Frameworks',
  42. '{0} Framework.framework'.format(PROJECT_NAME),
  43. 'Resources')
  44. for bin_file in bin_files:
  45. shutil.copy2(bin_file, bin_out_path)
  46. elif sys.platform == 'win32':
  47. electron = os.path.join(app_path, '{0}.exe'.format(PROJECT_NAME))
  48. else:
  49. electron = os.path.join(app_path, PROJECT_NAME)
  50. subprocess.check_call([electron, test_path])
  51. print 'ok successfully used custom snapshot.'
  52. except subprocess.CalledProcessError as e:
  53. print 'not ok an error was encountered while testing mksnapshot.'
  54. print e
  55. returncode = e.returncode
  56. except KeyboardInterrupt:
  57. print 'Other error'
  58. returncode = 0
  59. print 'Returning with error code: {0}'.format(returncode)
  60. return returncode
  61. # Create copy of app to install custom snapshot
  62. def create_app_copy(initial_app_path):
  63. print 'Creating copy of app for testing'
  64. app_path = os.path.join(os.path.dirname(initial_app_path),
  65. os.path.basename(initial_app_path)
  66. + '-mksnapshot-test')
  67. rm_rf(app_path)
  68. shutil.copytree(initial_app_path, app_path, symlinks=True)
  69. return app_path
  70. def get_binary_path(binary_name, root_path):
  71. if sys.platform == 'win32':
  72. binary_path = os.path.join(root_path, '{0}.exe'.format(binary_name))
  73. else:
  74. binary_path = os.path.join(root_path, binary_name)
  75. return binary_path
  76. def parse_args():
  77. parser = argparse.ArgumentParser(description='Test mksnapshot')
  78. parser.add_argument('-b', '--build-dir',
  79. help='Path to an Electron build folder. \
  80. Relative to the --source-root.',
  81. default=None,
  82. required=True)
  83. parser.add_argument('--source-root',
  84. default=SOURCE_ROOT,
  85. required=False)
  86. return parser.parse_args()
  87. if __name__ == '__main__':
  88. sys.exit(main())