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 1282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1293 import checkstyle | 1293 import checkstyle |
1294 finally: | 1294 finally: |
1295 # Restore sys.path to what it was before. | 1295 # Restore sys.path to what it was before. |
1296 sys.path = original_sys_path | 1296 sys.path = original_sys_path |
1297 | 1297 |
1298 return checkstyle.RunCheckstyle( | 1298 return checkstyle.RunCheckstyle( |
1299 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml', | 1299 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml', |
1300 black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) | 1300 black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) |
1301 | 1301 |
1302 | 1302 |
1303 def _CheckAndroidCrLogUsage(input_api, output_api): | |
1304 """Checks that new logs using org.chromium.base.Log: | |
1305 - Are using 'TAG' as variable name for the tags (warn) | |
1306 - Are using the suggested name format for the tags: "cr.<PackageTag>" (warn) | |
1307 - Are using a tag that is shorter than 23 characters (error) | |
1308 """ | |
1309 cr_log_import_pattern = input_api.re.compile( | |
1310 r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE); | |
1311 # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` | |
1312 cr_log_pattern = input_api.re.compile(r'^\s*Log\.\w\((?P<tag>\"?\w+\"?)\,') | |
1313 log_decl_pattern = input_api.re.compile( | |
1314 r'^\s*private static final String TAG = "(?P<name>(.*)")', | |
1315 input_api.re.MULTILINE) | |
1316 log_name_pattern = input_api.re.compile(r'^cr[.\w]*') | |
1317 | |
1318 REF_MSG = ('See base/android/java/src/org/chromium/base/README_logging.md ' | |
1319 'or contact dgn@chromium.org for more info.') | |
1320 sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) | |
1321 tag_errors = [] | |
1322 tag_decl_errors = [] | |
1323 tag_length_errors = [] | |
1324 | |
1325 for f in input_api.AffectedSourceFiles(sources): | |
1326 file_content = input_api.ReadFile(f) | |
1327 has_modified_logs = False | |
1328 | |
1329 # Per line checks | |
1330 if cr_log_import_pattern.search(file_content): | |
1331 for line_num, line in f.ChangedContents(): | |
1332 | |
1333 # Check if the new line is doing some logging | |
1334 match = cr_log_pattern.search(line) | |
1335 if match: | |
1336 has_modified_logs = True | |
1337 | |
1338 # Make sure it uses "TAG" | |
1339 if not match.group('tag') == 'TAG': | |
1340 tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) | |
1341 | |
1342 # Per file checks | |
1343 if has_modified_logs: | |
1344 # Make sure the tag is using the "cr" prefix and is not too long | |
1345 match = log_decl_pattern.search(file_content) | |
1346 tag_name = match.group('name') if match else '' | |
1347 if not log_name_pattern.search(tag_name ): | |
1348 tag_decl_errors.append(f.LocalPath()) | |
1349 if len(tag_name) > 23: | |
1350 tag_length_errors.append(f.LocalPath()) | |
1351 | |
1352 results = [] | |
1353 if tag_decl_errors: | |
1354 results.append(output_api.PresubmitPromptWarning( | |
1355 'Please define your tags using the suggested format: .\n' | |
1356 '"private static final String TAG = "cr.<package tag>".\n' + REF_MSG, | |
1357 tag_decl_errors)) | |
1358 | |
1359 if tag_length_errors: | |
1360 results.append(output_api.PresubmitError( | |
1361 'The tag length is restricted by the system to be at most ' | |
1362 '23 characters.\n' + REF_MSG, | |
1363 tag_length_errors)) | |
1364 | |
1365 if tag_errors: | |
1366 results.append(output_api.PresubmitPromptWarning( | |
1367 'Please use a variable named "TAG" for your log tags.\n' + REF_MSG, | |
1368 tag_errors)) | |
1369 | |
1370 return results | |
1371 | |
1372 | |
1373 # TODO(dgn): refactor with _CheckAndroidCrLogUsage | |
1303 def _CheckNoNewUtilLogUsage(input_api, output_api): | 1374 def _CheckNoNewUtilLogUsage(input_api, output_api): |
dgn
2015/05/18 17:56:11
Did not touch at this one because downstream uses
| |
1304 """Checks that new logs are using org.chromium.base.Log.""" | 1375 """Checks that new logs are using org.chromium.base.Log.""" |
1305 | 1376 |
1306 chromium_log_import_pattern = input_api.re.compile( | 1377 chromium_log_import_pattern = input_api.re.compile( |
1307 r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE); | 1378 r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE); |
1308 log_pattern = input_api.re.compile(r'^\s*(android\.util\.)?Log\.\w') | 1379 log_pattern = input_api.re.compile(r'^\s*(android\.util\.)?Log\.\w') |
1309 sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) | 1380 sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) |
1310 | 1381 |
1311 errors = [] | 1382 errors = [] |
1312 | 1383 |
1313 for f in input_api.AffectedSourceFiles(sources): | 1384 for f in input_api.AffectedSourceFiles(sources): |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1445 for fpath in input_api.AffectedFiles(file_filter=file_filter): | 1516 for fpath in input_api.AffectedFiles(file_filter=file_filter): |
1446 for lnum, line in fpath.ChangedContents(): | 1517 for lnum, line in fpath.ChangedContents(): |
1447 for (deprecated, replacement) in _DEPRECATED_JS: | 1518 for (deprecated, replacement) in _DEPRECATED_JS: |
1448 if deprecated in line: | 1519 if deprecated in line: |
1449 results.append(output_api.PresubmitError( | 1520 results.append(output_api.PresubmitError( |
1450 "%s:%d: Use of deprecated JS %s, use %s instead" % | 1521 "%s:%d: Use of deprecated JS %s, use %s instead" % |
1451 (fpath.LocalPath(), lnum, deprecated, replacement))) | 1522 (fpath.LocalPath(), lnum, deprecated, replacement))) |
1452 return results | 1523 return results |
1453 | 1524 |
1454 | 1525 |
1526 def _AndroidSpecificChecks(input_api, output_api): | |
1527 """Groups checks that target android code.""" | |
1528 results = [] | |
1529 results.extend(_CheckNoNewUtilLogUsage(input_api, output_api)) | |
1530 results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) | |
1531 return results | |
1532 | |
1533 | |
1455 def _CommonChecks(input_api, output_api): | 1534 def _CommonChecks(input_api, output_api): |
1456 """Checks common to both upload and commit.""" | 1535 """Checks common to both upload and commit.""" |
1457 results = [] | 1536 results = [] |
1458 results.extend(input_api.canned_checks.PanProjectChecks( | 1537 results.extend(input_api.canned_checks.PanProjectChecks( |
1459 input_api, output_api, | 1538 input_api, output_api, |
1460 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS)) | 1539 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS)) |
1461 results.extend(_CheckAuthorizedAuthor(input_api, output_api)) | 1540 results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
1462 results.extend( | 1541 results.extend( |
1463 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) | 1542 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
1464 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) | 1543 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
(...skipping 25 matching lines...) Expand all Loading... | |
1490 results.extend(_CheckForAnonymousVariables(input_api, output_api)) | 1569 results.extend(_CheckForAnonymousVariables(input_api, output_api)) |
1491 results.extend(_CheckCygwinShell(input_api, output_api)) | 1570 results.extend(_CheckCygwinShell(input_api, output_api)) |
1492 results.extend(_CheckUserActionUpdate(input_api, output_api)) | 1571 results.extend(_CheckUserActionUpdate(input_api, output_api)) |
1493 results.extend(_CheckNoDeprecatedCSS(input_api, output_api)) | 1572 results.extend(_CheckNoDeprecatedCSS(input_api, output_api)) |
1494 results.extend(_CheckNoDeprecatedJS(input_api, output_api)) | 1573 results.extend(_CheckNoDeprecatedJS(input_api, output_api)) |
1495 results.extend(_CheckParseErrors(input_api, output_api)) | 1574 results.extend(_CheckParseErrors(input_api, output_api)) |
1496 results.extend(_CheckForIPCRules(input_api, output_api)) | 1575 results.extend(_CheckForIPCRules(input_api, output_api)) |
1497 results.extend(_CheckForCopyrightedCode(input_api, output_api)) | 1576 results.extend(_CheckForCopyrightedCode(input_api, output_api)) |
1498 results.extend(_CheckForWindowsLineEndings(input_api, output_api)) | 1577 results.extend(_CheckForWindowsLineEndings(input_api, output_api)) |
1499 results.extend(_CheckSingletonInHeaders(input_api, output_api)) | 1578 results.extend(_CheckSingletonInHeaders(input_api, output_api)) |
1500 results.extend(_CheckNoNewUtilLogUsage(input_api, output_api)) | 1579 results.extend(_AndroidSpecificChecks(input_api, output_api)) |
1501 | 1580 |
1502 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): | 1581 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): |
1503 results.extend(input_api.canned_checks.RunUnitTestsInDirectory( | 1582 results.extend(input_api.canned_checks.RunUnitTestsInDirectory( |
1504 input_api, output_api, | 1583 input_api, output_api, |
1505 input_api.PresubmitLocalPath(), | 1584 input_api.PresubmitLocalPath(), |
1506 whitelist=[r'^PRESUBMIT_test\.py$'])) | 1585 whitelist=[r'^PRESUBMIT_test\.py$'])) |
1507 return results | 1586 return results |
1508 | 1587 |
1509 | 1588 |
1510 def _CheckAuthorizedAuthor(input_api, output_api): | 1589 def _CheckAuthorizedAuthor(input_api, output_api): |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1803 # Explicitly iterate over copies of dicts since we mutate them. | 1882 # Explicitly iterate over copies of dicts since we mutate them. |
1804 for master in builders.keys(): | 1883 for master in builders.keys(): |
1805 for builder in builders[master].keys(): | 1884 for builder in builders[master].keys(): |
1806 # Do not trigger presubmit builders, since they're likely to fail | 1885 # Do not trigger presubmit builders, since they're likely to fail |
1807 # (e.g. OWNERS checks before finished code review), and we're | 1886 # (e.g. OWNERS checks before finished code review), and we're |
1808 # running local presubmit anyway. | 1887 # running local presubmit anyway. |
1809 if 'presubmit' in builder: | 1888 if 'presubmit' in builder: |
1810 builders[master].pop(builder) | 1889 builders[master].pop(builder) |
1811 | 1890 |
1812 return builders | 1891 return builders |
OLD | NEW |