| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/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 import argparse | 6 import argparse |
| 7 import json | 7 import json |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 import sys | 10 import sys |
| 11 | 11 |
| 12 _BASE_REGEX_STRING = '^\s*goog\.%s\(\s*[\'"](.+)[\'"]\s*\)' | 12 _BASE_REGEX_STRING = '^\s*goog\.%s\(\s*[\'"](.+)[\'"]\s*\)' |
| 13 require_regex = re.compile(_BASE_REGEX_STRING % 'require') | 13 require_regex = re.compile(_BASE_REGEX_STRING % 'require') |
| 14 provide_regex = re.compile(_BASE_REGEX_STRING % 'provide') | 14 provide_regex = re.compile(_BASE_REGEX_STRING % 'provide') |
| 15 | 15 |
| 16 base = os.path.join('third_party', |
| 17 'closure_library', |
| 18 'closure', |
| 19 'goog', |
| 20 'base.js') |
| 16 | 21 |
| 17 def ProcessFile(filename): | 22 def ProcessFile(filename): |
| 18 """Extracts provided and required namespaces. | 23 """Extracts provided and required namespaces. |
| 19 | 24 |
| 20 Description: | 25 Description: |
| 21 Scans Javascript file for provided and required namespaces. | 26 Scans Javascript file for provided and required namespaces. |
| 22 | 27 |
| 23 Args: | 28 Args: |
| 24 filename: name of the file to process. | 29 filename: name of the file to process. |
| 25 | 30 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 return | 92 return |
| 88 | 93 |
| 89 # Export requirements before file. | 94 # Export requirements before file. |
| 90 if source_filename in requirements: | 95 if source_filename in requirements: |
| 91 for namespace in requirements[source_filename]: | 96 for namespace in requirements[source_filename]: |
| 92 if namespace in providers: | 97 if namespace in providers: |
| 93 dependency = providers[namespace] | 98 dependency = providers[namespace] |
| 94 if dependency: | 99 if dependency: |
| 95 Export(target_file, dependency, providers, requirements, processed) | 100 Export(target_file, dependency, providers, requirements, processed) |
| 96 | 101 |
| 102 processed.add(source_filename) |
| 103 |
| 97 # Export file | 104 # Export file |
| 98 processed.add(source_filename) | |
| 99 for name in providers: | 105 for name in providers: |
| 100 if providers[name] == source_filename: | 106 if providers[name] == source_filename: |
| 101 target_file.write('// %s%s' % (name, os.linesep)) | 107 target_file.write('// %s%s' % (name, os.linesep)) |
| 102 source_file = open(source_filename, 'r') | 108 source_file = open(source_filename, 'r') |
| 103 try: | 109 try: |
| 104 comment_block = False | 110 comment_block = False |
| 105 for line in source_file: | 111 for line in source_file: |
| 106 # Skip require and provide statements. | 112 # Skip require statements. |
| 107 if (not re.match(require_regex, line) and not | 113 if (not re.match(require_regex, line)): |
| 108 re.match(provide_regex, line)): | |
| 109 formatted = line.rstrip() | 114 formatted = line.rstrip() |
| 110 if comment_block: | 115 if comment_block: |
| 111 # Scan for trailing */ in multi-line comment. | 116 # Scan for trailing */ in multi-line comment. |
| 112 index = formatted.find('*/') | 117 index = formatted.find('*/') |
| 113 if index >= 0: | 118 if index >= 0: |
| 114 formatted = formatted[index + 2:] | 119 formatted = formatted[index + 2:] |
| 115 comment_block = False | 120 comment_block = False |
| 116 else: | 121 else: |
| 117 formatted = '' | 122 formatted = '' |
| 118 # Remove // style comments. | 123 # Remove full-line // style comments. |
| 119 index = formatted.find('//') | 124 if formatted.lstrip().startswith('//'): |
| 120 if index >= 0: | 125 formatted = ''; |
| 121 formatted = formatted[:index] | |
| 122 # Remove /* */ style comments. | 126 # Remove /* */ style comments. |
| 123 start_comment = formatted.find('/*') | 127 start_comment = formatted.find('/*') |
| 124 end_comment = formatted.find('*/') | 128 end_comment = formatted.find('*/') |
| 125 while start_comment >= 0: | 129 while start_comment >= 0: |
| 126 if end_comment > start_comment: | 130 if end_comment > start_comment: |
| 127 formatted = (formatted[:start_comment] | 131 formatted = (formatted[:start_comment] |
| 128 + formatted[end_comment + 2:]) | 132 + formatted[end_comment + 2:]) |
| 129 start_comment = formatted.find('/*') | 133 start_comment = formatted.find('/*') |
| 130 end_comment = formatted.find('*/') | 134 end_comment = formatted.find('*/') |
| 131 else: | 135 else: |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 187 |
| 184 sources = ExtractSources(options) | 188 sources = ExtractSources(options) |
| 185 assert sources, 'Missing source files.' | 189 assert sources, 'Missing source files.' |
| 186 | 190 |
| 187 providers = {} | 191 providers = {} |
| 188 requirements = {} | 192 requirements = {} |
| 189 for file in sources: | 193 for file in sources: |
| 190 ExtractDependencies(file, providers, requirements) | 194 ExtractDependencies(file, providers, requirements) |
| 191 | 195 |
| 192 with open(options.target[0], 'w') as target_file: | 196 with open(options.target[0], 'w') as target_file: |
| 197 target_file.write('var CLOSURE_NO_DEPS=true;%s' % os.linesep) |
| 193 processed = set() | 198 processed = set() |
| 199 base_path = base |
| 200 if options.path: |
| 201 base_path = os.path.join(options.path, base_path) |
| 202 Export(target_file, base_path, providers, requirements, processed) |
| 194 for source_filename in sources: | 203 for source_filename in sources: |
| 195 Export(target_file, source_filename, providers, requirements, processed) | 204 Export(target_file, source_filename, providers, requirements, processed) |
| 196 | 205 |
| 197 if __name__ == '__main__': | 206 if __name__ == '__main__': |
| 198 main() | 207 main() |
| OLD | NEW |