28
28
parser .add_argument ("-e" , "--skip-exceptions" , help = "ignore hack/verify-flags/exceptions.txt and print all output" , action = "store_true" )
29
29
args = parser .parse_args ()
30
30
31
-
32
- dashRE = re .compile ('[-_]' )
33
-
34
31
# Cargo culted from http://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python
35
32
def is_binary (pathname ):
36
33
"""Return true if the given filename is binary.
@@ -108,14 +105,16 @@ def line_has_bad_flag(line, flagre):
108
105
# If running the golang files finds a new flag not in that file, return an
109
106
# error and tell the user to add the flag to the flag list.
110
107
def get_flags (rootdir , files ):
111
- # use a set for uniqueness
112
- flags = set ()
113
-
114
108
# preload the 'known' flags
115
109
pathname = os .path .join (rootdir , "hack/verify-flags/known-flags.txt" )
116
110
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 ())
119
118
f .close ()
120
119
121
120
regexs = [ re .compile ('Var[P]?\([^,]*, "([^"]*)"' ),
@@ -126,6 +125,7 @@ def get_flags(rootdir, files):
126
125
re .compile ('.StringSlice[P]?\("([^"]*)",[^,]+,[^)]+\)' ) ]
127
126
128
127
new_flags = set ()
128
+ new_excluded_flags = set ()
129
129
# walk all the files looking for any flags being declared
130
130
for pathname in files :
131
131
if not pathname .endswith (".go" ):
@@ -137,23 +137,33 @@ def get_flags(rootdir, files):
137
137
for regex in regexs :
138
138
matches = matches + regex .findall (data )
139
139
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 :
142
145
continue
143
146
if flag not in flags :
144
147
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 )
145
153
if len (new_flags ) != 0 :
146
154
print ("Found flags in golang files not in the list of known flags. Please add these to hack/verify-flags/known-flags.txt" )
147
155
print ("%s" % "\n " .join (new_flags ))
148
156
sys .exit (1 )
149
157
return list (flags )
150
158
151
159
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 ('[-_]' )
153
162
flagREs = []
154
163
for flag in flags :
155
164
# turn all flag names into regexs which will find both types
156
165
newre = dashRE .sub ('[-_]' , flag )
166
+ # only match if there is not a leading or trailing alphanumeric character
157
167
flagREs .append ("[^\w]" + newre + "[^\w]" )
158
168
# turn that list of regex strings into a single large RE
159
169
flagRE = "|" .join (flagREs )
0 commit comments