Giacomo Travaglini has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/44512 )
Change subject: util: Replace optparse with argparse
......................................................................
util: Replace optparse with argparse
JIRA: https://gem5.atlassian.net/browse/GEM5-543
Change-Id: Id270ed29f14199f4f8eb6eb5739451a43d100484
Signed-off-by: Giacomo Travaglini <giacomo.travagl...@arm.com>
---
M util/checkpoint-tester.py
M util/cpt_upgrader.py
M util/gem5img.py
M util/gen_arm_fs_files.py
M util/memtest-soak.py
M util/o3-pipeview.py
M util/style/sort_includes.py
7 files changed, 134 insertions(+), 134 deletions(-)
diff --git a/util/checkpoint-tester.py b/util/checkpoint-tester.py
index e2051cd..3a9c96c 100755
--- a/util/checkpoint-tester.py
+++ b/util/checkpoint-tester.py
@@ -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='+')
-(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 @@
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]
diff --git a/util/cpt_upgrader.py b/util/cpt_upgrader.py
index cf46b58..30e79f2 100755
--- a/util/cpt_upgrader.py
+++ b/util/cpt_upgrader.py
@@ -260,26 +260,31 @@
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 [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,27 @@
print(" \"{}\",".format(tag))
print("};")
exit(0)
- elif len(args) != 1:
- 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")
diff --git a/util/gem5img.py b/util/gem5img.py
index 9523a77..cd84e0f 100755
--- a/util/gem5img.py
+++ b/util/gem5img.py
@@ -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
@@ -222,7 +222,7 @@
class Command(object):
def addOption(self, *args, **kargs):
- self.parser.add_option(*args, **kargs)
+ self.parser.add_argument(*args, **kargs)
def __init__(self, name, description, posArgs):
self.name = name
@@ -238,12 +238,14 @@
usage += ' %s' % argName
posUsage += '\n %s: %s' % posArg
usage += posUsage
- self.parser = OptionParser(usage=usage, description=description)
+ self.parser = ArgumentParser(usage=usage, description=description)
self.addOption('-d', '--debug', dest='debug', action='store_true',
help='Verbose output.')
+ self.addOption('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
diff --git a/util/gen_arm_fs_files.py b/util/gen_arm_fs_files.py
index 84189dc..e0cda7c 100755
--- a/util/gen_arm_fs_files.py
+++ b/util/gen_arm_fs_files.py
@@ -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
from subprocess import call
from platform import machine
from distutils import spawn
@@ -66,16 +66,16 @@
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 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 @@
"""
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,48 @@
"xen" : xen,
}
-parser = OptionParser()
+parser = ArgumentParser()
-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",
+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,
+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",
+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 +310,7 @@
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 +319,7 @@
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]()
diff --git a/util/memtest-soak.py b/util/memtest-soak.py
index 94339d6..8bea048 100755
--- a/util/memtest-soak.py
+++ b/util/memtest-soak.py
@@ -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 @@
# 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)
diff --git a/util/o3-pipeview.py b/util/o3-pipeview.py
index 630c536..9de82ba 100755
--- a/util/o3-pipeview.py
+++ b/util/o3-pipeview.py
@@ -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,68 @@
def main():
- # Parse options
+ # Parse args
usage = ('%prog [OPTION]... TRACE_FILE')
- parser = optparse.OptionParser(usage=usage)
- parser.add_option(
+ parser = argparse.ArgumentParser(usage=usage)
+ parser.add_argument(
'-o',
dest='outfile',
default=os.path.join(os.getcwd(), 'o3-pipeview.out'),
help="output file (default: '%default')")
- parser.add_option(
+ parser.add_argument(
'-t',
dest='tick_range',
default='0:-1',
help="tick range (default: '%default'; -1 == inf.)")
- parser.add_option(
+ parser.add_argument(
'-i',
dest='inst_range',
default='0:-1',
help="instruction range (default: '%default'; -1 == inf.)")
- parser.add_option(
+ parser.add_argument(
'-w',
dest='width',
- type='int', default=80,
+ type=int, default=80,
help="timeline width (default: '%default')")
- parser.add_option(
+ parser.add_argument(
'--color',
action='store_true', default=False,
help="enable colored output (default: '%default')")
- parser.add_option(
+ parser.add_argument(
'-c', '--cycle-time',
- type='int', default=1000,
+ type=int, default=1000,
help="CPU cycle time in ticks (default: '%default')")
- parser.add_option(
+ parser.add_argument(
'--timestamps',
action='store_true', default=False,
help="print fetch and retire timestamps (default: '%default')")
- parser.add_option(
+ parser.add_argument(
'--only_committed',
action='store_true', default=False,
help="display only committed (completed) instructions
(default: '%default')")
- parser.add_option(
+ 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)
+ 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!')
diff --git a/util/style/sort_includes.py b/util/style/sort_includes.py
index 041c07d..0e4216b 100644
--- a/util/style/sort_includes.py
+++ b/util/style/sort_includes.py
@@ -280,40 +280,44 @@
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())
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/44512
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Id270ed29f14199f4f8eb6eb5739451a43d100484
Gerrit-Change-Number: 44512
Gerrit-PatchSet: 1
Gerrit-Owner: Giacomo Travaglini <giacomo.travagl...@arm.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s