microcode_tool: Convert to Python 3

Convert this tool to Python 3 and make it use that, to meet the 2020
deadline.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2019-10-31 07:42:56 -06:00
parent 3c19dc8b68
commit 879ca27656

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0+ # SPDX-License-Identifier: GPL-2.0+
# #
# Copyright (c) 2014 Google, Inc # Copyright (c) 2014 Google, Inc
@ -126,15 +126,15 @@ def List(date, microcodes, model):
microcodes: Dict of Microcode objects indexed by name microcodes: Dict of Microcode objects indexed by name
model: Model string to search for, or None model: Model string to search for, or None
""" """
print 'Date: %s' % date print('Date: %s' % date)
if model: if model:
mcode_list, tried = FindMicrocode(microcodes, model.lower()) mcode_list, tried = FindMicrocode(microcodes, model.lower())
print 'Matching models %s:' % (', '.join(tried)) print('Matching models %s:' % (', '.join(tried)))
else: else:
print 'All models:' print('All models:')
mcode_list = [microcodes[m] for m in microcodes.keys()] mcode_list = [microcodes[m] for m in list(microcodes.keys())]
for mcode in mcode_list: for mcode in mcode_list:
print '%-20s: model %s' % (mcode.name, mcode.model) print('%-20s: model %s' % (mcode.name, mcode.model))
def FindMicrocode(microcodes, model): def FindMicrocode(microcodes, model):
"""Find all the microcode chunks which match the given model. """Find all the microcode chunks which match the given model.
@ -164,7 +164,7 @@ def FindMicrocode(microcodes, model):
for i in range(3): for i in range(3):
abbrev = model[:-i] if i else model abbrev = model[:-i] if i else model
tried.append(abbrev) tried.append(abbrev)
for mcode in microcodes.values(): for mcode in list(microcodes.values()):
if mcode.model.startswith(abbrev): if mcode.model.startswith(abbrev):
found.append(mcode) found.append(mcode)
if found: if found:
@ -229,17 +229,17 @@ data = <%s
args += [mcode.words[i] for i in range(7)] args += [mcode.words[i] for i in range(7)]
args.append(words) args.append(words)
if outfile == '-': if outfile == '-':
print out % tuple(args) print(out % tuple(args))
else: else:
if not outfile: if not outfile:
if not os.path.exists(MICROCODE_DIR): if not os.path.exists(MICROCODE_DIR):
print >> sys.stderr, "Creating directory '%s'" % MICROCODE_DIR print("Creating directory '%s'" % MICROCODE_DIR, file=sys.stderr)
os.makedirs(MICROCODE_DIR) os.makedirs(MICROCODE_DIR)
outfile = os.path.join(MICROCODE_DIR, mcode.name + '.dtsi') outfile = os.path.join(MICROCODE_DIR, mcode.name + '.dtsi')
print >> sys.stderr, "Writing microcode for '%s' to '%s'" % ( print("Writing microcode for '%s' to '%s'" % (
', '.join([mcode.name for mcode in mcodes]), outfile) ', '.join([mcode.name for mcode in mcodes]), outfile), file=sys.stderr)
with open(outfile, 'w') as fd: with open(outfile, 'w') as fd:
print >> fd, out % tuple(args) print(out % tuple(args), file=fd)
def MicrocodeTool(): def MicrocodeTool():
"""Run the microcode tool""" """Run the microcode tool"""
@ -289,14 +289,14 @@ def MicrocodeTool():
if cmd == 'list': if cmd == 'list':
List(date, microcodes, options.model) List(date, microcodes, options.model)
elif cmd == 'license': elif cmd == 'license':
print '\n'.join(license_text) print('\n'.join(license_text))
elif cmd == 'create': elif cmd == 'create':
if not options.model: if not options.model:
parser.error('You must specify a model to create') parser.error('You must specify a model to create')
model = options.model.lower() model = options.model.lower()
if options.model == 'all': if options.model == 'all':
options.multiple = True options.multiple = True
mcode_list = microcodes.values() mcode_list = list(microcodes.values())
tried = [] tried = []
else: else:
mcode_list, tried = FindMicrocode(microcodes, model) mcode_list, tried = FindMicrocode(microcodes, model)