From 9ad0ea3e7860c549fa47c2cd9ddf900b5fc8d58b Mon Sep 17 00:00:00 2001 From: Hans Anderson Date: Fri, 21 Jun 2019 19:12:26 -0600 Subject: [PATCH] Switch from getopt to argparse --- sbysrc/sby.py | 169 ++++++++++++++++++++------------------------------ 1 file changed, 66 insertions(+), 103 deletions(-) diff --git a/sbysrc/sby.py b/sbysrc/sby.py index 70b5070..af36c71 100644 --- a/sbysrc/sby.py +++ b/sbysrc/sby.py @@ -17,111 +17,77 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # -import os, sys, getopt, shutil, tempfile +import argparse, os, sys, shutil, tempfile ##yosys-sys-path## from sby_core import SbyJob, SbyAbort from time import localtime -sbyfile = None -workdir = None -tasknames = list() -opt_force = False -opt_backup = False -opt_tmpdir = False -exe_paths = dict() -throw_err = False -dump_cfg = False -dump_tasks = False +class DictAction(argparse.Action): + def __call__(self, parser, namespace, values, option_string=None): + assert isinstance(getattr(namespace, self.dest), dict), "Use ArgumentParser.set_defaults() to initialize {} to dict()".format(self.dest) + name = option_string.lstrip(parser.prefix_chars).replace("-", "_") + getattr(namespace, self.dest)[name] = values + +parser = argparse.ArgumentParser(prog="sby", + usage="%(prog)s [options] [.sby [tasknames] | ]") +parser.set_defaults(exe_paths=dict()) + +parser.add_argument("-d", metavar="", dest="workdir", + help="set workdir name (ignored if is given as a positional argument). default: (without .sby)") +parser.add_argument("-f", action="store_true", dest="force", + help="remove workdir if it already exists") +parser.add_argument("-b", action="store_true", dest="backup", + help="backup workdir if it already exists") +parser.add_argument("-t", action="store_true", dest="tmpdir", + help="run in a temporary workdir (remove when finished)") +parser.add_argument("-T", metavar="", action="append", dest="tasknames", + help="add taskname (useful when sby file is read from stdin, ignored if tasknames are given as positional arguments)") +parser.add_argument("-E", action="store_true", dest="throw_err", + help="throw an exception (incl stack trace) for most errors") + +parser.add_argument("--yosys", metavar="", + action=DictAction, dest="exe_paths") +parser.add_argument("--abc", metavar="", + action=DictAction, dest="exe_paths") +parser.add_argument("--smtbmc", metavar="", + action=DictAction, dest="exe_paths") +parser.add_argument("--suprove", metavar="", + action=DictAction, dest="exe_paths") +parser.add_argument("--aigbmc", metavar="", + action=DictAction, dest="exe_paths") +parser.add_argument("--avy", metavar="", + action=DictAction, dest="exe_paths") +parser.add_argument("--btormc", metavar="", + action=DictAction, dest="exe_paths", + help="configure which executable to use for the respective tool") +parser.add_argument("--dumpcfg", action="store_true", dest="dump_cfg", + help="print the pre-processed configuration file") +parser.add_argument("--dumptasks", action="store_true", dest="dump_tasks", + help="print the list of tasks") +parser.add_argument("--setup", action="store_true", dest="setupmode", + help="set up the working directory and exit") + +parser.add_argument("sbyfile", metavar=".sby | ", nargs="?", + help=".sby file OR directory containing config.sby file (causes -d to be ignored if is used)") +parser.add_argument("arg_tasknames", metavar="tasknames", nargs="*", + help="tasks to run (only valid when is used, causes -T to be ignored)") + +args = parser.parse_args() + +sbyfile = args.sbyfile +workdir = args.workdir +tasknames = args.arg_tasknames if args.arg_tasknames else args.tasknames +opt_force = args.force +opt_backup = args.backup +opt_tmpdir = args.tmpdir +exe_paths = args.exe_paths +throw_err = args.throw_err +dump_cfg = args.dump_cfg +dump_tasks = args.dump_tasks reusedir = False -setupmode = False +setupmode = args.setupmode -def usage(): - print(""" -sby [options] [.sby [tasknames] | ] - - -d - set workdir name. default: (without .sby) - - -f - remove workdir if it already exists - - -b - backup workdir if it already exists - - -t - run in a temporary workdir (remove when finished) - - -T taskname - add taskname (useful when sby file is read from stdin) - - -E - throw an exception (incl stack trace) for most errors - - --yosys - --abc - --smtbmc - --suprove - --aigbmc - --avy - --btormc - configure which executable to use for the respective tool - - --dumpcfg - print the pre-processed configuration file - - --dumptasks - print the list of tasks - - --setup - set up the working directory and exit -""") - sys.exit(1) - -try: - opts, args = getopt.getopt(sys.argv[1:], "d:btfT:E", ["yosys=", - "abc=", "smtbmc=", "suprove=", "aigbmc=", "avy=", "btormc=", - "dumpcfg", "dumptasks", "setup"]) -except: - usage() - -for o, a in opts: - if o == "-d": - workdir = a - elif o == "-f": - opt_force = True - elif o == "-b": - opt_backup = True - elif o == "-t": - opt_tmpdir = True - elif o == "-T": - tasknames.append(a) - elif o == "-E": - throw_err = True - elif o == "--yosys": - exe_paths["yosys"] = a - elif o == "--abc": - exe_paths["abc"] = a - elif o == "--smtbmc": - exe_paths["smtbmc"] = a - elif o == "--suprove": - exe_paths["suprove"] = a - elif o == "--aigbmc": - exe_paths["aigbmc"] = a - elif o == "--avy": - exe_paths["avy"] = a - elif o == "--btormc": - exe_paths["btormc"] = a - elif o == "--dumpcfg": - dump_cfg = True - elif o == "--dumptasks": - dump_tasks = True - elif o == "--setup": - setupmode = True - else: - usage() - -if len(args) > 0: - sbyfile = args[0] +if sbyfile is not None: if os.path.isdir(sbyfile): workdir = sbyfile sbyfile += "/config.sby" @@ -129,7 +95,7 @@ if len(args) > 0: if not opt_force and os.path.exists(workdir + "/model"): print("ERROR: Use -f to re-run in existing directory.", file=sys.stderr) sys.exit(1) - if len(args) > 1: + if tasknames: print("ERROR: Can't use tasks when running in existing directory.", file=sys.stderr) sys.exit(1) if setupmode: @@ -143,9 +109,6 @@ if len(args) > 0: print("ERROR: Sby file does not have .sby file extension.", file=sys.stderr) sys.exit(1) -if len(args) > 1: - tasknames = args[1:] - early_logmsgs = list() -- 2.30.2