Skip to content

Commit

Permalink
Execute format on existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmicko committed Aug 10, 2020
1 parent 1a78199 commit 359fe2c
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 141 deletions.
107 changes: 51 additions & 56 deletions sbysrc/sby.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,44 @@

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)
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] [<jobname>.sby [tasknames] | <dirname>]")
parser = argparse.ArgumentParser(prog="sby", usage="%(prog)s [options] [<jobname>.sby [tasknames] | <dirname>]")
parser.set_defaults(exe_paths=dict())

parser.add_argument("-d", metavar="<dirname>", dest="workdir",
help="set workdir name. default: <jobname> or <jobname>_<taskname>")
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)")
help="set workdir name. default: <jobname> or <jobname>_<taskname>")
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="<taskname>", action="append", dest="tasknames", default=list(),
help="add taskname (useful when sby file is read from stdin)")
help="add taskname (useful when sby file is read from stdin)")
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="<path_to_executable>",
action=DictAction, dest="exe_paths")
parser.add_argument("--abc", metavar="<path_to_executable>",
action=DictAction, dest="exe_paths")
parser.add_argument("--smtbmc", metavar="<path_to_executable>",
action=DictAction, dest="exe_paths")
parser.add_argument("--suprove", metavar="<path_to_executable>",
action=DictAction, dest="exe_paths")
parser.add_argument("--aigbmc", metavar="<path_to_executable>",
action=DictAction, dest="exe_paths")
parser.add_argument("--avy", metavar="<path_to_executable>",
action=DictAction, dest="exe_paths")
parser.add_argument("--btormc", metavar="<path_to_executable>",
action=DictAction, dest="exe_paths")
parser.add_argument("--pono", metavar="<path_to_executable>",
action=DictAction, dest="exe_paths",
help="configure which executable to use for the respective tool")
help="throw an exception (incl stack trace) for most errors")

parser.add_argument("--yosys", metavar="<path_to_executable>", action=DictAction, dest="exe_paths")
parser.add_argument("--abc", metavar="<path_to_executable>", action=DictAction, dest="exe_paths")
parser.add_argument("--smtbmc", metavar="<path_to_executable>", action=DictAction, dest="exe_paths")
parser.add_argument("--suprove", metavar="<path_to_executable>", action=DictAction, dest="exe_paths")
parser.add_argument("--aigbmc", metavar="<path_to_executable>", action=DictAction, dest="exe_paths")
parser.add_argument("--avy", metavar="<path_to_executable>", action=DictAction, dest="exe_paths")
parser.add_argument("--btormc", metavar="<path_to_executable>", action=DictAction, dest="exe_paths")
parser.add_argument("--pono", metavar="<path_to_executable>", 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("--dumpfiles", action="store_true", dest="dump_files",
help="print the list of source files")
parser.add_argument("--setup", action="store_true", dest="setupmode",
help="set up the working directory and exit")

parser.add_argument("--init-config-file", dest="init_config_file",
help="create a default .sby config file")
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("--dumpfiles", action="store_true", dest="dump_files", help="print the list of source files")
parser.add_argument("--setup", action="store_true", dest="setupmode", help="set up the working directory and exit")

parser.add_argument("--init-config-file", dest="init_config_file", help="create a default .sby config file")
parser.add_argument("sbyfile", metavar="<jobname>.sby | <dirname>", nargs="?",
help=".sby file OR directory containing config.sby file")
help=".sby file OR directory containing config.sby file")
parser.add_argument("arg_tasknames", metavar="tasknames", nargs="*",
help="tasks to run (only valid when <jobname>.sby is used)")
help="tasks to run (only valid when <jobname>.sby is used)")

args = parser.parse_args()

Expand Down Expand Up @@ -198,11 +183,11 @@ def handle_line(line):
task_skip_line = False

for t in task_tags_all:
if line.startswith(t+":"):
line = line[len(t)+1:].lstrip()
if line.startswith(t + ":"):
line = line[len(t) + 1:].lstrip()
match = t in task_tags_active
elif line.startswith("~"+t+":"):
line = line[len(t)+2:].lstrip()
elif line.startswith("~" + t + ":"):
line = line[len(t) + 2:].lstrip()
match = t not in task_tags_active
else:
continue
Expand Down Expand Up @@ -268,7 +253,6 @@ def handle_line(line):

return cfgdata, tasklist


sbydata = list()
with (open(sbyfile, "r") if sbyfile is not None else sys.stdin) as f:
for line in f:
Expand All @@ -295,7 +279,7 @@ def find_files(taskname):
if start_index == -1:
return

for line in sbyconfig[start_index+1:]:
for line in sbyconfig[start_index + 1:]:
line = line.strip()
if line.startswith("["):
break
Expand Down Expand Up @@ -341,7 +325,9 @@ def run_job(taskname):
backup_idx = 0
while os.path.exists("{}.bak{:03d}".format(my_workdir, backup_idx)):
backup_idx += 1
early_log(my_workdir, "Moving directory '{}' to '{}'.".format(my_workdir, "{}.bak{:03d}".format(my_workdir, backup_idx)))
early_log(
my_workdir, "Moving directory '{}' to '{}'.".format(my_workdir,
"{}.bak{:03d}".format(my_workdir, backup_idx)))
shutil.move(my_workdir, "{}.bak{:03d}".format(my_workdir, backup_idx))

if opt_force and not reusedir:
Expand All @@ -361,7 +347,8 @@ def run_job(taskname):
my_opt_tmpdir = True
my_workdir = tempfile.mkdtemp()

junit_ts_name = os.path.basename(sbyfile[:-4]) if sbyfile is not None else workdir if workdir is not None else "stdin"
junit_ts_name = os.path.basename(
sbyfile[:-4]) if sbyfile is not None else workdir if workdir is not None else "stdin"
junit_tc_name = taskname if taskname is not None else "default"

if reusedir:
Expand Down Expand Up @@ -404,33 +391,41 @@ def run_job(taskname):
junit_errors = 1 if job.retcode == 16 else 0
junit_failures = 1 if job.retcode != 0 and junit_errors == 0 else 0
print('<?xml version="1.0" encoding="UTF-8"?>', file=f)
print('<testsuites disabled="0" errors="{}" failures="{}" tests="1" time="{}">'.format(junit_errors, junit_failures, job.total_time), file=f)
print('<testsuite disabled="0" errors="{}" failures="{}" name="{}" skipped="0" tests="1" time="{}">'.format(junit_errors, junit_failures, junit_ts_name, job.total_time), file=f)
print(
'<testsuites disabled="0" errors="{}" failures="{}" tests="1" time="{}">'.format(
junit_errors, junit_failures, job.total_time), file=f)
print(
'<testsuite disabled="0" errors="{}" failures="{}" name="{}" skipped="0" tests="1" time="{}">'.format(
junit_errors, junit_failures, junit_ts_name, job.total_time), file=f)
print('<properties>', file=f)
print('<property name="os" value="{}"/>'.format(os.name), file=f)
print('</properties>', file=f)
print('<testcase classname="{}" name="{}" status="{}" time="{}">'.format(junit_ts_name, junit_tc_name, job.status, job.total_time), file=f)
print(
'<testcase classname="{}" name="{}" status="{}" time="{}">'.format(junit_ts_name, junit_tc_name,
job.status, job.total_time), file=f)
if junit_errors:
print('<error message="{}" type="{}"/>'.format(job.status, job.status), file=f)
if junit_failures:
print('<failure message="{}" type="{}"/>'.format(job.status, job.status), file=f)
print('<system-out>', end="", file=f)
with open("{}/logfile.txt".format(job.workdir), "r") as logf:
for line in logf:
print(line.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"), end="", file=f)
print(
line.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"),
end="", file=f)
print('</system-out></testcase></testsuite></testsuites>', file=f)
with open("{}/status".format(job.workdir), "w") as f:
print("{} {} {}".format(job.status, job.retcode, job.total_time), file=f)

return job.retcode


retcode = 0
for t in tasknames:
retcode |= run_job(t)

if retcode and (len(tasknames) > 1 or tasknames[0] is not None):
tm = localtime()
print("SBY {:2d}:{:02d}:{:02d} One or more tasks produced a non-zero return code.".format(tm.tm_hour, tm.tm_min, tm.tm_sec))
print("SBY {:2d}:{:02d}:{:02d} One or more tasks produced a non-zero return code.".format(
tm.tm_hour, tm.tm_min, tm.tm_sec))

sys.exit(retcode)
75 changes: 45 additions & 30 deletions sbysrc/sby_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def __init__(self, job, info, deps, cmdline, logfile=None, logstderr=True, silen
# Windows command interpreter equivalents for sequential
# commands (; => &) command grouping ({} => ()).
replacements = {
";" : "&",
"{" : "(",
"}" : ")",
";": "&",
"{": "(",
"}": ")",
}

cmdline_copy = cmdline
Expand Down Expand Up @@ -140,19 +140,21 @@ def poll(self):
self.job.log("{}: starting process \"{}\"".format(self.info, self.cmdline))

if os.name == "posix":

def preexec_fn():
signal.signal(signal.SIGINT, signal.SIG_IGN)
os.setpgrp()

self.p = subprocess.Popen(["/usr/bin/env", "bash", "-c", self.cmdline], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=(subprocess.STDOUT if self.logstderr else None), preexec_fn=preexec_fn)
self.p = subprocess.Popen(["/usr/bin/env", "bash", "-c", self.cmdline], stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=(subprocess.STDOUT if self.logstderr else None), preexec_fn=preexec_fn)

fl = fcntl.fcntl(self.p.stdout, fcntl.F_GETFL)
fcntl.fcntl(self.p.stdout, fcntl.F_SETFL, fl | os.O_NONBLOCK)

else:
self.p = subprocess.Popen(self.cmdline, shell=True, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=(subprocess.STDOUT if self.logstderr else None))
stderr=(subprocess.STDOUT if self.logstderr else None))

self.job.tasks_pending.remove(self)
self.job.tasks_running.append(self)
Expand Down Expand Up @@ -200,11 +202,9 @@ def preexec_fn():
next_task.poll()
return


class SbyAbort(BaseException):
pass


class SbyJob:
def __init__(self, sbyconfig, workdir, early_logs, reusedir):
self.options = dict()
Expand Down Expand Up @@ -285,13 +285,19 @@ def taskloop(self):

def log(self, logmessage):
tm = localtime()
print("SBY {:2d}:{:02d}:{:02d} [{}] {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage), flush=True)
print("SBY {:2d}:{:02d}:{:02d} [{}] {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage), file=self.logfile, flush=True)
print("SBY {:2d}:{:02d}:{:02d} [{}] {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage),
flush=True)
print("SBY {:2d}:{:02d}:{:02d} [{}] {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage),
file=self.logfile, flush=True)

def error(self, logmessage):
tm = localtime()
print("SBY {:2d}:{:02d}:{:02d} [{}] ERROR: {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage), flush=True)
print("SBY {:2d}:{:02d}:{:02d} [{}] ERROR: {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage), file=self.logfile, flush=True)
print(
"SBY {:2d}:{:02d}:{:02d} [{}] ERROR: {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage),
flush=True)
print(
"SBY {:2d}:{:02d}:{:02d} [{}] ERROR: {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage),
file=self.logfile, flush=True)
self.status = "ERROR"
if "ERROR" not in self.expect:
self.retcode = 16
Expand Down Expand Up @@ -385,9 +391,9 @@ def make_model(self, model_name):
print("hierarchy -simcheck", file=f)
print("write_ilang ../model/design{}.il".format("" if model_name == "base" else "_nomem"), file=f)

task = SbyTask(self, model_name, [],
"cd {}/src; {} -ql ../model/design{s}.log ../model/design{s}.ys".format(self.workdir, self.exe_paths["yosys"],
s="" if model_name == "base" else "_nomem"))
task = SbyTask(
self, model_name, [], "cd {}/src; {} -ql ../model/design{s}.log ../model/design{s}.ys".format(
self.workdir, self.exe_paths["yosys"], s="" if model_name == "base" else "_nomem"))
task.checkretcode = True

return [task]
Expand All @@ -410,8 +416,10 @@ def make_model(self, model_name):
else:
print("write_smt2 -wires design_{}.smt2".format(model_name), file=f)

task = SbyTask(self, model_name, self.model("nomem" if "_nomem" in model_name else "base"),
"cd {}/model; {} -ql design_{s}.log design_{s}.ys".format(self.workdir, self.exe_paths["yosys"], s=model_name))
task = SbyTask(
self, model_name, self.model("nomem" if "_nomem" in model_name else "base"),
"cd {}/model; {} -ql design_{s}.log design_{s}.ys".format(self.workdir, self.exe_paths["yosys"],
s=model_name))
task.checkretcode = True

return [task]
Expand All @@ -433,10 +441,14 @@ def make_model(self, model_name):
print("delete -output", file=f)
print("dffunmap", file=f)
print("stat", file=f)
print("write_btor {}-i design_{m}.info design_{m}.btor".format("-c " if self.opt_mode == "cover" else "", m=model_name), file=f)

task = SbyTask(self, model_name, self.model("nomem" if "_nomem" in model_name else "base"),
"cd {}/model; {} -ql design_{s}.log design_{s}.ys".format(self.workdir, self.exe_paths["yosys"], s=model_name))
print(
"write_btor {}-i design_{m}.info design_{m}.btor".format("-c " if self.opt_mode == "cover" else "",
m=model_name), file=f)

task = SbyTask(
self, model_name, self.model("nomem" if "_nomem" in model_name else "base"),
"cd {}/model; {} -ql design_{s}.log design_{s}.ys".format(self.workdir, self.exe_paths["yosys"],
s=model_name))
task.checkretcode = True

return [task]
Expand All @@ -458,8 +470,9 @@ def make_model(self, model_name):
print("stat", file=f)
print("write_aiger -I -B -zinit -map design_aiger.aim design_aiger.aig", file=f)

task = SbyTask(self, "aig", self.model("nomem"),
"cd {}/model; {} -ql design_aiger.log design_aiger.ys".format(self.workdir, self.exe_paths["yosys"]))
task = SbyTask(
self, "aig", self.model("nomem"),
"cd {}/model; {} -ql design_aiger.log design_aiger.ys".format(self.workdir, self.exe_paths["yosys"]))
task.checkretcode = True

return [task]
Expand Down Expand Up @@ -668,16 +681,18 @@ def run(self, setupmode):
self.total_time = total_process_time

self.summary = [
"Elapsed clock time [H:MM:SS (secs)]: {}:{:02d}:{:02d} ({})".format
(total_clock_time // (60*60), (total_clock_time // 60) % 60, total_clock_time % 60, total_clock_time),
"Elapsed process time [H:MM:SS (secs)]: {}:{:02d}:{:02d} ({})".format
(total_process_time // (60*60), (total_process_time // 60) % 60, total_process_time % 60, total_process_time),
"Elapsed clock time [H:MM:SS (secs)]: {}:{:02d}:{:02d} ({})".format(
total_clock_time // (60 * 60),
(total_clock_time // 60) % 60, total_clock_time % 60, total_clock_time),
"Elapsed process time [H:MM:SS (secs)]: {}:{:02d}:{:02d} ({})".format(
total_process_time // (60 * 60),
(total_process_time // 60) % 60, total_process_time % 60, total_process_time),
] + self.summary
else:
self.summary = [
"Elapsed clock time [H:MM:SS (secs)]: {}:{:02d}:{:02d} ({})".format
(total_clock_time // (60*60), (total_clock_time // 60) % 60, total_clock_time % 60, total_clock_time),
"Elapsed process time unvailable on Windows"
"Elapsed clock time [H:MM:SS (secs)]: {}:{:02d}:{:02d} ({})".format(
total_clock_time // (60 * 60), (total_clock_time // 60) % 60, total_clock_time % 60,
total_clock_time), "Elapsed process time unvailable on Windows"
] + self.summary

for line in self.summary:
Expand Down
Loading

0 comments on commit 359fe2c

Please sign in to comment.