-
Notifications
You must be signed in to change notification settings - Fork 3
/
makepile.py
326 lines (257 loc) · 8.87 KB
/
makepile.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
r"""
makepile.py
A simplistic, pure Python make tool without dependencies.
This is a modified copy.
Usage:
python makepile.py
python makepile.py <target>
Documentation:
The script works with old school python functions.
Any public function you add becomes a target.
If you have two functions in the file, named `install()` and `test()`
you can call a target like:
python makepile.py install
or
python makepile.py test
To provide dependencies, simply call a function from another.
makepile.py is MIT licensed:
Copyright (c) 2020 Caglar Toklu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# pylint: disable=missing-docstring
# pylint: disable=C0111
# C0111 - Missing %s docstring
# C: 1, 0: Missing module docstring (missing-docstring)
# C: 13, 0: Missing function docstring (missing-docstring)
# pylint: disable=invalid-name
# pylint: disable=C0103
# C: 10, 0: Invalid constant name "variable1" (invalid-name)
# pylint: disable=line-too-long
# pylint: disable=C0301
# C: 22, 0: Line too long (127/100) (line-too-long)
# pylint: disable=trailing-newlines
# pylint: disable=C0303
# C: 59, 0: Trailing newlines (trailing-newlines)
from __future__ import print_function
import inspect # pylint: disable=unused-import
import os # pylint: disable=unused-import
import shutil # pylint: disable=unused-import
import sys
# change directory to script dir
os.chdir(os.path.abspath(os.path.dirname(__file__)))
def _multiline_str_to_list(haystack):
r"""
Converts a multiline string to a list.
Empty lines are removed, and all the lines are stripped.
"""
assert isinstance(haystack, str)
list1 = haystack.split("\n")
list1 = [x.strip() for x in list1 if x.strip()]
return list1
def _get_python_command():
# TODO: 6 what about macOS and FreeBSD?
if sys.platform.startswith('win32'): # pylint: disable=no-else-return
return "python"
else:
return "python3"
def _get_pip_command():
return "pip"
# BEGIN TARGETs SECTION _________________________________________
def install():
r"""
Installs the package using pip.
cmd = "pip install -e ."
"""
# cmd = "python setup.py install"
python = _get_python_command()
pip = _get_pip_command()
cmd = f"{python} -m {pip} install -e ."
print(cmd)
os.system(cmd)
def run():
r"""
Runs the package without installing.
cmd = "python shotlast/shotlastmain.py"
"""
python = _get_python_command()
# pip = _get_pip_command()
cmd = f"{python} shotlast/shotlastmain.py"
print(cmd)
os.system(cmd)
def uninstall():
r"""
Uninstalls the package using pip.
"""
python = _get_python_command()
pip = _get_pip_command()
cmd = f"{python} -m f{pip} uninstall shotlast"
print(cmd)
os.system(cmd)
def clean():
r"""
Deletes the generated files and directories.
"""
dirs_as_str = """
shotlast.egg-info
dist
build
shotlast/.pylint.d
shotlast/__pycache__
tests/.pylint.d
tests/__pycache__
README.rst.html
shotlast.spec
"""
dirs = _multiline_str_to_list(dirs_as_str)
for item_name in dirs:
print(item_name)
if os.path.isdir(item_name):
shutil.rmtree(os.path.normpath(item_name), ignore_errors=True)
print(" removed dir:", item_name)
elif os.path.isfile(item_name):
os.remove(os.path.normpath(item_name))
print(" removed file:", item_name)
else:
print(" not a directory/file: ", item_name)
def pyinstaller():
r"""
Makes a Windows executable using PyInstaller.
requires:
pip install pyinstaller
"""
install()
readme()
main_source_file = os.path.normpath("shotlast/shotlastmain.py")
# root_name = os.path.split(main_source_file)[1] # filename.py
# root_name = os.path.splitext(root_name)[0] # shotlastmain
target_name = "shotlast"
# the full path will be:
# dist/<target_name>/<target_name>.exe
cmd = r"pyinstaller --noconfirm --clean --onedir --noupx --nowindowed "
# TODO: 7 the following line did not work.
# we needed it for optimization.
# cmd = r"python -O -m pyinstaller --noconfirm --clean --onedir --noupx --nowindowed "
# https://pyinstaller.readthedocs.io/en/stable/usage.html#running-pyinstaller-with-python-optimizations
cmd += main_source_file
cmd += " -n" + target_name
os.system(cmd)
files_to_bundle = """
LICENSE.txt
README.rst.html
"""
# also possible to add:
# shotlast/shotlast.py
files_to_bundle = _multiline_str_to_list(files_to_bundle)
for file_name in files_to_bundle:
src = os.path.normpath(file_name)
# note that src can be a single file name or a path/file_name.
only_file_name = os.path.split(src)[1]
dst = os.path.normpath("dist/" + target_name + "/" + only_file_name)
# not needed since we are directly copying .html file:
# if only_file_name.lower() == "readme.rst":
# # Rename .rst file as .rst.txt for distribution.
# dst = dst + ".txt"
print("\ncopying:")
print(" ", src)
print("as:")
print(" ", dst)
shutil.copyfile(src, dst)
# def demo():
# """
# Runs a demo using files in examples directory.
# """
# clean()
# install()
# pass
def linecount():
r"""
Counts lines in the project using cloc utility.
Requires:
https://github.com/AlDanial/cloc
"""
cmd = "cloc . --exclude-dir=.private"
print(cmd)
os.system(cmd)
def ctags():
r"""
Generates tags file using ctags.
Requires:
https://ctags.sourceforge.net/
"""
cmd = "ctags -R --exclude=.git --exclude=.private shotlast makepile.py"
print(cmd)
os.system(cmd)
def readme():
r"""
Converts README.rst to README.rst.html.
Requires:
https://pypi.org/project/rst2html5/
"""
cmd = "rst2html5 README.rst > README.rst.html"
print(cmd)
os.system(cmd)
# END TARGETs SECTION _________________________________________
def main():
# any code not to mess with global namespace.
pass
items_in_module = dir()
# ['A_CONSTANT', '__builtins__', '__cached__', '__doc__', '__file__',
# '__loader__', '__name__', '__package__', '__spec__', 'function1', 'main',
# 'os', 'print_function', 'function2', 'shutil', 'sys']
# detect the list of "target" functions in the module
targets_in_module = []
for item in items_in_module:
code = "inspect.isfunction(" + item + ")"
result = eval(code) # pylint: disable=eval-used
if result:
if item not in ['main']: # remove some fixed functions
# remove private functions:
if not item.startswith("_"):
# this also covers __
targets_in_module.append(item)
print("Possible targets:")
print(targets_in_module, "\n")
target_candidate = None
# print(len(sys.argv))
# print(sys.argv)
if len(sys.argv) == 1:
# no target specified, no can do.
print("No target specified")
print("if you are on Windows, make sure you are running the script:")
print(" python makepile.py target")
print("instead of:")
print(" makepile.py target")
print()
target_candidate = input("enter target: >>>")
elif len(sys.argv) == 2:
# exactly one target specified as required.
target_candidate = sys.argv[1]
else:
# more than one target specified, no can do.
print("Please specify exactly one target. Possible targets:")
print(targets_in_module)
target_found = False
if target_candidate:
if target_candidate in targets_in_module:
print("\n/// target:", target_candidate)
function_call = target_candidate + "()"
exec(function_call) # pylint: disable=exec-used
target_found = True
if not target_found:
print("Target not found : ", target_candidate)
print("Possible targets:")
print(targets_in_module)