1- from pythonforandroid .toolchain import Bootstrap , shprint , current_directory , info , warning , ArchARM , info_main
1+ from pythonforandroid .toolchain import (
2+ Bootstrap , shprint , current_directory , info , info_main )
3+ from pythonforandroid .util import ensure_dir
24from os .path import join , exists , curdir , abspath
35from os import walk
46import glob
57import sh
68
7- class SDL2Bootstrap (Bootstrap ):
8- name = 'sdl2'
9+
10+ EXCLUDE_EXTS = (".py" , ".pyc" , ".so.o" , ".so.a" , ".so.libs" , ".pyx" )
11+
12+
13+ class SDL2GradleBootstrap (Bootstrap ):
14+ name = 'sdl2_gradle'
915
1016 recipe_depends = ['sdl2' , ('python2' , 'python3crystax' )]
1117
1218 def run_distribute (self ):
13- info_main ('# Creating Android project from build and {} bootstrap' .format (
14- self .name ))
19+ info_main ("# Creating Android project ({})" .format (self .name ))
20+
21+ arch = self .ctx .archs [0 ]
22+ python_install_dir = self .ctx .get_python_install_dir ()
23+ from_crystax = self .ctx .python_recipe .from_crystax
24+ crystax_python_dir = join ("crystax_python" , "crystax_python" )
25+
26+ if len (self .ctx .archs ) > 1 :
27+ raise ValueError ("SDL2/gradle support only one arch" )
28+
29+ info ("Copying SDL2/gradle build for {}" .format (arch ))
30+ shprint (sh .rm , "-rf" , self .dist_dir )
31+ shprint (sh .cp , "-r" , self .build_dir , self .dist_dir )
1532
16- info ('This currently just copies the SDL2 build stuff straight from the build dir.' )
17- shprint (sh .rm , '-rf' , self .dist_dir )
18- shprint (sh .cp , '-r' , self .build_dir , self .dist_dir )
33+ # either the build use environemnt variable (ANDROID_HOME)
34+ # or the local.properties if exists
1935 with current_directory (self .dist_dir ):
2036 with open ('local.properties' , 'w' ) as fileh :
2137 fileh .write ('sdk.dir={}' .format (self .ctx .sdk_dir ))
2238
23- arch = self .ctx .archs [0 ]
24- if len (self .ctx .archs ) > 1 :
25- raise ValueError ('built for more than one arch, but bootstrap cannot handle that yet' )
26- info ('Bootstrap running with arch {}' .format (arch ))
27-
2839 with current_directory (self .dist_dir ):
29- info (' Copying python distribution' )
40+ info (" Copying Python distribution" )
3041
31- if not exists ('private' ) and not self .ctx .python_recipe .from_crystax :
32- shprint (sh .mkdir , 'private' )
33- if not exists ('crystax_python' ) and self .ctx .python_recipe .from_crystax :
34- shprint (sh .mkdir , 'crystax_python' )
35- shprint (sh .mkdir , 'crystax_python/crystax_python' )
36- if not exists ('assets' ):
37- shprint (sh .mkdir , 'assets' )
42+ if not exists ("private" ) and not from_crystax :
43+ ensure_dir ("private" )
44+ if not exists ("crystax_python" ) and from_crystax :
45+ ensure_dir (crystax_python_dir )
3846
3947 hostpython = sh .Command (self .ctx .hostpython )
40- if not self . ctx . python_recipe . from_crystax :
48+ if not from_crystax :
4149 try :
4250 shprint (hostpython , '-OO' , '-m' , 'compileall' ,
43- self . ctx . get_python_install_dir () ,
51+ python_install_dir ,
4452 _tail = 10 , _filterout = "^Listing" )
4553 except sh .ErrorReturnCode :
4654 pass
4755 if not exists ('python-install' ):
48- shprint (sh .cp , '-a' , self .ctx .get_python_install_dir (), './python-install' )
56+ shprint (
57+ sh .cp , '-a' , python_install_dir , './python-install' )
4958
5059 self .distribute_libs (arch , [self .ctx .get_libs_dir (arch .arch )])
51- self .distribute_aars (arch )
52- self .distribute_javaclasses (self .ctx .javaclass_dir )
53-
54- if not self .ctx .python_recipe .from_crystax :
55- info ('Filling private directory' )
56- if not exists (join ('private' , 'lib' )):
57- info ('private/lib does not exist, making' )
58- shprint (sh .cp , '-a' , join ('python-install' , 'lib' ), 'private' )
59- shprint (sh .mkdir , '-p' , join ('private' , 'include' , 'python2.7' ))
60+ self .distribute_javaclasses (self .ctx .javaclass_dir ,
61+ dest_dir = join ("src" , "main" , "java" ))
62+
63+ if not from_crystax :
64+ info ("Filling private directory" )
65+ if not exists (join ("private" , "lib" )):
66+ info ("private/lib does not exist, making" )
67+ shprint (sh .cp , "-a" ,
68+ join ("python-install" , "lib" ), "private" )
69+ shprint (sh .mkdir , "-p" ,
70+ join ("private" , "include" , "python2.7" ))
6071
6172 # AND: Copylibs stuff should go here
62- if exists (join ('libs' , arch .arch , 'libpymodules.so' )):
63- shprint (sh .mv , join ('libs' , arch .arch , 'libpymodules.so' ), 'private/' )
64- shprint (sh .cp , join ('python-install' , 'include' , 'python2.7' , 'pyconfig.h' ), join ('private' , 'include' , 'python2.7/' ))
73+ libpymodules_fn = join ("libs" , arch .arch , "libpymodules.so" )
74+ if exists (libpymodules_fn ):
75+ shprint (sh .mv , libpymodules_fn , 'private/' )
76+ shprint (sh .cp ,
77+ join ('python-install' , 'include' ,
78+ 'python2.7' , 'pyconfig.h' ),
79+ join ('private' , 'include' , 'python2.7/' ))
6580
6681 info ('Removing some unwanted files' )
6782 shprint (sh .rm , '-f' , join ('private' , 'lib' , 'libpython2.7.so' ))
@@ -70,54 +85,53 @@ def run_distribute(self):
7085 libdir = join (self .dist_dir , 'private' , 'lib' , 'python2.7' )
7186 site_packages_dir = join (libdir , 'site-packages' )
7287 with current_directory (libdir ):
73- # shprint(sh.xargs, 'rm', sh.grep('-E', '*\.(py|pyx|so\.o|so\.a|so\.libs)$', sh.find('.')))
7488 removes = []
75- for dirname , something , filens in walk ('.' ):
76- for filename in filens :
77- for suffix in ( 'py' , 'pyc' , 'so.o' , 'so.a' , 'so.libs' ) :
89+ for dirname , root , filenames in walk ("." ):
90+ for filename in filenames :
91+ for suffix in EXCLUDE_EXTS :
7892 if filename .endswith (suffix ):
7993 removes .append (filename )
8094 shprint (sh .rm , '-f' , * removes )
8195
8296 info ('Deleting some other stuff not used on android' )
8397 # To quote the original distribute.sh, 'well...'
84- # shprint(sh.rm, '-rf', 'ctypes')
8598 shprint (sh .rm , '-rf' , 'lib2to3' )
8699 shprint (sh .rm , '-rf' , 'idlelib' )
87100 for filename in glob .glob ('config/libpython*.a' ):
88101 shprint (sh .rm , '-f' , filename )
89102 shprint (sh .rm , '-rf' , 'config/python.o' )
90- # shprint(sh.rm, '-rf', 'lib-dynload/_ctypes_test.so')
91- # shprint(sh.rm, '-rf', 'lib-dynload/_testcapi.so')
92103
93104 else : # Python *is* loaded from crystax
94105 ndk_dir = self .ctx .ndk_dir
95106 py_recipe = self .ctx .python_recipe
96- python_dir = join (ndk_dir , 'sources' , 'python' , py_recipe .version ,
97- 'libs' , arch .arch )
98-
99- shprint (sh .cp , '-r' , join (python_dir , 'stdlib.zip' ), 'crystax_python/crystax_python' )
100- shprint (sh .cp , '-r' , join (python_dir , 'modules' ), 'crystax_python/crystax_python' )
101- shprint (sh .cp , '-r' , self .ctx .get_python_install_dir (), 'crystax_python/crystax_python/site-packages' )
107+ python_dir = join (ndk_dir , 'sources' , 'python' ,
108+ py_recipe .version , 'libs' , arch .arch )
109+ shprint (sh .cp , '-r' , join (python_dir ,
110+ 'stdlib.zip' ), crystax_python_dir )
111+ shprint (sh .cp , '-r' , join (python_dir ,
112+ 'modules' ), crystax_python_dir )
113+ shprint (sh .cp , '-r' , self .ctx .get_python_install_dir (),
114+ join (crystax_python_dir , 'site-packages' ))
102115
103116 info ('Renaming .so files to reflect cross-compile' )
104- site_packages_dir = 'crystax_python/crystax_python/site-packages'
105- filens = shprint (sh .find , site_packages_dir , '-iname' , '*.so' ).stdout .decode (
106- 'utf-8' ).split ('\n ' )[:- 1 ]
107- for filen in filens :
108- parts = filen .split ('.' )
117+ site_packages_dir = join (crystax_python_dir , "site-packages" )
118+ find_ret = shprint (
119+ sh .find , site_packages_dir , '-iname' , '*.so' )
120+ filenames = find_ret .stdout .decode ('utf-8' ).split ('\n ' )[:- 1 ]
121+ for filename in filenames :
122+ parts = filename .split ('.' )
109123 if len (parts ) <= 2 :
110124 continue
111- shprint (sh .mv , filen , filen .split ('.' )[0 ] + '.so' )
125+ shprint (sh .mv , filename , filename .split ('.' )[0 ] + '.so' )
112126 site_packages_dir = join (abspath (curdir ),
113127 site_packages_dir )
114128 if 'sqlite3' not in self .ctx .recipe_build_order :
115129 with open ('blacklist.txt' , 'a' ) as fileh :
116130 fileh .write ('\n sqlite3/*\n lib-dynload/_sqlite3.so\n ' )
117131
118-
119132 self .strip_libraries (arch )
120133 self .fry_eggs (site_packages_dir )
121- super (SDL2Bootstrap , self ).run_distribute ()
134+ super (SDL2GradleBootstrap , self ).run_distribute ()
135+
122136
123- bootstrap = SDL2Bootstrap ()
137+ bootstrap = SDL2GradleBootstrap ()
0 commit comments