OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Top-level presubmit script for Chromium. | 5 """Top-level presubmit script for Chromium. |
6 | 6 |
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
8 for more details about the presubmit API built into depot_tools. | 8 for more details about the presubmit API built into depot_tools. |
9 """ | 9 """ |
10 | 10 |
(...skipping 1293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1304 black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) | 1304 black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) |
1305 | 1305 |
1306 | 1306 |
1307 def _CheckAndroidCrLogUsage(input_api, output_api): | 1307 def _CheckAndroidCrLogUsage(input_api, output_api): |
1308 """Checks that new logs using org.chromium.base.Log: | 1308 """Checks that new logs using org.chromium.base.Log: |
1309 - Are using 'TAG' as variable name for the tags (warn) | 1309 - Are using 'TAG' as variable name for the tags (warn) |
1310 - Are using the suggested name format for the tags: "cr.<PackageTag>" (warn) | 1310 - Are using the suggested name format for the tags: "cr.<PackageTag>" (warn) |
1311 - Are using a tag that is shorter than 23 characters (error) | 1311 - Are using a tag that is shorter than 23 characters (error) |
1312 """ | 1312 """ |
1313 cr_log_import_pattern = input_api.re.compile( | 1313 cr_log_import_pattern = input_api.re.compile( |
1314 r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE); | 1314 r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE) |
1315 class_in_base_pattern = input_api.re.compile( | |
1316 r'^package org\.chromium\.base;$', input_api.re.MULTILINE) | |
1317 has_some_log_import_pattern = input_api.re.compile( | |
1318 r'^import .*\.Log;$', input_api.re.MULTILINE) | |
1315 # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` | 1319 # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
1316 cr_log_pattern = input_api.re.compile(r'^\s*Log\.\w\((?P<tag>\"?\w+\"?)\,') | 1320 log_call_pattern = input_api.re.compile(r'^\s*Log\.\w\((?P<tag>\"?\w+\"?)\,') |
1317 log_decl_pattern = input_api.re.compile( | 1321 log_decl_pattern = input_api.re.compile( |
1318 r'^\s*private static final String TAG = "(?P<name>(.*)")', | 1322 r'^\s*private static final String TAG = "(?P<name>(.*)")', |
1319 input_api.re.MULTILINE) | 1323 input_api.re.MULTILINE) |
1320 log_name_pattern = input_api.re.compile(r'^cr[.\w]*') | 1324 log_name_pattern = input_api.re.compile(r'^cr[.\w]*') |
1321 | 1325 |
1322 REF_MSG = ('See base/android/java/src/org/chromium/base/README_logging.md ' | 1326 REF_MSG = ('See base/android/java/src/org/chromium/base/README_logging.md ' |
1323 'or contact dgn@chromium.org for more info.') | 1327 'or contact dgn@chromium.org for more info.') |
1324 sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) | 1328 sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) |
1325 tag_errors = [] | 1329 |
1326 tag_decl_errors = [] | 1330 tag_decl_errors = [] |
1327 tag_length_errors = [] | 1331 tag_length_errors = [] |
1332 tag_errors = [] | |
1333 util_log_errors = [] | |
1328 | 1334 |
1329 for f in input_api.AffectedSourceFiles(sources): | 1335 for f in input_api.AffectedSourceFiles(sources): |
1330 file_content = input_api.ReadFile(f) | 1336 file_content = input_api.ReadFile(f) |
1331 has_modified_logs = False | 1337 has_modified_logs = False |
1332 | 1338 |
1333 # Per line checks | 1339 # Per line checks |
1334 if cr_log_import_pattern.search(file_content): | 1340 if (cr_log_import_pattern.search(file_content) or |
1341 (class_in_base_pattern.search(file_content) and | |
dgn
2015/06/11 15:33:58
How can I make it in one check?
I think the close
| |
1342 not has_some_log_import_pattern.search(file_content))): | |
1343 # Checks to run for files using cr log | |
1335 for line_num, line in f.ChangedContents(): | 1344 for line_num, line in f.ChangedContents(): |
1336 | 1345 |
1337 # Check if the new line is doing some logging | 1346 # Check if the new line is doing some logging |
1338 match = cr_log_pattern.search(line) | 1347 match = log_call_pattern.search(line) |
1339 if match: | 1348 if match: |
1340 has_modified_logs = True | 1349 has_modified_logs = True |
1341 | 1350 |
1342 # Make sure it uses "TAG" | 1351 # Make sure it uses "TAG" |
1343 if not match.group('tag') == 'TAG': | 1352 if not match.group('tag') == 'TAG': |
1344 tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) | 1353 tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
1354 else: | |
1355 # Report non cr Log function calls in changed lines | |
1356 for line_num, line in f.ChangedContents(): | |
1357 if log_call_pattern.search(line): | |
1358 util_log_errors.append("%s:%d" % (f.LocalPath(), line_num)) | |
1345 | 1359 |
1346 # Per file checks | 1360 # Per file checks |
1347 if has_modified_logs: | 1361 if has_modified_logs: |
1348 # Make sure the tag is using the "cr" prefix and is not too long | 1362 # Make sure the tag is using the "cr" prefix and is not too long |
1349 match = log_decl_pattern.search(file_content) | 1363 match = log_decl_pattern.search(file_content) |
1350 tag_name = match.group('name') if match else '' | 1364 tag_name = match.group('name') if match else '' |
1351 if not log_name_pattern.search(tag_name ): | 1365 if not log_name_pattern.search(tag_name ): |
1352 tag_decl_errors.append(f.LocalPath()) | 1366 tag_decl_errors.append(f.LocalPath()) |
1353 if len(tag_name) > 23: | 1367 if len(tag_name) > 23: |
1354 tag_length_errors.append(f.LocalPath()) | 1368 tag_length_errors.append(f.LocalPath()) |
1355 | 1369 |
1356 results = [] | 1370 results = [] |
1357 if tag_decl_errors: | 1371 if tag_decl_errors: |
1358 results.append(output_api.PresubmitPromptWarning( | 1372 results.append(output_api.PresubmitPromptWarning( |
1359 'Please define your tags using the suggested format: .\n' | 1373 'Please define your tags using the suggested format: .\n' |
1360 '"private static final String TAG = "cr.<package tag>".\n' + REF_MSG, | 1374 '"private static final String TAG = "cr.<package tag>".\n' + REF_MSG, |
1361 tag_decl_errors)) | 1375 tag_decl_errors)) |
1362 | 1376 |
1363 if tag_length_errors: | 1377 if tag_length_errors: |
1364 results.append(output_api.PresubmitError( | 1378 results.append(output_api.PresubmitError( |
1365 'The tag length is restricted by the system to be at most ' | 1379 'The tag length is restricted by the system to be at most ' |
1366 '23 characters.\n' + REF_MSG, | 1380 '23 characters.\n' + REF_MSG, |
1367 tag_length_errors)) | 1381 tag_length_errors)) |
1368 | 1382 |
1369 if tag_errors: | 1383 if tag_errors: |
1370 results.append(output_api.PresubmitPromptWarning( | 1384 results.append(output_api.PresubmitPromptWarning( |
1371 'Please use a variable named "TAG" for your log tags.\n' + REF_MSG, | 1385 'Please use a variable named "TAG" for your log tags.\n' + REF_MSG, |
1372 tag_errors)) | 1386 tag_errors)) |
1373 | 1387 |
1388 if util_log_errors: | |
1389 results.append(output_api.PresubmitPromptWarning( | |
1390 'Please use org.chromium.base.Log for new logs.\n' + REF_MSG, | |
1391 util_log_errors)) | |
1392 | |
1374 return results | 1393 return results |
1375 | 1394 |
1376 | 1395 |
1377 # TODO(dgn): refactor with _CheckAndroidCrLogUsage | |
1378 def _CheckNoNewUtilLogUsage(input_api, output_api): | |
1379 """Checks that new logs are using org.chromium.base.Log.""" | |
1380 | |
1381 chromium_log_import_pattern = input_api.re.compile( | |
1382 r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE); | |
1383 log_pattern = input_api.re.compile(r'^\s*(android\.util\.)?Log\.\w') | |
1384 sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) | |
1385 | |
1386 errors = [] | |
1387 | |
1388 for f in input_api.AffectedSourceFiles(sources): | |
1389 if chromium_log_import_pattern.search(input_api.ReadFile(f)) is not None: | |
1390 # Uses org.chromium.base.Log already | |
1391 continue | |
1392 | |
1393 for line_num, line in f.ChangedContents(): | |
1394 if log_pattern.search(line): | |
1395 errors.append("%s:%d" % (f.LocalPath(), line_num)) | |
1396 | |
1397 results = [] | |
1398 if len(errors): | |
1399 results.append(output_api.PresubmitPromptWarning( | |
1400 'Please use org.chromium.base.Log for new logs.\n' + | |
1401 'See base/android/java/src/org/chromium/base/README_logging.md ' + | |
1402 'or contact dgn@chromium.org for more info.', | |
1403 errors)) | |
1404 return results | |
1405 | |
1406 | |
1407 def _CheckForCopyrightedCode(input_api, output_api): | 1396 def _CheckForCopyrightedCode(input_api, output_api): |
1408 """Verifies that newly added code doesn't contain copyrighted material | 1397 """Verifies that newly added code doesn't contain copyrighted material |
1409 and is properly licensed under the standard Chromium license. | 1398 and is properly licensed under the standard Chromium license. |
1410 | 1399 |
1411 As there can be false positives, we maintain a whitelist file. This check | 1400 As there can be false positives, we maintain a whitelist file. This check |
1412 also verifies that the whitelist file is up to date. | 1401 also verifies that the whitelist file is up to date. |
1413 """ | 1402 """ |
1414 import sys | 1403 import sys |
1415 original_sys_path = sys.path | 1404 original_sys_path = sys.path |
1416 try: | 1405 try: |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1525 if deprecated in line: | 1514 if deprecated in line: |
1526 results.append(output_api.PresubmitError( | 1515 results.append(output_api.PresubmitError( |
1527 "%s:%d: Use of deprecated JS %s, use %s instead" % | 1516 "%s:%d: Use of deprecated JS %s, use %s instead" % |
1528 (fpath.LocalPath(), lnum, deprecated, replacement))) | 1517 (fpath.LocalPath(), lnum, deprecated, replacement))) |
1529 return results | 1518 return results |
1530 | 1519 |
1531 | 1520 |
1532 def _AndroidSpecificOnUploadChecks(input_api, output_api): | 1521 def _AndroidSpecificOnUploadChecks(input_api, output_api): |
1533 """Groups checks that target android code.""" | 1522 """Groups checks that target android code.""" |
1534 results = [] | 1523 results = [] |
1535 results.extend(_CheckNoNewUtilLogUsage(input_api, output_api)) | |
1536 results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) | 1524 results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
1537 return results | 1525 return results |
1538 | 1526 |
1539 | 1527 |
1540 def _CommonChecks(input_api, output_api): | 1528 def _CommonChecks(input_api, output_api): |
1541 """Checks common to both upload and commit.""" | 1529 """Checks common to both upload and commit.""" |
1542 results = [] | 1530 results = [] |
1543 results.extend(input_api.canned_checks.PanProjectChecks( | 1531 results.extend(input_api.canned_checks.PanProjectChecks( |
1544 input_api, output_api, | 1532 input_api, output_api, |
1545 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS)) | 1533 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS)) |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1889 # (e.g. OWNERS checks before finished code review), and we're | 1877 # (e.g. OWNERS checks before finished code review), and we're |
1890 # running local presubmit anyway. | 1878 # running local presubmit anyway. |
1891 if 'presubmit' in builder: | 1879 if 'presubmit' in builder: |
1892 masters[master].pop(builder) | 1880 masters[master].pop(builder) |
1893 else: | 1881 else: |
1894 # Convert testfilter format to the one expected by git-cl-try. | 1882 # Convert testfilter format to the one expected by git-cl-try. |
1895 testfilter = masters[master][builder].get('testfilter', 'defaulttests') | 1883 testfilter = masters[master][builder].get('testfilter', 'defaulttests') |
1896 masters[master][builder] = [testfilter] | 1884 masters[master][builder] = [testfilter] |
1897 | 1885 |
1898 return masters | 1886 return masters |
OLD | NEW |