-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathpost_gen_project.py
117 lines (99 loc) · 3.51 KB
/
post_gen_project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import shutil
from copy import copy
from pathlib import Path
# https://github.com/cookiecutter/cookiecutter/issues/824
# our workaround is to include these utility functions in the CCDS package
from ccds.hook_utils.custom_config import write_custom_config
from ccds.hook_utils.dependencies import (
basic,
flake8_black_isort,
packages,
ruff,
scaffold,
write_dependencies,
)
#
# TEMPLATIZED VARIABLES FILLED IN BY COOKIECUTTER
#
packages_to_install = copy(packages)
# {% if cookiecutter.dataset_storage.s3 %}
packages_to_install += ["awscli"]
# {% endif %} #
# {% if cookiecutter.include_code_scaffold == "Yes" %}
packages_to_install += scaffold
# {% endif %}
# {% if cookiecutter.pydata_packages == "basic" %}
packages_to_install += basic
# {% endif %}
# {% if cookiecutter.linting_and_formatting == "ruff" %}
packages_to_install += ruff
# Remove setup.cfg
Path("setup.cfg").unlink()
# {% elif cookiecutter.linting_and_formatting == "flake8+black+isort" %}
packages_to_install += flake8_black_isort
# {% endif %}
# track packages that are not available through conda
pip_only_packages = [
"awscli",
"python-dotenv",
]
# Select testing framework
tests_path = Path("tests")
# {% if cookiecutter.testing_framework == "pytest" %}
packages_to_install += ["pytest"]
# {% endif %}
# {% if cookiecutter.testing_framework == "none" %}
shutil.rmtree(tests_path)
# {% else %}
tests_subpath = tests_path / "{{ cookiecutter.testing_framework }}"
for obj in tests_subpath.iterdir():
shutil.move(str(obj), str(tests_path))
# Remove all remaining tests templates
for tests_template in tests_path.iterdir():
if tests_template.is_dir() and not tests_template.name == "tests":
shutil.rmtree(tests_template)
# {% endif %}
# Use the selected documentation package specified in the config,
# or none if none selected
docs_path = Path("docs")
# {% if cookiecutter.docs != "none" %}
packages_to_install += ["{{ cookiecutter.docs }}"]
pip_only_packages += ["{{ cookiecutter.docs }}"]
docs_subpath = docs_path / "{{ cookiecutter.docs }}"
for obj in docs_subpath.iterdir():
shutil.move(str(obj), str(docs_path))
# {% endif %}
# Remove all remaining docs templates
for docs_template in docs_path.iterdir():
if docs_template.is_dir() and not docs_template.name == "docs":
shutil.rmtree(docs_template)
#
# POST-GENERATION FUNCTIONS
#
write_dependencies(
"{{ cookiecutter.dependency_file }}",
packages_to_install,
pip_only_packages,
repo_name="{{ cookiecutter.repo_name }}",
module_name="{{ cookiecutter.module_name }}",
python_version="{{ cookiecutter.python_version_number }}",
)
write_custom_config("{{ cookiecutter.custom_config }}")
# Remove LICENSE if "No license file"
if "{{ cookiecutter.open_source_license }}" == "No license file":
Path("LICENSE").unlink()
# Make single quotes prettier
# Jinja tojson escapes single-quotes with \u0027 since it's meant for HTML/JS
pyproject_text = Path("pyproject.toml").read_text()
Path("pyproject.toml").write_text(pyproject_text.replace(r"\u0027", "'"))
# {% if cookiecutter.include_code_scaffold == "No" %}
# remove everything except __init__.py so result is an empty package
for generated_path in Path("{{ cookiecutter.module_name }}").iterdir():
if generated_path.is_dir():
shutil.rmtree(generated_path)
elif generated_path.name != "__init__.py":
generated_path.unlink()
elif generated_path.name == "__init__.py":
# remove any content in __init__.py since it won't be available
generated_path.write_text("")
# {% endif %}