-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkproj.py
executable file
·266 lines (213 loc) · 7.74 KB
/
mkproj.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
#!/usr/bin/env python3
import argparse
import os
import shutil
import subprocess
import sys
import textwrap
from pathlib import Path
from mkproj_licenses import licenses, year
from mkproj_types import types
confirm_string = """\
You have chosen to make a new project with the following options:
* name: %s
* type: %s
"""
def delete_path(path):
try:
os.remove(path)
except IsADirectoryError:
shutil.rmtree(path)
except PermissionError:
die(f"Could not remove {path}: permission denied.")
def die(*msg, exitcode=1):
print(*msg, file=sys.stderr)
exit(exitcode)
def write_str_to_file(file: str, string: str):
with open(file, "a+") as fp:
fp.write(string)
def write_line_to_file(file: str, string: str):
write_str_to_file(file, string + "\n")
def try_shell_launch():
init_shell = input_bool("Initialise a shell in the new dir? (yes/no) ")
if init_shell:
shell = os.getenv("SHELL")
if shell:
subprocess.run([shell])
else:
print("Couldn't find your shell, exiting.")
exit(0)
else:
exit(0)
def get_user_input(desc, retry = True):
rval = ""
try:
rval = input(f"Enter {desc}: ")
except:
rval = None
while retry and rval == "":
try:
rval = input(f"Enter {desc}: ")
except:
rval = None
return rval
def cancellable_input(prompt):
try:
return input(prompt)
except:
die("\nCanceled.")
def cancellable_input_graceful(prompt):
try:
return input(prompt)
except:
return False
def input_bool(prompt):
try:
return input(prompt) in ['y', 'yes']
except:
return False
def initialise_parser(parser):
parser.add_argument('-n', '--name', type=str,
help='Name of the directory to create.')
parser.add_argument(
'-t', '--type', type=str, help='Type of the project to create.', choices=types.keys())
parser.add_argument('-r', '--readme', action='store_true',
help='Creates a README file if this option is present.')
parser.add_argument('-T', '--todo', action='store_true',
help='Creates a TODO file if this option is present.')
parser.add_argument('-s', '--shebang', action='store_true',
help='Adds a shebang for supported files if this option is present.')
parser.add_argument('-l', '--license', type=str,
help='The name of a license to use.', choices=set(licenses.keys()))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Create a new blank project directory with necessary boilerplate.',
prog='mkproj',
exit_on_error=True
)
initialise_parser(parser)
args = parser.parse_args()
lic = args.license
if not len(sys.argv) > 1 or not args.name or not args.type:
name = args.name
if not name:
name = get_user_input('a name for your project')
if not name:
die('Cancelled.')
t = args.type
if not t:
t = get_user_input(
'a type from [' + ', '.join(types.keys()) + '] for your project'
)
if t not in types.keys():
die('Invalid type selected.')
readme = args.readme
if not readme:
readme = input_bool('Create a readme? (y/N) ')
todo = args.todo
if not todo:
todo = input_bool('Create a todo? (y/N) ')
shebang = args.shebang
if not shebang:
shebang = input_bool('Add shebangs to supported files? (y/N) ')
if not lic:
lic = get_user_input(
'a license from [' + ', '.join(licenses.keys()) + '] for your project.'
'\nLeave empty to skip',
False
)
if lic != '' and lic not in licenses.keys():
die('Invalid license selected.')
elif lic == '':
pass
constructed_args = ['-n', name, '-t', t]
if readme:
constructed_args.append('-r')
if todo:
constructed_args.append('-T')
if shebang:
constructed_args.append('-s')
if lic:
if lic in licenses.keys():
constructed_args += ['-l', lic]
else:
die('Invalid license selected.')
args = parser.parse_args(constructed_args)
args_list = []
if (args.todo):
args_list.append('Create todo')
if (args.readme):
args_list.append('Create readme')
if (args.license):
args_list.append(f'Create license [you picked: {lic}]')
if (args.shebang):
args_list.append('Add shebangs to supported files')
args_string = ''
if len(args_list) > 0:
args_string += ' * ' + '\n * '.join(args_list)
print((confirm_string + args_string) % (args.name, args.type))
cnf = input_bool('Is this correct? (y/N): ')
if not cnf:
die('Cancelled.')
proj_dir = os.path.join(os.getcwd(), args.name)
def get_proj_path(x): return os.path.join(proj_dir, x)
if os.path.exists(proj_dir):
overwrite = input_bool(
"Cannot create project directory: a file or directory with the same name already exists. Try deleting it? (y/N) "
)
if not overwrite:
die('Cancelled.')
confirm = cancellable_input(
f'Type "{args.name.replace("/", "")}" (without the quotes) to confirm deletion. THIS CANNOT BE UNDONE! '
)
if confirm != args.name:
die("Canceled.")
delete_path(proj_dir)
try:
os.mkdir(proj_dir)
except PermissionError:
die("Cannot create project directory: permission denied.")
os.chdir(proj_dir)
subprocess.run(['git', 'init'])
proj = types[args.type]
if 'files' in proj:
for file in proj['files']:
fobj = proj['files'][file]
if (file.endswith('/')):
Path(os.path.join(proj_dir, file)).mkdir(exist_ok=True, parents=True)
continue
if os.path.dirname(file) != '':
Path(os.path.join(proj_dir, os.path.dirname(file))).mkdir(exist_ok=True, parents=True)
fpath = get_proj_path(file)
if args.shebang and 'shebang' in fobj:
write_line_to_file(fpath, fobj['shebang'])
write_str_to_file(fpath, fobj['contents'])
if 'commands' in proj:
for command in proj['commands']:
subprocess.run(command)
if args.todo:
Path(get_proj_path('TODO')).touch()
if args.readme:
readme_path = get_proj_path('README.md')
print("Creating readme...")
pretty_name = args.name
desc = get_user_input("a line describing your project") or "Description"
write_line_to_file(readme_path, f"# {pretty_name}")
write_line_to_file(readme_path, "")
write_line_to_file(readme_path, f"{desc}")
if args.license:
print("Creating license...")
name = get_user_input("your name") or "<copyright holders>"
lic = licenses[args.license]
for file in lic['files']:
body_str = textwrap.dedent(lic['files'][file]['contents'])
if file == "LICENSE":
try:
body_str = body_str.replace("YYYY", year) % name
except:
print("License does not need name field. Proceeding silently.")
write_str_to_file(get_proj_path(file), body_str)
print("License created. Be sure to read up on the terms of your license and add headers to each project file!")
if 'message' in proj:
print(proj['message'])
try_shell_launch()