|
| 1 | +from pythonforandroid.toolchain import Bootstrap, shprint, current_directory, info, warning, ArchARM, info_main |
| 2 | +from os.path import join, exists, curdir, abspath |
| 3 | +from os import walk |
| 4 | +import glob |
| 5 | +import sh |
| 6 | + |
| 7 | +class WebViewBootstrap(Bootstrap): |
| 8 | + name = 'webview' |
| 9 | + |
| 10 | + recipe_depends = ['webviewjni', ('python2', 'python3crystax')] |
| 11 | + |
| 12 | + def run_distribute(self): |
| 13 | + info_main('# Creating Android project from build and {} bootstrap'.format( |
| 14 | + self.name)) |
| 15 | + |
| 16 | + shprint(sh.rm, '-rf', self.dist_dir) |
| 17 | + shprint(sh.cp, '-r', self.build_dir, self.dist_dir) |
| 18 | + with current_directory(self.dist_dir): |
| 19 | + with open('local.properties', 'w') as fileh: |
| 20 | + fileh.write('sdk.dir={}'.format(self.ctx.sdk_dir)) |
| 21 | + |
| 22 | + arch = self.ctx.archs[0] |
| 23 | + if len(self.ctx.archs) > 1: |
| 24 | + raise ValueError('built for more than one arch, but bootstrap cannot handle that yet') |
| 25 | + info('Bootstrap running with arch {}'.format(arch)) |
| 26 | + |
| 27 | + with current_directory(self.dist_dir): |
| 28 | + info('Copying python distribution') |
| 29 | + |
| 30 | + if not exists('private') and not self.ctx.python_recipe.from_crystax: |
| 31 | + shprint(sh.mkdir, 'private') |
| 32 | + if not exists('crystax_python') and self.ctx.python_recipe.from_crystax: |
| 33 | + shprint(sh.mkdir, 'crystax_python') |
| 34 | + shprint(sh.mkdir, 'crystax_python/crystax_python') |
| 35 | + if not exists('assets'): |
| 36 | + shprint(sh.mkdir, 'assets') |
| 37 | + |
| 38 | + hostpython = sh.Command(self.ctx.hostpython) |
| 39 | + if not self.ctx.python_recipe.from_crystax: |
| 40 | + try: |
| 41 | + shprint(hostpython, '-OO', '-m', 'compileall', |
| 42 | + self.ctx.get_python_install_dir(), |
| 43 | + _tail=10, _filterout="^Listing") |
| 44 | + except sh.ErrorReturnCode: |
| 45 | + pass |
| 46 | + if not exists('python-install'): |
| 47 | + shprint(sh.cp, '-a', self.ctx.get_python_install_dir(), './python-install') |
| 48 | + |
| 49 | + self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)]) |
| 50 | + self.distribute_aars(arch) |
| 51 | + self.distribute_javaclasses(self.ctx.javaclass_dir) |
| 52 | + |
| 53 | + if not self.ctx.python_recipe.from_crystax: |
| 54 | + info('Filling private directory') |
| 55 | + if not exists(join('private', 'lib')): |
| 56 | + info('private/lib does not exist, making') |
| 57 | + shprint(sh.cp, '-a', join('python-install', 'lib'), 'private') |
| 58 | + shprint(sh.mkdir, '-p', join('private', 'include', 'python2.7')) |
| 59 | + |
| 60 | + # AND: Copylibs stuff should go here |
| 61 | + if exists(join('libs', arch.arch, 'libpymodules.so')): |
| 62 | + shprint(sh.mv, join('libs', arch.arch, 'libpymodules.so'), 'private/') |
| 63 | + shprint(sh.cp, join('python-install', 'include' , 'python2.7', 'pyconfig.h'), join('private', 'include', 'python2.7/')) |
| 64 | + |
| 65 | + info('Removing some unwanted files') |
| 66 | + shprint(sh.rm, '-f', join('private', 'lib', 'libpython2.7.so')) |
| 67 | + shprint(sh.rm, '-rf', join('private', 'lib', 'pkgconfig')) |
| 68 | + |
| 69 | + libdir = join(self.dist_dir, 'private', 'lib', 'python2.7') |
| 70 | + site_packages_dir = join(libdir, 'site-packages') |
| 71 | + with current_directory(libdir): |
| 72 | + # shprint(sh.xargs, 'rm', sh.grep('-E', '*\.(py|pyx|so\.o|so\.a|so\.libs)$', sh.find('.'))) |
| 73 | + removes = [] |
| 74 | + for dirname, something, filens in walk('.'): |
| 75 | + for filename in filens: |
| 76 | + for suffix in ('py', 'pyc', 'so.o', 'so.a', 'so.libs'): |
| 77 | + if filename.endswith(suffix): |
| 78 | + removes.append(filename) |
| 79 | + shprint(sh.rm, '-f', *removes) |
| 80 | + |
| 81 | + info('Deleting some other stuff not used on android') |
| 82 | + # To quote the original distribute.sh, 'well...' |
| 83 | + # shprint(sh.rm, '-rf', 'ctypes') |
| 84 | + shprint(sh.rm, '-rf', 'lib2to3') |
| 85 | + shprint(sh.rm, '-rf', 'idlelib') |
| 86 | + for filename in glob.glob('config/libpython*.a'): |
| 87 | + shprint(sh.rm, '-f', filename) |
| 88 | + shprint(sh.rm, '-rf', 'config/python.o') |
| 89 | + # shprint(sh.rm, '-rf', 'lib-dynload/_ctypes_test.so') |
| 90 | + # shprint(sh.rm, '-rf', 'lib-dynload/_testcapi.so') |
| 91 | + |
| 92 | + else: # Python *is* loaded from crystax |
| 93 | + ndk_dir = self.ctx.ndk_dir |
| 94 | + py_recipe = self.ctx.python_recipe |
| 95 | + python_dir = join(ndk_dir, 'sources', 'python', py_recipe.version, |
| 96 | + 'libs', arch.arch) |
| 97 | + |
| 98 | + shprint(sh.cp, '-r', join(python_dir, 'stdlib.zip'), 'crystax_python/crystax_python') |
| 99 | + shprint(sh.cp, '-r', join(python_dir, 'modules'), 'crystax_python/crystax_python') |
| 100 | + shprint(sh.cp, '-r', self.ctx.get_python_install_dir(), 'crystax_python/crystax_python/site-packages') |
| 101 | + |
| 102 | + info('Renaming .so files to reflect cross-compile') |
| 103 | + site_packages_dir = 'crystax_python/crystax_python/site-packages' |
| 104 | + filens = shprint(sh.find, site_packages_dir, '-iname', '*.so').stdout.decode( |
| 105 | + 'utf-8').split('\n')[:-1] |
| 106 | + for filen in filens: |
| 107 | + parts = filen.split('.') |
| 108 | + if len(parts) <= 2: |
| 109 | + continue |
| 110 | + shprint(sh.mv, filen, filen.split('.')[0] + '.so') |
| 111 | + site_packages_dir = join(abspath(curdir), |
| 112 | + site_packages_dir) |
| 113 | + |
| 114 | + |
| 115 | + self.strip_libraries(arch) |
| 116 | + self.fry_eggs(site_packages_dir) |
| 117 | + super(WebViewBootstrap, self).run_distribute() |
| 118 | + |
| 119 | +bootstrap = WebViewBootstrap() |
0 commit comments