Skip to content

Commit

Permalink
util: Replace optparse with argparse
Browse files Browse the repository at this point in the history
JIRA: https://gem5.atlassian.net/browse/GEM5-543

Change-Id: Id270ed29f14199f4f8eb6eb5739451a43d100484
Signed-off-by: Giacomo Travaglini <[email protected]>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/44512
Reviewed-by: Daniel Carvalho <[email protected]>
Maintainer: Gabe Black <[email protected]>
Tested-by: kokoro <[email protected]>
  • Loading branch information
giactra committed Apr 21, 2021
1 parent 2f424f6 commit 8d5a8f0
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 161 deletions.
29 changes: 15 additions & 14 deletions util/checkpoint-tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]*)')
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion util/checkpoint_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
53 changes: 29 additions & 24 deletions util/cpt_upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

(options, args) = parser.parse_args()
verbose_print = options.verbose
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='?')

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>")
Expand All @@ -289,30 +294,30 @@ def process_file(path, **kwargs):
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")
Expand Down
30 changes: 16 additions & 14 deletions util/gem5img.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -221,8 +221,8 @@ def mountPointToDev(mountPoint):
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
Expand All @@ -231,19 +231,21 @@ def __init__(self, name, description, posArgs):
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
Expand All @@ -261,9 +263,9 @@ def runCom(self):
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.',
Expand Down Expand Up @@ -365,9 +367,9 @@ def partitionComFunc(options, args):
# 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)])
Expand Down
60 changes: 27 additions & 33 deletions util/gen_arm_fs_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -260,52 +260,46 @@ def xen():
"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)
Expand All @@ -314,7 +308,7 @@ def xen():
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",
Expand All @@ -323,7 +317,7 @@ def xen():
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]()

Expand Down
Loading

0 comments on commit 8d5a8f0

Please sign in to comment.