Skip to content

Commit 4f2f4fb

Browse files
authored
Detect unmatched skip begin/end commands (#1309)
* 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`
1 parent 8c75754 commit 4f2f4fb

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

.ci/scripts/run-docs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fi
77

88
if [ "$1" == "readme" ]; then
99
echo "::group::Create script to run README"
10-
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
10+
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
1111
# for good measure, if something happened to updown processor,
1212
# and it did not error out, fail with an exit 1
1313
echo "exit 1" >> ./run-readme.sh

torchchat/utils/scripts/updown.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import re
1111

12+
skip_nesting_level = 0
1213

1314
###############################################################################################
1415
###
@@ -115,7 +116,7 @@ def updown_process_line(
115116
return
116117
if len(options) > 1:
117118
output(
118-
"echo 'cross product of options not yet supported anot line {line} of {filename}'\nexit 1",
119+
"echo 'cross product of options not yet supported in line {line} of {filename}'\nexit 1",
119120
suppress_list=None,
120121
replace_list=None,
121122
)
@@ -137,7 +138,8 @@ def updown_process_line(
137138
def process_command(
138139
line, lineno, filename, predicate_list, replace_list, suppress_list, create_sections
139140
) -> bool:
140-
141+
142+
global skip_nesting_level
141143
command = r"^\[\s*(\w+)\s+(\w+)\s*\]\s*:\s*(.*)"
142144
match = re.search(command, line)
143145

@@ -168,12 +170,26 @@ def process_command(
168170
)
169171
elif keyword == "skip":
170172
if trailing_command == "begin":
173+
skip_nesting_level += 1
174+
if skip_nesting_level > 1:
175+
output(
176+
"echo 'nested skips are discouraged in line {lineno} of {filename} and may be prohibited in the future'",
177+
replace_list=replace_list,
178+
suppress_list=suppress_list,
179+
)
171180
output(
172181
"if false; then",
173182
replace_list=replace_list,
174183
suppress_list=suppress_list,
175184
)
176185
elif trailing_command == "end":
186+
skip_nesting_level -= 1
187+
if skip_nesting_level < 0:
188+
output(
189+
"echo 'skip end without matching skip begin in line {lineno} of {filename}'\nexit 1;",
190+
replace_list=replace_list,
191+
suppress_list=suppress_list,
192+
)
177193
output(
178194
"fi",
179195
replace_list=replace_list,
@@ -187,6 +203,12 @@ def process_command(
187203
)
188204
exit(1)
189205
elif keyword == "end":
206+
if skip_nesting_level > 0:
207+
output(
208+
"echo 'skip begin without matching skip end in line {lineno} of {filename}'\nexit 1;",
209+
replace_list=replace_list,
210+
suppress_list=suppress_list,
211+
)
190212
if create_sections:
191213
output(
192214
"echo '::endgroup::'",
@@ -303,7 +325,7 @@ def updown_processor(
303325
)
304326

305327
output(
306-
"echo 'reached end of file without exit command'\nexit 1;",
328+
"echo 'reached end of file without `end` command'\nexit 1;",
307329
suppress_list=None,
308330
replace_list=None,
309331
)

0 commit comments

Comments
 (0)