|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | """Pre-gen project hook script
|
3 | 3 |
|
4 |
| -Takes care of deploying the correct license file. |
| 4 | +Takes care of setting the correct license file in pyproject.toml. |
5 | 5 | """
|
6 | 6 |
|
7 |
| -import json |
8 | 7 | from pathlib import Path
|
| 8 | +from shutil import copy |
| 9 | + |
9 | 10 |
|
10 | 11 | PROJECT_DIR = Path(".")
|
11 | 12 | LICENSE_DIR = PROJECT_DIR.joinpath("licenses")
|
12 |
| -LICENSE_CONFIG = LICENSE_DIR.joinpath("config.json") |
| 13 | +LICENSES = {{cookiecutter._license_map | jsonify}} |
13 | 14 | LICENSE_SELECTED = "{{ cookiecutter.license }}"
|
| 15 | +SELECTED_LICENSE_SOURCE = "{{ cookiecutter.__license_src }}" |
| 16 | +SELECTED_LICENSE_DESTINATION = "{{ cookiecutter.__license_dest }}" |
14 | 17 |
|
15 | 18 |
|
16 | 19 | def deploy_license() -> None:
|
17 |
| - """Move the selected license file into the project root |
18 |
| -
|
19 |
| - Licenses not selected for the project are removed. |
20 |
| - """ |
| 20 | + """Move the selected license file into the project root""" |
21 | 21 | sources: set[Path] = set()
|
22 |
| - selected_source = None |
23 |
| - with LICENSE_CONFIG.open(mode="r", encoding="utf-8") as config_file: |
24 |
| - config = json.load(config_file) |
25 |
| - for license_option, license_data in config.items(): |
26 |
| - source_path = LICENSE_DIR.joinpath(license_data["source"]) |
27 |
| - if license_option == LICENSE_SELECTED: |
28 |
| - selected_source = source_path |
29 |
| - destination_path = PROJECT_DIR.joinpath(license_data["destination"]) |
30 |
| - source_path.rename(destination_path) |
31 |
| - sources.discard(source_path) |
32 |
| - elif license_data["source"] != selected_source: |
33 |
| - sources.add(source_path) |
34 |
| - for source in sources: |
35 |
| - source.unlink() |
36 |
| - LICENSE_CONFIG.unlink() |
| 22 | + for license_option, license_config in LICENSES.items(): |
| 23 | + source_path = LICENSE_DIR.joinpath(license_config["source"]) |
| 24 | + if license_option == LICENSE_SELECTED: |
| 25 | + destination_path = PROJECT_DIR.joinpath( |
| 26 | + SELECTED_LICENSE_DESTINATION |
| 27 | + ) |
| 28 | + copy(source_path, destination_path) |
| 29 | + elif license_option != SELECTED_LICENSE_SOURCE: |
| 30 | + sources.add(source_path) |
| 31 | + for source in sources: |
| 32 | + source.unlink() |
37 | 33 | LICENSE_DIR.rmdir()
|
38 | 34 |
|
39 | 35 |
|
|
0 commit comments