2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-22 09:58:01 +00:00

build-aux/extract-ofp-errors: Fix flake8 and syntax errors.

A few general style issues like extra spacing and lines being too long,
unused variable 'error_types', passing more arguments than a format
string has.  And a few invalid escape sequences, which are not actual
escape sequences, but cause actual syntax warnings starting python 3.12
and will eventually become syntax errors [1]:

 extract-ofp-errors:244: SyntaxWarning: invalid escape sequence '\.'
  m = re.match('Expected: (.*)\.$', comment)
 extract-ofp-errors:249: SyntaxWarning: invalid escape sequence '\.'
  m = re.match('((?:.(?!\.  ))+.)\.\s+(.*)$', comment)
 extract-ofp-errors:256: SyntaxWarning: invalid escape sequence '\s'
  m = re.match('\s+(?:OFPERR_([A-Z0-9_]+))(\s*=\s*OFPERR_OFS)?,',
 extract-ofp-errors:265: SyntaxWarning: invalid escape sequence '\['
  comments.append(re.sub('\[[^]]*\]', '', comment))

These are fixed by converting to raw strings.

[1] https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences

Acked-by: Eelco Chaudron <echaudro@redhat.com>
Reviewed-By: Ihar Hrachyshka <ihar@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
This commit is contained in:
Ilya Maximets 2023-10-30 21:10:46 +01:00
parent 51fb992904
commit 6625f6f2f2

View File

@ -22,6 +22,9 @@ tokenRe = "#?" + idRe + "|[0-9]+|."
inComment = False inComment = False
inDirective = False inDirective = False
n_errors = 0
def open_file(fn): def open_file(fn):
global fileName global fileName
global inputFile global inputFile
@ -30,6 +33,7 @@ def open_file(fn):
inputFile = open(fileName) inputFile = open(fileName)
lineNumber = 0 lineNumber = 0
def tryGetLine(): def tryGetLine():
global inputFile global inputFile
global line global line
@ -38,10 +42,12 @@ def tryGetLine():
lineNumber += 1 lineNumber += 1
return line != "" return line != ""
def getLine(): def getLine():
if not tryGetLine(): if not tryGetLine():
fatal("unexpected end of input") fatal("unexpected end of input")
def getToken(): def getToken():
global token global token
global line global line
@ -82,37 +88,43 @@ def getToken():
line = line[:-2] + inputFile.readline() line = line[:-2] + inputFile.readline()
lineNumber += 1 lineNumber += 1
if line == "": if line == "":
if token == None: if token is None:
fatal("unexpected end of input") fatal("unexpected end of input")
token = None token = None
return False return False
n_errors = 0
def error(msg): def error(msg):
global n_errors global n_errors
sys.stderr.write("%s:%d: %s\n" % (fileName, lineNumber, msg)) sys.stderr.write("%s:%d: %s\n" % (fileName, lineNumber, msg))
n_errors += 1 n_errors += 1
def fatal(msg): def fatal(msg):
error(msg) error(msg)
sys.exit(1) sys.exit(1)
def skipDirective(): def skipDirective():
getToken() getToken()
while token != '$': while token != '$':
getToken() getToken()
def isId(s): def isId(s):
return re.match(idRe + "$", s) != None return re.match(idRe + "$", s) is not None
def forceId(): def forceId():
if not isId(token): if not isId(token):
fatal("identifier expected") fatal("identifier expected")
def forceInteger(): def forceInteger():
if not re.match('[0-9]+$', token): if not re.match(r'[0-9]+$', token):
fatal("integer expected") fatal("integer expected")
def match(t): def match(t):
if token == t: if token == t:
getToken() getToken()
@ -120,10 +132,12 @@ def match(t):
else: else:
return False return False
def forceMatch(t): def forceMatch(t):
if not match(t): if not match(t):
fatal("%s expected" % t) fatal("%s expected" % t)
def parseTaggedName(): def parseTaggedName():
assert token in ('struct', 'union') assert token in ('struct', 'union')
name = token name = token
@ -133,26 +147,26 @@ def parseTaggedName():
getToken() getToken()
return name return name
def print_enum(tag, constants, storage_class): def print_enum(tag, constants, storage_class):
print (""" print("""
%(storage_class)sconst char * %(storage_class)sconst char *
%(tag)s_to_string(uint16_t value) %(tag)s_to_string(uint16_t value)
{ {
switch (value) {\ switch (value) {\
""" % {"tag": tag, """ % {"tag": tag,
"bufferlen": len(tag) + 32,
"storage_class": storage_class}) "storage_class": storage_class})
for constant in constants: for constant in constants:
print (" case %s: return \"%s\";" % (constant, constant)) print(" case %s: return \"%s\";" % (constant, constant))
print ("""\ print("""\
} }
return NULL; return NULL;
}\ }""")
""" % {"tag": tag})
def usage(): def usage():
argv0 = os.path.basename(sys.argv[0]) argv0 = os.path.basename(sys.argv[0])
print ('''\ print('''\
%(argv0)s, for extracting OpenFlow error codes from header files %(argv0)s, for extracting OpenFlow error codes from header files
usage: %(argv0)s ERROR_HEADER VENDOR_HEADER usage: %(argv0)s ERROR_HEADER VENDOR_HEADER
@ -167,6 +181,7 @@ The output is suitable for use as lib/ofp-errors.inc.\
''' % {"argv0": argv0}) ''' % {"argv0": argv0})
sys.exit(0) sys.exit(0)
def extract_vendor_ids(fn): def extract_vendor_ids(fn):
global vendor_map global vendor_map
vendor_map = {} vendor_map = {}
@ -174,7 +189,10 @@ def extract_vendor_ids(fn):
open_file(fn) open_file(fn)
while tryGetLine(): while tryGetLine():
m = re.match(r'#define\s+([A-Z0-9_]+)_VENDOR_ID\s+(0x[0-9a-fA-F]+|[0-9]+)', line) m = re.match(
r'#define\s+([A-Z0-9_]+)_VENDOR_ID\s+(0x[0-9a-fA-F]+|[0-9]+)',
line
)
if not m: if not m:
continue continue
@ -202,9 +220,8 @@ def extract_vendor_ids(fn):
% (id_, vendor_reverse_map[id_], name)) % (id_, vendor_reverse_map[id_], name))
vendor_reverse_map[id_] = name vendor_reverse_map[id_] = name
def extract_ofp_errors(fn):
error_types = {}
def extract_ofp_errors(fn):
comments = [] comments = []
names = [] names = []
domain = {} domain = {}
@ -220,14 +237,14 @@ def extract_ofp_errors(fn):
while True: while True:
getLine() getLine()
if re.match('enum ofperr', line): if re.match(r'enum ofperr', line):
break break
while True: while True:
getLine() getLine()
if line.startswith('/*') or not line or line.isspace(): if line.startswith('/*') or not line or line.isspace():
continue continue
elif re.match('}', line): elif re.match(r'}', line):
break break
if not line.lstrip().startswith('/*'): if not line.lstrip().startswith('/*'):
@ -241,19 +258,19 @@ def extract_ofp_errors(fn):
comment += ' %s' % line.lstrip('* \t').rstrip(' \t\r\n') comment += ' %s' % line.lstrip('* \t').rstrip(' \t\r\n')
comment = comment[:-2].rstrip() comment = comment[:-2].rstrip()
m = re.match('Expected: (.*)\.$', comment) m = re.match(r'Expected: (.*)\.$', comment)
if m: if m:
expected_errors[m.group(1)] = (fileName, lineNumber) expected_errors[m.group(1)] = (fileName, lineNumber)
continue continue
m = re.match('((?:.(?!\. ))+.)\.\s+(.*)$', comment) m = re.match(r'((?:.(?!\. ))+.)\.\s+(.*)$', comment)
if not m: if not m:
fatal("unexpected syntax between errors") fatal("unexpected syntax between errors")
dsts, comment = m.groups() dsts, comment = m.groups()
getLine() getLine()
m = re.match('\s+(?:OFPERR_([A-Z0-9_]+))(\s*=\s*OFPERR_OFS)?,', m = re.match(r'\s+(?:OFPERR_([A-Z0-9_]+))(\s*=\s*OFPERR_OFS)?,',
line) line)
if not m: if not m:
fatal("syntax error expecting OFPERR_ enum value") fatal("syntax error expecting OFPERR_ enum value")
@ -262,11 +279,14 @@ def extract_ofp_errors(fn):
if enum in names: if enum in names:
fatal("%s specified twice" % enum) fatal("%s specified twice" % enum)
comments.append(re.sub('\[[^]]*\]', '', comment)) comments.append(re.sub(r'\[[^]]*\]', '', comment))
names.append(enum) names.append(enum)
for dst in dsts.split(', '): for dst in dsts.split(', '):
m = re.match(r'([A-Z]+)([0-9.]+)(\+|-[0-9.]+)?\((\d+)(?:,(\d+))?\)$', dst) m = re.match(
r'([A-Z]+)([0-9.]+)(\+|-[0-9.]+)?\((\d+)(?:,(\d+))?\)$',
dst
)
if not m: if not m:
fatal("%r: syntax error in destination" % dst) fatal("%r: syntax error in destination" % dst)
vendor_name = m.group(1) vendor_name = m.group(1)
@ -313,8 +333,7 @@ def extract_ofp_errors(fn):
# mechanism that includes a type but not a code. # mechanism that includes a type but not a code.
if v1 < version_map['1.2'] or v2 < version_map['1.2']: if v1 < version_map['1.2'] or v2 < version_map['1.2']:
if code is None: if code is None:
fatal("%s: NX1.0 and NX1.1 domains require code" fatal("%s: NX1.0 and NX1.1 domains require code" % dst)
% (dst, vendor_name))
if v1 >= version_map['1.2'] or v2 >= version_map['1.2']: if v1 >= version_map['1.2'] or v2 >= version_map['1.2']:
if code is not None: if code is not None:
fatal("%s: NX1.2+ domains do not have codes" % dst) fatal("%s: NX1.2+ domains do not have codes" % dst)
@ -340,11 +359,13 @@ def extract_ofp_errors(fn):
del expected_errors[msg] del expected_errors[msg]
else: else:
error("%s: %s." % (dst, msg)) error("%s: %s." % (dst, msg))
sys.stderr.write("%s:%d: %s: Here is the location " sys.stderr.write(
"of the previous definition.\n" "%s:%d: %s: Here is the location "
% (domain[version][vendor][type_][code][1], "of the previous definition.\n"
domain[version][vendor][type_][code][2], % (domain[version][vendor][type_][code][1],
dst)) domain[version][vendor][type_][code][2],
dst)
)
else: else:
domain[version][vendor][type_][code] = (enum, fileName, domain[version][vendor][type_][code] = (enum, fileName,
lineNumber) lineNumber)
@ -361,7 +382,7 @@ def extract_ofp_errors(fn):
if n_errors: if n_errors:
sys.exit(1) sys.exit(1)
print ("""\ print("""\
/* Generated automatically; do not modify! -*- buffer-read-only: t -*- */ /* Generated automatically; do not modify! -*- buffer-read-only: t -*- */
#define OFPERR_N_ERRORS %d #define OFPERR_N_ERRORS %d
@ -386,7 +407,7 @@ static const char *error_comments[OFPERR_N_ERRORS] = {
for comment in comments))) for comment in comments)))
def output_domain(map, name, description, version): def output_domain(map, name, description, version):
print (""" print("""
static enum ofperr static enum ofperr
%s_decode(uint32_t vendor, uint16_t type, uint16_t code) %s_decode(uint32_t vendor, uint16_t type, uint16_t code)
{ {
@ -405,16 +426,16 @@ static enum ofperr
vendor_s = "(%#xULL << 32) | " % vendor vendor_s = "(%#xULL << 32) | " % vendor
else: else:
vendor_s = "" vendor_s = ""
print (" case %s ((uint32_t) %d << 16) | %d:" % (vendor_s, print(" case %s ((uint32_t) %d << 16) | %d:" % (vendor_s,
type_, code)) type_, code))
print (" return OFPERR_%s;" % enum) print(" return OFPERR_%s;" % enum)
print ("""\ print("""\
} }
return 0; return 0;
}""") }""")
print (""" print("""
static const struct ofperr_domain %s = { static const struct ofperr_domain %s = {
"%s", "%s",
%d, %d,
@ -423,20 +444,22 @@ static const struct ofperr_domain %s = {
for enum in names: for enum in names:
if enum in map: if enum in map:
vendor, type_, code = map[enum] vendor, type_, code = map[enum]
if code == None: if code is None:
code = -1 code = -1
print (" { %#8x, %2d, %3d }, /* %s */" % (vendor, type_, code, enum)) print(" { %#8x, %2d, %3d }, /* %s */" % (vendor, type_,
code, enum))
else: else:
print (" { -1, -1, -1 }, /* %s */" % enum) print(" { -1, -1, -1 }, /* %s */" % enum)
print ("""\ print("""\
}, },
};""") };""")
for version_name, id_ in version_map.items(): for version_name, id_ in version_map.items():
var = 'ofperr_of' + re.sub('[^A-Za-z0-9_]', '', version_name) var = 'ofperr_of' + re.sub(r'[^A-Za-z0-9_]', '', version_name)
description = "OpenFlow %s" % version_name description = "OpenFlow %s" % version_name
output_domain(reverse[id_], var, description, id_) output_domain(reverse[id_], var, description, id_)
if __name__ == '__main__': if __name__ == '__main__':
if '--help' in sys.argv: if '--help' in sys.argv:
usage() usage()