Chromium Code Reviews| Index: PRESUBMIT.py |
| diff --git a/PRESUBMIT.py b/PRESUBMIT.py |
| index 860de560d5a4363c84a1d50642f08460bc459940..3d51cb57a29ce89d05e0616970803e3fc33bc780 100644 |
| --- a/PRESUBMIT.py |
| +++ b/PRESUBMIT.py |
| @@ -1300,6 +1300,77 @@ def _CheckJavaStyle(input_api, output_api): |
| black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) |
| +def _CheckAndroidCrLogUsage(input_api, output_api): |
| + """Checks that new logs using org.chromium.base.Log: |
| + - Are using 'TAG' as variable name for the tags (warn) |
| + - Are using the suggested name format for the tags: "cr.<PackageTag>" (warn) |
| + - Are using a tag that is shorter than 23 characters (error) |
| + """ |
| + cr_log_import_pattern = input_api.re.compile( |
| + r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE); |
| + # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
| + cr_log_pattern = input_api.re.compile(r'^\s*Log\.\w\((?P<tag>\"?\w+\"?)\,') |
| + log_decl_pattern = input_api.re.compile( |
| + r'^\s*private static final String TAG = "(?P<name>(.*)")', |
| + input_api.re.MULTILINE) |
| + log_name_pattern = input_api.re.compile(r'^cr[.\w]*') |
| + |
| + REF_MSG = ('See base/android/java/src/org/chromium/base/README_logging.md ' |
| + 'or contact dgn@chromium.org for more info.') |
| + sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) |
| + tag_errors = [] |
| + tag_decl_errors = [] |
| + tag_length_errors = [] |
| + |
| + for f in input_api.AffectedSourceFiles(sources): |
| + file_content = input_api.ReadFile(f) |
| + has_modified_logs = False |
| + |
| + # Per line checks |
| + if cr_log_import_pattern.search(file_content): |
| + for line_num, line in f.ChangedContents(): |
| + |
| + # Check if the new line is doing some logging |
| + match = cr_log_pattern.search(line) |
| + if match: |
| + has_modified_logs = True |
| + |
| + # Make sure it uses "TAG" |
| + if not match.group('tag') == 'TAG': |
| + tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| + |
| + # Per file checks |
| + if has_modified_logs: |
| + # Make sure the tag is using the "cr" prefix and is not too long |
| + match = log_decl_pattern.search(file_content) |
| + tag_name = match.group('name') if match else '' |
| + if not log_name_pattern.search(tag_name ): |
| + tag_decl_errors.append(f.LocalPath()) |
| + if len(tag_name) > 23: |
| + tag_length_errors.append(f.LocalPath()) |
| + |
| + results = [] |
| + if tag_decl_errors: |
| + results.append(output_api.PresubmitPromptWarning( |
| + 'Please define your tags using the suggested format: .\n' |
| + '"private static final String TAG = "cr.<package tag>".\n' + REF_MSG, |
| + tag_decl_errors)) |
| + |
| + if tag_length_errors: |
| + results.append(output_api.PresubmitError( |
| + 'The tag length is restricted by the system to be at most ' |
| + '23 characters.\n' + REF_MSG, |
| + tag_length_errors)) |
| + |
| + if tag_errors: |
| + results.append(output_api.PresubmitPromptWarning( |
| + 'Please use a variable named "TAG" for your log tags.\n' + REF_MSG, |
| + tag_errors)) |
| + |
| + return results |
| + |
| + |
| +# TODO(dgn): refactor with _CheckAndroidCrLogUsage |
| def _CheckNoNewUtilLogUsage(input_api, output_api): |
|
dgn
2015/05/18 17:56:11
Did not touch at this one because downstream uses
|
| """Checks that new logs are using org.chromium.base.Log.""" |
| @@ -1452,6 +1523,14 @@ def _CheckNoDeprecatedJS(input_api, output_api): |
| return results |
| +def _AndroidSpecificChecks(input_api, output_api): |
| + """Groups checks that target android code.""" |
| + results = [] |
| + results.extend(_CheckNoNewUtilLogUsage(input_api, output_api)) |
| + results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
| + return results |
| + |
| + |
| def _CommonChecks(input_api, output_api): |
| """Checks common to both upload and commit.""" |
| results = [] |
| @@ -1497,7 +1576,7 @@ def _CommonChecks(input_api, output_api): |
| results.extend(_CheckForCopyrightedCode(input_api, output_api)) |
| results.extend(_CheckForWindowsLineEndings(input_api, output_api)) |
| results.extend(_CheckSingletonInHeaders(input_api, output_api)) |
| - results.extend(_CheckNoNewUtilLogUsage(input_api, output_api)) |
| + results.extend(_AndroidSpecificChecks(input_api, output_api)) |
| if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): |
| results.extend(input_api.canned_checks.RunUnitTestsInDirectory( |