-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_gen_project.py
174 lines (134 loc) · 5.05 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
"""cookiecutter-py3-package post package generation jobs."""
import os
import subprocess # nosec
PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)
REMOTE_REPO = "git@github.com:{{cookiecutter.github_username}}/\
{{cookiecutter.git_project_name}}.git"
GIT_USER = "{{cookiecutter.full_name}}"
GIT_EMAIL = "{{cookiecutter.email}}"
def post_gen_setup(*args, supress_exception=False, cwd=None):
"""Helper to set up the package with the chosen options."""
cur_dir = os.getcwd()
try:
if cwd:
os.chdir(cwd)
with subprocess.Popen( # nosec
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) as proc:
out, err = proc.communicate()
out = out.decode("utf-8")
err = err.decode("utf-8")
if err and not supress_exception:
raise Exception(err)
if err and supress_exception:
return out
return out
finally:
os.chdir(cur_dir)
def init_git():
"""Initialise git repository and set the remote."""
if not os.path.exists(os.path.join(PROJECT_DIRECTORY, ".git")):
post_gen_setup(
"git",
"init",
"--initial-branch=main",
cwd=PROJECT_DIRECTORY,
)
post_gen_setup(
"git",
"remote",
"add",
"origin",
REMOTE_REPO,
cwd=PROJECT_DIRECTORY,
)
post_gen_setup(
"git",
"config",
"user.name",
GIT_USER,
cwd=PROJECT_DIRECTORY,
)
post_gen_setup(
"git",
"config",
"user.email",
GIT_EMAIL,
cwd=PROJECT_DIRECTORY,
)
def git_add_and_commit_initial():
"""Add the local files and commit to the git repository."""
post_gen_setup(
"git",
"add",
"-A",
cwd=PROJECT_DIRECTORY,
)
post_gen_setup(
"git",
"commit",
"-m",
'"chore(git): Initial Commit"',
cwd=PROJECT_DIRECTORY,
)
def git_configure_custom_commit_message():
"""Configure git to use the custom commit message template."""
if os.path.exists(os.path.join(PROJECT_DIRECTORY, ".git")):
post_gen_setup(
"git",
"config",
"--local",
"commit.template",
".github/.git-commit-template.txt",
cwd=PROJECT_DIRECTORY,
)
def remove_file(filepath):
"""Remove files not required for this generated python package."""
if os.path.exists(os.path.join(PROJECT_DIRECTORY, filepath)):
os.remove(os.path.join(PROJECT_DIRECTORY, filepath))
if __name__ == "__main__":
if "{{ cookiecutter.create_author_file }}" != "y":
remove_file("AUTHORS.rst")
remove_file("docs/source/authors.rst")
if "no" in "{{ cookiecutter.command_line_interface|lower }}":
cli_file = os.path.join("{{ cookiecutter.project_slug }}", "cli.py")
remove_file(cli_file)
if "Not open source" == "{{ cookiecutter.open_source_license }}":
remove_file("LICENSE")
if "{{ cookiecutter.create_conventional_commits_edit_message }}" != "y":
remove_file(".github/.git-commit-template.txt")
if "{{ cookiecutter.create_auto_CHANGELOG }}" != "y":
remove_file(".github/workflows/update-changelog.yaml")
if "{{ cookiecutter.create_auto_CHANGELOG }}" == "y":
remove_file("HISTORY.rst")
if "{{ cookiecutter.create_repo_auto_test_workflow }}" != "y":
remove_file(".github/workflows/test_contribution.yaml")
if "{{ cookiecutter.use_GH_action_semantic_version }}" != "y":
remove_file(".github/workflows/semantic_release.yaml")
remove_file(".github/semantic.yaml")
if "{{ cookiecutter.use_GH_action_semantic_version }}" == "y":
remove_file("HISTORY.rst")
if (
"{{ cookiecutter.use_GH_action_semantic_version }}" != "y"
and "{{ cookiecutter.create_auto_CHANGELOG }}" != "y"
):
remove_file("CHANGELOG.md")
if "{{ cookiecutter.use_GH_custom_issue_templates }}" != "y":
remove_file(".github/ISSUE_TEMPLATE/bug-report.md")
remove_file(".github/ISSUE_TEMPLATE/chore.md")
remove_file(".github/ISSUE_TEMPLATE/documentation-request.md")
remove_file(".github/ISSUE_TEMPLATE/feature-request.md")
if "{{ cookiecutter.use_GH_custom_issue_templates }}" == "y":
remove_file(".github/ISSUE_TEMPLATE.md")
if "{{ cookiecutter.use_pre_commit }}" != "y":
remove_file(".pre-commit-config.yaml")
if "{{ cookiecutter.automatic_set_up_git_and_initial_commit }}" == "y":
init_git()
git_add_and_commit_initial()
if "{{ cookiecutter.create_conventional_commits_edit_message}}" == "y":
git_configure_custom_commit_message()
if "{{ cookiecutter.use_release_to_test_pypi_with_tags }}" != "y":
remove_file(".github/workflows/semantic_release_test_pypi.yaml")
if "{{ cookiecutter.use_pyup_io }}" != "y":
remove_file(".pyup.yml")