Skip to content

Commit cec5df6

Browse files
committed
add a blacklist behavior to remove all the python files we are not using by default.
1 parent ec73b33 commit cec5df6

File tree

5 files changed

+133
-57
lines changed

5 files changed

+133
-57
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The basic layout of a distribution is::
139139
private.mp3 - (*) fake package that will contain all the python installation
140140
public.mp3 - (*) fake package that will contain your application
141141
bin/ - contain all the apk generated from build.py
142+
blacklist.txt - list of file patterns to not include in the APK
142143
buildlib/ - internals libraries for build.py
143144
build.py - build script to use for packaging your application
144145
build.xml - (*) build settings (generated from templates)

distribute.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ function run_distribute() {
434434
try cp -a $SRC_PATH/src .
435435
try cp -a $SRC_PATH/templates .
436436
try cp -a $SRC_PATH/res .
437+
try cp -a $SRC_PATH/blacklist.txt .
437438

438439
debug "Copy python distribution"
439440
try cp -a $BUILD_PATH/python-install .
@@ -468,6 +469,8 @@ function run_distribute() {
468469
try rm -rf config/libpython*.a
469470
try rm -rf config/python.o
470471
try rm -rf curses
472+
try rm -rf lib-dynload/_ctypes_test.so
473+
try rm -rf lib-dynload/_testcapi.so
471474

472475
debug "Strip libraries"
473476
push_arm

src/blacklist.txt

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# eggs
2+
*.egg-info
3+
4+
# unit test
5+
unittest/*
6+
7+
# python config
8+
config/makesetup
9+
10+
# unused pygame files
11+
pygame/_camera_*
12+
pygame/*.html
13+
pygame/*.bmp
14+
pygame/*.svg
15+
pygame/cdrom.so
16+
pygame/pygame_icon.icns
17+
pygame/LGPL
18+
pygame/threads/Py25Queue.pyo
19+
pygame/*.ttf
20+
21+
# unused kivy files (platform specific)
22+
kivy/input/providers/wm_*
23+
kivy/input/providers/mactouch*
24+
kivy/input/providers/probesysfs*
25+
kivy/input/providers/mtdev*
26+
kivy/input/providers/hidinput*
27+
kivy/core/camera/camera_videocapture*
28+
kivy/core/spelling/*osx*
29+
kivy/core/video/video_pyglet*
30+
31+
# unused encodings
32+
lib-dynload/*codec*
33+
encodings/cp*.pyo
34+
encodings/tis*
35+
encodings/shift*
36+
encodings/bz2*
37+
encodings/zlib*
38+
encodings/iso*
39+
encodings/undefined*
40+
encodings/johab*
41+
encodings/p*
42+
encodings/m*
43+
encodings/euc*
44+
encodings/k*
45+
encodings/unicode_internal*
46+
encodings/quo*
47+
encodings/gb*
48+
encodings/big5*
49+
encodings/h*
50+
51+
# unused python modules
52+
bsddb/*
53+
wsgiref/*
54+
sqlite3/*
55+
hotshot/*
56+
pydoc_data/*
57+
tty.pyo
58+
anydbm.pyo
59+
nturl2path.pyo
60+
LICENCE.txt
61+
macurl2path.pyo
62+
dummy_threading.pyo
63+
audiodev.pyo
64+
antigravity.pyo
65+
dumbdbm.pyo
66+
sndhdr.pyo
67+
__phello__.foo.pyo
68+
sunaudio.pyo
69+
os2emxpath.pyo
70+
multiprocessing/dummy*
71+
72+
# unused binaries python modules
73+
lib-dynload/termios.so
74+
lib-dynload/_lsprof.so
75+
lib-dynload/*audioop.so
76+
lib-dynload/mmap.so
77+
lib-dynload/_hotshot.so
78+
lib-dynload/_csv.so
79+
lib-dynload/future_builtins.so
80+
lib-dynload/_lsprof.so
81+
lib-dynload/_heapq.so
82+
lib-dynload/_json.so
83+
lib-dynload/grp.so
84+
lib-dynload/resource.so
85+
lib-dynload/pyexpat.so
86+
87+
# odd files
88+
plat-linux3/regen

src/build.py

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
sys.path.insert(0, 'buildlib/jinja2.egg')
66
sys.path.insert(0, 'buildlib')
77

8-
# import zlib
9-
# zlib.Z_DEFAULT_COMPRESSION = 9
10-
8+
from fnmatch import fnmatch
119
import tarfile
1210
import os
1311
import shutil
1412
import subprocess
1513
import time
16-
1714
import jinja2
1815

1916
# The extension of the android and ant commands.
@@ -29,27 +26,18 @@
2926
# Try to find a host version of Python that matches our ARM version.
3027
PYTHON = join(curdir, 'python-install', 'bin', 'python.host')
3128

32-
# Files and extensions we should not package.
33-
BLACKLIST_FILES = [
34-
'icon.ico',
35-
'icon.icns',
36-
'launcherinfo.py',
37-
'.nomedia',
38-
]
39-
40-
BLACKLIST_EXTENSIONS = [
41-
'~',
42-
'.bak',
43-
'.rpy',
44-
'.swp',
45-
]
46-
47-
BLACKLIST_DIRS = [
29+
BLACKLIST_PATTERNS = [
30+
# code versionning
4831
'.hg',
4932
'.git',
5033
'.bzr',
5134
'.svn',
52-
]
35+
36+
# temp files
37+
'~',
38+
'.bak',
39+
'.swp',
40+
]
5341

5442
# Used by render.
5543
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(
@@ -76,25 +64,25 @@ def compile_dir(dfn):
7664
# -OO = strip docstrings
7765
subprocess.call([PYTHON,'-OO','-m','compileall','-f',dfn])
7866

67+
def is_blacklist(name):
68+
for pattern in BLACKLIST_PATTERNS:
69+
if fnmatch(name, '*/' + pattern):
70+
return True
71+
7972
def make_tar(fn, source_dirs, ignore_path=[]):
8073
'''
8174
Make a zip file `fn` from the contents of source_dis.
8275
'''
8376

84-
# zf = zipfile.ZipFile(fn, 'w')
8577
tf = tarfile.open(fn, 'w:gz')
8678

87-
8879
for sd in source_dirs:
89-
if '.py' in BLACKLIST_EXTENSIONS:
90-
compile_dir(sd)
80+
compile_dir(sd)
9181

9282
sd = os.path.abspath(sd)
9383

9484
for dir, dirs, files in os.walk(sd):
95-
for bd in BLACKLIST_DIRS:
96-
if bd in dirs:
97-
dirs.remove(bd)
85+
dirs = [d for d in dirs if not is_blacklist(d)]
9886

9987
ignore = False
10088
for ip in ignore_path:
@@ -112,18 +100,8 @@ def make_tar(fn, source_dirs, ignore_path=[]):
112100
for fn in files:
113101
fn = os.path.join(dir, fn)
114102
relfn = os.path.relpath(fn, sd)
115-
116-
bl = False
117-
for e in BLACKLIST_EXTENSIONS:
118-
if relfn.endswith(e):
119-
bl = True
120-
121-
if bl:
122-
continue
123-
124-
if relfn in BLACKLIST_FILES:
103+
if is_blacklist(relfn):
125104
continue
126-
127105
tf.add(fn, relfn)
128106
print 'add', fn
129107

@@ -216,6 +194,7 @@ def make_package(args):
216194
# Build.
217195
map(lambda arg: subprocess.call([ANT, arg]), args.command)
218196

197+
'''
219198
def shelve_lib(lfn):
220199
for root,dirs,files in os.walk('libs'):
221200
for fn in files:
@@ -232,7 +211,7 @@ def unshelve_libs():
232211
lib_dir = root[len('.shelf/'):]
233212
shutil.move(os.path.join(root,fn), lib_dir)
234213
shutil.rmtree('.shelf')
235-
214+
'''
236215

237216
if __name__ == '__main__':
238217
import argparse
@@ -259,9 +238,9 @@ def unshelve_libs():
259238
ap.add_argument('--presplash', dest='presplash', help='A jpeg file to use as a screen while the application is loading.')
260239
ap.add_argument('--install-location', dest='install_location', default='auto', help='The default install location. Should be "auto", "preferExternal" or "internalOnly".')
261240
ap.add_argument('--compile-pyo', dest='compile_pyo', action='store_true', help='Compile all .py files to .pyo, and only distribute the compiled bytecode.')
262-
ap.add_argument('--with-sqlite3', dest='with_sqlite3', action='store_true', help='Include sqlite3 module.')
263-
ap.add_argument('--with-PIL', dest='with_PIL', action='store_true', help='Include the Python Imaging Library (PIL).')
264-
ap.add_argument('--with-ffmpeg', dest='with_ffmpeg', action='store_true', help='Include the FFMPEG android libraries (PIL).')
241+
#ap.add_argument('--with-sqlite3', dest='with_sqlite3', action='store_true', help='Include sqlite3 module.')
242+
#ap.add_argument('--with-PIL', dest='with_PIL', action='store_true', help='Include the Python Imaging Library (PIL).')
243+
#ap.add_argument('--with-ffmpeg', dest='with_ffmpeg', action='store_true', help='Include the FFMPEG android libraries (PIL).')
265244

266245
ap.add_argument('command', nargs='*', help='The command to pass to ant.')
267246

@@ -279,21 +258,26 @@ def unshelve_libs():
279258
if args.compile_pyo:
280259
if PYTHON is None:
281260
ap.error('To use --compile-pyo, you need Python 2.7.1 installed and in your PATH.')
282-
BLACKLIST_EXTENSIONS += ['.py', '.pyc']
261+
BLACKLIST_PATTERNS += ['*.py', '*.pyc']
283262

263+
'''
284264
if not args.with_sqlite3:
285-
BLACKLIST_DIRS += ['sqlite3']
286-
BLACKLIST_FILES += ['_sqlite3.so']
265+
BLACKLIST_PATTERNS += ['sqlite3', '_sqlite3.so']
287266
shelve_lib('libsqlite3.so')
288267
289268
if not args.with_PIL:
290-
BLACKLIST_DIRS += ['PIL']
291-
BLACKLIST_FILES += ['_imaging.so','_imagingft.so','_imagingmath.so']
269+
BLACKLIST_PATTERNS += ['PIL', '_imaging.so', '_imagingft.so', '_imagingmath.so']
292270
293271
if not args.with_ffmpeg:
294-
BLACKLIST_DIRS += ['ffmpeg']
272+
BLACKLIST_PATTERNS += ['ffmpeg']
273+
'''
274+
275+
with open(join(curdir, 'blacklist.txt')) as fd:
276+
patterns = [x.strip() for x in fd.read().splitlines() if x.strip() or
277+
x.startswith('#')]
278+
BLACKLIST_PATTERNS += patterns
295279

296280
make_package(args)
297-
unshelve_libs()
281+
#unshelve_libs()
298282

299283

src/templates/AndroidManifest.tmpl.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
android:installLocation="{{ args.install_location }}"
77
>
88

9-
<application android:label="@string/appName"
9+
<application android:label="@string/appName"
1010
android:icon="@drawable/icon"
1111
>
1212

@@ -30,7 +30,7 @@
3030
<action android:name="android.intent.action.MAIN" />
3131
<category android:name="android.intent.category.LAUNCHER" />
3232
</intent-filter>
33-
{% endif %}
33+
{% endif %}
3434

3535
</activity>
3636

@@ -46,16 +46,16 @@
4646

4747
</activity>
4848
{% endif %}
49-
49+
5050
</application>
5151

52-
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="5"/>
53-
<uses-permission android:name="android.permission.WAKE_LOCK" />
52+
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
53+
<uses-permission android:name="android.permission.WAKE_LOCK" />
5454
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
5555

5656
{% for perm in args.permissions %}
5757
<uses-permission android:name="android.permission.{{ perm }}" />
5858
{% endfor %}
59-
59+
6060
{{ manifest_extra }}
61-
</manifest>
61+
</manifest>

0 commit comments

Comments
 (0)