Skip to content

Commit

Permalink
Detect unmatched skip begin/end commands (pytorch#1309)
Browse files Browse the repository at this point in the history
* Warn about nesting and dangling skip in updown.py

The updown processor should warn when skip begin/end are garbled

* Update updown.py

adjust nesting level on skip end

* Update run-docs 

replacing llama3 with stories15 meant we got stories15.1.
Fixing for `run-docs readme`
  • Loading branch information
mikekgfb authored Oct 18, 2024
1 parent 8c75754 commit 4f2f4fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .ci/scripts/run-docs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fi

if [ "$1" == "readme" ]; then
echo "::group::Create script to run README"
python3 torchchat/utils/scripts/updown.py --create-sections --file README.md --replace 'llama3:stories15M,-l 3:-l 2' --suppress huggingface-cli,HF_TOKEN > ./run-readme.sh
python3 torchchat/utils/scripts/updown.py --create-sections --file README.md --replace 'llama3.1:stories15M,-l 3:-l 2' --suppress huggingface-cli,HF_TOKEN > ./run-readme.sh
# for good measure, if something happened to updown processor,
# and it did not error out, fail with an exit 1
echo "exit 1" >> ./run-readme.sh
Expand Down
28 changes: 25 additions & 3 deletions torchchat/utils/scripts/updown.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import re

skip_nesting_level = 0

###############################################################################################
###
Expand Down Expand Up @@ -115,7 +116,7 @@ def updown_process_line(
return
if len(options) > 1:
output(
"echo 'cross product of options not yet supported anot line {line} of {filename}'\nexit 1",
"echo 'cross product of options not yet supported in line {line} of {filename}'\nexit 1",
suppress_list=None,
replace_list=None,
)
Expand All @@ -137,7 +138,8 @@ def updown_process_line(
def process_command(
line, lineno, filename, predicate_list, replace_list, suppress_list, create_sections
) -> bool:


global skip_nesting_level
command = r"^\[\s*(\w+)\s+(\w+)\s*\]\s*:\s*(.*)"
match = re.search(command, line)

Expand Down Expand Up @@ -168,12 +170,26 @@ def process_command(
)
elif keyword == "skip":
if trailing_command == "begin":
skip_nesting_level += 1
if skip_nesting_level > 1:
output(
"echo 'nested skips are discouraged in line {lineno} of {filename} and may be prohibited in the future'",
replace_list=replace_list,
suppress_list=suppress_list,
)
output(
"if false; then",
replace_list=replace_list,
suppress_list=suppress_list,
)
elif trailing_command == "end":
skip_nesting_level -= 1
if skip_nesting_level < 0:
output(
"echo 'skip end without matching skip begin in line {lineno} of {filename}'\nexit 1;",
replace_list=replace_list,
suppress_list=suppress_list,
)
output(
"fi",
replace_list=replace_list,
Expand All @@ -187,6 +203,12 @@ def process_command(
)
exit(1)
elif keyword == "end":
if skip_nesting_level > 0:
output(
"echo 'skip begin without matching skip end in line {lineno} of {filename}'\nexit 1;",
replace_list=replace_list,
suppress_list=suppress_list,
)
if create_sections:
output(
"echo '::endgroup::'",
Expand Down Expand Up @@ -303,7 +325,7 @@ def updown_processor(
)

output(
"echo 'reached end of file without exit command'\nexit 1;",
"echo 'reached end of file without `end` command'\nexit 1;",
suppress_list=None,
replace_list=None,
)
Expand Down

0 comments on commit 4f2f4fb

Please sign in to comment.