1212 from urlparse import urlparse
1313except ImportError :
1414 from urllib .parse import urlparse
15- from pythonforandroid .logger import (logger , info , warning , shprint , info_main )
15+ from pythonforandroid .logger import (logger , info , warning , error , shprint , info_main )
1616from pythonforandroid .util import (urlretrieve , current_directory , ensure_dir )
1717
1818# this import is necessary to keep imp.load_source from complaining :)
@@ -563,6 +563,13 @@ def clean_build(self, arch=None):
563563 warning (('Attempted to clean build for {} but build '
564564 'did not exist' ).format (self .name ))
565565
566+ def install_libs (self , arch , lib , * libs ):
567+ libs_dir = self .ctx .get_libs_dir (arch .arch )
568+ shprint (sh .cp , '-t' , libs_dir , lib , * libs )
569+
570+ def has_libs (self , arch , * libs ):
571+ return all (map (lambda l : self .ctx .has_lib (arch .arch , l ), libs ))
572+
566573 @classmethod
567574 def recipe_dirs (cls , ctx ):
568575 return [ctx .local_recipes ,
@@ -694,21 +701,44 @@ class PythonRecipe(Recipe):
694701 call_hostpython_via_targetpython is False.
695702 '''
696703
704+ install_in_targetpython = True
705+ '''If True, installs the module in the targetpython installation dir.
706+ This is almost always what you want to do.'''
707+
697708 setup_extra_args = []
698709 '''List of extra arugments to pass to setup.py'''
699710
711+ @property
712+ def real_hostpython_location (self ):
713+ if 'hostpython2' in self .ctx .build_order :
714+ return join (
715+ Recipe .get_recipe ('hostpython2' , self .ctx ).get_build_dir (),
716+ 'hostpython' )
717+ else :
718+ python_recipe = self .ctx .python_recipe
719+ return 'python{}' .format (python_recipe .version )
720+
700721 @property
701722 def hostpython_location (self ):
702723 if not self .call_hostpython_via_targetpython :
703- if 'hostpython2' in self .ctx .build_order :
704- return join (
705- Recipe .get_recipe ('hostpython2' , self .ctx ).get_build_dir (),
706- 'hostpython' )
707- else :
708- python_recipe = self .ctx .python_recipe
709- return 'python{}' .format (python_recipe .version )
724+ return self .real_hostpython_location
710725 return self .ctx .hostpython
711726
727+ def get_recipe_env (self , arch = None , with_flags_in_cc = True ):
728+ env = super (PythonRecipe , self ).get_recipe_env (arch , with_flags_in_cc )
729+ if not self .call_hostpython_via_targetpython :
730+ hppath = []
731+ hppath .append (join (dirname (self .hostpython_location ), 'Lib' ))
732+ hppath .append (join (hppath [0 ], 'site-packages' ))
733+ builddir = join (dirname (self .hostpython_location ), 'build' )
734+ hppath += [join (builddir , d ) for d in listdir (builddir )
735+ if isdir (join (builddir , d ))]
736+ if 'PYTHONPATH' in env :
737+ env ['PYTHONPATH' ] = ':' .join (hppath + [env ['PYTHONPATH' ]])
738+ else :
739+ env ['PYTHONPATH' ] = ':' .join (hppath )
740+ return env
741+
712742 def should_build (self , arch ):
713743 print ('name is' , self .site_packages_name , type (self ))
714744 name = self .site_packages_name
@@ -738,7 +768,6 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
738768 info ('Installing {} into site-packages' .format (self .name ))
739769
740770 with current_directory (self .get_build_dir (arch .arch )):
741- # hostpython = sh.Command(self.ctx.hostpython)
742771 hostpython = sh .Command (self .hostpython_location )
743772 # hostpython = sh.Command('python3.5')
744773
@@ -784,10 +813,20 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
784813
785814 # If asked, also install in the hostpython build dir
786815 if self .install_in_hostpython :
787- shprint (hostpython , 'setup.py' , 'install' , '-O2' ,
788- '--root={}' .format (dirname (self .hostpython_location )),
789- '--install-lib=Lib/site-packages' ,
790- _env = env , * self .setup_extra_args )
816+ self .install_hostpython_package (arch )
817+
818+ def get_hostrecipe_env (self , arch ):
819+ env = environ .copy ()
820+ env ['PYTHONPATH' ] = join (dirname (self .real_hostpython_location ), 'Lib' , 'site-packages' )
821+ return env
822+
823+ def install_hostpython_package (self , arch ):
824+ env = self .get_hostrecipe_env (arch )
825+ real_hostpython = sh .Command (self .real_hostpython_location )
826+ shprint (real_hostpython , 'setup.py' , 'install' , '-O2' ,
827+ '--root={}' .format (dirname (self .real_hostpython_location )),
828+ '--install-lib=Lib/site-packages' ,
829+ _env = env , * self .setup_extra_args )
791830
792831
793832class CompiledComponentsPythonRecipe (PythonRecipe ):
@@ -809,22 +848,27 @@ def build_compiled_components(self, arch):
809848 env = self .get_recipe_env (arch )
810849 with current_directory (self .get_build_dir (arch .arch )):
811850 hostpython = sh .Command (self .hostpython_location )
812- if self .call_hostpython_via_targetpython :
813- shprint (hostpython , 'setup.py' , self .build_cmd , '-v' ,
814- _env = env , * self .setup_extra_args )
815- else :
816- hppath = join (dirname (self .hostpython_location ), 'Lib' ,
817- 'site-packages' )
818- if 'PYTHONPATH' in env :
819- env ['PYTHONPATH' ] = hppath + ':' + env ['PYTHONPATH' ]
820- else :
821- env ['PYTHONPATH' ] = hppath
822- shprint (hostpython , 'setup.py' , self .build_cmd , '-v' , _env = env ,
823- * self .setup_extra_args )
851+ if self .install_in_hostpython :
852+ shprint (hostpython , 'setup.py' , 'clean' , '--all' , _env = env )
853+ shprint (hostpython , 'setup.py' , self .build_cmd , '-v' ,
854+ _env = env , * self .setup_extra_args )
824855 build_dir = glob .glob ('build/lib.*' )[0 ]
825856 shprint (sh .find , build_dir , '-name' , '"*.o"' , '-exec' ,
826857 env ['STRIP' ], '{}' , ';' , _env = env )
827858
859+ def install_hostpython_package (self , arch ):
860+ env = self .get_hostrecipe_env (arch )
861+ self .rebuild_compiled_components (arch , env )
862+ super (CompiledComponentsPythonRecipe , self ).install_hostpython_package (arch )
863+
864+ def rebuild_compiled_components (self , arch , env ):
865+ info ('Rebuilding compiled components in {}' .format (self .name ))
866+
867+ hostpython = sh .Command (self .real_hostpython_location )
868+ shprint (hostpython , 'setup.py' , 'clean' , '--all' , _env = env )
869+ shprint (hostpython , 'setup.py' , self .build_cmd , '-v' , _env = env ,
870+ * self .setup_extra_args )
871+
828872
829873class CythonRecipe (PythonRecipe ):
830874 pre_build_ext = False
@@ -880,13 +924,15 @@ def build_cython_components(self, arch):
880924
881925 if manually_cythonise :
882926 info ('Running cython where appropriate' )
883- if self .ctx .python_recipe .from_crystax :
884- shprint (sh .find , self .get_build_dir (arch .arch ), '-iname' , '*.pyx' ,
885- '-exec' , 'cython' , '{}' , ';' )
886- # AND: Need to choose cython version more carefully
887- else :
888- shprint (sh .find , self .get_build_dir (arch .arch ), '-iname' , '*.pyx' ,
889- '-exec' , self .ctx .cython , '{}' , ';' , _env = env )
927+ cyenv = env .copy ()
928+ if 'CYTHONPATH' in cyenv :
929+ cyenv ['PYTHONPATH' ] = cyenv ['CYTHONPATH' ]
930+ elif 'PYTHONPATH' in cyenv :
931+ del cyenv ['PYTHONPATH' ]
932+ cython = 'cython' if self .ctx .python_recipe .from_crystax else self .ctx .cython
933+ cython_cmd = 'find "{}" -iname *.pyx | xargs "{}"' .format (
934+ self .get_build_dir (arch .arch ), cython )
935+ shprint (sh .sh , '-c' , cython_cmd , _env = cyenv )
890936 info ('ran cython' )
891937
892938 shprint (hostpython , 'setup.py' , 'build_ext' , '-v' , _env = env ,
@@ -900,7 +946,6 @@ def build_cython_components(self, arch):
900946 shprint (sh .find , build_lib [0 ], '-name' , '*.o' , '-exec' ,
901947 env ['STRIP' ], '{}' , ';' , _env = env )
902948 print ('stripped!?' )
903- # exit(1)
904949
905950 # def cythonize_file(self, filename):
906951 # if filename.startswith(self.build_dir):
0 commit comments