OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 # if pre-cached this is read-only, but writing creates a race condition. | 394 # if pre-cached this is read-only, but writing creates a race condition. |
395 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 395 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
396 keep_trailing_newline=True, # newline-terminate generated files | 396 keep_trailing_newline=True, # newline-terminate generated files |
397 lstrip_blocks=True, # so can indent control flow tags | 397 lstrip_blocks=True, # so can indent control flow tags |
398 trim_blocks=True) | 398 trim_blocks=True) |
399 jinja_env.filters.update({ | 399 jinja_env.filters.update({ |
400 'blink_capitalize': capitalize, | 400 'blink_capitalize': capitalize, |
401 'conditional': conditional_if_endif, | 401 'conditional': conditional_if_endif, |
402 'exposed': exposed_if, | 402 'exposed': exposed_if, |
403 'runtime_enabled': runtime_enabled_if, | 403 'runtime_enabled': runtime_enabled_if, |
| 404 'experiment_enabled': experiment_enabled_if, |
404 }) | 405 }) |
405 return jinja_env | 406 return jinja_env |
406 | 407 |
407 | 408 |
408 def generate_indented_conditional(code, conditional): | 409 def generate_indented_conditional(code, conditional): |
409 # Indent if statement to level of original code | 410 # Indent if statement to level of original code |
410 indent = re.match(' *', code).group(0) | 411 indent = re.match(' *', code).group(0) |
411 return ('%sif (%s) {\n' % (indent, conditional) + | 412 return ('%sif (%s) {\n' % (indent, conditional) + |
412 ' %s\n' % '\n '.join(code.splitlines()) + | 413 ' %s\n' % '\n '.join(code.splitlines()) + |
413 '%s}\n' % indent) | 414 '%s}\n' % indent) |
(...skipping 16 matching lines...) Expand all Loading... |
430 return generate_indented_conditional(code, 'executionContext && (%s)' % expo
sed_test) | 431 return generate_indented_conditional(code, 'executionContext && (%s)' % expo
sed_test) |
431 | 432 |
432 | 433 |
433 # [RuntimeEnabled] | 434 # [RuntimeEnabled] |
434 def runtime_enabled_if(code, runtime_enabled_function_name): | 435 def runtime_enabled_if(code, runtime_enabled_function_name): |
435 if not runtime_enabled_function_name: | 436 if not runtime_enabled_function_name: |
436 return code | 437 return code |
437 return generate_indented_conditional(code, '%s()' % runtime_enabled_function
_name) | 438 return generate_indented_conditional(code, '%s()' % runtime_enabled_function
_name) |
438 | 439 |
439 | 440 |
| 441 # [ExperimentEnabled] |
| 442 def experiment_enabled_if(code, experiment_name, error_message): |
| 443 if not experiment_name: |
| 444 return code |
| 445 return generate_indented_conditional(code, 'Experiments::isApiEnabled(curren
tExecutionContext(isolate), "%s", %s)' % (experiment_name, error_message)) |
| 446 |
440 ################################################################################ | 447 ################################################################################ |
441 | 448 |
442 def main(argv): | 449 def main(argv): |
443 # If file itself executed, cache templates | 450 # If file itself executed, cache templates |
444 try: | 451 try: |
445 cache_dir = argv[1] | 452 cache_dir = argv[1] |
446 dummy_filename = argv[2] | 453 dummy_filename = argv[2] |
447 except IndexError as err: | 454 except IndexError as err: |
448 print 'Usage: %s CACHE_DIR DUMMY_FILENAME' % argv[0] | 455 print 'Usage: %s CACHE_DIR DUMMY_FILENAME' % argv[0] |
449 return 1 | 456 return 1 |
450 | 457 |
451 # Cache templates | 458 # Cache templates |
452 jinja_env = initialize_jinja_env(cache_dir) | 459 jinja_env = initialize_jinja_env(cache_dir) |
453 template_filenames = [filename for filename in os.listdir(templates_dir) | 460 template_filenames = [filename for filename in os.listdir(templates_dir) |
454 # Skip .svn, directories, etc. | 461 # Skip .svn, directories, etc. |
455 if filename.endswith(('.cpp', '.h'))] | 462 if filename.endswith(('.cpp', '.h'))] |
456 for template_filename in template_filenames: | 463 for template_filename in template_filenames: |
457 jinja_env.get_template(template_filename) | 464 jinja_env.get_template(template_filename) |
458 | 465 |
459 # Create a dummy file as output for the build system, | 466 # Create a dummy file as output for the build system, |
460 # since filenames of individual cache files are unpredictable and opaque | 467 # since filenames of individual cache files are unpredictable and opaque |
461 # (they are hashes of the template path, which varies based on environment) | 468 # (they are hashes of the template path, which varies based on environment) |
462 with open(dummy_filename, 'w') as dummy_file: | 469 with open(dummy_filename, 'w') as dummy_file: |
463 pass # |open| creates or touches the file | 470 pass # |open| creates or touches the file |
464 | 471 |
465 | 472 |
466 if __name__ == '__main__': | 473 if __name__ == '__main__': |
467 sys.exit(main(sys.argv)) | 474 sys.exit(main(sys.argv)) |
OLD | NEW |