-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchange_project_attributes.py
More file actions
219 lines (188 loc) · 6.84 KB
/
change_project_attributes.py
File metadata and controls
219 lines (188 loc) · 6.84 KB
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
r"""
This script is used to change the name of a project. It is useful when you want to change the name of a project and you
want to update all the references to the old name in the project.
You can delete this script after you have changed the name of the project.
"""
import json
import os
import re
import shutil
import sys
import glob
import datetime
import argparse
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--project_name",
default=None,
type=str,
help="New name of the project",
required=True,
)
parser.add_argument(
"--package_name",
default=None,
type=str,
help="New name of the package",
required=False,
)
parser.add_argument(
"--author",
default=None,
type=str,
help="Author of the project",
required=True,
)
parser.add_argument(
"--email",
default=None,
type=str,
help="Email of the author of the project",
required=True,
)
parser.add_argument(
"--url",
default=None,
type=str,
help="URL of the project",
required=False,
)
return parser
def update_pyproject_toml(args):
with open("pyproject.toml", "r") as f:
content = f.read()
if args.project_name is not None:
content = re.sub(r'name = "(.*?)"', f'name = "{args.project_name}"', content)
if args.author is not None:
if args.email is None:
new_author = 'authors = [{name = "%s"},]' % args.author
else:
new_author = 'authors = [{name = "%s", email = "%s"},]' % (args.author, args.email)
print(f"Setting author to {new_author}")
content = re.sub(r'authors = \[.*?]', new_author, content)
if args.package_name is not None:
content = re.sub(
r'packages = \[{include = ".*?", from="src"\}]',
f'packages = [{{include = "{args.package_name}", from="src"}}]',
content
)
with open("pyproject.toml", "w") as f:
f.write(content)
return 0
def update_init_file(args):
# find first __init__.py file in src directory
init_file = glob.glob("src/**/__init__.py", recursive=True)[0]
with open(init_file, "r") as f:
content = f.read()
if args.author is not None:
content = re.sub(r'__author__ = "(.*?)"', f'__author__ = "{args.author}"', content)
year = datetime.datetime.now().year
content = re.sub(r'__copyright__ = "(.*?)"', f'__copyright__ = "Copyright {year}, {args.author}"', content)
if args.email is not None:
content = re.sub(r'__email__ = "(.*?)"', f'__email__ = "{args.email}"', content)
if args.url is not None:
content = re.sub(r'__url__ = "(.*?)"', f'__url__ = "{args.url}"', content)
if args.package_name is not None:
content = re.sub(r'__package__ = "(.*?)"', f'__package__ = "{args.package_name}"', content)
with open(init_file, "w") as f:
f.write(content)
return 0
def update_pkg_folder_name(args):
if args.package_name is not None:
# get the first directory in src
pkg_dir = glob.glob("src/*")[0]
os.rename(pkg_dir, f"src/{args.package_name}")
return 0
def update_sphinx_conf_py(args):
with open("sphinx/source/conf.py", "r") as f:
content = f.read()
if args.project_name is not None:
content = re.sub(r'project = "(.*?)"', f'project = "{args.project_name}"', content)
if args.package_name is not None:
content = re.sub(r'import (.*?) as pkg', f'import {args.package_name} as pkg', content)
with open("sphinx/source/conf.py", "w") as f:
f.write(content)
return 0
def update_sphinx_index_rst(args):
with open("sphinx/source/index.rst", "r") as f:
content = f.read()
if args.project_name is not None:
content = re.sub(r'Welcome to (.*?)’s documentation!', f'Welcome to {args.project_name}’s documentation!', content)
if args.package_name is not None:
content = re.sub(r'.. automodule:: pkg', f'.. automodule:: {args.package_name}', content)
with open("sphinx/source/index.rst", "w") as f:
f.write(content)
return 0
def update_sphinx_modules(args):
with open("sphinx/source/modules.rst", "r") as f:
content = f.read()
if args.package_name is not None:
content = re.sub(r'pkg', args.package_name, content)
with open("sphinx/source/modules.rst", "w") as f:
f.write(content)
return 0
def update_readme_md(args):
with open("README.md", "r") as f:
content = f.read()
if args.project_name is not None:
content = re.sub(r'# PythonProject-Template', f'# {args.project_name}\n', content)
with open("README.md", "w") as f:
f.write(content)
return 0
def update_license(args):
if not os.path.exists("LICENSE"):
return 0
with open("LICENSE", "r") as f:
content = f.read()
if args.author is not None:
content = re.sub(r'Copyright 2024 Jeremie Gince', f'Copyright {datetime.datetime.now().year} {args.author}', content)
with open("LICENSE", "w") as f:
f.write(content)
return 0
def update_docs_yml(args):
if not os.path.exists(".github/workflows/docs.yml"):
return 0
with open(".github/workflows/docs.yml", "r") as f:
content = f.read()
if args.package_name is not None:
content = re.sub(r'<package_name>', args.package_name, content)
with open(".github/workflows/docs.yml", "w") as f:
f.write(content)
return 0
def update_dockerfile(args):
if not os.path.exists("Dockerfile"):
return 0
with open("Dockerfile", "r") as f:
content = f.read()
root_dir = os.path.basename(os.path.dirname(__file__))
print(f"ROOT_DIR=/{root_dir}")
content = re.sub(r'ENV ROOT_DIR=/(.*?)\n', f'ENV ROOT_DIR=/{root_dir}\n', content)
with open("Dockerfile", "w") as f:
f.write(content)
return 0
def main():
parser = get_parser()
args = parser.parse_args()
if args.package_name is None:
# Camel case to snake case
args.package_name = re.sub(r'(?<!^)(?=[A-Z])', '_', args.project_name).lower()
print(f"Package name not provided. Setting package name to {args.package_name}")
if args.url is None:
args.url = f"https://github.com/{args.author}/{args.project_name}"
print(f"URL not provided. Setting URL to {args.url}")
print(f"Updating project variables with the following values:")
print(json.dumps(vars(args), indent=4))
update_pyproject_toml(args)
update_init_file(args)
update_pkg_folder_name(args)
update_sphinx_conf_py(args)
update_sphinx_index_rst(args)
update_sphinx_modules(args)
update_readme_md(args)
update_license(args)
update_docs_yml(args)
update_dockerfile(args)
return 0
if __name__ == '__main__':
sys.exit(main())