Skip to content

Commit

Permalink
Improve license checker line ending validation
Browse files Browse the repository at this point in the history
The license checker previously assumed that the
lines of the license will always end with \n
characters. However when checking a file
with \r\n line endings (should only happen for test files)
the checker can incorrectly report invalid license
as the line endings are incorrect.

Additional note #1: in Python when reading a file
in text mode it can happen that the line endings are
converted to the host system's line ending.
That is the `\r\n` will be converted to `\n` on Linux.
To resolve this problem the io module to disable
the line ending conversion.

Additional note #2: it is possible that there
are input test files which are not utf-8 conformant
(eg.: to test the parser). These files can't be read
as utf-8 strings and an exception would occur.
By ignoring these errors the tool can check
the file's license. In the license text there is no
invalid utf-8 character so the check will work
correctly.

JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
  • Loading branch information
galpeter committed Jun 8, 2018
1 parent be59d0a commit 4dad260
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions tools/check-license.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@

from __future__ import print_function

import io
import os
import re
import sys
import settings

LICENSE = re.compile(
r'((#|//|\*) Copyright .*\n'
r')+\s?\2\n'
r'\s?\2 Licensed under the Apache License, Version 2.0 \(the "License"\);\n'
r'\s?\2 you may not use this file except in compliance with the License.\n'
r'\s?\2 You may obtain a copy of the License at\n'
r'\s?\2\n'
r'\s?\2 http://www.apache.org/licenses/LICENSE-2.0\n'
r'\s?\2\n'
r'\s?\2 Unless required by applicable law or agreed to in writing, software\n'
r'\s?\2 distributed under the License is distributed on an "AS IS" BASIS\n'
r'\s?\2 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
r'\s?\2 See the License for the specific language governing permissions and\n'
r'\s?\2 limitations under the License.\n'
r'((#|//|\*) Copyright .*(\r\n|\n|\r)'
r')+\s?\2\3'
r'\s?\2 Licensed under the Apache License, Version 2.0 \(the "License"\);\3'
r'\s?\2 you may not use this file except in compliance with the License.\3'
r'\s?\2 You may obtain a copy of the License at\3'
r'\s?\2\3'
r'\s?\2 http://www.apache.org/licenses/LICENSE-2.0\3'
r'\s?\2\3'
r'\s?\2 Unless required by applicable law or agreed to in writing, software\3'
r'\s?\2 distributed under the License is distributed on an "AS IS" BASIS\3'
r'\s?\2 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\3'
r'\s?\2 See the License for the specific language governing permissions and\3'
r'\s?\2 limitations under the License.\3'
)

INCLUDE_DIRS = [
Expand Down Expand Up @@ -78,7 +79,7 @@ def main():
for fname in files:
if any(fname.endswith(ext) for ext in EXTENSIONS):
fpath = os.path.join(root, fname)
with open(fpath) as curr_file:
with io.open(fpath, 'r', errors='ignore', newline='') as curr_file:
if not LICENSE.search(curr_file.read()):
print('%s: incorrect license' % fpath)
is_ok = False
Expand Down

0 comments on commit 4dad260

Please sign in to comment.