-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_toml.py
276 lines (187 loc) · 6.66 KB
/
generate_toml.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# region Pre-Defined
import os
from pydantic import BaseModel
from crimson.templator import format_insert, format_indent, format_insert_loop
from typing import List
import shutil
import json
topics_t = r""""\\[topic\\]",
"""
dependencies_t = r'"\\[dependency\\]",'
template = r"""[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "\[name_space\]-\[module_name\]"
version = "\[version\]"
description = "\[description\]"
readme = "README.md"
authors = [
{ name="\[name\]", email="\[email\]" },
]
classifiers = [
"Development Status :: 2 - Pre-Alpha",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Intended Audience :: Developers",
\{topics_f\}
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Typing :: Typed",
]
dependencies = [
\{dependencies_f\}
]
requires-python = ">=3.9"
[project.urls]
"Homepage" = "https://github.com/\[github_id\]/\[repo_name\]"
"Bug Tracker" = "https://github.com/\[github_id\]/\[repo_name\]/issues"
"""
class Kwargs(BaseModel):
name: str = "Sisung Kim"
email: str = "[email protected]"
github_id: str = "crimson206"
repo_name: str
version: str
name_space: str
module_name: str
description: str
topics: List[str]
dependencies: List[str]
class Options(BaseModel):
discussion: bool = False
def add_options(template: str, options: Options) -> str:
if options.discussion:
discussion_block = r'''"Discussion" = "https://github.com/\[github_id\]/\[module_name\]/discussions"'''
template += discussion_block
return template
# endregion
# ******************************************************
# region Utils
def create_skeleton(name_space: str, module_name: str, base_dir: str) -> None:
module_name = module_name.replace("-", "_")
path = os.path.join(
base_dir,
f"src/{name_space}/{module_name}",
)
os.makedirs(path, exist_ok=True)
with open(f"{path}/__init__.py", "w") as f:
f.write("# Init file for the module")
setup_env_template = r"""\[bin_bash\]
read -p "Please enter the Python version you want to use (e.g., 3.9): " PYTHON_VERSION
conda create --name \[module_name\] python=$PYTHON_VERSION -y
conda activate \[module_name\]
pip install -r requirements.txt
pip install -r requirements_test.txt
pip install -r requirements_dev.txt
"""
def generate_setup_env_script(
module_name: str, setup_env_template: str, base_dir: str
) -> None:
dir = os.path.join(base_dir, "scripts")
os.makedirs(dir, exist_ok=True)
path = f"{dir}/setup_env.sh"
with open(path, "w") as file:
script = format_insert(
setup_env_template, module_name=module_name, bin_bash="# !bin/bash"
)
file.write(script)
print(
f"Now, you can access the module name {module_name} in your terminal by $MODULE_NAME"
)
print("To generate an conda env for your new module, run following command.")
print(f"cd {base_dir}")
print("source scripts/setup_env.sh")
def generate_requirements(dependencies_f: str, base_dir: str):
os.makedirs(base_dir, exist_ok=True)
dependencies_f = dependencies_f.replace('"', "").replace(",", "")
with open(f"{base_dir}/requirements.txt", "w") as file:
file.write(dependencies_f)
def generate_toml(template: str, kwargs: Kwargs, base_dir: str):
template: str = add_options(template, options=options)
pyproject_body: str = format_insert(template, **kwargs.model_dump())
topics_f: str = format_insert_loop(topics_t, kwargs_list={"topic": kwargs.topics})
dependencies_f: str = format_insert_loop(
dependencies_t, kwargs_list={"dependency": kwargs.dependencies}
)
if dependencies_f == '"",':
dependencies_f = ""
pyproject_body: str = format_indent(
pyproject_body,
topics_f=topics_f,
dependencies_f=dependencies_f,
)
os.makedirs(base_dir, exist_ok=True)
path = os.path.join(base_dir, "pyproject.toml")
with open(path, "w") as file:
file.write(pyproject_body)
def copy_and_paste_extra_requirements(base_dir: str):
files = ["requirements_dev.txt", "requirements_test.txt"]
for file in files:
dest = os.path.join(base_dir, file)
shutil.copy(file, dest)
def build_setup(template: str, kwargs: Kwargs, base_dir: str) -> None:
kwargs_skeleton = kwargs.model_copy()
kwargs_skeleton.name_space = kwargs_skeleton.name_space.replace("-", "/")
dependencies_f: str = format_insert_loop(
dependencies_t, kwargs_list={"dependency": kwargs.dependencies}
)
generate_toml(template=template, kwargs=kwargs, base_dir=base_dir)
create_skeleton(
name_space=kwargs_skeleton.name_space,
module_name=kwargs_skeleton.module_name,
base_dir=base_dir,
)
generate_setup_env_script(
module_name=kwargs.module_name,
setup_env_template=setup_env_template,
base_dir=base_dir,
)
generate_requirements(
dependencies_f=dependencies_f,
base_dir=base_dir,
)
copy_and_paste_extra_requirements(base_dir)
def generate_repo_info(github_id: str, repo_name: str, **kwarg_safe) -> None:
info_to_env_json = {
"repo-folder-root": f"https://github.com/{github_id}/{repo_name}/blob/main/"
}
json_string = json.dumps(info_to_env_json, indent=2)
os.makedirs("env", exist_ok=True)
open("env/env.json", "w").write(json_string)
# endregion
# ******************************************************
# region User Setup
options = Options(
# Will you use the discussion session in your repo?
discussion=False
)
dependencies = """
"""
def split_dependencies(dependencies: str):
return dependencies.strip().split("\n")
dependencies = split_dependencies(dependencies)
# Define the general information of your package
repo_name = module_name = "module_name"
kwargs = Kwargs(
name="Sisung Kim",
email="[email protected]",
github_id="crimson206",
repo_name=repo_name,
version="0.1.0",
name_space="crimson",
module_name=module_name,
description="Your package description.",
topics=["Topic :: Software Development :: Libraries :: Python Modules"],
dependencies=dependencies,
)
# endregion
# ******************************************************
# region Execution
build_setup(template, kwargs, base_dir="stable")
kwargs.module_name = kwargs.module_name + "-beta"
build_setup(template, kwargs, base_dir="beta")
generate_repo_info(**kwargs.model_dump())