Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hack/boilerplate/bolierplate.py: format python file according to PEP8… #82240

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions hack/boilerplate/boilerplate.py
Original file line number Diff line number Diff line change
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())