Roll src/third_party/WebKit ebb1ba5:b65f743 (svn 202609:202610)
[chromium-blink-merge.git] / gin / fingerprint / fingerprint_v8_snapshot.py
blobd1f70923335ae8802f837b90d0edaf3c84b3f45d
1 #!/usr/bin/env python
3 # Copyright 2015 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Fingerprints the V8 snapshot blob files.
9 Constructs a SHA256 fingerprint of the V8 natives and snapshot blob files and
10 creates a .cc file which includes these fingerprint as variables.
11 """
13 import hashlib
14 import optparse
15 import os
16 import sys
18 _HEADER = """// Copyright 2015 The Chromium Authors. All rights reserved.
19 // Use of this source code is governed by a BSD-style license that can be
20 // found in the LICENSE file.
22 // This file was generated by fingerprint_v8_snapshot.py.
24 namespace gin {
25 """
27 _FOOTER = """
28 } // namespace gin
29 """
32 def FingerprintFile(file_path):
33 input_file = open(file_path, 'rb')
34 sha256 = hashlib.sha256()
35 while True:
36 block = input_file.read(sha256.block_size)
37 if not block:
38 break
39 sha256.update(block)
40 return sha256.digest()
43 def WriteFingerprint(output_file, variable_name, fingerprint):
44 output_file.write('\nextern const unsigned char %s[] = { ' % variable_name)
45 for byte in fingerprint:
46 output_file.write(str(ord(byte)) + ', ')
47 output_file.write('};\n')
50 def WriteOutputFile(natives_fingerprint,
51 snapshot_fingerprint,
52 output_file_path):
53 output_dir_path = os.path.dirname(output_file_path)
54 if not os.path.exists(output_dir_path):
55 os.makedirs(output_dir_path)
56 output_file = open(output_file_path, 'w')
58 output_file.write(_HEADER)
59 WriteFingerprint(output_file, 'g_natives_fingerprint', natives_fingerprint)
60 output_file.write('\n')
61 WriteFingerprint(output_file, 'g_snapshot_fingerprint', snapshot_fingerprint)
62 output_file.write(_FOOTER)
65 def main():
66 parser = optparse.OptionParser()
68 parser.add_option('--snapshot_file',
69 help='The input V8 snapshot blob file path.')
70 parser.add_option('--natives_file',
71 help='The input V8 natives blob file path.')
72 parser.add_option('--output_file',
73 help='The path for the output cc file which will be write.')
75 options, _ = parser.parse_args()
77 natives_fingerprint = FingerprintFile(options.natives_file)
78 snapshot_fingerprint = FingerprintFile(options.snapshot_file)
79 WriteOutputFile(
80 natives_fingerprint, snapshot_fingerprint, options.output_file)
82 return 0
85 if __name__ == '__main__':
86 sys.exit(main())