Skip to content

Commit 6f34b76

Browse files
authored
Properly translate paths from the python package name (#199)
When the `package_name` is changed from the default, we need to make sure we adapt the paths in the generated project to match the new package name, otherwise those packages will not be found. With this general approach, we can also remove the special case for `lib` projects, as it is now handled by the general case. Fix #198.
2 parents bd99332 + 341e141 commit 6f34b76

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929

3030
* Fix typo: `Freqenz` -> `Frequenz`
3131
* Fix `mkdocs.yml` to avoid specifying `custom_templates` for `mkdocstrings` as it is unused and is checked for existence in newer versions.
32+
* Fix paths that are not translated properly from the python package name (#198)

cookiecutter/hooks/post_gen_project.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def finish_setup() -> None:
115115

116116
remove_unneeded_files()
117117

118+
adjust_src_root()
119+
118120
match cookiecutter.type:
119121
case "actor":
120122
finish_actor_setup()
@@ -396,6 +398,27 @@ def remove_unneeded_files() -> None:
396398
_shutil.rmtree("proto")
397399

398400

401+
def adjust_src_root() -> None:
402+
"""Adjust the root of the source code.
403+
404+
Makes the root of the source code match the python package name.
405+
"""
406+
name_as_path = cookiecutter.name.lower().replace("-", "_")
407+
python_package_as_path = cookiecutter.python_package.replace(".", "/")
408+
old_path = _pathlib.Path(f"src/frequenz/{cookiecutter.type}/{name_as_path}")
409+
new_path = _pathlib.Path(f"src/{python_package_as_path}")
410+
411+
if new_path == old_path:
412+
return
413+
414+
recursive_overwrite_move(old_path, new_path)
415+
416+
# Remove the original directories if they were empty
417+
for path in (old_path.parent, old_path.parent.parent):
418+
if not any(path.iterdir()):
419+
path.rmdir()
420+
421+
399422
def is_file_empty(path: _pathlib.Path) -> bool:
400423
"""Check if the file is empty.
401424
@@ -501,20 +524,7 @@ def finish_lib_setup() -> None:
501524
502525
This function is called after the project has been generated and can be used
503526
to perform any additional setup steps that are required for a library.
504-
505-
Operations performed by this function:
506-
507-
* Avoid having a subfolder for the type
508-
509-
- `lib`: `src/frequenz/{name}`
510-
- `rest`: `src/frequenz/{type}/{name}`
511527
"""
512-
name = cookiecutter.name.lower().replace("-", "_")
513-
recursive_overwrite_move(
514-
_pathlib.Path(f"src/frequenz/{cookiecutter.type}/{name}"),
515-
_pathlib.Path(f"src/frequenz/{name}"),
516-
)
517-
_pathlib.Path(f"src/frequenz/{cookiecutter.type}").rmdir()
518528

519529

520530
def finish_model_setup() -> None:

0 commit comments

Comments
 (0)