Commit b9e7adaa authored by maruel@chromium.org's avatar maruel@chromium.org

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
parent 3529954d
......@@ -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,
......
......@@ -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'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment