Add tasks in .sby files
authorClifford Wolf <clifford@clifford.at>
Mon, 5 Mar 2018 12:09:20 +0000 (13:09 +0100)
committerClifford Wolf <clifford@clifford.at>
Mon, 5 Mar 2018 12:09:20 +0000 (13:09 +0100)
Signed-off-by: Clifford Wolf <clifford@clifford.at>
sbysrc/sby.py
sbysrc/sby_core.py

index e643173f8de07087b4c5a470d432635d6c2a7816..03f98c370072fda9c12f387c57ca7b5d80779384 100644 (file)
@@ -23,6 +23,7 @@ from sby_core import SbyJob
 
 sbyfile = None
 workdir = None
+taskname = None
 opt_force = False
 opt_backup = False
 opt_tmpdir = False
@@ -30,7 +31,7 @@ exe_paths = dict()
 
 def usage():
     print("""
-sby [options] [<jobname>.sby]
+sby [options] [<jobname>.sby [taskname]]
 
     -d <dirname>
         set workdir name. default: <jobname> (without .sby)
@@ -44,6 +45,9 @@ sby [options] [<jobname>.sby]
     -t
         run in a temporary workdir (remove when finished)
 
+    -T taskname
+        set the taskname (useful when sby file is read from stdin)
+
     --yosys <path_to_executable>
     --abc <path_to_executable>
     --smtbmc <path_to_executable>
@@ -55,7 +59,7 @@ sby [options] [<jobname>.sby]
     sys.exit(1)
 
 try:
-    opts, args = getopt.getopt(sys.argv[1:], "d:btf", ["yosys=",
+    opts, args = getopt.getopt(sys.argv[1:], "d:btfT:", ["yosys=",
             "abc=", "smtbmc=", "suprove=", "aigbmc=", "avy="])
 except:
     usage()
@@ -69,6 +73,8 @@ for o, a in opts:
         opt_backup = True
     elif o == "-t":
         opt_tmpdir = True
+    elif o == "-T":
+        taskname = a
     elif o == "--yosys":
         exe_paths["yosys"] = a
     elif o == "--abc":
@@ -84,13 +90,17 @@ for o, a in opts:
     else:
         usage()
 
-if len(args) > 1:
+if len(args) > 2:
     usage()
 
-if len(args) == 1:
+if len(args) > 0:
     sbyfile = args[0]
     assert sbyfile.endswith(".sby")
 
+if len(args) > 1:
+    assert taskname is None
+    taskname = args[1]
+
 early_logmsgs = list()
 
 def early_log(msg):
@@ -99,6 +109,8 @@ def early_log(msg):
 
 if workdir is None and sbyfile is not None and not opt_tmpdir:
     workdir = sbyfile[:-4]
+    if taskname is not None:
+        workdir += "_" + taskname
 
 if workdir is not None:
     if opt_backup:
@@ -119,7 +131,7 @@ else:
     opt_tmpdir = True
     workdir = tempfile.mkdtemp()
 
-job = SbyJob(sbyfile, workdir, early_logmsgs)
+job = SbyJob(sbyfile, taskname, workdir, early_logmsgs)
 
 for k, v in exe_paths.items():
     job.exe_paths[k] = v
index 95d11a1662b8e9857d63191c2f30b5ef7df8fd16..eb6378350a09a0dea41ac77985e526fe2b08f3e1 100644 (file)
@@ -126,7 +126,7 @@ class SbyTask:
 
 
 class SbyJob:
-    def __init__(self, filename, workdir, early_logs):
+    def __init__(self, filename, taskname, workdir, early_logs):
         self.options = dict()
         self.used_options = set()
         self.engines = list()
@@ -169,16 +169,66 @@ class SbyJob:
         with (open(filename, "r") if filename else sys.stdin) as f:
             with open("%s/config.sby" % workdir, "w") as cfgfile:
                 pycode = None
+                tasks_section = False
+                task_tags_active = set()
+                task_tags_all = set()
+                task_skip_block = False
                 for line in f:
                     line = line.rstrip("\n")
                     line = line.rstrip("\r")
-                    if line == "--pycode-begin--":
+
+                    if tasks_section and line.startswith("["):
+                        tasks_section = False
+
+                    tasks_skip = False
+                    if task_skip_block:
+                        if line == "":
+                            task_skip_block = False
+                    else:
+                        for t in task_tags_all:
+                            if line.startswith(t+":"):
+                                line = line[len(t)+1:].lstrip()
+                                if t not in task_tags_active:
+                                    if line == "":
+                                        task_skip_block = True
+                                    tasks_skip = True
+                                break
+                            if line.startswith("~"+t+":"):
+                                line = line[len(t)+2:].lstrip()
+                                if t in task_tags_active:
+                                    if line == "":
+                                        task_skip_block = True
+                                    tasks_skip = True
+                                break
+
+                    if tasks_skip or task_skip_block:
+                        continue
+
+                    if tasks_section:
+                        line = line.split()
+                        if len(line) > 0:
+                            if taskname is None:
+                                taskname = line[0]
+                                self.log("Configuration file contains tasks. Running default task '%s'." % taskname)
+                        for t in line:
+                            if taskname == line[0]:
+                                task_tags_active.add(t)
+                            task_tags_all.add(t)
+
+                    elif line == "[tasks]":
+                        tasks_section = True
+
+                    elif line == "--pycode-begin--":
                         pycode = ""
+
                     elif line == "--pycode-end--":
                         gdict = globals().copy()
                         gdict["cfgfile"] = cfgfile
+                        gdict["filename"] = filename
+                        gdict["taskname"] = taskname
                         exec("def output(*args, **kwargs):\n  print(*args, **kwargs, file=cfgfile)\n" + pycode, gdict)
                         pycode = None
+
                     else:
                         if pycode is None:
                             print(line, file=cfgfile)