Skip to content

Commit

Permalink
License header (apache#13178)
Browse files Browse the repository at this point in the history
* Minor fix to license_header documentation

* Handle UnicodeError when checking license
  • Loading branch information
larroy authored and Jose Luis Contreras committed Nov 13, 2018
1 parent 17c2590 commit 9c611e2
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions tools/license_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
- add the default license header to source files that do not contain a valid
license:
python license_header.py add
license_header.py add
- check if every files has a license header
python license_header.py check
license_header.py check
"""

import re
Expand Down Expand Up @@ -126,24 +126,27 @@ def _valid_file(fname, verbose=False):
def process_file(fname, action, verbose=True):
if not _valid_file(fname, verbose):
return True
with open(fname, 'r', encoding="utf-8") as f:
lines = f.readlines()
if not lines:
try:
with open(fname, 'r', encoding="utf-8") as f:
lines = f.readlines()
if not lines:
return True
if _has_license(lines):
return True
elif action == 'check':
return False
_, ext = os.path.splitext(fname)
with open(fname, 'w', encoding="utf-8") as f:
# shebang line
if lines[0].startswith('#!'):
f.write(lines[0].rstrip()+'\n\n')
del lines[0]
f.write(_get_license(_LANGS[ext]))
for l in lines:
f.write(l.rstrip()+'\n')
logging.info('added license header to ' + fname)
except UnicodeError:
return True
if _has_license(lines):
return True
elif action == 'check':
return False
_, ext = os.path.splitext(fname)
with open(fname, 'w', encoding="utf-8") as f:
# shebang line
if lines[0].startswith('#!'):
f.write(lines[0].rstrip()+'\n\n')
del lines[0]
f.write(_get_license(_LANGS[ext]))
for l in lines:
f.write(l.rstrip()+'\n')
logging.info('added license header to ' + fname)
return True

def process_folder(root, action):
Expand All @@ -155,7 +158,7 @@ def process_folder(root, action):
excepts.append(fname)
if action == 'check' and excepts:
logging.warning('The following files do not contain a valid license, '+
'you can use `python tools/license_header.py add [file]` to add'+
'you can use `tools/license_header.py add [file]` to add'+
'them automatically: ')
for x in excepts:
logging.warning(x)
Expand Down

0 comments on commit 9c611e2

Please sign in to comment.