util: Replace optparse with argparse

JIRA: https://gem5.atlassian.net/browse/GEM5-543

Change-Id: Id270ed29f14199f4f8eb6eb5739451a43d100484
Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/44512
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Giacomo Travaglini
2021-04-15 23:31:11 +01:00
parent 2f424f6a13
commit 8d5a8f01e4
8 changed files with 162 additions and 159 deletions

View File

@@ -39,7 +39,7 @@
# c. Dump a checkpoint and end the simulation
# d. Diff the new checkpoint with the original checkpoint N+1
#
# Note that '--' must be used to separate the script options from the
# Note that '--' must be used to separate the script args from the
# M5 command line.
#
# Caveats:
@@ -66,39 +66,40 @@
import os, sys, re
import subprocess
import optparse
import argparse
parser = optparse.OptionParser()
parser = argparse.ArgumentParser()
parser.add_option('-i', '--interval', type='int')
parser.add_option('-d', '--directory', default='checkpoint-test')
parser.add_argument('-i', '--interval', type=int)
parser.add_argument('-d', '--directory', default='checkpoint-test')
parser.add_argument('cmdline', nargs='+', help='gem5 command line')
(options, args) = parser.parse_args()
args = parser.parse_args()
interval = options.interval
interval = args.interval
if os.path.exists(options.directory):
print('Error: test directory', options.directory, 'exists')
if os.path.exists(args.directory):
print('Error: test directory', args.directory, 'exists')
print(' Tester needs to create directory from scratch')
sys.exit(1)
top_dir = options.directory
top_dir = args.directory
os.mkdir(top_dir)
cmd_echo = open(os.path.join(top_dir, 'command'), 'w')
print(' '.join(sys.argv), file=cmd_echo)
cmd_echo.close()
m5_binary = args[0]
m5_binary = args.cmdline[0]
options = args[1:]
args = args.cmdline[1:]
initial_args = ['--take-checkpoints', '%d,%d' % (interval, interval)]
cptdir = os.path.join(top_dir, 'm5out')
print('===> Running initial simulation.')
subprocess.call([m5_binary] + ['-red', cptdir] + options + initial_args)
subprocess.call([m5_binary] + ['-red', cptdir] + args + initial_args)
dirs = os.listdir(cptdir)
expr = re.compile('cpt\.([0-9]*)')
@@ -117,7 +118,7 @@ cpts.sort()
for i in range(1, len(cpts)):
print('===> Running test %d of %d.' % (i, len(cpts)-1))
mydir = os.path.join(top_dir, 'test.%d' % i)
subprocess.call([m5_binary] + ['-red', mydir] + options + initial_args +
subprocess.call([m5_binary] + ['-red', mydir] + args + initial_args +
['--max-checkpoints' , '1', '--checkpoint-dir', cptdir,
'--checkpoint-restore', str(i)])
cpt_name = 'cpt.%d' % cpts[i]

View File

@@ -143,7 +143,7 @@ def aggregate(output_dir, cpts, no_compress, memory_size):
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser("usage: %prog [options] <directory names which "\
parser = ArgumentParser(usage="%(prog)s [options] <directory names which "\
"hold the checkpoints to be combined>")
parser.add_argument("-o", "--output-dir", action="store",
help="Output directory")

View File

@@ -260,26 +260,31 @@ def process_file(path, **kwargs):
cpt.write(open(path, 'w'))
if __name__ == '__main__':
from optparse import OptionParser, SUPPRESS_HELP
parser = OptionParser("usage: %prog [options] <filename or directory>")
parser.add_option("-r", "--recurse", action="store_true",
help="Recurse through all subdirectories modifying "\
"each checkpoint that is found")
parser.add_option("-N", "--no-backup", action="store_false",
dest="backup", default=True,
help="Do no backup each checkpoint before modifying it")
parser.add_option("-v", "--verbose", action="store_true",
help="Print out debugging information as")
parser.add_option("--get-cc-file", action="store_true",
# used during build; generate src/sim/tags.cc and exit
help=SUPPRESS_HELP)
from argparse import ArgumentParser, SUPPRESS
parser = ArgumentParser(usage="%(prog)s [args] <filename or directory>")
parser.add_argument(
"-r", "--recurse", action="store_true",
help="Recurse through all subdirectories modifying "\
"each checkpoint that is found")
parser.add_argument(
"-N", "--no-backup", action="store_false",
dest="backup", default=True,
help="Do no backup each checkpoint before modifying it")
parser.add_argument(
"-v", "--verbose", action="store_true",
help="Print out debugging information as")
parser.add_argument(
"--get-cc-file", action="store_true",
# used during build; generate src/sim/tags.cc and exit
help=SUPPRESS)
parser.add_argument("checkpoint", nargs='?')
(options, args) = parser.parse_args()
verbose_print = options.verbose
args = parser.parse_args()
verbose_print = args.verbose
Upgrader.load_all()
if options.get_cc_file:
if args.get_cc_file:
print("// this file is auto-generated by util/cpt_upgrader.py")
print("#include <string>")
print("#include <set>")
@@ -289,30 +294,30 @@ if __name__ == '__main__':
print(" \"{}\",".format(tag))
print("};")
exit(0)
elif len(args) != 1:
parser.error("You must specify a checkpoint file to modify or a "\
elif not args.checkpoint:
parser.error("You must specify a checkpoint file to modify or a "
"directory of checkpoints to recursively update")
# Deal with shell variables and ~
path = osp.expandvars(osp.expanduser(args[0]))
path = osp.expandvars(osp.expanduser(args.checkpoint))
# Process a single file if we have it
if osp.isfile(path):
process_file(path, **vars(options))
process_file(path, **vars(args))
# Process an entire directory
elif osp.isdir(path):
cpt_file = osp.join(path, 'm5.cpt')
if options.recurse:
if args.recurse:
# Visit very file and see if it matches
for root,dirs,files in os.walk(path):
for name in files:
if name == 'm5.cpt':
process_file(osp.join(root,name), **vars(options))
process_file(osp.join(root,name), **vars(args))
for dir in dirs:
pass
# Maybe someone passed a cpt.XXXXXXX directory and not m5.cpt
elif osp.isfile(cpt_file):
process_file(cpt_file, **vars(options))
process_file(cpt_file, **vars(args))
else:
print("Error: checkpoint file not found in {} ".format(path))
print("and recurse not specified")

View File

@@ -42,7 +42,7 @@
# Script for managing a gem5 disk image.
#
from optparse import OptionParser
from argparse import ArgumentParser
import os
from os import environ as env
import string
@@ -221,8 +221,8 @@ commands = {}
commandOrder = []
class Command(object):
def addOption(self, *args, **kargs):
self.parser.add_option(*args, **kargs)
def addArgument(self, *args, **kargs):
self.parser.add_argument(*args, **kargs)
def __init__(self, name, description, posArgs):
self.name = name
@@ -231,19 +231,21 @@ class Command(object):
self.posArgs = posArgs
commands[self.name] = self
commandOrder.append(self.name)
usage = 'usage: %prog [options]'
usage = '%(prog)s [options]'
posUsage = ''
for posArg in posArgs:
(argName, argDesc) = posArg
usage += ' %s' % argName
posUsage += '\n %s: %s' % posArg
usage += posUsage
self.parser = OptionParser(usage=usage, description=description)
self.addOption('-d', '--debug', dest='debug', action='store_true',
help='Verbose output.')
self.parser = ArgumentParser(usage=usage, description=description)
self.addArgument('-d', '--debug', dest='debug', action='store_true',
help='Verbose output.')
self.addArgument('pos', nargs='*')
def parseArgs(self, argv):
(self.options, self.args) = self.parser.parse_args(argv[2:])
self.options = self.parser.parse_args(argv[2:])
self.args = self.options.pos
if len(self.args) != len(self.posArgs):
self.parser.error('Incorrect number of arguments')
global debug
@@ -261,9 +263,9 @@ class Command(object):
initCom = Command('init', 'Create an image with an empty file system.',
[('file', 'Name of the image file.'),
('mb', 'Size of the file in MB.')])
initCom.addOption('-t', '--type', dest='fstype', action='store',
default='ext2',
help='Type of file system to use. Appended to mkfs.')
initCom.addArgument('-t', '--type', dest='fstype', action='store',
default='ext2',
help='Type of file system to use. Appended to mkfs.')
# A command to mount the first partition in the image.
mountCom = Command('mount', 'Mount the first partition in the disk image.',
@@ -365,9 +367,9 @@ partitionCom.func = partitionComFunc
# A command to format the first partition in the image.
formatCom = Command('format', 'Formatting part of "init".',
[('file', 'Name of the image file.')])
formatCom.addOption('-t', '--type', dest='fstype', action='store',
default='ext2',
help='Type of file system to use. Appended to mkfs.')
formatCom.addArgument('-t', '--type', dest='fstype', action='store',
default='ext2',
help='Type of file system to use. Appended to mkfs.')
def formatImage(dev, fsType):
return runPriv([findProg('mkfs.%s' % fsType, dev), str(dev)])

View File

@@ -38,7 +38,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from optparse import OptionParser
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from subprocess import call
from platform import machine
from distutils import spawn
@@ -66,16 +66,16 @@ def run_cmd(explanation, working_dir, cmd, stdout = None):
def linux_clone():
kernel_vexpress_gem5_dir = os.path.join(
options.dest_dir, "linux-kernel-vexpress_gem5")
args.dest_dir, "linux-kernel-vexpress_gem5")
run_cmd("clone linux kernel for VExpress_GEM5_V1 platform",
options.dest_dir,
args.dest_dir,
["git", "clone", "https://gem5.googlesource.com/arm/linux",
kernel_vexpress_gem5_dir])
def linux64():
kernel_vexpress_gem5_dir = os.path.join(
options.dest_dir, "linux-kernel-vexpress_gem5")
args.dest_dir, "linux-kernel-vexpress_gem5")
linux_bin = os.path.join(
binaries_dir, "vmlinux.vexpress_gem5_v1_64")
@@ -103,7 +103,7 @@ def linux64():
def linux32():
kernel_vexpress_gem5_dir = os.path.join(
options.dest_dir, "linux-kernel-vexpress_gem5")
args.dest_dir, "linux-kernel-vexpress_gem5")
linux_bin = os.path.join(
binaries_dir, "vmlinux.vexpress_gem5_v1")
@@ -186,25 +186,25 @@ def xen():
"""
Build Xen for aarch64
"""
xen_dir = os.path.join(options.dest_dir, "xen")
bootwrapper_dir = os.path.join(options.dest_dir, "bootwrapper")
xen_dir = os.path.join(args.dest_dir, "xen")
bootwrapper_dir = os.path.join(args.dest_dir, "bootwrapper")
linux_cmdline = "console=hvc0 root=/dev/vda rw mem=1G"
xen_cmdline = "dtuart=/uart@1c090000 console=dtuart no-bootscrub " + \
"dom0_mem=1G loglvl=all guest_loglvl=all"
run_cmd("clone Xen",
options.dest_dir,
args.dest_dir,
["git", "clone", "git://xenbits.xen.org/xen.git",
xen_dir])
run_cmd("clone boot-wrapper-aarch64",
options.dest_dir,
args.dest_dir,
["git", "clone", "git://git.kernel.org/pub/" +
"scm/linux/kernel/git/mark/boot-wrapper-aarch64.git",
bootwrapper_dir])
# Need to compile arm64 Linux
linux_dir = os.path.join(options.dest_dir, "linux-kernel-vexpress_gem5")
linux_dir = os.path.join(args.dest_dir, "linux-kernel-vexpress_gem5")
linux_bin = os.path.join(linux_dir,
"arch", "arm64", "boot", "Image")
if not os.path.exists(linux_bin):
@@ -260,52 +260,46 @@ all_binaries = {
"xen" : xen,
}
parser = OptionParser()
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_option("--gem5-dir", default = gem5_dir,
parser.add_argument("--gem5-dir", default = gem5_dir,
metavar = "GEM5_DIR",
help = "gem5 root directory to be used for bootloader and "
"VExpress_GEM5_V1 DTB sources. The default value is the gem5 root "
"directory of the executed script (%default)")
parser.add_option("--dest-dir", default = "/tmp",
"directory of the executed script")
parser.add_argument("--dest-dir", default = "/tmp",
metavar = "DEST_DIR",
help = "Directory to use for checking out the different kernel "
"repositories. Generated files will be copied to "
"DEST_DIR/binaries (which must not exist). The default "
"value is %default")
parser.add_option("-j", "--make-jobs", type = "int", default = 1,
"DEST_DIR/binaries (which must not exist)")
parser.add_argument("-j", "--make-jobs", type = int, default = 1,
metavar = "MAKE_JOBS",
help = "Number of jobs to use with the 'make' commands. Default value: "
"%default")
parser.add_option("-b", "--fs-binaries", action="append",
help = "Number of jobs to use with the 'make' commands.")
parser.add_argument("-b", "--fs-binaries", action="append",
choices=list(all_binaries.keys()), default=[],
help = "List of FS files to be generated. Defaulting to all")
(options, args) = parser.parse_args()
args = parser.parse_args()
if args:
print("Unrecognized argument(s) %s." % args)
if not os.path.isdir(args.dest_dir):
print("Error: %s is not a directory." % args.dest_dir)
sys.exit(1)
if not os.path.isdir(options.dest_dir):
print("Error: %s is not a directory." % options.dest_dir)
sys.exit(1)
if not os.path.isdir(options.gem5_dir):
print("Error: %s is not a directory." % options.gem5_dir)
if not os.path.isdir(args.gem5_dir):
print("Error: %s is not a directory." % args.gem5_dir)
sys.exit(1)
if machine() != "x86_64":
print("Error: This script should run in a x86_64 machine")
sys.exit(1)
binaries_dir = options.dest_dir + "/binaries"
binaries_dir = args.dest_dir + "/binaries"
if os.path.exists(binaries_dir):
print("Error: %s already exists." % binaries_dir)
sys.exit(1)
revisions_dir = options.dest_dir + "/revisions"
revisions_dir = args.dest_dir + "/revisions"
if os.path.exists(revisions_dir):
print("Error: %s already exists." %revisions_dir)
@@ -314,7 +308,7 @@ if os.path.exists(revisions_dir):
os.mkdir(binaries_dir);
os.mkdir(revisions_dir);
make_jobs_str = "-j" + str(options.make_jobs)
make_jobs_str = "-j" + str(args.make_jobs)
rev_file = open(revisions_dir + "/gem5", "w+")
run_cmd("write revision of gem5 repo",
@@ -323,7 +317,7 @@ run_cmd("write revision of gem5 repo",
rev_file)
rev_file.close()
binaries = options.fs_binaries if options.fs_binaries else list(all_binaries.keys())
binaries = args.fs_binaries if args.fs_binaries else list(all_binaries.keys())
for fs_binary in binaries:
all_binaries[fs_binary]()

View File

@@ -35,11 +35,11 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import optparse
import argparse
import subprocess
import sys
parser = optparse.OptionParser()
parser = argparse.ArgumentParser()
# This script lets the user run a soak test using the false-sharing
# memtest.py example script. It runs a number of iterations with
@@ -47,20 +47,15 @@ parser = optparse.OptionParser()
# of ticks. Both the iteration count and the ticks for each run can be
# set on the command line.
parser.add_option('-c', '--count', type='int', default=100)
parser.add_option('-t', '--ticks', type='int', default=100000000000)
parser.add_argument('-c', '--count', type=int, default=100)
parser.add_argument('-t', '--ticks', type=int, default=100000000000)
parser.add_argument('binary')
(options, args) = parser.parse_args()
args = parser.parse_args()
if len(args) != 1:
print("Error: Expecting a single argument specifying the gem5 binary")
sys.exit(1)
gem5_binary = args[0]
for i in range(options.count):
status = subprocess.call([gem5_binary, 'configs/example/memtest.py',
'-r', '-m %d' % (options.ticks)])
for i in range(args.count):
status = subprocess.call([args.binary, 'configs/example/memtest.py',
'-r', '-m %d' % (args.ticks)])
if status != 0:
print("Error: memtest run failed\n")
sys.exit(1)

View File

@@ -37,7 +37,7 @@
# Pipeline activity viewer for the O3 CPU model.
import optparse
import argparse
import os
import sys
import copy
@@ -300,68 +300,70 @@ def validate_range(my_range):
def main():
# Parse options
usage = ('%prog [OPTION]... TRACE_FILE')
parser = optparse.OptionParser(usage=usage)
parser.add_option(
# Parse args
usage = ('%(prog)s [OPTION]... TRACE_FILE')
parser = argparse.ArgumentParser(
usage=usage,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-o',
dest='outfile',
default=os.path.join(os.getcwd(), 'o3-pipeview.out'),
help="output file (default: '%default')")
parser.add_option(
help="output file")
parser.add_argument(
'-t',
dest='tick_range',
default='0:-1',
help="tick range (default: '%default'; -1 == inf.)")
parser.add_option(
help="tick range (-1 == inf.)")
parser.add_argument(
'-i',
dest='inst_range',
default='0:-1',
help="instruction range (default: '%default'; -1 == inf.)")
parser.add_option(
help="instruction range (-1 == inf.)")
parser.add_argument(
'-w',
dest='width',
type='int', default=80,
help="timeline width (default: '%default')")
parser.add_option(
type=int, default=80,
help="timeline width")
parser.add_argument(
'--color',
action='store_true', default=False,
help="enable colored output (default: '%default')")
parser.add_option(
help="enable colored output")
parser.add_argument(
'-c', '--cycle-time',
type='int', default=1000,
help="CPU cycle time in ticks (default: '%default')")
parser.add_option(
type=int, default=1000,
help="CPU cycle time in ticks")
parser.add_argument(
'--timestamps',
action='store_true', default=False,
help="print fetch and retire timestamps (default: '%default')")
parser.add_option(
help="print fetch and retire timestamps")
parser.add_argument(
'--only_committed',
action='store_true', default=False,
help="display only committed (completed) instructions (default: '%default')")
parser.add_option(
help="display only committed (completed) instructions")
parser.add_argument(
'--store_completions',
action='store_true', default=False,
help="additionally display store completion ticks (default: '%default')")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error('incorrect number of arguments')
sys.exit(1)
tick_range = validate_range(options.tick_range)
help="additionally display store completion ticks")
parser.add_argument(
'tracefile')
args = parser.parse_args()
tick_range = validate_range(args.tick_range)
if not tick_range:
parser.error('invalid range')
sys.exit(1)
inst_range = validate_range(options.inst_range)
inst_range = validate_range(args.inst_range)
if not inst_range:
parser.error('invalid range')
sys.exit(1)
# Process trace
print('Processing trace... ', end=' ')
with open(args[0], 'r') as trace:
with open(options.outfile, 'w') as out:
process_trace(trace, out, options.cycle_time, options.width,
options.color, options.timestamps,
options.only_committed, options.store_completions,
with open(args.tracefile, 'r') as trace:
with open(args.outfile, 'w') as out:
process_trace(trace, out, args.cycle_time, args.width,
args.color, args.timestamps,
args.only_committed, args.store_completions,
*(tick_range + inst_range))
print('done!')

View File

@@ -280,40 +280,44 @@ class SortIncludes(object):
default_languages = frozenset(('C', 'C++', 'isa', 'python', 'scons', 'swig'))
def options():
import optparse
options = optparse.OptionParser()
add_option = options.add_option
add_option('-d', '--dir_ignore', metavar="DIR[,DIR]", type='string',
default=','.join(default_dir_ignore),
help="ignore directories")
add_option('-f', '--file_ignore', metavar="FILE[,FILE]", type='string',
default=','.join(default_file_ignore),
help="ignore files")
add_option('-l', '--languages', metavar="LANG[,LANG]", type='string',
default=','.join(default_languages),
help="languages")
add_option('-n', '--dry-run', action='store_true',
help="don't overwrite files")
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'-d', '--dir_ignore', metavar="DIR[,DIR]", type=str,
default=','.join(default_dir_ignore),
help="ignore directories")
parser.add_argument(
'-f', '--file_ignore', metavar="FILE[,FILE]", type=str,
default=','.join(default_file_ignore),
help="ignore files")
parser.add_argument(
'-l', '--languages', metavar="LANG[,LANG]", type=str,
default=','.join(default_languages),
help="languages")
parser.add_argument(
'-n', '--dry-run', action='store_true',
help="don't overwrite files")
parser.add_argument('bases', nargs='*')
return options
return parser
def parse_args(parser):
opts,args = parser.parse_args()
args = parser.parse_args()
opts.dir_ignore = frozenset(opts.dir_ignore.split(','))
opts.file_ignore = frozenset(opts.file_ignore.split(','))
opts.languages = frozenset(opts.languages.split(','))
args.dir_ignore = frozenset(args.dir_ignore.split(','))
args.file_ignore = frozenset(args.file_ignore.split(','))
args.languages = frozenset(args.languages.split(','))
return opts,args
return args
if __name__ == '__main__':
parser = options()
opts, args = parse_args(parser)
args = parse_args(parser)
for base in args:
for filename,language in find_files(base, languages=opts.languages,
file_ignore=opts.file_ignore, dir_ignore=opts.dir_ignore):
if opts.dry_run:
for base in args.bases:
for filename,language in find_files(base, languages=args.languages,
file_ignore=args.file_ignore, dir_ignore=args.dir_ignore):
if args.dry_run:
print("{}: {}".format(filename, language))
else:
update_file(filename, filename, language, SortIncludes())