Skip to content

Commit bb00ebb

Browse files
authored
Merge pull request #1582 from MIS-Analytics/main
Fix issue #1581: _re_proj in update_proj replaces **all** name attributes in pyproject.toml
2 parents 1e755c6 + 849f50b commit bb00ebb

2 files changed

Lines changed: 37 additions & 21 deletions

File tree

nbdev/config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,16 @@ def show_src(src, lang='python'): return Markdown(f'```{lang}\n{src}\n```')
267267
268268
[tool.uv]
269269
cache-keys = [{ file = "pyproject.toml" }, { file = "setup.py" }]
270+
271+
[[tool.uv.index]]
272+
name = "pytorch-cpu"
273+
url = "https://download.pytorch.org/whl/cpu"
274+
explicit = true
270275
"""
271276

272277
# %% ../nbs/api/01_config.ipynb #f1c85f45
273278
_re_version = re.compile(r'^__version__\s*=\s*[\'"]([^\'"]+)[\'"]', re.MULTILINE)
274-
_re_proj = re.compile(r'^name\s*=\s*".*$', re.MULTILINE)
279+
_re_proj = re.compile(r'(\[project\](?:\n(?!\[).*)*?\n)name\s*=\s*"[^"]*"')
275280
_re_reqpy = re.compile(r'^requires-python\s*=\s*".*$', re.MULTILINE)
276281
_init = '__init__.py'
277282
_pyproj = 'pyproject.toml'
@@ -320,7 +325,7 @@ def update_proj(path):
320325
fname = path/_pyproj
321326
if not fname.exists(): fname.write_text(pyproj_tmpl)
322327
txt = fname.read_text()
323-
txt = _re_proj.sub(f'name = "{get_config().lib_name}"', txt)
328+
txt = _re_proj.sub(rf'\1name = "{get_config().lib_name}"', txt)
324329
txt = _re_reqpy.sub(f'requires-python = ">={get_config().min_python}"', txt)
325330
fname.write_text(txt)
326331

nbs/api/01_config.ipynb

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -622,23 +622,7 @@
622622
"execution_count": null,
623623
"id": "6c2a835d",
624624
"metadata": {},
625-
"outputs": [
626-
{
627-
"data": {
628-
"text/markdown": [
629-
"```python\n",
630-
"print(create_output('text', 'text/plain'))\n",
631-
"```"
632-
],
633-
"text/plain": [
634-
"<IPython.core.display.Markdown object>"
635-
]
636-
},
637-
"execution_count": null,
638-
"metadata": {},
639-
"output_type": "execute_result"
640-
}
641-
],
625+
"outputs": [],
642626
"source": [
643627
"show_src(\"print(create_output('text', 'text/plain'))\")"
644628
]
@@ -671,6 +655,11 @@
671655
"\n",
672656
"[tool.uv]\n",
673657
"cache-keys = [{ file = \"pyproject.toml\" }, { file = \"setup.py\" }]\n",
658+
"\n",
659+
"[[tool.uv.index]]\n",
660+
"name = \"pytorch-cpu\"\n",
661+
"url = \"https://download.pytorch.org/whl/cpu\"\n",
662+
"explicit = true\n",
674663
"\"\"\""
675664
]
676665
},
@@ -683,7 +672,7 @@
683672
"source": [
684673
"#| export\n",
685674
"_re_version = re.compile(r'^__version__\\s*=\\s*[\\'\"]([^\\'\"]+)[\\'\"]', re.MULTILINE)\n",
686-
"_re_proj = re.compile(r'^name\\s*=\\s*\".*$', re.MULTILINE)\n",
675+
"_re_proj = re.compile(r'(\\[project\\](?:\\n(?!\\[).*)*?\\n)name\\s*=\\s*\"[^\"]*\"')\n",
687676
"_re_reqpy = re.compile(r'^requires-python\\s*=\\s*\".*$', re.MULTILINE)\n",
688677
"_init = '__init__.py'\n",
689678
"_pyproj = 'pyproject.toml'"
@@ -800,7 +789,7 @@
800789
" fname = path/_pyproj\n",
801790
" if not fname.exists(): fname.write_text(pyproj_tmpl)\n",
802791
" txt = fname.read_text()\n",
803-
" txt = _re_proj.sub(f'name = \"{get_config().lib_name}\"', txt)\n",
792+
" txt = _re_proj.sub(rf'\\1name = \"{get_config().lib_name}\"', txt)\n",
804793
" txt = _re_reqpy.sub(f'requires-python = \">={get_config().min_python}\"', txt)\n",
805794
" fname.write_text(txt)"
806795
]
@@ -827,6 +816,28 @@
827816
" if get_config().get('update_pyproject', True): update_proj(path.parent)"
828817
]
829818
},
819+
{
820+
"cell_type": "code",
821+
"execution_count": null,
822+
"id": "066be7c6",
823+
"metadata": {},
824+
"outputs": [],
825+
"source": [
826+
"with tempfile.TemporaryDirectory() as d:\n",
827+
" d = Path(d)\n",
828+
" cfg_text = (pyproj_tmpl.replace('name = \"FILL_IN\"', 'name = \"testpkg\"').replace('requires-python=\"FILL_IN\"', 'requires-python=\">=3.10\"'))\n",
829+
" cfg_text += '\\n[tool.nbdev]\\n'\n",
830+
" (d/_pyproj).write_text(cfg_text)\n",
831+
" (d/'testpkg').mkdir()\n",
832+
"\n",
833+
" with working_directory(d):\n",
834+
" update_proj(d)\n",
835+
"\n",
836+
" result = (d/_pyproj).read_text()\n",
837+
" assert 'name = \"testpkg\"' in result\n",
838+
" assert 'name = \"pytorch-cpu\"' in result, \"update_proj incorrectly modified the uv index name!\""
839+
]
840+
},
830841
{
831842
"cell_type": "markdown",
832843
"id": "63b789b2",

0 commit comments

Comments
 (0)