Skip to content

Commit

Permalink
hack/boilerplate/bolierplate.py: format python file according to PEP8…
Browse files Browse the repository at this point in the history
… guidelines
  • Loading branch information
keerthigd committed Sep 2, 2019
1 parent 06dc8cf commit 7ac394a
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions hack/boilerplate/boilerplate.py
Expand Up @@ -48,6 +48,7 @@

verbose_out = sys.stderr if args.verbose else open("/dev/null", "w")


def get_refs():
refs = {}

Expand All @@ -61,6 +62,7 @@ def get_refs():

return refs


def is_generated_file(filename, data, regexs):
for d in skipped_ungenerated_files:
if d in filename:
Expand All @@ -69,6 +71,7 @@ def is_generated_file(filename, data, regexs):
p = regexs["generated"]
return p.search(data)


def file_passes(filename, refs, regexs):
try:
f = open(filename, 'r')
Expand Down Expand Up @@ -119,9 +122,11 @@ def file_passes(filename, refs, regexs):
for d in data:
if p.search(d):
if generated:
print('File %s has the YEAR field, but it should not be in generated file' % filename, file=verbose_out)
print('File %s has the YEAR field, but it should not be in generated file' %
filename, file=verbose_out)
else:
print('File %s has the YEAR field, but missing the year of date' % filename, file=verbose_out)
print('File %s has the YEAR field, but missing the year of date' %
filename, file=verbose_out)
return False

if not generated:
Expand All @@ -134,7 +139,8 @@ def file_passes(filename, refs, regexs):

# if we don't match the reference at this point, fail
if ref != data:
print("Header in %s does not match reference, diff:" % filename, file=verbose_out)
print("Header in %s does not match reference, diff:" %
filename, file=verbose_out)
if args.verbose:
print(file=verbose_out)
for line in difflib.unified_diff(ref, data, 'reference', filename, lineterm=''):
Expand All @@ -144,15 +150,19 @@ def file_passes(filename, refs, regexs):

return True


def file_extension(filename):
return os.path.splitext(filename)[1].split(".")[-1].lower()


skipped_dirs = ['Godeps', 'third_party', '_gopath', '_output', '.git', 'cluster/env.sh',
"vendor", "test/e2e/generated/bindata.go", "hack/boilerplate/test",
"staging/src/k8s.io/kubectl/pkg/generated/bindata.go"]

# list all the files contain 'DO NOT EDIT', but are not generated
skipped_ungenerated_files = ['hack/lib/swagger.sh', 'hack/boilerplate/boilerplate.py']
skipped_ungenerated_files = [
'hack/lib/swagger.sh', 'hack/boilerplate/boilerplate.py']


def normalize_files(files):
newfiles = []
Expand All @@ -165,6 +175,7 @@ def normalize_files(files):
newfiles[i] = os.path.join(args.rootdir, pathname)
return newfiles


def get_files(extensions):
files = []
if len(args.filenames) > 0:
Expand Down Expand Up @@ -192,25 +203,29 @@ def get_files(extensions):
outfiles.append(pathname)
return outfiles


def get_dates():
years = datetime.datetime.now().year
return '(%s)' % '|'.join((str(year) for year in range(2014, years+1)))


def get_regexs():
regexs = {}
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
regexs["year"] = re.compile( 'YEAR' )
regexs["year"] = re.compile('YEAR')
# get_dates return 2014, 2015, 2016, 2017, or 2018 until the current year as a regex like: "(2014|2015|2016|2017|2018)";
# company holder names can be anything
regexs["date"] = re.compile(get_dates())
# strip // +build \n\n build constraints
regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE)
regexs["go_build_constraints"] = re.compile(
r"^(// \+build.*\n)+\n", re.MULTILINE)
# strip #!.* from scripts
regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
# Search for generated files
regexs["generated"] = re.compile( 'DO NOT EDIT' )
regexs["generated"] = re.compile('DO NOT EDIT')
return regexs


def main():
regexs = get_regexs()
refs = get_refs()
Expand All @@ -222,5 +237,6 @@ def main():

return 0


if __name__ == "__main__":
sys.exit(main())
sys.exit(main())

0 comments on commit 7ac394a

Please sign in to comment.