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 |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 """ | 468 """ |
469 | 469 |
470 | 470 |
471 def read_json_files_from_gypi(gypi_file): | 471 def read_json_files_from_gypi(gypi_file): |
472 with open(gypi_file, 'r') as f: | 472 with open(gypi_file, 'r') as f: |
473 gypi_text = f.read() | 473 gypi_text = f.read() |
474 json_files = ast.literal_eval(gypi_text)['variables']['baked_in_configs'] | 474 json_files = ast.literal_eval(gypi_text)['variables']['baked_in_configs'] |
475 return json_files | 475 return json_files |
476 | 476 |
477 | 477 |
478 def domain_is_whitelisted(domain): | 478 def origin_is_whitelisted(origin): |
479 if origin.startswith('https://') and origin.endswith('/'): | |
480 domain = origin[8:-1] | |
481 else: | |
482 return False | |
479 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST) | 483 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST) |
480 | 484 |
481 | 485 |
482 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'): | 486 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'): |
483 max_length = width - len(prefix) - len(suffix) | 487 max_length = width - len(prefix) - len(suffix) |
484 output = prefix | 488 output = prefix |
485 line_length = 0 | 489 line_length = 0 |
486 for c in text: | 490 for c in text: |
487 if c == "\"": | 491 if c == "\"": |
488 c = "\\\"" | 492 c = "\\\"" |
(...skipping 30 matching lines...) Expand all Loading... | |
519 | 523 |
520 for json_file in json_files: | 524 for json_file in json_files: |
521 with open(json_file, 'r') as f: | 525 with open(json_file, 'r') as f: |
522 json_text = f.read() | 526 json_text = f.read() |
523 try: | 527 try: |
524 config = json.loads(json_text) | 528 config = json.loads(json_text) |
525 except ValueError, e: | 529 except ValueError, e: |
526 print >> sys.stderr, "%s: error parsing JSON: %s" % (json_file, e) | 530 print >> sys.stderr, "%s: error parsing JSON: %s" % (json_file, e) |
527 found_invalid_config = True | 531 found_invalid_config = True |
528 continue | 532 continue |
529 if 'monitored_domain' not in config: | 533 if 'origin' not in config: |
Randy Smith (Not in Mondays)
2015/10/29 22:36:10
Just confirming that all the .json files being par
Deprecated (see juliatuttle)
2015/11/02 23:19:30
Yes. We're not accepting *anything* from the web y
| |
530 print >> sys.stderr, '%s: no monitored_domain found' % json_file | 534 print >> sys.stderr, '%s: no origin found' % json_file |
531 found_invalid_config = True | 535 found_invalid_config = True |
532 continue | 536 continue |
533 domain = config['monitored_domain'] | 537 origin = config['origin'] |
534 if not domain_is_whitelisted(domain): | 538 if not origin_is_whitelisted(origin): |
535 print >> sys.stderr, ('%s: monitored_domain "%s" not in whitelist' % | 539 print >> sys.stderr, ('%s: origin "%s" not in whitelist' % |
536 (json_file, domain)) | 540 (json_file, origin)) |
537 found_invalid_config = True | 541 found_invalid_config = True |
538 continue | 542 continue |
539 | 543 |
540 # Re-dump JSON to get a more compact representation. | 544 # Re-dump JSON to get a more compact representation. |
541 dumped_json_text = json.dumps(config, separators=(',', ':')) | 545 dumped_json_text = json.dumps(config, separators=(',', ':')) |
542 | 546 |
543 cpp_code += " // " + json_file + ":\n" | 547 cpp_code += " // " + json_file + ":\n" |
544 cpp_code += quote_and_wrap_text(dumped_json_text) + ",\n" | 548 cpp_code += quote_and_wrap_text(dumped_json_text) + ",\n" |
545 cpp_code += "\n" | 549 cpp_code += "\n" |
546 | 550 |
547 cpp_code += CC_FOOTER | 551 cpp_code += CC_FOOTER |
548 | 552 |
549 if found_invalid_config: | 553 if found_invalid_config: |
550 return 1 | 554 return 1 |
551 | 555 |
552 with open(cpp_file, 'wb') as f: | 556 with open(cpp_file, 'wb') as f: |
553 f.write(cpp_code) | 557 f.write(cpp_code) |
554 | 558 |
555 return 0 | 559 return 0 |
556 | 560 |
557 | 561 |
558 if __name__ == '__main__': | 562 if __name__ == '__main__': |
559 sys.exit(main()) | 563 sys.exit(main()) |
OLD | NEW |