Skip to content

Commit 276a3b7

Browse files
authored
issue(medcat-tutorials): CU-869c87xpy Make sure all the tests run against current state of core lib (#350)
* CU-869c87tt9: Add small bit to workflow to check what noteboo patching does * CU-869c87xpy: Add same debug output to migration and relcat workflow * CU-869c87xpy: Fix install target changers * CU-869c87xpy: Apply notebook patch more robustly (i.e quotes) * CU-869c87xpy: Fix regex eating the ending quote in some cases * CU-869c87xpy: Add failure option for notebook patching script * CU-869c87xpy: Remove debug output from workflow * CU-869c87xpy: Make workflow fial if/when there is not enough changes in the notebook patcher * CU-869c87xpy: Remove another debug output and fix more patches * CU-869c87xpy: [TO REMOVE] Add debug output * Revert "CU-869c87xpy: [TO REMOVE] Add debug output" This reverts commit b3eebf9.
1 parent f35e148 commit 276a3b7

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

.github/workflows/medcat-v2-tutorials_main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626

2727
- name: Update install targets in notebooks
2828
run: |
29-
python .ci/patch_notebook_installs.py .
29+
python .ci/patch_notebook_installs.py . --expect-min-changes 4
3030
3131
- name: Install dependencies
3232
run: |
@@ -69,7 +69,7 @@ jobs:
6969

7070
- name: Update install targets in notebooks
7171
run: |
72-
python .ci/patch_notebook_installs.py .
72+
python .ci/patch_notebook_installs.py . --expect-min-changes 4
7373
7474
- name: Install dependencies
7575
run: |

medcat-v2-tutorials/.ci/patch_notebook_installs.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,80 @@
22
import pathlib
33
import re
44
from functools import partial
5+
import argparse
56

67

7-
# rel_install_path = "../medcat-v2/"
8-
# abs_install_path = str(pathlib.Path(rel_install_path).resolve())
8+
rel_install_path = "../medcat-v2/"
9+
abs_install_path = str(pathlib.Path(rel_install_path).resolve())
910

1011
# Matches either:
11-
# 1. `! pip install medcat[extras]`
12+
# 1. `! pip install medcat[extras]~=version`
1213
# 2. `! pip install medcat[extras] @ git+...`
1314
shell_pattern = re.compile(
14-
r'(!\s*pip\s+install\s+)(\\["\']?)medcat(\[.*?\])'
15-
r'(\s*@\s*git\+[^"\'\s]+)?\2'
15+
r'(!\s*pip\s+install\s+)' # group 1: the install command
16+
r'(\\?"?)' # group 2: optional opening \"
17+
r'medcat'
18+
r'(\[.*?\])?' # group 3: optional extras
19+
r'(?:'
20+
r'\s*@\s*git\+[^"\'\s]+'
21+
r'|'
22+
r'\s*[~=!<>][^"\'\\s]*'
23+
r')'
24+
# only match \" (escaped quote), never a bare "
25+
r'(\\")?' # group 4: optional closing \"
1626
)
1727
req_txt_pattern = re.compile(
1828
r'^(medcat(\[.*?\])?)\s*@\s*git\+\S+', flags=re.MULTILINE
1929
)
2030

2131

2232
def repl_nb(m, file_path: pathlib.Path):
23-
# extras = m[3]
24-
old_url = m[4]
25-
if old_url and "medcat/v" in old_url:
26-
print(f"[WARN] {file_path} refers to alpha/tagged release: "
27-
f"{old_url.strip()}")
28-
# to_write = f'{m[1]}\\"{abs_install_path}{extras}\\"'
29-
to_write = '! pip install \\"pip\\"'
33+
extras = m[3] or ""
34+
to_write = f'! pip install \\"{abs_install_path}{extras}\\"'
3035
print(f"[PATCHED] {file_path}\n with: '{to_write}'")
3136
return to_write
3237

3338

3439
def do_patch(nb_path: pathlib.Path,
35-
regex: re.Pattern = shell_pattern, repl_method=repl_nb):
40+
regex: re.Pattern = shell_pattern,
41+
repl_method=repl_nb) -> bool:
3642
nb_text = nb_path.read_text(encoding="utf-8")
3743

3844
repl = partial(repl_method, file_path=nb_path)
3945
new_text = regex.sub(repl, nb_text)
4046

4147
if nb_text != new_text:
4248
nb_path.write_text(new_text, encoding="utf-8")
49+
return True
50+
return False
4351

4452

45-
def main(path: str):
53+
def main(path: str, expect_min_changes: int):
54+
total_changes = 0
4655
for nb_path in pathlib.Path(path).rglob("**/*.ipynb"):
47-
do_patch(nb_path)
56+
if do_patch(nb_path):
57+
total_changes += 1
58+
if expect_min_changes >= 0 and total_changes < expect_min_changes:
59+
print(f"Expected a minimum of {expect_min_changes} changes,"
60+
f"but only found {total_changes} changes. "
61+
"This will force a non-zero exit status so GHA workflow "
62+
"can fail")
63+
sys.exit(1)
4864

4965

5066
if __name__ == "__main__":
51-
if len(sys.argv) != 2:
52-
print("Usage: python patch_notebook_installs.py <path>")
53-
sys.exit(1)
67+
parser = argparse.ArgumentParser()
68+
parser.add_argument("path", help="The path to start looking at",
69+
type=str)
70+
parser.add_argument("--expect-min-changes", "-c",
71+
help="Expect at lest this number of chagnes",
72+
type=int, default=-1)
73+
args = parser.parse_args()
5474

55-
path = sys.argv[1]
75+
path = args.path
5676

5777
if not pathlib.Path(path).exists():
5878
print(f"Path {path} does not exist.")
5979
sys.exit(1)
6080

61-
main(path)
81+
main(path, args.expect_min_changes)

0 commit comments

Comments
 (0)