Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 56f6ad0

Browse files
committed
Specifically exclude some flag definitions in verify-flag-underscore.sh
We know there are some flags (declared with an _) which we wish to ignore. These flags are used by container definitions, e2e, etc. By explicitly ignoring those flags we can cut the amount of noise in the whitelist.
1 parent 30d34d0 commit 56f6ad0

File tree

4 files changed

+44
-126
lines changed

4 files changed

+44
-126
lines changed

hack/verify-flags-underscore.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
parser.add_argument("-e", "--skip-exceptions", help="ignore hack/verify-flags/exceptions.txt and print all output", action="store_true")
2929
args = parser.parse_args()
3030

31-
32-
dashRE = re.compile('[-_]')
33-
3431
# Cargo culted from http://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python
3532
def is_binary(pathname):
3633
"""Return true if the given filename is binary.
@@ -108,14 +105,16 @@ def line_has_bad_flag(line, flagre):
108105
# If running the golang files finds a new flag not in that file, return an
109106
# error and tell the user to add the flag to the flag list.
110107
def get_flags(rootdir, files):
111-
# use a set for uniqueness
112-
flags = set()
113-
114108
# preload the 'known' flags
115109
pathname = os.path.join(rootdir, "hack/verify-flags/known-flags.txt")
116110
f = open(pathname, 'r')
117-
for line in f.read().splitlines():
118-
flags.add(line)
111+
flags = set(f.read().splitlines())
112+
f.close()
113+
114+
# preload the 'known' flags which don't follow the - standard
115+
pathname = os.path.join(rootdir, "hack/verify-flags/excluded-flags.txt")
116+
f = open(pathname, 'r')
117+
excluded_flags = set(f.read().splitlines())
119118
f.close()
120119

121120
regexs = [ re.compile('Var[P]?\([^,]*, "([^"]*)"'),
@@ -126,6 +125,7 @@ def get_flags(rootdir, files):
126125
re.compile('.StringSlice[P]?\("([^"]*)",[^,]+,[^)]+\)') ]
127126

128127
new_flags = set()
128+
new_excluded_flags = set()
129129
# walk all the files looking for any flags being declared
130130
for pathname in files:
131131
if not pathname.endswith(".go"):
@@ -137,23 +137,33 @@ def get_flags(rootdir, files):
137137
for regex in regexs:
138138
matches = matches + regex.findall(data)
139139
for flag in matches:
140-
# if the flag doesn't have a - or _ it is not interesting
141-
if not dashRE.search(flag):
140+
if any(x in flag for x in excluded_flags):
141+
continue
142+
if "_" in flag:
143+
new_excluded_flags.add(flag)
144+
if not "-" in flag:
142145
continue
143146
if flag not in flags:
144147
new_flags.add(flag)
148+
if len(new_excluded_flags) != 0:
149+
print("Found a flag declared with an _ but which is not explicitly listed as a valid flag name in hack/verify-flags/excluded-flags.txt")
150+
print("Are you certain this flag should not have been declared with an - instead?")
151+
print("%s" % "\n".join(new_excluded_flags))
152+
sys.exit(1)
145153
if len(new_flags) != 0:
146154
print("Found flags in golang files not in the list of known flags. Please add these to hack/verify-flags/known-flags.txt")
147155
print("%s" % "\n".join(new_flags))
148156
sys.exit(1)
149157
return list(flags)
150158

151159
def flags_to_re(flags):
152-
"""turn the list of all flags we found into a regex find both - and _ version"""
160+
"""turn the list of all flags we found into a regex find both - and _ versions"""
161+
dashRE = re.compile('[-_]')
153162
flagREs = []
154163
for flag in flags:
155164
# turn all flag names into regexs which will find both types
156165
newre = dashRE.sub('[-_]', flag)
166+
# only match if there is not a leading or trailing alphanumeric character
157167
flagREs.append("[^\w]" + newre + "[^\w]")
158168
# turn that list of regex strings into a single large RE
159169
flagRE = "|".join(flagREs)

0 commit comments

Comments
 (0)