Skip to content

Commit a6b7dd9

Browse files
committed
Set license in pyproject.toml
Replaces the license JSON data with cookiecutter private dictionary variables. https://cookiecutter.readthedocs.io/en/stable/advanced/private_variables.html https://cookiecutter.readthedocs.io/en/stable/advanced/dict_variables.html
1 parent 912f61b commit a6b7dd9

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

cookiecutter.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"package_display_name": "Package-Name",
3-
"package_name": "{{cookiecutter.package_display_name.lower().replace(' ', '_').replace('-', '_')}}",
3+
"package_name": "{{ cookiecutter.package_display_name.lower().replace(' ', '_').replace('-', '_') }}",
44
"package_short_description": "A description of the package",
55
"version": "0.0.1",
66
"full_name": "Your Name",
@@ -18,5 +18,17 @@
1818
"GPL-3.0-only",
1919
"GPL-3.0-or-later"
2020
],
21-
"year": "{% now 'utc', '%Y' %}"
21+
"year": "{% now 'utc', '%Y' %}",
22+
"_license_map": {
23+
"AGPL-3.0-only": {"source": "AGPL3", "destination": "COPYING"},
24+
"AGPL-3.0-or-later": {"source": "AGPL3", "destination": "COPYING"},
25+
"Apache-2.0": {"source": "APACHE-2.0", "destination": "LICENSE"},
26+
"BSD-3-Clause": {"source": "BSD-3-CLAUSE", "destination": "LICENSE"},
27+
"GPL-2.0-only": {"source": "GPL2", "destination": "COPYING"},
28+
"GPL-2.0-or-later": {"source": "GPL2", "destination": "COPYING"},
29+
"GPL-3.0-only": {"source": "GPL3", "destination": "COPYING"},
30+
"GPL-3.0-or-later": {"source": "GPL3", "destination": "COPYING"}
31+
},
32+
"__license_src": "{{ cookiecutter._license_map.get(cookiecutter.license, {}).get('source', '') }}",
33+
"__license_dest": "{{ cookiecutter._license_map.get(cookiecutter.license, {}).get('destination', '') }}"
2234
}

hooks/post_gen_project.py

+18-22
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
#!/usr/bin/env python3
22
"""Pre-gen project hook script
33
4-
Takes care of deploying the correct license file.
4+
Takes care of setting the correct license file in pyproject.toml.
55
"""
66

7-
import json
87
from pathlib import Path
8+
from shutil import copy
9+
910

1011
PROJECT_DIR = Path(".")
1112
LICENSE_DIR = PROJECT_DIR.joinpath("licenses")
12-
LICENSE_CONFIG = LICENSE_DIR.joinpath("config.json")
13+
LICENSES = {{cookiecutter._license_map | jsonify}}
1314
LICENSE_SELECTED = "{{ cookiecutter.license }}"
15+
SELECTED_LICENSE_SOURCE = "{{ cookiecutter.__license_src }}"
16+
SELECTED_LICENSE_DESTINATION = "{{ cookiecutter.__license_dest }}"
1417

1518

1619
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"""
2121
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()
3733
LICENSE_DIR.rmdir()
3834

3935

{{cookiecutter.package_name}}/licenses/config.json

-10
This file was deleted.

{{cookiecutter.package_name}}/pyproject.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ build-backend = "hatchling.build"
55
[project]
66
name = "{{cookiecutter.package_name}}"
77
dynamic = ["version"]
8+
description = "{{cookiecutter.package_short_description}}"
9+
readme = "README.rst"
810
requires-python = ">= 3.11"
11+
{%- if cookiecutter.license != "Not licensed for distribution (no license)" %}
12+
license = "{{ cookiecutter.license }}"
13+
{%- endif %}
914
dependencies = []
1015
authors = [
1116
{name = "{{cookiecutter.full_name}}", email = "{{cookiecutter.email}}"},
1217
]
1318
keywords=[
1419
"{{cookiecutter.package_name}}"
1520
]
16-
description = "{{cookiecutter.package_short_description}}"
17-
readme = "README.rst"
1821
classifiers = [
1922
"Development Status :: 2 - Pre-Alpha",
2023
"Intended Audience :: Developers",

0 commit comments

Comments
 (0)