verify-mksnapshot.py 4.6 KB

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