Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 | 6 |
| 7 """Takes the JSON files in components/domain_reliability/baked_in_configs and | 7 """Takes the JSON files in components/domain_reliability/baked_in_configs and |
| 8 encodes their contents as an array of C strings that gets compiled in to Chrome | 8 encodes their contents as an array of C strings that gets compiled in to Chrome |
| 9 and loaded at runtime.""" | 9 and loaded at runtime.""" |
| 10 | 10 |
| 11 | 11 |
| 12 import ast | 12 import ast |
| 13 import json | 13 import json |
| 14 import optparse | |
| 14 import os | 15 import os |
| 16 import shlex | |
| 15 import sys | 17 import sys |
| 16 | 18 |
| 17 | 19 |
| 18 # A whitelist of domains that the script will accept when baking configs in to | 20 # A whitelist of domains that the script will accept when baking configs in to |
| 19 # Chrome, to ensure incorrect ones are not added accidentally. Subdomains of | 21 # Chrome, to ensure incorrect ones are not added accidentally. Subdomains of |
| 20 # whitelist entries are also allowed (e.g. maps.google.com, ssl.gstatic.com). | 22 # whitelist entries are also allowed (e.g. maps.google.com, ssl.gstatic.com). |
| 21 DOMAIN_WHITELIST = ( | 23 DOMAIN_WHITELIST = ( |
| 22 '2mdn.net', | 24 '2mdn.net', |
| 23 'admob.biz', | 25 'admob.biz', |
| 24 'admob.co.in', | 26 'admob.co.in', |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 """ | 470 """ |
| 469 | 471 |
| 470 | 472 |
| 471 def read_json_files_from_gypi(gypi_file): | 473 def read_json_files_from_gypi(gypi_file): |
| 472 with open(gypi_file, 'r') as f: | 474 with open(gypi_file, 'r') as f: |
| 473 gypi_text = f.read() | 475 gypi_text = f.read() |
| 474 json_files = ast.literal_eval(gypi_text)['variables']['baked_in_configs'] | 476 json_files = ast.literal_eval(gypi_text)['variables']['baked_in_configs'] |
| 475 return json_files | 477 return json_files |
| 476 | 478 |
| 477 | 479 |
| 480 def read_json_files_from_file(list_file): | |
| 481 with open(list_file, 'r') as f: | |
| 482 list_text = f.read() | |
| 483 return shlex.split(list_text) | |
| 484 | |
| 485 | |
| 478 def domain_is_whitelisted(domain): | 486 def domain_is_whitelisted(domain): |
| 479 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST) | 487 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST) |
| 480 | 488 |
| 481 | 489 |
| 482 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'): | 490 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'): |
| 483 max_length = width - len(prefix) - len(suffix) | 491 max_length = width - len(prefix) - len(suffix) |
| 484 output = prefix | 492 output = prefix |
| 485 line_length = 0 | 493 line_length = 0 |
| 486 for c in text: | 494 for c in text: |
| 487 if c == "\"": | 495 if c == "\"": |
| 488 c = "\\\"" | 496 c = "\\\"" |
| 489 elif c == "\n": | 497 elif c == "\n": |
| 490 c = "\\n" | 498 c = "\\n" |
| 491 elif c == "\\": | 499 elif c == "\\": |
| 492 c = "\\\\" | 500 c = "\\\\" |
| 493 if line_length + len(c) > max_length: | 501 if line_length + len(c) > max_length: |
| 494 output += suffix + "\n" + prefix | 502 output += suffix + "\n" + prefix |
| 495 line_length = 0 | 503 line_length = 0 |
| 496 output += c | 504 output += c |
| 497 line_length += len(c) | 505 line_length += len(c) |
| 498 output += suffix | 506 output += suffix |
| 499 return output | 507 return output |
| 500 | 508 |
| 501 | 509 |
| 502 def main(): | 510 def main(): |
| 503 if len(sys.argv) != 4: | 511 parser = optparse.OptionParser(usage="bake_in_configs.py [options]") |
| 504 print >> sys.stderr, (('Usage: %s <JSON pathname base directory> ' + | 512 parser.add_option("", "--output", metavar="FILE", |
| 505 '<input .gypi file> <output .cpp file>') % | 513 help="[Required] Name of the .cc file to write.") |
| 506 sys.argv[0]) | 514 |
| 507 print >> sys.stderr, sys.modules[__name__].__doc__ | 515 # For response file reading. |
| 516 parser.add_option("", "--file-list", metavar="FILE", | |
| 517 help="File containing whitespace separated names of " + | |
|
scottmg
2015/11/04 22:35:33
Don't need + at the end of these lines.
| |
| 518 "the baked in configs files.") | |
| 519 | |
| 520 # For .gypi file reading. | |
| 521 parser.add_option("", "--gypi-file", metavar="FILE", | |
| 522 help=".gypi file containing baked_in_configs variable.") | |
| 523 parser.add_option("", "--gypi-relative-to", metavar="PATH", | |
| 524 help="Directory the baked_in_configs in the --gypi-file" + | |
| 525 "are relative to.""") | |
| 526 | |
| 527 opts, args = parser.parse_args() | |
| 528 | |
| 529 if not opts.output: | |
| 530 print >> sys.stderr, "--output argument required" | |
| 508 return 1 | 531 return 1 |
| 509 json_path = sys.argv[1] | |
| 510 gypi_file = sys.argv[2] | |
| 511 cpp_file = sys.argv[3] | |
| 512 | 532 |
| 513 json_files = read_json_files_from_gypi(gypi_file) | 533 if opts.gypi_file: |
| 514 json_files = [ os.path.join(json_path, f) for f in json_files ] | 534 # .gypi-style input. |
| 515 json_files = [ os.path.normpath(f) for f in json_files ] | 535 if not opts.gypi_relative_to: |
| 536 print >> sys.stderr, "--gypi-relative-to is required with --gypi-file" | |
| 537 return 1 | |
| 538 | |
| 539 json_files = read_json_files_from_gypi(opts.gypi_file) | |
| 540 json_files = [ os.path.join(opts.gypi_relative_to, f) for f in json_files ] | |
| 541 json_files = [ os.path.normpath(f) for f in json_files ] | |
| 542 | |
|
scottmg
2015/11/04 22:35:33
Remove blank line.
| |
| 543 elif opts.file_list: | |
| 544 # Regular file list input. | |
| 545 json_files = read_json_files_from_file(opts.file_list) | |
| 546 | |
|
scottmg
2015/11/04 22:35:33
And here.
| |
| 547 else: | |
| 548 print >> sys.stderr, "Either --file-list or --gypi-file is required." | |
| 549 return 1 | |
| 516 | 550 |
| 517 cpp_code = CC_HEADER | 551 cpp_code = CC_HEADER |
| 518 found_invalid_config = False | 552 found_invalid_config = False |
| 519 | 553 |
| 520 for json_file in json_files: | 554 for json_file in json_files: |
| 521 with open(json_file, 'r') as f: | 555 with open(json_file, 'r') as f: |
| 522 json_text = f.read() | 556 json_text = f.read() |
| 523 try: | 557 try: |
| 524 config = json.loads(json_text) | 558 config = json.loads(json_text) |
| 525 except ValueError, e: | 559 except ValueError, e: |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 542 | 576 |
| 543 cpp_code += " // " + json_file + ":\n" | 577 cpp_code += " // " + json_file + ":\n" |
| 544 cpp_code += quote_and_wrap_text(dumped_json_text) + ",\n" | 578 cpp_code += quote_and_wrap_text(dumped_json_text) + ",\n" |
| 545 cpp_code += "\n" | 579 cpp_code += "\n" |
| 546 | 580 |
| 547 cpp_code += CC_FOOTER | 581 cpp_code += CC_FOOTER |
| 548 | 582 |
| 549 if found_invalid_config: | 583 if found_invalid_config: |
| 550 return 1 | 584 return 1 |
| 551 | 585 |
| 552 with open(cpp_file, 'wb') as f: | 586 with open(opts.output, 'wb') as f: |
| 553 f.write(cpp_code) | 587 f.write(cpp_code) |
| 554 | 588 |
| 555 return 0 | 589 return 0 |
| 556 | 590 |
| 557 | 591 |
| 558 if __name__ == '__main__': | 592 if __name__ == '__main__': |
| 559 sys.exit(main()) | 593 sys.exit(main()) |
| OLD | NEW |