Skip to content

Commit b47c9c9

Browse files
cursoragentP4X-ng
andcommitted
Preserve non-generated files during cleanup
Co-authored-by: P4x-ng <P4X-ng@users.noreply.github.com>
1 parent 2140462 commit b47c9c9

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

generator/generate.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,25 @@ def parse(json_path, output_path):
954954
return domains
955955

956956

957+
def cleanup_generated_files(directory: Path,
958+
preserved_names: typing.Iterable[str] = (),
959+
generated_suffix: typing.Optional[str] = None) -> None:
960+
'''
961+
Remove generated files from a directory while preserving dotfiles and any
962+
explicitly listed support files.
963+
'''
964+
for subpath in directory.iterdir():
965+
if not subpath.is_file():
966+
continue
967+
if subpath.name.startswith('.'):
968+
continue
969+
if subpath.name in preserved_names:
970+
continue
971+
if generated_suffix is not None and subpath.suffix != generated_suffix:
972+
continue
973+
subpath.unlink()
974+
975+
957976
def generate_init(init_path, domains):
958977
'''
959978
Generate an ``__init__.py`` that exports the specified modules.
@@ -977,8 +996,7 @@ def generate_docs(docs_path, domains):
977996
logger.info('Generating Sphinx documents')
978997

979998
# Remove generated documents
980-
for subpath in docs_path.iterdir():
981-
subpath.unlink()
999+
cleanup_generated_files(docs_path, generated_suffix='.rst')
9821000

9831001
# Generate document for each domain
9841002
for domain in domains:
@@ -1030,9 +1048,12 @@ def main():
10301048
output_path.mkdir(exist_ok=True)
10311049

10321050
# Remove generated code
1033-
for subpath in output_path.iterdir():
1034-
if subpath.is_file() and subpath.name not in ('py.typed', 'util.py', 'connection.py', '__init__.py') and not subpath.name.startswith('.'):
1035-
subpath.unlink()
1051+
cleanup_generated_files(output_path, preserved_names=(
1052+
'py.typed',
1053+
'util.py',
1054+
'connection.py',
1055+
'__init__.py',
1056+
))
10361057

10371058
# Parse domains
10381059
domains = list()

generator/test_generate.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
codegen tests is almost always easier with the values displayed on stdout.
1111
'''
1212

13+
from pathlib import Path
1314
from textwrap import dedent
1415

15-
from generate import CdpCommand, CdpDomain, CdpEvent, CdpType, docstring
16+
from generate import (
17+
CdpCommand,
18+
CdpDomain,
19+
CdpEvent,
20+
CdpType,
21+
cleanup_generated_files,
22+
docstring,
23+
)
1624

1725

1826
def test_docstring():
@@ -977,3 +985,55 @@ def test_cdp_domain_sphinx():
977985
domain = CdpDomain.from_json(json_domain)
978986
actual = domain.generate_sphinx()
979987
assert expected == actual
988+
989+
990+
def test_cleanup_generated_files_preserves_hidden_and_support_files(tmp_path: Path):
991+
generated = tmp_path / 'generated.py'
992+
util = tmp_path / 'util.py'
993+
connection = tmp_path / 'connection.py'
994+
init_file = tmp_path / '__init__.py'
995+
py_typed = tmp_path / 'py.typed'
996+
hidden = tmp_path / '.bish-index'
997+
subdir = tmp_path / 'nested'
998+
999+
generated.write_text('# generated\n')
1000+
util.write_text('# keep\n')
1001+
connection.write_text('# keep\n')
1002+
init_file.write_text('# keep\n')
1003+
py_typed.write_text('')
1004+
hidden.write_text('keep hidden metadata\n')
1005+
subdir.mkdir()
1006+
1007+
cleanup_generated_files(tmp_path, preserved_names=(
1008+
'py.typed',
1009+
'util.py',
1010+
'connection.py',
1011+
'__init__.py',
1012+
))
1013+
1014+
assert not generated.exists()
1015+
assert util.exists()
1016+
assert connection.exists()
1017+
assert init_file.exists()
1018+
assert py_typed.exists()
1019+
assert hidden.exists()
1020+
assert subdir.exists()
1021+
1022+
1023+
def test_cleanup_generated_files_only_removes_generated_docs(tmp_path: Path):
1024+
generated_doc = tmp_path / 'page.rst'
1025+
hidden = tmp_path / '.bish-index'
1026+
support_file = tmp_path / 'README.txt'
1027+
subdir = tmp_path / 'images'
1028+
1029+
generated_doc.write_text('generated docs\n')
1030+
hidden.write_text('keep hidden metadata\n')
1031+
support_file.write_text('keep support file\n')
1032+
subdir.mkdir()
1033+
1034+
cleanup_generated_files(tmp_path, generated_suffix='.rst')
1035+
1036+
assert not generated_doc.exists()
1037+
assert hidden.exists()
1038+
assert support_file.exists()
1039+
assert subdir.exists()

0 commit comments

Comments
 (0)