This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
generated from KryptedGaming/django-package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.py
222 lines (187 loc) · 6.08 KB
/
manage.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
from manager import Manager
import fileinput
import argparse
import subprocess
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
manager = Manager()
example_files = [
'.travis.yml',
'setup.py',
'mkdocs.yml',
'tox.ini',
'MANIFEST.in',
'.coveragerc'
]
get_package_name = subprocess.run(
['basename `git rev-parse --show-toplevel`'],
stdout=subprocess.PIPE,
shell=True)
get_package_author = subprocess.run(
['git config user.name'],
stdout=subprocess.PIPE,
shell=True)
get_package_email = subprocess.run(
['git config user.email'],
stdout=subprocess.PIPE,
shell=True)
get_package_url = subprocess.run(
['git remote get-url origin'],
stdout=subprocess.PIPE,
shell=True)
package_name = get_package_name.stdout.decode('utf-8').strip()
description = "A Django Application template by Krypted Gaming"
app_name = package_name.replace("-", "_")
title = app_name.title().replace("_", " ")
config_name = ''.join(x for x in app_name.title() if x != '_')
package_author = get_package_author.stdout.decode('utf-8').strip()
package_email = get_package_email.stdout.decode('utf-8').strip()
package_url = get_package_url.stdout.decode('utf-8').strip()
def commands_succeeded(rcs):
if list(rcs)[0] != 0 or len(set(rcs)) > 1:
return False
return True
@manager.command
def echo():
"""Echo package directory."""
print(dir_path)
@manager.command
def deploy():
"""Deploy package and docs. Type in username and password for PyPi."""
commands = [
"python3 setup.py sdist",
"python3 -m twine upload dist/*",
"python3 -m mkdocs gh-deploy"
]
command_rcs = set()
for command in commands:
print(command)
command_rcs.add(subprocess.run(
[command],
stdout=subprocess.PIPE,
shell=True).returncode)
@manager.command
def venv():
"""Create a virtual environment."""
command_rc = subprocess.run(
[f"python3 -m venv {dir_path}/venv"],
stdout=subprocess.PIPE,
shell=True).returncode
if command_rc != 0:
print(
f"Failed to create virtual env: RC={command_rc}")
exit()
@ manager.command
def init():
"""Initialize the project."""
# Create application
create_project = subprocess.run(
[f"django-admin startapp {app_name}"],
stdout=subprocess.PIPE,
shell=True)
if create_project.returncode != 0 and create_project.returncode != 1:
print(
f"Failed to create Django application: RC={create_project.returncode}")
exit()
# Create files
example_file_creation_rcs = set()
for example_file in example_files:
command = f'cp {dir_path}/{example_file}.example {dir_path}/{example_file}'
print(command)
example_file_creation_rcs.add(subprocess.run(
[command],
stdout=subprocess.PIPE,
shell=True
).returncode)
if not commands_succeeded(example_file_creation_rcs):
print("Failed to create necessary files.")
exit()
# Modify Files
for example_file in example_files:
lines_replaced = 0
with fileinput.FileInput(f"{dir_path}/{example_file}", inplace=True) as file:
for line in file:
if 'django_eveonline_doctrine_manager' in line:
lines_replaced += 1
print(line.replace('django_eveonline_doctrine_manager', app_name), end='')
print(
f"Updating file: {dir_path}/{example_file} ... {lines_replaced} lines replaced")
# Create __init__.py
init_py = (
f"__title__ = '{title}'" + "\n"
f"__description__ = '{description}'" + "\n"
f"__package_name__ = '{package_name}'" + "\n"
f"__github_url__ = '{package_url}'" + "\n"
f"__version__ = '0.0.1'" + "\n"
f"__author__ = '{package_author}'" + "\n"
f"__author_email__ = '{package_email}'" + "\n"
f"__license__ = 'MIT License'" + "\n"
f"__copyright__ = f\"Copyright © 2017-2020 {package_author}. All rights reserved.\"" + "\n"
f"default_app_config = \"{app_name}.apps.{config_name}Config\"" + "\n"
)
f = open(f"{app_name}/__init__.py", "a+")
f.write(init_py)
print("Successfully initialized project")
@ manager.command
def prune():
"""Prune all .example files. WARNING: Init will no longer function."""
for example_files in example_files:
command_rcs = set()
command = f'rm -rf {dir_path}/{example_file}.example'
print(command)
command_rcs.add(subprocess.run(
[command],
stdout=subprocess.PIPE,
shell=True
).returncode)
if not commands_succeeded(command_rcs):
print("Failed to clear files")
exit()
@ manager.command
def purge():
"""Purge all created files."""
for example_file in example_files:
command_rcs = set()
command = f'rm -rf {dir_path}/{example_file}'
print(command)
command_rcs.add(subprocess.run(
[command],
stdout=subprocess.PIPE,
shell=True
).returncode)
if not commands_succeeded(command_rcs):
print("Failed to clear files")
exit()
command = f'rm -rf {dir_path}/{app_name}'
print(command)
command_rcs.add(subprocess.run(
[command],
stdout=subprocess.PIPE,
shell=True
).returncode)
if not commands_succeeded(command_rcs):
print("Failed to clear files")
exit()
extra_files = [
'coverage.xml',
'dist',
'.coverage',
'*.egg-info',
'site',
'.tox',
'venv'
]
for example_file in extra_files:
command_rcs = set()
command = f'rm -rf {dir_path}/{example_file}'
print(command)
command_rcs.add(subprocess.run(
[command],
stdout=subprocess.PIPE,
shell=True
).returncode)
if not commands_succeeded(command_rcs):
print("Failed to clear files")
exit()
if __name__ == '__main__':
manager.main()