From b9e7adaa828ffc8c38fb37af2e086b01c88a9373 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Wed, 27 Jan 2010 23:12:39 +0000 Subject: [PATCH] Add presubmit_canned_checks.CheckLicense() It does a regexp search on the file contents to make sure it contains expected license boiler plate. It is a generalization to implement the equivalent of ubuntu's license check script into the corresponding PRESUBMIT.py files. TEST=unit tests BUG=28291 Review URL: http://codereview.chromium.org/558007 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@37338 0039d316-1c4b-4281-b951-d872f2087c98 --- presubmit_canned_checks.py | 19 ++++++++++++ tests/presubmit_unittest.py | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 3a5749007..577dfc995 100755 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -267,6 +267,25 @@ def CheckLongLines(input_api, output_api, maxlen=80, source_file_filter=None): return [] +def CheckLicense(input_api, output_api, license, source_file_filter=None): + """Verifies the license header. + """ + license_re = input_api.re.compile(license, input_api.re.MULTILINE) + bad_files = [] + for f in input_api.AffectedSourceFiles(source_file_filter): + contents = input_api.ReadFile(f, 'rb') + if not license_re.search(contents): + bad_files.append(f.LocalPath()) + if bad_files: + if input_api.is_committing: + res_type = output_api.PresubmitPromptWarning + else: + res_type = output_api.PresubmitNotifyResult + return [res_type( + "Found a bad license header in these files:", items=bad_files)] + return [] + + def CheckChangeSvnEolStyle(input_api, output_api, source_file_filter=None): """Checks that the source files have svn:eol-style=LF.""" return CheckSvnProperty(input_api, output_api, diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index c11283e37..3ed655fad 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -1069,6 +1069,7 @@ class CannedChecksUnittest(PresubmitTestsBase): 'CheckChangeHasTestField', 'CheckChangeLintsClean', 'CheckChangeSvnEolStyle', + 'CheckLicense', 'CheckSvnModifiedDirectories', 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', 'CheckDoNotSubmit', @@ -1282,6 +1283,66 @@ class CannedChecksUnittest(PresubmitTestsBase): 'svn:eol-style', 'LF', '', False, presubmit.OutputApi.PresubmitNotifyResult, True) + def _LicenseCheck(self, text, license, committing, expected_result): + change = self.mox.CreateMock(presubmit.SvnChange) + change.scm = 'svn' + input_api = self.MockInputApi(change, committing) + affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) + input_api.AffectedSourceFiles(42).AndReturn([affected_file]) + input_api.ReadFile(affected_file, 'rb').AndReturn(text) + if expected_result: + affected_file.LocalPath().AndReturn('bleh') + + self.mox.ReplayAll() + result = presubmit_canned_checks.CheckLicense( + input_api, presubmit.OutputApi, license, 42) + if expected_result: + self.assertEqual(len(result), 1) + self.assertEqual(result[0].__class__, expected_result) + else: + self.assertEqual(result, []) + + def testCheckLicenseSuccess(self): + text = ( + "#!/bin/python\n" + "# Copyright (c) 2037 Nobody.\n" + "# All Rights Reserved.\n" + "print 'foo'\n" + ) + license = ( + r".*? Copyright \(c\) 2037 Nobody." "\n" + r".*? All Rights Reserved\." "\n" + ) + self._LicenseCheck(text, license, True, None) + + def testCheckLicenseFailCommit(self): + text = ( + "#!/bin/python\n" + "# Copyright (c) 2037 Nobody.\n" + "# All Rights Reserved.\n" + "print 'foo'\n" + ) + license = ( + r".*? Copyright \(c\) 0007 Nobody." "\n" + r".*? All Rights Reserved\." "\n" + ) + self._LicenseCheck(text, license, True, + presubmit.OutputApi.PresubmitPromptWarning) + + def testCheckLicenseFailUpload(self): + text = ( + "#!/bin/python\n" + "# Copyright (c) 2037 Nobody.\n" + "# All Rights Reserved.\n" + "print 'foo'\n" + ) + license = ( + r".*? Copyright \(c\) 0007 Nobody." "\n" + r".*? All Rights Reserved\." "\n" + ) + self._LicenseCheck(text, license, False, + presubmit.OutputApi.PresubmitNotifyResult) + def testCannedCheckSvnAccidentalSubmission(self): modified_dir_file = 'foo/' accidental_submssion_file = 'foo/bar.cc'