Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions docs/script_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

BASH = "# bash"
BASH_IGNORE = "# bash-ignore"
BASH_MULTILINE_COMMENT_START = ": '"
BASH_MULTILINE_COMMENT_END = "'"
BASH_MULTILINE_COMMENT = "# bash-comment"


def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
Expand All @@ -33,6 +32,8 @@ def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
bash_detected = False
bash_ignore_detected = False
new_line_required = False
bash_multiline_comment_detected = False

while line:
line = line.strip("\n").strip("\r")
if bash_detected:
Expand All @@ -42,7 +43,7 @@ def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
dest_f.write("\n")
python_code = "# .. code-block:: bash\n#\n"
for bash_line in bash_block:
python_code += f"#\t {bash_line}\n"
python_code += f"# \t {bash_line}\n"
python_code += "#"
dest_f.write(python_code)

Expand All @@ -55,29 +56,34 @@ def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
elif bash_ignore_detected:
if line == BASH_IGNORE:
bash_ignore_detected = False
new_line_required = True
else:
new_line_required = False
pass
elif bash_multiline_comment_detected:
if line == BASH_MULTILINE_COMMENT:
bash_multiline_comment_detected = False
else:
if line != "#":
assert len(line) > 2, "Detected empty line."
dest_f.write(line[2:])
new_line_required = True
else:
if line == BASH:
bash_detected = True
elif line == BASH_IGNORE:
bash_ignore_detected = True
elif line in [BASH_MULTILINE_COMMENT_START, BASH_MULTILINE_COMMENT_END]:
if new_line_required:
dest_f.write("\n")
dest_f.write('"""')
new_line_required = True
elif line == BASH_MULTILINE_COMMENT:
bash_multiline_comment_detected = True
else:
if new_line_required:
dest_f.write("\n")
dest_f.write(f"{line}")
new_line_required = True

line = src_f.readline()
if new_line_required:
dest_f.write("\n")
if new_line_required:
dest_f.write("\n")
new_line_required = False


def main():
Expand Down
31 changes: 19 additions & 12 deletions gallery/how_to/work_with_microtvm/micro_tvmc.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
Expand All @@ -15,18 +16,24 @@
# specific language governing permissions and limitations
# under the License.

: '
.. _tutorial-micro-cli-tool:

1. microTVM CLI Tool
====================
**Author**: `Mehrdad Hessar <https://github.com/mehrdadh>`_
# bash-ignore
set -euxo pipefail
# bash-ignore

This tutorial explains how to compile a tiny model for a micro device,
build a program on Zephyr platform to execute this model, flash the program
and run the model all using `tvmc micro` command.
You need to install python and Zephyr dependencies before processing with this tutorial.
'
# bash-comment
# """
# .. _tutorial-micro-cli-tool:
#
# 1. microTVM CLI Tool
# ====================
# **Author**: `Mehrdad Hessar <https://github.com/mehrdadh>`_
#
# This tutorial explains how to compile a tiny model for a micro device,
# build a program on Zephyr platform to execute this model, flash the program
# and run the model all using `tvmc micro` command.
# You need to install python and Zephyr dependencies before processing with this tutorial.
# """
# bash-comment

######################################################################
#
Expand Down Expand Up @@ -126,7 +133,7 @@ tvmc micro create \
project \
model.tar \
zephyr \
--project-option project_type=host_driven zephyr_board=qemu_x86
--project-option project_type=host_driven board=qemu_x86
# bash
# This will generate a ``Host-Driven`` Zephyr project for ``qemu_x86`` Zephyr board. In Host-Driven template project,
# the Graph Executor will run on host and perform the model execution on Zephyr device by issuing commands to the
Expand Down
21 changes: 8 additions & 13 deletions tests/python/ci/test_script_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@
from .test_utils import REPO_ROOT

sys.path.insert(0, str(REPO_ROOT / "docs"))
from script_convert import (
bash_to_python,
BASH,
BASH_IGNORE,
BASH_MULTILINE_COMMENT_START,
BASH_MULTILINE_COMMENT_END,
)
from script_convert import bash_to_python, BASH, BASH_IGNORE, BASH_MULTILINE_COMMENT

# pylint: enable=wrong-import-position,wrong-import-order

Expand All @@ -57,7 +51,7 @@ def test_bash_cmd():
with open(dest_path, "r") as dest_f:
generated_cmd = dest_f.read()

expected_cmd = "# .. code-block:: bash\n" "#\n" "#\t tvmc\n" "#\n"
expected_cmd = "# .. code-block:: bash\n" "#\n" "# \t tvmc\n" "#\n"

assert generated_cmd == expected_cmd

Expand Down Expand Up @@ -126,7 +120,7 @@ def test_text_and_bash_command():
with open(dest_path, "r") as dest_f:
generated_cmd = dest_f.read()

expected_cmd = "# start\n" "# .. code-block:: bash\n" "#\n" "#\t tvmc\n" "#\n" "# end\n"
expected_cmd = "# start\n" "# .. code-block:: bash\n" "#\n" "# \t tvmc\n" "#\n" "# end\n"

assert generated_cmd == expected_cmd

Expand Down Expand Up @@ -158,18 +152,19 @@ def test_multiline_comment():
dest_path = temp / "dest.py"

with open(src_path, "w") as src_f:
src_f.write(BASH_MULTILINE_COMMENT_START)
src_f.write(BASH_MULTILINE_COMMENT)
src_f.write("\n")
src_f.write("comment\n")
src_f.write(BASH_MULTILINE_COMMENT_END)
src_f.write('# """\n')
src_f.write("# comment\n")
src_f.write(BASH_MULTILINE_COMMENT)
src_f.write("\n")

bash_to_python(src_path, dest_path)

with open(dest_path, "r") as dest_f:
generated_cmd = dest_f.read()

expected_cmd = '"""\n' "comment\n" '"""\n'
expected_cmd = '"""\ncomment\n'

assert generated_cmd == expected_cmd

Expand Down
2 changes: 2 additions & 0 deletions tests/scripts/setup-pytest-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ function run_pytest() {
if [ "$exit_code" -ne "0" ] && [ "$exit_code" -ne "5" ]; then
pytest_errors+=("${suite_name}: $@")
fi
# To avoid overwriting.
set -e
}