11from __future__ import print_function
22from setuptools import Command
33from pythonforandroid import toolchain
4+
45import sys
6+ from os .path import realpath , join , exists , dirname , curdir , basename , split
7+ from os import makedirs
8+ from glob import glob
9+ from shutil import rmtree , copyfile
510
611def argv_contains (t ):
712 for arg in sys .argv :
@@ -31,6 +36,8 @@ def initialize_options(self):
3136 for option in self .user_options :
3237 setattr (self , option [0 ].strip ('=' ).replace ('-' , '_' ), None )
3338
39+ self .bdist_dir = None
40+
3441 def finalize_options (self ):
3542
3643 # Inject some argv options from setup.py if the user did not
@@ -52,14 +59,66 @@ def finalize_options(self):
5259 version = self .distribution .get_version ()
5360 print ('version is' , version )
5461 sys .argv .append ('--version={}' .format (version ))
62+
63+ if not argv_contains ('--arch' ):
64+ arch = 'armeabi'
65+ self .arch = arch
66+ sys .argv .append ('--arch={}' .format (arch ))
5567
5668
5769 def run (self ):
58- print ('running' )
70+
71+ self .prepare_build_dir ()
72+
5973 from pythonforandroid .toolchain import main
6074 sys .argv [1 ] = 'apk'
6175 main ()
6276
77+ def prepare_build_dir (self ):
78+
79+ if argv_contains ('--private' ):
80+ print ('WARNING: Received --private argument when this would '
81+ 'normally be generated automatically.' )
82+ print (' This is probably bad unless you meant to do '
83+ 'that.' )
84+
85+ bdist_dir = 'build/bdist.android-{}' .format (self .arch )
86+ rmtree (bdist_dir )
87+ makedirs (bdist_dir )
88+
89+ globs = []
90+ for directory , patterns in self .distribution .package_data .items ():
91+ for pattern in patterns :
92+ globs .append (join (directory , pattern ))
93+
94+ filens = []
95+ for pattern in globs :
96+ filens .extend (glob (pattern ))
97+
98+ main_py_dirs = []
99+ for filen in filens :
100+ new_dir = join (bdist_dir , dirname (filen ))
101+ if not exists (new_dir ):
102+ makedirs (new_dir )
103+ print ('Including {}' .format (filen ))
104+ copyfile (filen , join (bdist_dir , filen ))
105+ if basename (filen ) in ('main.py' , 'main.pyo' ):
106+ main_py_dirs .append (filen )
107+
108+ # This feels ridiculous, but how else to define the main.py dir?
109+ # Maybe should just fail?
110+ print ('main_py_dirs' , main_py_dirs )
111+ if len (main_py_dirs ) == 0 :
112+ print ('ERROR: Could not find main.py, so no app build dir defined' )
113+ print ('You should name your app entry point main.py' )
114+ exit (1 )
115+ if len (main_py_dirs ) > 1 :
116+ print ('WARNING: Multiple main.py dirs found, using the shortest path' )
117+ main_py_dirs = sorted (main_py_dirs , key = lambda j : len (split (j )))
118+
119+ sys .argv .append ('--private={}' .format (join (realpath (curdir ), bdist_dir ,
120+ dirname (main_py_dirs [0 ]))))
121+ print ('new argv' , sys .argv )
63122
64123
65124def _set_user_options ():
0 commit comments