Skip to content

Commit 965d5d8

Browse files
committed
Make boilerplate.py smarter about generated
Don't just grep for DO NOT EDIT - anchor it in something that looks like a comment and alone on a line. Also ignore __* dirs Prevent it from triggering on update-generated-swagger-docs (hack, but better than before)
1 parent 4d42fbc commit 965d5d8

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

hack/boilerplate/boilerplate.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ def get_refs():
6262

6363

6464
def is_generated_file(filename, data, regexs):
65-
for d in skipped_ungenerated_files:
66-
if d in filename:
67-
return False
68-
6965
p = regexs["generated"]
7066
return p.search(data)
7167

@@ -151,21 +147,15 @@ def file_extension(filename):
151147
return os.path.splitext(filename)[1].split(".")[-1].lower()
152148

153149

154-
skipped_dirs = ['third_party', '_gopath', '_output', '.git', 'cluster/env.sh',
155-
"vendor", "test/e2e/generated/bindata.go", "hack/boilerplate/test",
156-
"staging/src/k8s.io/kubectl/pkg/generated/bindata.go"]
157-
158-
# list all the files contain 'DO NOT EDIT', but are not generated
159-
skipped_ungenerated_files = [
160-
'hack/update-generated-swagger-docs.sh',
161-
'hack/boilerplate/boilerplate.py'
162-
]
150+
skipped_names = ['third_party', '_gopath', '_output', '.git', 'cluster/env.sh',
151+
"vendor", "test/e2e/generated/bindata.go", "hack/boilerplate/test",
152+
"staging/src/k8s.io/kubectl/pkg/generated/bindata.go"]
163153

164154

165155
def normalize_files(files):
166156
newfiles = []
167157
for pathname in files:
168-
if any(x in pathname for x in skipped_dirs):
158+
if any(x in pathname for x in skipped_names):
169159
continue
170160
newfiles.append(pathname)
171161
for i, pathname in enumerate(newfiles):
@@ -184,9 +174,13 @@ def get_files(extensions):
184174
# as we would prune these later in normalize_files(). But doing it
185175
# cuts down the amount of filesystem walking we do and cuts down
186176
# the size of the file list
187-
for d in skipped_dirs:
177+
for d in skipped_names:
188178
if d in dirs:
189179
dirs.remove(d)
180+
for d in dirs:
181+
# dirs that start with __ are ignored
182+
if re.match("^__", d):
183+
dirs.remove(d)
190184

191185
for name in walkfiles:
192186
pathname = os.path.join(root, name)
@@ -222,7 +216,7 @@ def get_regexs():
222216
# strip #!.* from scripts
223217
regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
224218
# Search for generated files
225-
regexs["generated"] = re.compile('DO NOT EDIT')
219+
regexs["generated"] = re.compile(r"^[/*#]+ +.* DO NOT EDIT\.$", re.MULTILINE)
226220
return regexs
227221

228222

hack/update-generated-swagger-docs.sh

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,21 @@ gen_types_swagger_doc() {
4141
{
4242
echo -e "$(cat hack/boilerplate/boilerplate.generatego.txt)\n"
4343
echo "package ${group_version##*/}"
44+
# Indenting here prevents the boilerplate checker from thinking this file
45+
# is generated - gofmt will fix the indents anyway.
4446
cat <<EOF
4547
46-
// This file contains a collection of methods that can be used from go-restful to
47-
// generate Swagger API documentation for its models. Please read this PR for more
48-
// information on the implementation: https://github.com/emicklei/go-restful/pull/215
49-
//
50-
// TODOs are ignored from the parser (e.g. TODO(andronat):... || TODO:...) if and only if
51-
// they are on one line! For multiple line or blocks that you want to ignore use ---.
52-
// Any context after a --- is ignored.
53-
//
54-
// Those methods can be generated by using hack/update-generated-swagger-docs.sh
55-
56-
// AUTO-GENERATED FUNCTIONS START HERE. DO NOT EDIT.
48+
// This file contains a collection of methods that can be used from go-restful to
49+
// generate Swagger API documentation for its models. Please read this PR for more
50+
// information on the implementation: https://github.com/emicklei/go-restful/pull/215
51+
//
52+
// TODOs are ignored from the parser (e.g. TODO(andronat):... || TODO:...) if and only if
53+
// they are on one line! For multiple line or blocks that you want to ignore use ---.
54+
// Any context after a --- is ignored.
55+
//
56+
// Those methods can be generated by using hack/update-generated-swagger-docs.sh
57+
58+
// AUTO-GENERATED FUNCTIONS START HERE. DO NOT EDIT.
5759
EOF
5860
} > "${TMPFILE}"
5961

0 commit comments

Comments
 (0)