Skip to content

Commit b40cf06

Browse files
Fix script command
1 parent c5155bc commit b40cf06

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

FABulous/FABulous.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def main():
5050
description="The command line interface for FABulous"
5151
)
5252

53+
script_group = parser.add_mutually_exclusive_group()
54+
5355
parser.add_argument(
5456
"project_dir",
5557
help="The directory to the project folder",
@@ -63,15 +65,16 @@ def main():
6365
help="Create a new project",
6466
)
6567

66-
parser.add_argument(
68+
script_group.add_argument(
6769
"-fs",
6870
"--FABulousScript",
6971
default="",
7072
help="Run FABulous with a FABulous script. A FABulous script is a text file containing only FABulous commands"
7173
"This will automatically exit the CLI once the command finish execution, and the exit will always happen gracefully.",
7274
type=Path,
7375
)
74-
parser.add_argument(
76+
77+
script_group.add_argument(
7578
"-ts",
7679
"--TCLScript",
7780
default="",
@@ -80,6 +83,12 @@ def main():
8083
type=Path,
8184
)
8285

86+
script_group.add_argument(
87+
"-p",
88+
"--commands",
89+
help="execute <commands> (to chain commands, separate them with semicolon + whitespace: 'cmd1; cmd2')",
90+
)
91+
8392
parser.add_argument(
8493
"-log",
8594
default=False,
@@ -186,29 +195,45 @@ def main():
186195
os.getenv("FAB_PROJ_LANG"),
187196
projectDir,
188197
Path().cwd(),
189-
FABulousScript=args.FABulousScript,
190-
TCLScript=args.TCLScript,
191198
force=args.force,
192199
)
193200
fab_CLI.debug = args.debug
194201

195-
if args.verbose == 2:
196-
fab_CLI.verbose = True
197-
if args.metaDataDir:
198-
if Path(args.metaDataDir).exists():
199-
metaDataDir = args.metaDataDir
200-
201-
if args.log:
202-
with open(args.log, "w") as log:
203-
with redirect_stdout(log):
204-
logger.info("Logging to file: " + args.log)
205-
logger.info(f"Setting current working directory to: {projectDir}")
206-
os.chdir(projectDir)
207-
fab_CLI.cmdloop()
202+
if commands := args.commands:
203+
commands = commands.split("; ")
204+
for c in commands:
205+
if fab_CLI.onecmd_plus_hooks(c):
206+
exit(1)
207+
else:
208+
logger.info(
209+
f'Commands "{'; '.join(i.strip() for i in commands)}" executed successfully'
210+
)
211+
exit(0)
212+
elif args.FABulousScript != Path(""):
213+
if fab_CLI.onecmd_plus_hooks(f"run_script {args.FABulousScript}"):
214+
exit(1)
215+
else:
216+
logger.info(
217+
f"FABulous script {args.FABulousScript} executed successfully"
218+
)
219+
exit(0)
220+
elif args.TCLScript != Path(""):
221+
if fab_CLI.onecmd_plus_hooks(f"run_script {args.TCLScript}"):
222+
exit(1)
223+
else:
224+
logger.info(f"TCL script {args.TCLScript} executed successfully")
225+
exit(0)
208226
else:
209-
logger.info(f"Setting current working directory to: {projectDir}")
210-
os.chdir(projectDir)
211-
fab_CLI.cmdloop()
227+
if args.verbose == 2:
228+
fab_CLI.verbose = True
229+
230+
if args.log:
231+
with open(args.log, "w") as log:
232+
with redirect_stdout(log):
233+
fab_CLI.cmdloop()
234+
else:
235+
exit_code = fab_CLI.cmdloop()
236+
exit(exit_code)
212237

213238

214239
if __name__ == "__main__":

FABulous/FABulous_CLI/FABulous_CLI.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ def __init__(
115115
writerType: str | None,
116116
projectDir: Path,
117117
enteringDir: Path,
118-
FABulousScript: Path = Path(),
119-
TCLScript: Path = Path(),
120118
force: bool = False,
121119
):
122120
"""Initialises the FABulous shell instance.
@@ -136,7 +134,6 @@ def __init__(
136134
super().__init__(
137135
persistent_history_file=f"{os.getenv('FAB_PROJ_DIR')}/{META_DATA_DIR}/.fabulous_history",
138136
allow_cli_args=False,
139-
startup_script=str(FABulousScript) if not FABulousScript.is_dir() else "",
140137
)
141138
self.enteringDir = enteringDir
142139

@@ -212,22 +209,6 @@ def __init__(
212209
CMD_HELPER, "Helper commands are disabled until fabric is loaded"
213210
)
214211

215-
if not TCLScript.is_dir() and TCLScript.exists():
216-
self._startup_commands.append(f"run_tcl {Path(TCLScript).absolute()}")
217-
self._startup_commands.append("exit")
218-
elif not TCLScript.is_dir() and not TCLScript.exists():
219-
logger.error(f"Cannot find {TCLScript}")
220-
exit(1)
221-
222-
if not FABulousScript.is_dir() and FABulousScript.exists():
223-
self._startup_commands.append(
224-
f"run_script {Path(FABulousScript).absolute()}"
225-
)
226-
self._startup_commands.append("exit")
227-
elif not FABulousScript.is_dir() and not FABulousScript.exists():
228-
logger.error(f"Cannot find {FABulousScript}")
229-
exit(1)
230-
231212
def onecmd(self, *arg, **kwargs):
232213
"""Override the onecmd method to handle exceptions."""
233214
try:
@@ -637,7 +618,6 @@ def do_gen_top_wrapper(self, *ignored):
637618
logger.info("Generated top wrapper")
638619

639620
@with_category(CMD_FABRIC_FLOW)
640-
@allow_blank
641621
def do_run_FABulous_fabric(self, *ignored):
642622
"""Generates the fabric based on the CSV file, creates bitstream specification
643623
of the fabric, top wrapper of the fabric, Nextpnr model of the fabric and
@@ -646,11 +626,11 @@ def do_run_FABulous_fabric(self, *ignored):
646626
Does this by calling the respective functions 'do_gen_[function]'.
647627
"""
648628
logger.info("Running FABulous")
649-
self.do_gen_fabric()
650-
self.do_gen_bitStream_spec()
651-
self.do_gen_top_wrapper()
652-
self.do_gen_model_npnr()
653-
self.do_gen_geometry()
629+
self.onecmd_plus_hooks("gen_fabric")
630+
self.onecmd_plus_hooks("gen_bitStream_spec")
631+
self.onecmd_plus_hooks("gen_top_wrapper")
632+
self.onecmd_plus_hooks("gen_model_npnr")
633+
self.onecmd_plus_hooks("gen_geometry")
654634
logger.info("FABulous fabric flow complete")
655635
return
656636

@@ -970,9 +950,9 @@ def do_run_FABulous_bitstream(self, args):
970950
else:
971951
logger.info("No external primsLib found.")
972952

973-
self.do_synthesis(do_synth_args)
974-
self.do_place_and_route(str(json_file_path))
975-
self.do_gen_bitStream_binary(str(fasm_file_path))
953+
self.onecmd_plus_hooks(f"synthesis {do_synth_args}")
954+
self.onecmd_plus_hooks(f"place_and_route {json_file_path}")
955+
self.onecmd_plus_hooks(f"gen_bitStream_binary {fasm_file_path}")
976956

977957
@with_category(CMD_SCRIPT)
978958
@with_argparser(filePathRequireParser)

0 commit comments

Comments
 (0)