Factor out re-usable code from _check_license
This factors out the string parsing code from _check_license. This
enables us to easily add hooks for various licenses and other header
information which may apply to third_party projects that do not
follow the Chrome/Chromium OS convention.
Hooks which check for content in a file header will only need to do
two things:
1. Specify the content to find.
2. Specify a custom string to display in case of a mismatch.
BUG=none
TEST=tested by attempting to add a file with bad CrOS-style header
Change-Id: Ibf750863dfa8425ae8dd72a536e6e2e909df081c
Signed-off-by: David Hendricks <dhendrix@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/42594
Reviewed-by: David James <davidjames@chromium.org>
Reviewed-by: Doug Anderson <dianders@chromium.org>
diff --git a/pre-upload.py b/pre-upload.py
index 9b7bfd3..1ca755c 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -133,6 +133,34 @@
return filtered
+def _verify_header_content(commit, content, fail_msg):
+ """Verify that file headers contain specified content.
+
+ Args:
+ commit: the affected commit.
+ content: the content of the header to be verified.
+ fail_msg: the first message to display in case of failure.
+
+ Returns:
+ The return value of HookFailure().
+ """
+ license_re = re.compile(content, re.MULTILINE)
+ bad_files = []
+ files = _filter_files(_get_affected_files(commit),
+ COMMON_INCLUDED_PATHS,
+ COMMON_EXCLUDED_PATHS)
+
+ for f in files:
+ contents = open(f).read()
+ if len(contents) == 0: continue # Ignore empty files
+ if not license_re.search(contents):
+ bad_files.append(f)
+ if bad_files:
+ msg = "%s:\n%s\n%s" % (fail_msg, license_re.pattern,
+ "Found a bad header in these files:")
+ return HookFailure(msg, bad_files)
+
+
# Git Helpers
@@ -325,22 +353,9 @@
r".*? found in the LICENSE file\."
"\n"
)
+ FAIL_MSG = "License must match"
- license_re = re.compile(LICENSE_HEADER, re.MULTILINE)
- bad_files = []
- files = _filter_files(_get_affected_files(commit),
- COMMON_INCLUDED_PATHS,
- COMMON_EXCLUDED_PATHS)
-
- for f in files:
- contents = open(f).read()
- if len(contents) == 0: continue # Ignore empty files
- if not license_re.search(contents):
- bad_files.append(f)
- if bad_files:
- return HookFailure('License must match:\n%s\n' % license_re.pattern +
- 'Found a bad license header in these files:',
- bad_files)
+ return _verify_header_content(commit, LICENSE_HEADER, FAIL_MSG)
# Project-specific hooks