in hack/boilerplate/verify_boilerplate.py [0:0]
def file_passes(filename, refs, regexs): # pylint: disable=too-many-locals
try:
# Pass the encoding parameter to avoid ascii decode error for some
# platform.
with open(filename, 'r', encoding='utf-8') as fp:
data = fp.read()
except IOError:
return False
if not data:
return True # Nothing to copyright in this empty file.
basename = os.path.basename(filename)
extension = file_extension(filename)
if extension != "":
ref = refs[extension]
else:
ref = refs[basename]
# check for and skip generated files
if is_generated(data):
return True
# remove build tags from the top of Go files
if extension == "go":
con = regexs["go_build_constraints"]
(data, found) = con.subn("", data, 1)
# remove shebang from the top of shell files
if extension in ("sh", "py"):
she = regexs["shebang"]
(data, found) = she.subn("", data, 1)
data = data.splitlines()
# if our test file is smaller than the reference it surely fails!
if len(ref) > len(data):
return False
# trim our file to the same number of lines as the reference file
data = data[:len(ref)]
year = regexs["year"]
for datum in data:
if year.search(datum):
return False
# Replace all occurrences of the regex "2017|2016|2015|2014" with "YEAR"
when = regexs["date"]
for idx, datum in enumerate(data):
(data[idx], found) = when.subn('YEAR', datum)
if found != 0:
break
# if we don't match the reference at this point, fail
if ref != data:
return False
return True