def create_python_bundle(self, dirn, arch): """ Create a packaged python bundle in the target directory, by copying all the modules and standard library to the right place. """ # Bundle compiled python modules to a folder modules_dir = join(dirn, 'modules') ensure_dir(modules_dir) # Todo: find a better way to find the build libs folder modules_build_dir = join( self.get_build_dir(arch.arch), 'android-build', 'build', 'lib.linux{}-arm-{}'.format( '2' if self.version[0] == '2' else '', self.major_minor_version_string )) module_filens = (glob.glob(join(modules_build_dir, '*.so')) + glob.glob(join(modules_build_dir, '*.py'))) for filen in module_filens: shprint(sh.cp, filen, modules_dir) # zip up the standard library stdlib_zip = join(dirn, 'stdlib.zip') with current_directory(join(self.get_build_dir(arch.arch), 'Lib')): stdlib_filens = walk_valid_filens( '.', self.stdlib_dir_blacklist, self.stdlib_filen_blacklist) shprint(sh.zip, stdlib_zip, *stdlib_filens) # copy the site-packages into place ensure_dir(join(dirn, 'site-packages')) ensure_dir(self.ctx.get_python_install_dir()) # TODO: Improve the API around walking and copying the files with current_directory(self.ctx.get_python_install_dir()): filens = list(walk_valid_filens( '.', self.site_packages_dir_blacklist, self.site_packages_filen_blacklist)) for filen in filens: ensure_dir(join(dirn, 'site-packages', dirname(filen))) sh.cp(filen, join(dirn, 'site-packages', filen)) # copy the python .so files into place python_build_dir = join(self.get_build_dir(arch.arch), 'android-build') python_lib_name = 'libpython' + self.major_minor_version_string if self.major_minor_version_string[0] == '3': python_lib_name += 'm' shprint(sh.cp, join(python_build_dir, python_lib_name + '.so'), 'libs/{}'.format(arch.arch)) info('Renaming .so files to reflect cross-compile') self.reduce_object_file_names(join(dirn, 'site-packages')) return join(dirn, 'site-packages')
def create_python_bundle(self, dirn, arch): ndk_dir = self.ctx.ndk_dir # Bundle compiled python modules to a folder modules_dir = join(dirn, 'modules') ensure_dir(modules_dir) modules_build_dir = join( self.get_build_dir(arch.arch), 'android-build', 'build', 'lib.linux-arm-3.7') module_filens = (glob.glob(join(modules_build_dir, '*.so')) + glob.glob(join(modules_build_dir, '*.py'))) for filen in module_filens: shprint(sh.cp, filen, modules_dir) # zip up the standard library stdlib_zip = join(dirn, 'stdlib.zip') with current_directory(join(self.get_build_dir(arch.arch), 'Lib')): stdlib_filens = walk_valid_filens( '.', STDLIB_DIR_BLACKLIST, STDLIB_FILEN_BLACKLIST) shprint(sh.zip, stdlib_zip, *stdlib_filens) # copy the site-packages into place ensure_dir(join(dirn, 'site-packages')) # TODO: Improve the API around walking and copying the files with current_directory(self.ctx.get_python_install_dir()): filens = list(walk_valid_filens( '.', SITE_PACKAGES_DIR_BLACKLIST, SITE_PACKAGES_FILEN_BLACKLIST)) for filen in filens: ensure_dir(join(dirn, 'site-packages', dirname(filen))) sh.cp(filen, join(dirn, 'site-packages', filen)) # copy the python .so files into place python_build_dir = join(self.get_build_dir(arch.arch), 'android-build') shprint(sh.cp, join(python_build_dir, 'libpython{}m.so'.format(self.major_minor_version_string)), 'libs/{}'.format(arch.arch)) shprint(sh.cp, join(python_build_dir, 'libpython{}m.so.1.0'.format(self.major_minor_version_string)), 'libs/{}'.format(arch.arch)) info('Renaming .so files to reflect cross-compile') self.reduce_object_file_names(join(dirn, 'site-packages')) return join(dirn, 'site-packages')
def create_python_bundle(self, dirn, arch): ndk_dir = self.ctx.ndk_dir # Bundle compiled python modules to a folder modules_dir = join(dirn, 'modules') ensure_dir(modules_dir) modules_build_dir = join( self.get_build_dir(arch.arch), 'android-build', 'build', 'lib.linux-arm-3.7') module_filens = (glob.glob(join(modules_build_dir, '*.so')) + glob.glob(join(modules_build_dir, '*.py'))) for filen in module_filens: shprint(sh.cp, filen, modules_dir) # zip up the standard library stdlib_zip = join(dirn, 'stdlib.zip') with current_directory(join(self.get_build_dir(arch.arch), 'Lib')): stdlib_filens = walk_valid_filens( '.', STDLIB_DIR_BLACKLIST, STDLIB_FILEN_BLACKLIST) shprint(sh.zip, stdlib_zip, *stdlib_filens) # copy the site-packages into place ensure_dir(join(dirn, 'site-packages')) # TODO: Improve the API around walking and copying the files with current_directory(self.ctx.get_python_install_dir()): filens = list(walk_valid_filens( '.', SITE_PACKAGES_DIR_BLACKLIST, SITE_PACKAGES_FILEN_BLACKLIST)) for filen in filens: ensure_dir(join(dirn, 'site-packages', dirname(filen))) sh.cp(filen, join(dirn, 'site-packages', filen)) # copy the python .so files into place python_build_dir = join(self.get_build_dir(arch.arch), 'android-build') shprint(sh.cp, join(python_build_dir, 'libpython{}m.so'.format(self.major_minor_version_string)), 'libs/{}'.format(arch.arch)) shprint(sh.cp, join(python_build_dir, 'libpython{}m.so.1.0'.format(self.major_minor_version_string)), 'libs/{}'.format(arch.arch)) info('Renaming .so files to reflect cross-compile') self.reduce_object_file_names(join(dirn, 'site-packages')) return join(dirn, 'site-packages')
def test_walk_valid_filens(self, mock_walk): """ Test method :meth:`~pythonforandroid.util.walk_valid_filens` In here we simulate the following directory structure: /fake_dir |-- README |-- setup.py |-- __pycache__ |-- |__ |__Lib |-- abc.pyc |-- abc.py |__ ctypes |-- util.pyc |-- util.py Then we execute the method in order to check that we got the expected result, which should be: .. code-block:: python :emphasize-lines: 2-4 expected_result = { "/fake_dir/README", "/fake_dir/Lib/abc.pyc", "/fake_dir/Lib/ctypes/util.pyc", } """ simulated_walk_result = [ ["/fake_dir", ["__pycache__", "Lib"], ["README", "setup.py"]], ["/fake_dir/Lib", ["ctypes"], ["abc.pyc", "abc.py"]], ["/fake_dir/Lib/ctypes", [], ["util.pyc", "util.py"]], ] mock_walk.return_value = simulated_walk_result file_ens = util.walk_valid_filens( "/fake_dir", ["__pycache__"], ["*.py"] ) self.assertIsInstance(file_ens, types.GeneratorType) expected_result = { "/fake_dir/README", "/fake_dir/Lib/abc.pyc", "/fake_dir/Lib/ctypes/util.pyc", } result = set(file_ens) self.assertEqual(result, expected_result)
def create_python_bundle(self, dirn, arch): """ Create a packaged python bundle in the target directory, by copying all the modules and standard library to the right place. """ # Todo: find a better way to find the build libs folder modules_build_dir = join( self.get_build_dir(arch.arch), 'android-build', 'build', 'lib.linux{}-{}-{}'.format('2' if self.version[0] == '2' else '', arch.command_prefix.split('-')[0], self.major_minor_version_string)) # Compile to *.pyc/*.pyo the python modules self.compile_python_files(modules_build_dir) # Compile to *.pyc/*.pyo the standard python library self.compile_python_files(join(self.get_build_dir(arch.arch), 'Lib')) # Compile to *.pyc/*.pyo the other python packages (site-packages) self.compile_python_files(self.ctx.get_python_install_dir()) # Bundle compiled python modules to a folder modules_dir = join(dirn, 'modules') c_ext = self.compiled_extension ensure_dir(modules_dir) module_filens = (glob.glob(join(modules_build_dir, '*.so')) + glob.glob(join(modules_build_dir, '*' + c_ext))) info("Copy {} files into the bundle".format(len(module_filens))) for filen in module_filens: info(" - copy {}".format(filen)) copy2(filen, modules_dir) # zip up the standard library stdlib_zip = join(dirn, 'stdlib.zip') with current_directory(join(self.get_build_dir(arch.arch), 'Lib')): stdlib_filens = list( walk_valid_filens('.', self.stdlib_dir_blacklist, self.stdlib_filen_blacklist)) if 'SOURCE_DATE_EPOCH' in environ: # for reproducible builds stdlib_filens.sort() timestamp = int(environ['SOURCE_DATE_EPOCH']) for filen in stdlib_filens: utime(filen, (timestamp, timestamp)) info("Zip {} files into the bundle".format(len(stdlib_filens))) shprint(sh.zip, '-X', stdlib_zip, *stdlib_filens) # copy the site-packages into place ensure_dir(join(dirn, 'site-packages')) ensure_dir(self.ctx.get_python_install_dir()) # TODO: Improve the API around walking and copying the files with current_directory(self.ctx.get_python_install_dir()): filens = list( walk_valid_filens('.', self.site_packages_dir_blacklist, self.site_packages_filen_blacklist)) info("Copy {} files into the site-packages".format(len(filens))) for filen in filens: info(" - copy {}".format(filen)) ensure_dir(join(dirn, 'site-packages', dirname(filen))) copy2(filen, join(dirn, 'site-packages', filen)) # copy the python .so files into place python_build_dir = join(self.get_build_dir(arch.arch), 'android-build') python_lib_name = 'libpython' + self.link_version shprint(sh.cp, join(python_build_dir, python_lib_name + '.so'), join(self.ctx.bootstrap.dist_dir, 'libs', arch.arch)) info('Renaming .so files to reflect cross-compile') self.reduce_object_file_names(join(dirn, 'site-packages')) return join(dirn, 'site-packages')
def create_python_bundle(self, dirn, arch): """ Create a packaged python bundle in the target directory, by copying all the modules and standard library to the right place. """ # Todo: find a better way to find the build libs folder modules_build_dir = join( self.get_build_dir(arch.arch), 'android-build', 'build', 'lib.linux{}-{}-{}'.format( '2' if self.version[0] == '2' else '', arch.command_prefix.split('-')[0], self.major_minor_version_string )) # Compile to *.pyc/*.pyo the python modules self.compile_python_files(modules_build_dir) # Compile to *.pyc/*.pyo the standard python library self.compile_python_files(join(self.get_build_dir(arch.arch), 'Lib')) # Compile to *.pyc/*.pyo the other python packages (site-packages) self.compile_python_files(self.ctx.get_python_install_dir()) # Bundle compiled python modules to a folder modules_dir = join(dirn, 'modules') c_ext = self.compiled_extension ensure_dir(modules_dir) module_filens = (glob.glob(join(modules_build_dir, '*.so')) + glob.glob(join(modules_build_dir, '*' + c_ext))) info("Copy {} files into the bundle".format(len(module_filens))) for filen in module_filens: info(" - copy {}".format(filen)) copy2(filen, modules_dir) # zip up the standard library stdlib_zip = join(dirn, 'stdlib.zip') with current_directory(join(self.get_build_dir(arch.arch), 'Lib')): stdlib_filens = list(walk_valid_filens( '.', self.stdlib_dir_blacklist, self.stdlib_filen_blacklist)) info("Zip {} files into the bundle".format(len(stdlib_filens))) shprint(sh.zip, stdlib_zip, *stdlib_filens) # copy the site-packages into place ensure_dir(join(dirn, 'site-packages')) ensure_dir(self.ctx.get_python_install_dir()) # TODO: Improve the API around walking and copying the files with current_directory(self.ctx.get_python_install_dir()): filens = list(walk_valid_filens( '.', self.site_packages_dir_blacklist, self.site_packages_filen_blacklist)) info("Copy {} files into the site-packages".format(len(filens))) for filen in filens: info(" - copy {}".format(filen)) ensure_dir(join(dirn, 'site-packages', dirname(filen))) copy2(filen, join(dirn, 'site-packages', filen)) # copy the python .so files into place python_build_dir = join(self.get_build_dir(arch.arch), 'android-build') python_lib_name = 'libpython' + self.major_minor_version_string if self.major_minor_version_string[0] == '3': python_lib_name += 'm' shprint(sh.cp, join(python_build_dir, python_lib_name + '.so'), join(self.ctx.dist_dir, self.ctx.dist_name, 'libs', arch.arch)) info('Renaming .so files to reflect cross-compile') self.reduce_object_file_names(join(dirn, 'site-packages')) return join(dirn, 'site-packages')
def create_python_bundle(self, dirn, arch): """ Create a packaged python bundle in the target directory, by copying all the modules and standard library to the right place. """ # Todo: find a better way to find the build libs folder modules_build_dir = join( self.get_build_dir(arch.arch), "android-build", "build", "lib.linux{}-{}-{}".format( "2" if self.version[0] == "2" else "", arch.command_prefix.split("-")[0], self.major_minor_version_string, ), ) # Compile to *.pyc/*.pyo the python modules self.compile_python_files(modules_build_dir) # Compile to *.pyc/*.pyo the standard python library self.compile_python_files(join(self.get_build_dir(arch.arch), "Lib")) # Compile to *.pyc/*.pyo the other python packages (site-packages) self.compile_python_files(self.ctx.get_python_install_dir()) # Bundle compiled python modules to a folder modules_dir = join(dirn, "modules") c_ext = self.compiled_extension ensure_dir(modules_dir) module_filens = glob.glob(join(modules_build_dir, "*.so")) + glob.glob( join(modules_build_dir, "*" + c_ext)) info("Copy {} files into the bundle".format(len(module_filens))) for filen in module_filens: info(" - copy {}".format(filen)) copy2(filen, modules_dir) # zip up the standard library stdlib_zip = join(dirn, "stdlib.zip") with current_directory(join(self.get_build_dir(arch.arch), "Lib")): stdlib_filens = list( walk_valid_filens(".", self.stdlib_dir_blacklist, self.stdlib_filen_blacklist)) info("Zip {} files into the bundle".format(len(stdlib_filens))) shprint(sh.zip, stdlib_zip, *stdlib_filens) # copy the site-packages into place ensure_dir(join(dirn, "site-packages")) ensure_dir(self.ctx.get_python_install_dir()) # TODO: Improve the API around walking and copying the files with current_directory(self.ctx.get_python_install_dir()): filens = list( walk_valid_filens( ".", self.site_packages_dir_blacklist, self.site_packages_filen_blacklist, )) info("Copy {} files into the site-packages".format(len(filens))) for filen in filens: info(" - copy {}".format(filen)) ensure_dir(join(dirn, "site-packages", dirname(filen))) copy2(filen, join(dirn, "site-packages", filen)) # copy the python .so files into place python_build_dir = join(self.get_build_dir(arch.arch), "android-build") python_lib_name = "libpython" + self.major_minor_version_string if self.major_minor_version_string[0] == "3": python_lib_name += "m" shprint( sh.cp, join(python_build_dir, python_lib_name + ".so"), join(self.ctx.bootstrap.dist_dir, "libs", arch.arch), ) info("Renaming .so files to reflect cross-compile") self.reduce_object_file_names(join(dirn, "site-packages")) return join(dirn, "site-packages")