Skip to content

Commit f8ff509

Browse files
committed
Add clean and bundle commands to setup.py
1 parent 5bc33a0 commit f8ff509

3 files changed

Lines changed: 91 additions & 4 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ cd PythonTurtle
7373
python3 -m pythonturtle
7474
```
7575

76+
Build application bundles like this:
77+
78+
```bash
79+
python3 setup.py clean bundle
80+
```
81+
7682
Please [open a pull request](https://github.com/cool-RR/PythonTurtle/pulls
7783
) for contributions or bug fixes. If you can, please also add tests.
7884

setup.py

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,89 @@
22
"""
33
Packaging implementation for PythonTurtle.
44
"""
5-
from os.path import dirname, join
5+
from glob import glob
6+
import os
7+
import os.path
8+
import shutil
9+
10+
from setuptools.command.test import test
611
from setuptools import setup, find_packages
712

813
import pythonturtle as package
914

1015

16+
class Bundle(test):
17+
"""A setuptools command to build application bundles for all platforms"""
18+
19+
def run(self):
20+
import PyInstaller.__main__
21+
22+
resources_folder = os.path.join('pythonturtle', 'resources')
23+
24+
def resource_path(file_glob):
25+
return os.path.join(resources_folder, file_glob)
26+
27+
def include_resources(file_glob):
28+
return '{src}{separator}{dest}'.format(
29+
src=resource_path(file_glob),
30+
separator=os.pathsep,
31+
dest=resources_folder)
32+
33+
PyInstaller.__main__.run([
34+
'--name=%s' % package.name,
35+
'--onefile',
36+
'--windowed',
37+
'--add-binary=%s' % include_resources('*.ic*'),
38+
'--add-binary=%s' % include_resources('*.png'),
39+
'--add-data=%s' % include_resources('*.txt'),
40+
'--icon=%s' % resource_path('icon.ico'),
41+
os.path.join('pythonturtle', '__main__.py'),
42+
])
43+
44+
45+
class Clean(test):
46+
"""A setuptools command to remove build files and folders"""
47+
48+
def run(self):
49+
delete_in_root = [
50+
'build',
51+
'dist',
52+
'.eggs',
53+
'*.egg-info',
54+
'.pytest_cache',
55+
'*.spec',
56+
'.tox',
57+
]
58+
delete_everywhere = [
59+
'*.pyc',
60+
'__pycache__',
61+
]
62+
for candidate in delete_in_root:
63+
rmtree_glob(candidate)
64+
for visible_dir in glob('[A-Za-z0-9_]*'):
65+
for candidate in delete_everywhere:
66+
rmtree_glob(os.path.join(visible_dir, candidate))
67+
rmtree_glob(os.path.join(visible_dir, '*', candidate))
68+
rmtree_glob(os.path.join(visible_dir, '*', '*', candidate))
69+
70+
71+
def rmtree_glob(file_glob):
72+
"""Platform independent rmtree, which also allows wildcards (globbing)"""
73+
for item in glob(file_glob):
74+
try:
75+
shutil.rmtree(item)
76+
print('%s/ removed ...' % item)
77+
except OSError:
78+
try:
79+
os.remove(item)
80+
print('%s removed ...' % item)
81+
except OSError as err:
82+
print(err)
83+
84+
1185
def read_file(filename):
1286
"""Source the contents of a file"""
13-
with open(join(dirname(__file__), filename)) as file:
87+
with open(os.path.join(os.path.dirname(__file__), filename)) as file:
1488
return file.read()
1589

1690

@@ -49,6 +123,11 @@ def read_file(filename):
49123
zip_safe=True,
50124
# install_requires=['wxPython'],
51125
tests_require=['tox'],
126+
test_suite='tests',
127+
cmdclass={
128+
'bundle': Bundle,
129+
'clean': Clean,
130+
},
52131
entry_points={
53132
'console_scripts': [
54133
'PythonTurtle = pythonturtle.__main__:application.run',

tox.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ commands = flake8 {posargs}
2727

2828
[testenv:pylint]
2929
basepython = python3.6
30-
deps = pylint
30+
deps =
31+
pyinstaller
32+
pylint
3133
commands =
3234
pip install --find-links https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 wxPython
33-
pylint --rcfile tox.ini {posargs:pythonturtle}
35+
pylint --rcfile tox.ini {posargs:pythonturtle setup}
3436

3537
[behave]
3638
# default_format = progress

0 commit comments

Comments
 (0)