This is an automated email from the ASF dual-hosted git repository.

mehrdadh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new f7165a1328 [microTVM] Fix tvmc tutorial (#14076)
f7165a1328 is described below

commit f7165a1328d05d08f0e784c311a2d7032f956e8b
Author: Mehrdad Hessar <[email protected]>
AuthorDate: Fri Feb 24 17:14:09 2023 -0800

    [microTVM] Fix tvmc tutorial (#14076)
    
    This PR applies appropriate changes to make sure the CI fails if 
micro_tvmc.sh tutorial fails. This issue was captured in #14074.
    This PR also makes changes to avoid this breakage in bash script tutorials 
in future. In addition, this PR fixes the bug in running TVMC tutorial which 
happened due to renaming zephyr_board to board.
---
 docs/script_convert.py                          | 28 +++++++++++++---------
 gallery/how_to/work_with_microtvm/micro_tvmc.sh | 31 +++++++++++++++----------
 tests/python/ci/test_script_converter.py        | 21 +++++++----------
 tests/scripts/setup-pytest-env.sh               |  2 ++
 4 files changed, 46 insertions(+), 36 deletions(-)

diff --git a/docs/script_convert.py b/docs/script_convert.py
index edd173b295..6b4bd3be4c 100644
--- a/docs/script_convert.py
+++ b/docs/script_convert.py
@@ -20,8 +20,7 @@ import pathlib
 
 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):
@@ -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:
@@ -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)
 
@@ -55,20 +56,24 @@ 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")
@@ -76,8 +81,9 @@ def bash_to_python(src_path: pathlib.Path, dest_path: 
pathlib.Path):
                         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():
diff --git a/gallery/how_to/work_with_microtvm/micro_tvmc.sh 
b/gallery/how_to/work_with_microtvm/micro_tvmc.sh
index f7f27ed182..2da43f17a2 100755
--- a/gallery/how_to/work_with_microtvm/micro_tvmc.sh
+++ b/gallery/how_to/work_with_microtvm/micro_tvmc.sh
@@ -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
@@ -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
 
 ######################################################################
 #
@@ -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
diff --git a/tests/python/ci/test_script_converter.py 
b/tests/python/ci/test_script_converter.py
index e249827afe..2b1edd489e 100644
--- a/tests/python/ci/test_script_converter.py
+++ b/tests/python/ci/test_script_converter.py
@@ -28,13 +28,7 @@ from tvm.contrib import utils
 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
 
@@ -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
 
@@ -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
 
@@ -158,10 +152,11 @@ 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)
@@ -169,7 +164,7 @@ def test_multiline_comment():
     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
 
diff --git a/tests/scripts/setup-pytest-env.sh 
b/tests/scripts/setup-pytest-env.sh
index fbb1ad4cbd..129309f005 100755
--- a/tests/scripts/setup-pytest-env.sh
+++ b/tests/scripts/setup-pytest-env.sh
@@ -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
 }

Reply via email to