class ArchSetUpBaseClass(object): """ An class object which is intended to be used as a base class to configure an inherited class of `unittest.TestCase`. This class will override the `setUp` method. """ ctx = None expected_compiler = "" TEST_ARCH = 'armeabi-v7a' def setUp(self): self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx) self.ctx.bootstrap.distribution = Distribution.get_distribution( self.ctx, name="sdl2", recipes=["python3", "kivy"], arch_name=self.TEST_ARCH, ) self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx) # Here we define the expected compiler, which, as per ndk >= r19, # should be the same for all the tests (no more gcc compiler) self.expected_compiler = (f"/opt/android/android-ndk/toolchains/" f"llvm/prebuilt/{build_platform}/bin/clang")
class BaseClassSetupBootstrap(object): """ An class object which is intended to be used as a base class to configure an inherited class of `unittest.TestCase`. This class will override the `setUp` and `tearDown` methods. """ def setUp(self): self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.recipe_build_order = [ "hostpython3", "python3", "sdl2", "kivy", ] def setUp_distribution_with_bootstrap(self, bs): """ Extend the setUp by configuring a distribution, because some test needs a distribution to be set to be properly tested """ self.ctx.bootstrap = bs self.ctx.bootstrap.distribution = Distribution.get_distribution( self.ctx, name="test_prj", recipes=["python3", "kivy"]) def tearDown(self): """ Extend the `tearDown` by configuring a distribution, because some test needs a distribution to be set to be properly tested """ self.ctx.bootstrap = None
def setUp(self): self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx) self.ctx.bootstrap.distribution = Distribution.get_distribution( self.ctx, name="sdl2", recipes=["python3", "kivy"]) self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx)
def setUp(self): self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.recipe_build_order = [ "hostpython3", "python3", "sdl2", "kivy", ]
def setUp(self): """Configure a :class:`~pythonforandroid.build.Context` so we can perform our unittests""" self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.recipe_build_order = [ "hostpython3", "python3", "sdl2", "kivy", ]
def setUp(self): self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx) self.ctx.bootstrap.distribution = Distribution.get_distribution( self.ctx, name="sdl2", recipes=["python3", "kivy"]) self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx) # Here we define the expected compiler, which, as per ndk >= r19, # should be the same for all the tests (no more gcc compiler) self.expected_compiler = ("/opt/android/android-ndk/toolchains/" "llvm/prebuilt/linux-x86_64/bin/clang")
def main(): target_python = TargetPython.python3 recipes = modified_recipes() logger.info('recipes modified: {}'.format(recipes)) recipes -= CORE_RECIPES logger.info('recipes to build: {}'.format(recipes)) context = Context() # removing the deleted recipes for the given target (if any) for recipe_name in recipes.copy(): try: Recipe.get_recipe(recipe_name, context) except ValueError: # recipe doesn't exist, so probably we remove it recipes.remove(recipe_name) logger.warning( 'removed {} from recipes because deleted'.format(recipe_name)) # forces the default target recipes_and_target = recipes | set([target_python.name]) try: build_order, python_modules, bs = get_recipe_order_and_bootstrap( context, recipes_and_target, None) except BuildInterruptingException: # fallback to python2 if default target is not compatible logger.info('incompatible with {}'.format(target_python.name)) target_python = TargetPython.python2 logger.info('falling back to {}'.format(target_python.name)) # removing the known broken recipe for the given target broken_recipes = BROKEN_RECIPES[target_python] recipes -= broken_recipes logger.info('recipes to build (no broken): {}'.format(recipes)) build(target_python, recipes)
class TestReportLabRecipe(unittest.TestCase): def setUp(self): """ Setups recipe and context. """ self.context = Context() self.context.ndk_api = 21 self.context.android_api = 27 self.arch = ArchARMv7_a(self.context) self.recipe = Recipe.get_recipe('reportlab', self.context) self.recipe.ctx = self.context self.bootstrap = None recipe_build_order, python_modules, bootstrap = \ get_recipe_order_and_bootstrap( self.context, [self.recipe.name], self.bootstrap) self.context.recipe_build_order = recipe_build_order self.context.python_modules = python_modules self.context.setup_dirs(tempfile.gettempdir()) self.bootstrap = bootstrap self.recipe_dir = self.recipe.get_build_dir(self.arch.arch) ensure_dir(self.recipe_dir) def test_prebuild_arch(self): """ Makes sure `prebuild_arch()` runs without error and patches `setup.py` as expected. """ # `prebuild_arch()` dynamically replaces strings in the `setup.py` file setup_path = os.path.join(self.recipe_dir, 'setup.py') with open(setup_path, 'w') as setup_file: setup_file.write('_FT_LIB_\n') setup_file.write('_FT_INC_\n') # these sh commands are not relevant for the test and need to be mocked with \ patch('sh.patch'), \ patch('sh.touch'), \ patch('sh.unzip'), \ patch('os.path.isfile'): self.recipe.prebuild_arch(self.arch) # makes sure placeholder got replaced with library and include paths with open(setup_path, 'r') as setup_file: lines = setup_file.readlines() self.assertTrue(lines[0].endswith('freetype/objs/.libs\n')) self.assertTrue(lines[1].endswith('freetype/include\n'))
def clean_dists(self, args): '''Delete all compiled distributions in the internal distribution directory.''' parser = argparse.ArgumentParser( description="Delete any distributions that have been built.") args = parser.parse_args(args) ctx = Context() if exists(ctx.dist_dir): shutil.rmtree(ctx.dist_dir)
def setUp(self): """ Setups recipe and context. """ self.context = Context() self.arch = ArchARMv7_a(self.context) self.recipe = Recipe.get_recipe('reportlab', self.context) self.recipe.ctx = self.context self.bootstrap = None recipe_build_order, python_modules, bootstrap = \ get_recipe_order_and_bootstrap( self.context, [self.recipe.name], self.bootstrap) self.context.recipe_build_order = recipe_build_order self.context.python_modules = python_modules self.context.setup_dirs(tempfile.gettempdir()) self.bootstrap = bootstrap self.recipe_dir = self.recipe.get_build_dir(self.arch.arch) ensure_dir(self.recipe_dir)
def test_list_recipes(self): """ Trivial test verifying list_recipes returns a generator with some recipes. """ ctx = Context() recipes = Recipe.list_recipes(ctx) self.assertTrue(isinstance(recipes, types.GeneratorType)) recipes = list(recipes) self.assertIn('python3', recipes)
def setUp(self): """ Setups recipe and context. """ self.context = Context() self.context.ndk_api = 21 self.context.android_api = 27 self.arch = ArchARMv7_a(self.context) self.recipe = Recipe.get_recipe('gevent', self.context)
def get_dummy_python_recipe_for_download_tests(): """ Helper method for creating a test recipe used in download tests. """ recipe = DummyRecipe() filename = 'Python-3.7.4.tgz' url = 'https://www.python.org/ftp/python/3.7.4/{}'.format(filename) recipe._url = url recipe.ctx = Context() return recipe, filename
def test_recipe_dirs(self): """ Trivial `recipe_dirs()` test. Makes sure the list is not empty and has the root directory. """ ctx = Context() recipes_dir = Recipe.recipe_dirs(ctx) # by default only the root dir `recipes` directory self.assertEqual(len(recipes_dir), 1) self.assertTrue(recipes_dir[0].startswith(ctx.root_dir))
def print_context_info(self, args): '''Prints some debug information about which system paths python-for-android will internally use for package building, along with information about where the Android SDK and NDK will be called from.''' ctx = Context() for attribute in ('root_dir', 'build_dir', 'dist_dir', 'libs_dir', 'ccache', 'cython', 'sdk_dir', 'ndk_dir', 'ndk_platform', 'ndk_ver', 'android_api'): print('{} is {}'.format(attribute, getattr(ctx, attribute)))
class ArchSetUpBaseClass(object): """ An class object which is intended to be used as a base class to configure an inherited class of `unittest.TestCase`. This class will override the `setUp` method. """ ctx = None def setUp(self): self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx) self.ctx.bootstrap.distribution = Distribution.get_distribution( self.ctx, name="sdl2", recipes=["python3", "kivy"]) self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx)
def clean_download_cache(self, args): ''' Deletes any downloaded recipe packages. This does *not* delete the build caches or final distributions. ''' parser = argparse.ArgumentParser( description="Delete all download caches") args = parser.parse_args(args) ctx = Context() if exists(ctx.packages_path): shutil.rmtree(ctx.packages_path)
def distributions(self, args): '''Lists all distributions currently available (i.e. that have already been built).''' ctx = Context() dists = Distribution.get_distributions(ctx) if dists: print('{Style.BRIGHT}Distributions currently installed are:' '{Style.RESET_ALL}'.format(Style=Out_Style, Fore=Out_Fore)) pretty_log_dists(dists, print) else: print('{Style.BRIGHT}There are no dists currently built.' '{Style.RESET_ALL}'.format(Style=Out_Style))
def test_get_recipe(self): """ Makes sure `get_recipe()` returns a `Recipe` object when possible. """ ctx = Context() recipe_name = 'python3' recipe = Recipe.get_recipe(recipe_name, ctx) self.assertTrue(isinstance(recipe, Recipe)) self.assertEqual(recipe.name, recipe_name) recipe_name = 'does_not_exist' with self.assertRaises(ValueError) as e: Recipe.get_recipe(recipe_name, ctx) self.assertEqual(e.exception.args[0], 'Recipe does not exist: {}'.format(recipe_name))
def main(): target_python = TargetPython.python3 recipes = modified_recipes() print('recipes modified:', recipes) recipes -= CORE_RECIPES print('recipes to build:', recipes) context = Context() build_order, python_modules, bs = get_recipe_order_and_bootstrap( context, recipes, None) # fallback to python2 if default target is not compatible if target_python.name not in build_order: print('incompatible with {}'.format(target_python.name)) target_python = TargetPython.python2 print('falling back to {}'.format(target_python.name)) # removing the known broken recipe for the given target broken_recipes = BROKEN_RECIPES[target_python] recipes -= broken_recipes print('recipes to build (no broken):', recipes) build(target_python, recipes)
def clean_builds(self, args): '''Delete all build caches for each recipe, python-install, java code and compiled libs collection. This does *not* delete the package download cache or the final distributions. You can also use clean_recipe_build to delete the build of a specific recipe. ''' parser = argparse.ArgumentParser( description="Delete all build files (but not download caches)") args = parser.parse_args(args) ctx = Context() # if exists(ctx.dist_dir): # shutil.rmtree(ctx.dist_dir) if exists(ctx.build_dir): shutil.rmtree(ctx.build_dir) if exists(ctx.python_installs_dir): shutil.rmtree(ctx.python_installs_dir) libs_dir = join(self.ctx.build_dir, 'libs_collections') if exists(libs_dir): shutil.rmtree(libs_dir)
def main(): target_python = TargetPython.python3 recipes = modified_recipes() logger.info('recipes modified: {}'.format(recipes)) recipes -= CORE_RECIPES logger.info('recipes to build: {}'.format(recipes)) context = Context() # forces the default target recipes_and_target = recipes | set([target_python.name]) try: build_order, python_modules, bs = get_recipe_order_and_bootstrap( context, recipes_and_target, None) except BuildInterruptingException: # fallback to python2 if default target is not compatible logger.info('incompatible with {}'.format(target_python.name)) target_python = TargetPython.python2 logger.info('falling back to {}'.format(target_python.name)) # removing the known broken recipe for the given target broken_recipes = BROKEN_RECIPES[target_python] recipes -= broken_recipes logger.info('recipes to build (no broken): {}'.format(recipes)) build(target_python, recipes)
def main(): target_python = TargetPython.python3 recipes = modified_recipes() logger.info('recipes modified: {}'.format(recipes)) recipes -= CORE_RECIPES logger.info('recipes to build: {}'.format(recipes)) context = Context() # removing the deleted recipes for the given target (if any) for recipe_name in recipes.copy(): try: Recipe.get_recipe(recipe_name, context) except ValueError: # recipe doesn't exist, so probably we remove it recipes.remove(recipe_name) logger.warning( 'removed {} from recipes because deleted'.format(recipe_name) ) # removing the known broken recipe for the given target broken_recipes = BROKEN_RECIPES[target_python] recipes -= broken_recipes logger.info('recipes to build (no broken): {}'.format(recipes)) build(target_python, recipes)
class TestDistribution(unittest.TestCase): """ An inherited class of `unittest.TestCase`to test the module :mod:`~pythonforandroid.distribution`. """ TEST_ARCH = 'armeabi-v7a' def setUp(self): """Configure a :class:`~pythonforandroid.build.Context` so we can perform our unittests""" self.ctx = Context() self.ctx.ndk_api = 21 self.ctx.android_api = 27 self.ctx._sdk_dir = "/opt/android/android-sdk" self.ctx._ndk_dir = "/opt/android/android-ndk" self.ctx.setup_dirs(os.getcwd()) self.ctx.recipe_build_order = [ "hostpython3", "python3", "sdl2", "kivy", ] def setUp_distribution_with_bootstrap(self, bs, **kwargs): """Extend the setUp by configuring a distribution, because some test needs a distribution to be set to be properly tested""" self.ctx.bootstrap = bs self.ctx.bootstrap.distribution = Distribution.get_distribution( self.ctx, name=kwargs.pop("name", "test_prj"), recipes=kwargs.pop("recipes", ["python3", "kivy"]), arch_name=self.TEST_ARCH, **kwargs) def tearDown(self): """Here we make sure that we reset a possible bootstrap created in `setUp_distribution_with_bootstrap`""" self.ctx.bootstrap = None def test_properties(self): """Test that some attributes has the expected result (for now, we check that `__repr__` and `__str__` return the proper values""" self.setUp_distribution_with_bootstrap(Bootstrap().get_bootstrap( "sdl2", self.ctx)) distribution = self.ctx.bootstrap.distribution self.assertEqual(self.ctx, distribution.ctx) expected_repr = ( "<Distribution: name test_prj with recipes (python3, kivy)>") self.assertEqual(distribution.__str__(), expected_repr) self.assertEqual(distribution.__repr__(), expected_repr) @mock.patch("pythonforandroid.distribution.exists") def test_folder_exist(self, mock_exists): """Test that method :meth:`~pythonforandroid.distribution.Distribution.folder_exist` is called once with the proper arguments.""" mock_exists.return_value = False self.setUp_distribution_with_bootstrap( Bootstrap.get_bootstrap("sdl2", self.ctx)) self.ctx.bootstrap.distribution.folder_exists() mock_exists.assert_called_with( self.ctx.bootstrap.distribution.dist_dir) @mock.patch("pythonforandroid.distribution.rmtree") def test_delete(self, mock_rmtree): """Test that method :meth:`~pythonforandroid.distribution.Distribution.delete` is called once with the proper arguments.""" self.setUp_distribution_with_bootstrap(Bootstrap().get_bootstrap( "sdl2", self.ctx)) self.ctx.bootstrap.distribution.delete() mock_rmtree.assert_called_once_with( self.ctx.bootstrap.distribution.dist_dir) @mock.patch("pythonforandroid.distribution.exists") def test_get_distribution_no_name(self, mock_exists): """Test that method :meth:`~pythonforandroid.distribution.Distribution.get_distribution` returns the proper result which should `unnamed_dist_1`.""" mock_exists.return_value = False self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx) dist = Distribution.get_distribution(self.ctx, arch_name=self.TEST_ARCH) self.assertEqual(dist.name, "unnamed_dist_1") @mock.patch("pythonforandroid.util.chdir") @mock.patch("pythonforandroid.distribution.open", create=True) def test_save_info(self, mock_open_dist_info, mock_chdir): """Test that method :meth:`~pythonforandroid.distribution.Distribution.save_info` is called once with the proper arguments.""" self.setUp_distribution_with_bootstrap(Bootstrap().get_bootstrap( "sdl2", self.ctx)) self.ctx.hostpython = "/some/fake/hostpython3" self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx) self.ctx.python_modules = ["requests"] mock_open_dist_info.side_effect = [ mock.mock_open(read_data=json.dumps(dist_info_data)).return_value ] self.ctx.bootstrap.distribution.save_info("/fake_dir") mock_open_dist_info.assert_called_once_with("dist_info.json", "w") mock_open_dist_info.reset_mock() @mock.patch("pythonforandroid.distribution.open", create=True) @mock.patch("pythonforandroid.distribution.exists") @mock.patch("pythonforandroid.distribution.glob.glob") def test_get_distributions(self, mock_glob, mock_exists, mock_open_dist_info): """Test that method :meth:`~pythonforandroid.distribution.Distribution.get_distributions` returns some expected values: - A list of instances of class `~pythonforandroid.distribution.Distribution - That one of the distributions returned in the result has the proper values (`name`, `ndk_api` and `recipes`) """ self.setUp_distribution_with_bootstrap(Bootstrap().get_bootstrap( "sdl2", self.ctx)) mock_glob.return_value = ["sdl2-python3"] mock_open_dist_info.side_effect = [ mock.mock_open(read_data=json.dumps(dist_info_data)).return_value ] dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx) self.assertIsInstance(dists, list) self.assertEqual(len(dists), 1) self.assertIsInstance(dists[0], Distribution) self.assertEqual(dists[0].name, "sdl2_dist") self.assertEqual(dists[0].dist_dir, "sdl2-python3") self.assertEqual(dists[0].ndk_api, 21) self.assertEqual( dists[0].recipes, ["hostpython3", "python3", "sdl2", "kivy", "requests"], ) mock_open_dist_info.assert_called_with("sdl2-python3/dist_info.json") mock_open_dist_info.reset_mock() @mock.patch("pythonforandroid.distribution.open", create=True) @mock.patch("pythonforandroid.distribution.exists") @mock.patch("pythonforandroid.distribution.glob.glob") def test_get_distributions_error_ndk_api(self, mock_glob, mock_exists, mock_open_dist_info): """Test method :meth:`~pythonforandroid.distribution.Distribution.get_distributions` in case that `ndk_api` is not set..which should return a `None`. """ dist_info_data_no_ndk_api = dist_info_data.copy() dist_info_data_no_ndk_api.pop("ndk_api") self.setUp_distribution_with_bootstrap(Bootstrap().get_bootstrap( "sdl2", self.ctx)) mock_glob.return_value = ["sdl2-python3"] mock_open_dist_info.side_effect = [ mock.mock_open( read_data=json.dumps(dist_info_data_no_ndk_api)).return_value ] dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx) self.assertEqual(dists[0].ndk_api, None) mock_open_dist_info.assert_called_with("sdl2-python3/dist_info.json") mock_open_dist_info.reset_mock() @mock.patch("pythonforandroid.distribution.Distribution.get_distributions") @mock.patch("pythonforandroid.distribution.exists") @mock.patch("pythonforandroid.distribution.glob.glob") def test_get_distributions_error_ndk_api_mismatch(self, mock_glob, mock_exists, mock_get_dists): """Test that method :meth:`~pythonforandroid.distribution.Distribution.get_distribution` raises an error in case that we have some distribution already build, with a given `name` and `ndk_api`, and we try to get another distribution with the same `name` but different `ndk_api`. """ expected_dist = Distribution.get_distribution( self.ctx, name="test_prj", recipes=["python3", "kivy"], arch_name=self.TEST_ARCH, ) mock_get_dists.return_value = [expected_dist] mock_glob.return_value = ["sdl2-python3"] with self.assertRaises(BuildInterruptingException) as e: self.setUp_distribution_with_bootstrap( Bootstrap().get_bootstrap("sdl2", self.ctx), allow_replace_dist=False, ndk_api=22, ) self.assertEqual( e.exception.args[0], "Asked for dist with name test_prj with recipes (python3, kivy)" " and NDK API 22, but a dist with this name already exists and has" " either incompatible recipes (python3, kivy) or NDK API 21", ) def test_get_distributions_error_extra_dist_dirs(self): """Test that method :meth:`~pythonforandroid.distribution.Distribution.get_distributions` raises an exception of :class:`~pythonforandroid.util.BuildInterruptingException` in case that we supply the kwargs `extra_dist_dirs`. """ self.setUp_distribution_with_bootstrap(Bootstrap().get_bootstrap( "sdl2", self.ctx)) with self.assertRaises(BuildInterruptingException) as e: self.ctx.bootstrap.distribution.get_distributions( self.ctx, extra_dist_dirs=["/fake/extra/dist_dirs"]) self.assertEqual( e.exception.args[0], "extra_dist_dirs argument to get" "_distributions is not yet implemented", ) @mock.patch("pythonforandroid.distribution.Distribution.get_distributions") def test_get_distributions_possible_dists(self, mock_get_dists): """Test that method :meth:`~pythonforandroid.distribution.Distribution.get_distributions` returns the proper `:class:`~pythonforandroid.distribution.Distribution` in case that we already have it build and we request the same `:class:`~pythonforandroid.distribution.Distribution`. """ expected_dist = Distribution.get_distribution( self.ctx, name="test_prj", recipes=["python3", "kivy"], arch_name=self.TEST_ARCH, ) mock_get_dists.return_value = [expected_dist] self.setUp_distribution_with_bootstrap(Bootstrap().get_bootstrap( "sdl2", self.ctx), name="test_prj") dists = self.ctx.bootstrap.distribution.get_distributions(self.ctx) self.assertEqual(dists[0], expected_dist)
class ToolchainCL(object): def __init__(self): argv = sys.argv # Buildozer used to pass these arguments in a now-invalid order # If that happens, apply this fix # This fix will be removed once a fixed buildozer is released if (len(argv) > 2 and argv[1].startswith('--color') and argv[2].startswith('--storage-dir')): argv.append(argv.pop(1)) # the --color arg argv.append(argv.pop(1)) # the --storage-dir arg parser = NoAbbrevParser( description=('A packaging tool for turning Python scripts and apps ' 'into Android APKs')) generic_parser = argparse.ArgumentParser( add_help=False, description=('Generic arguments applied to all commands')) dist_parser = argparse.ArgumentParser( add_help=False, description=('Arguments for dist building')) generic_parser.add_argument( '--debug', dest='debug', action='store_true', default=False, help='Display debug output and all build info') generic_parser.add_argument( '--color', dest='color', choices=['always', 'never', 'auto'], help='Enable or disable color output (default enabled on tty)') generic_parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') generic_parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') generic_parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help='The Android API level to build against.') generic_parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default='', help=('The version of the Android NDK. This is optional, ' 'we try to work it out automatically from the ndk_dir.')) generic_parser.add_argument( '--symlink-java-src', '--symlink_java_src', action='store_true', dest='symlink_java_src', default=False, help=('If True, symlinks the java src folder during build and dist ' 'creation. This is useful for development only, it could also ' 'cause weird problems.')) default_storage_dir = user_data_dir('python-for-android') if ' ' in default_storage_dir: default_storage_dir = '~/.python-for-android' generic_parser.add_argument( '--storage-dir', dest='storage_dir', default=default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(default_storage_dir))) generic_parser.add_argument( '--arch', help='The archs to build for, separated by commas.', default='armeabi') # Options for specifying the Distribution generic_parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') generic_parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules'), default='') generic_parser.add_argument( '--bootstrap', help='The bootstrap to build with. Leave unset to choose automatically.', default=None) generic_parser.add_argument( '--hook', help='Filename to a module that contain python-for-android hooks', default=None) add_boolean_option( generic_parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution:') add_boolean_option( generic_parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) generic_parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') generic_parser.add_argument( '--java-build-tool', dest='java_build_tool', default='auto', choices=['auto', 'ant', 'gradle'], help=('The java build tool to use when packaging the APK, defaults ' 'to automatically selecting an appropriate tool.')) add_boolean_option( generic_parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)') self._read_configuration() subparsers = parser.add_subparsers(dest='subparser_name', help='The command to run') def add_parser(subparsers, *args, **kwargs): ''' argparse in python2 doesn't support the aliases option, so we just don't provide the aliases there. ''' if 'aliases' in kwargs and sys.version_info.major < 3: kwargs.pop('aliases') return subparsers.add_parser(*args, **kwargs) parser_recipes = add_parser(subparsers, 'recipes', parents=[generic_parser], help='List the available recipes') parser_recipes.add_argument( "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") parser_bootstraps = add_parser( subparsers, 'bootstraps', help='List the available bootstraps', parents=[generic_parser]) parser_clean_all = add_parser( subparsers, 'clean_all', aliases=['clean-all'], help='Delete all builds, dists and caches', parents=[generic_parser]) parser_clean_dists = add_parser( subparsers, 'clean_dists', aliases=['clean-dists'], help='Delete all dists', parents=[generic_parser]) parser_clean_bootstrap_builds = add_parser( subparsers, 'clean_bootstrap_builds', aliases=['clean-bootstrap-builds'], help='Delete all bootstrap builds', parents=[generic_parser]) parser_clean_builds = add_parser( subparsers, 'clean_builds', aliases=['clean-builds'], help='Delete all builds', parents=[generic_parser]) parser_clean = add_parser(subparsers, 'clean', help='Delete build components.', parents=[generic_parser]) parser_clean.add_argument( 'component', nargs='+', help=('The build component(s) to delete. You can pass any ' 'number of arguments from "all", "builds", "dists", ' '"distributions", "bootstrap_builds", "downloads".')) parser_clean_recipe_build = add_parser(subparsers, 'clean_recipe_build', aliases=['clean-recipe-build'], help=('Delete the build components of the given recipe. ' 'By default this will also delete built dists'), parents=[generic_parser]) parser_clean_recipe_build.add_argument('recipe', help='The recipe name') parser_clean_recipe_build.add_argument('--no-clean-dists', default=False, dest='no_clean_dists', action='store_true', help='If passed, do not delete existing dists') parser_clean_download_cache= add_parser(subparsers, 'clean_download_cache', aliases=['clean-download-cache'], help='Delete cached downloads for requirement builds', parents=[generic_parser]) parser_clean_download_cache.add_argument( 'recipes', nargs='*', help=('The recipes to clean (space-separated). If no recipe name is ' 'provided, the entire cache is cleared.')) parser_export_dist = add_parser(subparsers, 'export_dist', aliases=['export-dist'], help='Copy the named dist to the given path', parents=[generic_parser]) parser_export_dist.add_argument('output_dir', help=('The output dir to copy to')) parser_export_dist.add_argument('--symlink', action='store_true', help=('Symlink the dist instead of copying')) parser_apk = add_parser(subparsers, 'apk', help='Build an APK', parents=[generic_parser]) parser_apk.add_argument('--release', dest='build_mode', action='store_const', const='release', default='debug', help='Build the PARSER_APK. in Release mode') parser_apk.add_argument('--keystore', dest='keystore', action='store', default=None, help=('Keystore for JAR signing key, will use jarsigner ' 'default if not specified (release build only)')) parser_apk.add_argument('--signkey', dest='signkey', action='store', default=None, help='Key alias to sign PARSER_APK. with (release build only)') parser_apk.add_argument('--keystorepw', dest='keystorepw', action='store', default=None, help='Password for keystore') parser_apk.add_argument('--signkeypw', dest='signkeypw', action='store', default=None, help='Password for key alias') parser_create = add_parser(subparsers, 'create', help='Compile a set of requirements into a dist', parents=[generic_parser]) parser_archs = add_parser(subparsers, 'archs', help='List the available target architectures', parents=[generic_parser]) parser_distributions = add_parser(subparsers, 'distributions', aliases=['dists'], help='List the currently available (compiled) dists', parents=[generic_parser]) parser_delete_dist = add_parser(subparsers, 'delete_dist', aliases=['delete-dist'], help='Delete a compiled dist', parents=[generic_parser]) parser_sdk_tools = add_parser(subparsers, 'sdk_tools', aliases=['sdk-tools'], help='Run the given binary from the SDK tools dis', parents=[generic_parser]) parser_sdk_tools.add_argument( 'tool', help=('The tool binary name to run')) parser_adb = add_parser(subparsers, 'adb', help='Run adb from the given SDK', parents=[generic_parser]) parser_logcat = add_parser(subparsers, 'logcat', help='Run logcat from the given SDK', parents=[generic_parser]) parser_build_status = add_parser(subparsers, 'build_status', aliases=['build-status'], help='Print some debug information about current built components', parents=[generic_parser]) parser.add_argument('-v', '--version', action='version', version=__version__) args, unknown = parser.parse_known_args(sys.argv[1:]) args.unknown_args = unknown self.args = args if args.subparser_name is None: parser.print_help() exit(1) setup_color(args.color) if args.debug: logger.setLevel(logging.DEBUG) # strip version from requirements, and put them in environ if hasattr(args, 'requirements'): requirements = [] for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) self.ctx = Context() self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_version = args.ndk_version self.ctx.symlink_java_src = args.symlink_java_src self.ctx.java_build_tool = args.java_build_tool self._archs = split_argument_list(args.arch) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs # Each subparser corresponds to a method getattr(self, args.subparser_name.replace('-', '_'))(args) def hook(self, name): if not self.args.hook: return if not hasattr(self, "hook_module"): # first time, try to load the hook module self.hook_module = imp.load_source("pythonforandroid.hook", self.args.hook) if hasattr(self.hook_module, name): info("Hook: execute {}".format(name)) getattr(self.hook_module, name)(self) else: info("Hook: ignore {}".format(name)) @property def default_storage_dir(self): udd = user_data_dir('python-for-android') if ' ' in udd: udd = '~/.python-for-android' return udd def _read_configuration(self): # search for a .p4a configuration file in the current directory if not exists(".p4a"): return info("Reading .p4a configuration") with open(".p4a") as fd: lines = fd.readlines() lines = [shlex.split(line) for line in lines if not line.startswith("#")] for line in lines: for arg in line: sys.argv.append(arg) def recipes(self, args): ctx = self.ctx if args.compact: print(" ".join(set(Recipe.list_recipes(ctx)))) else: for name in sorted(Recipe.list_recipes(ctx)): try: recipe = Recipe.get_recipe(name, ctx) except IOError: warning('Recipe "{}" could not be loaded'.format(name)) except SyntaxError: import traceback traceback.print_exc() warning(('Recipe "{}" could not be loaded due to a ' 'syntax error').format(name)) version = str(recipe.version) print('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} ' '{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}' '{version:<8}{Style.RESET_ALL}'.format( recipe=recipe, Fore=Out_Fore, Style=Out_Style, version=version)) print(' {Fore.GREEN}depends: {recipe.depends}' '{Fore.RESET}'.format(recipe=recipe, Fore=Out_Fore)) if recipe.conflicts: print(' {Fore.RED}conflicts: {recipe.conflicts}' '{Fore.RESET}' .format(recipe=recipe, Fore=Out_Fore)) if recipe.opt_depends: print(' {Fore.YELLOW}optional depends: ' '{recipe.opt_depends}{Fore.RESET}' .format(recipe=recipe, Fore=Out_Fore)) def bootstraps(self, args): '''List all the bootstraps available to build with.''' for bs in Bootstrap.list_bootstraps(): bs = Bootstrap.get_bootstrap(bs, self.ctx) print('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}' .format(bs=bs, Fore=Out_Fore, Style=Out_Style)) print(' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format(bs=bs, Fore=Out_Fore)) def clean(self, args): components = args.component component_clean_methods = {'all': self.clean_all, 'dists': self.clean_dists, 'distributions': self.clean_dists, 'builds': self.clean_builds, 'bootstrap_builds': self.clean_bootstrap_builds, 'downloads': self.clean_download_cache} for component in components: if component not in component_clean_methods: raise ValueError(( 'Asked to clean "{}" but this argument is not ' 'recognised'.format(component))) component_clean_methods[component](args) def clean_all(self, args): '''Delete all build components; the package cache, package builds, bootstrap builds and distributions.''' self.clean_dists(args) self.clean_builds(args) self.clean_download_cache(args) def clean_dists(self, args): '''Delete all compiled distributions in the internal distribution directory.''' ctx = self.ctx if exists(ctx.dist_dir): shutil.rmtree(ctx.dist_dir) def clean_bootstrap_builds(self, args): '''Delete all the bootstrap builds.''' if exists(join(self.ctx.build_dir, 'bootstrap_builds')): shutil.rmtree(join(self.ctx.build_dir, 'bootstrap_builds')) # for bs in Bootstrap.list_bootstraps(): # bs = Bootstrap.get_bootstrap(bs, self.ctx) # if bs.build_dir and exists(bs.build_dir): # info('Cleaning build for {} bootstrap.'.format(bs.name)) # shutil.rmtree(bs.build_dir) def clean_builds(self, args): '''Delete all build caches for each recipe, python-install, java code and compiled libs collection. This does *not* delete the package download cache or the final distributions. You can also use clean_recipe_build to delete the build of a specific recipe. ''' ctx = self.ctx # if exists(ctx.dist_dir): # shutil.rmtree(ctx.dist_dir) if exists(ctx.build_dir): shutil.rmtree(ctx.build_dir) if exists(ctx.python_installs_dir): shutil.rmtree(ctx.python_installs_dir) libs_dir = join(self.ctx.build_dir, 'libs_collections') if exists(libs_dir): shutil.rmtree(libs_dir) def clean_recipe_build(self, args): '''Deletes the build files of the given recipe. This is intended for debug purposes, you may experience strange behaviour or problems with some recipes (if their build has done unexpected state changes). If this happens, run clean_builds, or attempt to clean other recipes until things work again. ''' recipe = Recipe.get_recipe(args.recipe, self.ctx) info('Cleaning build for {} recipe.'.format(recipe.name)) recipe.clean_build() if not args.no_clean_dists: self.clean_dists(args) def clean_download_cache(self, args): ''' Deletes a download cache for recipes stated as arguments. If no argument is passed, it'll delete *all* downloaded cache. :: p4a clean_download_cache kivy,pyjnius This does *not* delete the build caches or final distributions. ''' ctx = self.ctx if hasattr(args, 'recipes') and args.recipes: for package in args.recipes: remove_path = join(ctx.packages_path, package) if exists(remove_path): shutil.rmtree(remove_path) info('Download cache removed for: "{}"'.format(package)) else: warning('No download cache found for "{}", skipping'.format(package)) else: if exists(ctx.packages_path): shutil.rmtree(ctx.packages_path) info('Download cache removed.') else: print('No cache found at "{}"'.format(ctx.packages_path)) @require_prebuilt_dist def export_dist(self, args): '''Copies a created dist to an output dir. This makes it easy to navigate to the dist to investigate it or call build.py, though you do not in general need to do this and can use the apk command instead. ''' ctx = self.ctx dist = dist_from_args(ctx, args) if dist.needs_build: info('You asked to export a dist, but there is no dist ' 'with suitable recipes available. For now, you must ' ' create one first with the create argument.') exit(1) if args.symlink: shprint(sh.ln, '-s', dist.dist_dir, args.output_dir) else: shprint(sh.cp, '-r', dist.dist_dir, args.output_dir) @property def _dist(self): ctx = self.ctx dist = dist_from_args(ctx, self.args) return dist @require_prebuilt_dist def apk(self, args): '''Create an APK using the given distribution.''' ctx = self.ctx dist = self._dist # Manually fixing these arguments at the string stage is # unsatisfactory and should probably be changed somehow, but # we can't leave it until later as the build.py scripts assume # they are in the current directory. fix_args = ('--dir', '--private', '--add-jar', '--add-source', '--whitelist', '--blacklist', '--presplash', '--icon') unknown_args = args.unknown_args for i, arg in enumerate(unknown_args[:-1]): argx = arg.split('=') if argx[0] in fix_args: if len(argx) > 1: unknown_args[i] = '='.join((argx[0], realpath(expanduser(argx[1])))) else: unknown_args[i+1] = realpath(expanduser(unknown_args[i+1])) env = os.environ.copy() if args.build_mode == 'release': if args.keystore: env['P4A_RELEASE_KEYSTORE'] = realpath(expanduser(args.keystore)) if args.signkey: env['P4A_RELEASE_KEYALIAS'] = args.signkey if args.keystorepw: env['P4A_RELEASE_KEYSTORE_PASSWD'] = args.keystorepw if args.signkeypw: env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.signkeypw elif args.keystorepw and 'P4A_RELEASE_KEYALIAS_PASSWD' not in env: env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.keystorepw build = imp.load_source('build', join(dist.dist_dir, 'build.py')) with current_directory(dist.dist_dir): self.hook("before_apk_build") os.environ["ANDROID_API"] = str(self.ctx.android_api) build_args = build.parse_args(args.unknown_args) self.hook("after_apk_build") self.hook("before_apk_assemble") build_type = ctx.java_build_tool if build_type == 'auto': info('Selecting java build tool:') build_tools_versions = os.listdir(join(ctx.sdk_dir, 'build-tools')) build_tools_versions = sorted(build_tools_versions, key=LooseVersion) build_tools_version = build_tools_versions[-1] info(('Detected highest available build tools ' 'version to be {}').format(build_tools_version)) if build_tools_version >= '25.0' and exists('gradlew'): build_type = 'gradle' info(' Building with gradle, as gradle executable is present') else: build_type = 'ant' if build_tools_version < '25.0': info((' Building with ant, as the highest ' 'build-tools-version is only {}').format(build_tools_version)) else: info(' Building with ant, as no gradle executable detected') if build_type == 'gradle': # gradle-based build env["ANDROID_NDK_HOME"] = self.ctx.ndk_dir env["ANDROID_HOME"] = self.ctx.sdk_dir gradlew = sh.Command('./gradlew') if args.build_mode == "debug": gradle_task = "assembleDebug" elif args.build_mode == "release": gradle_task = "assembleRelease" else: error("Unknown build mode {} for apk()".format( args.build_mode)) exit(1) output = shprint(gradlew, gradle_task, _tail=20, _critical=True, _env=env) # gradle output apks somewhere else # and don't have version in file apk_dir = join(dist.dist_dir, "build", "outputs", "apk") apk_glob = "*-{}.apk" apk_add_version = True else: # ant-based build try: ant = sh.Command('ant') except sh.CommandNotFound: error('Could not find ant binary, please install it ' 'and make sure it is in your $PATH.') exit(1) output = shprint(ant, args.build_mode, _tail=20, _critical=True, _env=env) apk_dir = join(dist.dist_dir, "bin") apk_glob = "*-*-{}.apk" apk_add_version = False self.hook("after_apk_assemble") info_main('# Copying APK to current directory') apk_re = re.compile(r'.*Package: (.*\.apk)$') apk_file = None for line in reversed(output.splitlines()): m = apk_re.match(line) if m: apk_file = m.groups()[0] break if not apk_file: info_main('# APK filename not found in build output, trying to guess') if args.build_mode == "release": suffixes = ("release", "release-unsigned") else: suffixes = ("debug", ) for suffix in suffixes: apks = glob.glob(join(apk_dir, apk_glob.format(suffix))) if apks: if len(apks) > 1: info('More than one built APK found... guessing you ' 'just built {}'.format(apks[-1])) apk_file = apks[-1] break else: raise ValueError('Couldn\'t find the built APK') info_main('# Found APK file: {}'.format(apk_file)) if apk_add_version: info('# Add version number to APK') apk_name, apk_suffix = basename(apk_file).split("-", 1) apk_file_dest = "{}-{}-{}".format( apk_name, build_args.version, apk_suffix) info('# APK renamed to {}'.format(apk_file_dest)) shprint(sh.cp, apk_file, apk_file_dest) else: shprint(sh.cp, apk_file, './') @require_prebuilt_dist def create(self, args): '''Create a distribution directory if it doesn't already exist, run any recipes if necessary, and build the apk. ''' pass # The decorator does everything def archs(self, args): '''List the target architectures available to be built for.''' print('{Style.BRIGHT}Available target architectures are:' '{Style.RESET_ALL}'.format(Style=Out_Style)) for arch in self.ctx.archs: print(' {}'.format(arch.arch)) def dists(self, args): '''The same as :meth:`distributions`.''' self.distributions(args) def distributions(self, args): '''Lists all distributions currently available (i.e. that have already been built).''' ctx = self.ctx dists = Distribution.get_distributions(ctx) if dists: print('{Style.BRIGHT}Distributions currently installed are:' '{Style.RESET_ALL}'.format(Style=Out_Style, Fore=Out_Fore)) pretty_log_dists(dists, print) else: print('{Style.BRIGHT}There are no dists currently built.' '{Style.RESET_ALL}'.format(Style=Out_Style)) def delete_dist(self, args): dist = self._dist if dist.needs_build: info('No dist exists that matches your specifications, ' 'exiting without deleting.') shutil.rmtree(dist.dist_dir) def sdk_tools(self, args): '''Runs the android binary from the detected SDK directory, passing all arguments straight to it. This binary is used to install e.g. platform-tools for different API level targets. This is intended as a convenience function if android is not in your $PATH. ''' ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_ver=self.ndk_version) android = sh.Command(join(ctx.sdk_dir, 'tools', args.tool)) output = android( *args.unknown_args, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def adb(self, args): '''Runs the adb binary from the detected SDK directory, passing all arguments straight to it. This is intended as a convenience function if adb is not in your $PATH. ''' self._adb(args.unknown_args) def logcat(self, args): '''Runs ``adb logcat`` using the adb binary from the detected SDK directory. All extra args are passed as arguments to logcat.''' self._adb(['logcat'] + args.unknown_args) def _adb(self, commands): '''Call the adb executable from the SDK, passing the given commands as arguments.''' ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_ver=self.ndk_version) if platform in ('win32', 'cygwin'): adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb.exe')) else: adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb')) info_notify('Starting adb...') output = adb(*commands, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def build_status(self, args): print('{Style.BRIGHT}Bootstraps whose core components are probably ' 'already built:{Style.RESET_ALL}'.format(Style=Out_Style)) bootstrap_dir = join(self.ctx.build_dir, 'bootstrap_builds') if exists(bootstrap_dir): for filen in os.listdir(bootstrap_dir): print(' {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}' .format(filen=filen, Fore=Out_Fore, Style=Out_Style)) print('{Style.BRIGHT}Recipes that are probably already built:' '{Style.RESET_ALL}'.format(Style=Out_Style)) other_builds_dir = join(self.ctx.build_dir, 'other_builds') if exists(other_builds_dir): for filen in sorted(os.listdir(other_builds_dir)): name = filen.split('-')[0] dependencies = filen.split('-')[1:] recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}' '{Style.RESET_ALL}'.format( Style=Out_Style, name=name, Fore=Out_Fore)) if dependencies: recipe_str += ( ' ({Fore.BLUE}with ' + ', '.join(dependencies) + '{Fore.RESET})').format(Fore=Out_Fore) recipe_str += '{Style.RESET_ALL}'.format(Style=Out_Style) print(recipe_str)
def __init__(self): argv = sys.argv # Buildozer used to pass these arguments in a now-invalid order # If that happens, apply this fix # This fix will be removed once a fixed buildozer is released if (len(argv) > 2 and argv[1].startswith('--color') and argv[2].startswith('--storage-dir')): argv.append(argv.pop(1)) # the --color arg argv.append(argv.pop(1)) # the --storage-dir arg parser = NoAbbrevParser( description=('A packaging tool for turning Python scripts and apps ' 'into Android APKs')) generic_parser = argparse.ArgumentParser( add_help=False, description=('Generic arguments applied to all commands')) dist_parser = argparse.ArgumentParser( add_help=False, description=('Arguments for dist building')) generic_parser.add_argument( '--debug', dest='debug', action='store_true', default=False, help='Display debug output and all build info') generic_parser.add_argument( '--color', dest='color', choices=['always', 'never', 'auto'], help='Enable or disable color output (default enabled on tty)') generic_parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') generic_parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') generic_parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help='The Android API level to build against.') generic_parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default='', help=('The version of the Android NDK. This is optional, ' 'we try to work it out automatically from the ndk_dir.')) generic_parser.add_argument( '--symlink-java-src', '--symlink_java_src', action='store_true', dest='symlink_java_src', default=False, help=('If True, symlinks the java src folder during build and dist ' 'creation. This is useful for development only, it could also ' 'cause weird problems.')) default_storage_dir = user_data_dir('python-for-android') if ' ' in default_storage_dir: default_storage_dir = '~/.python-for-android' generic_parser.add_argument( '--storage-dir', dest='storage_dir', default=default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(default_storage_dir))) generic_parser.add_argument( '--arch', help='The archs to build for, separated by commas.', default='armeabi') # Options for specifying the Distribution generic_parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') generic_parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules'), default='') generic_parser.add_argument( '--bootstrap', help='The bootstrap to build with. Leave unset to choose automatically.', default=None) generic_parser.add_argument( '--hook', help='Filename to a module that contain python-for-android hooks', default=None) add_boolean_option( generic_parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution:') add_boolean_option( generic_parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) generic_parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') generic_parser.add_argument( '--java-build-tool', dest='java_build_tool', default='auto', choices=['auto', 'ant', 'gradle'], help=('The java build tool to use when packaging the APK, defaults ' 'to automatically selecting an appropriate tool.')) add_boolean_option( generic_parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)') self._read_configuration() subparsers = parser.add_subparsers(dest='subparser_name', help='The command to run') def add_parser(subparsers, *args, **kwargs): ''' argparse in python2 doesn't support the aliases option, so we just don't provide the aliases there. ''' if 'aliases' in kwargs and sys.version_info.major < 3: kwargs.pop('aliases') return subparsers.add_parser(*args, **kwargs) parser_recipes = add_parser(subparsers, 'recipes', parents=[generic_parser], help='List the available recipes') parser_recipes.add_argument( "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") parser_bootstraps = add_parser( subparsers, 'bootstraps', help='List the available bootstraps', parents=[generic_parser]) parser_clean_all = add_parser( subparsers, 'clean_all', aliases=['clean-all'], help='Delete all builds, dists and caches', parents=[generic_parser]) parser_clean_dists = add_parser( subparsers, 'clean_dists', aliases=['clean-dists'], help='Delete all dists', parents=[generic_parser]) parser_clean_bootstrap_builds = add_parser( subparsers, 'clean_bootstrap_builds', aliases=['clean-bootstrap-builds'], help='Delete all bootstrap builds', parents=[generic_parser]) parser_clean_builds = add_parser( subparsers, 'clean_builds', aliases=['clean-builds'], help='Delete all builds', parents=[generic_parser]) parser_clean = add_parser(subparsers, 'clean', help='Delete build components.', parents=[generic_parser]) parser_clean.add_argument( 'component', nargs='+', help=('The build component(s) to delete. You can pass any ' 'number of arguments from "all", "builds", "dists", ' '"distributions", "bootstrap_builds", "downloads".')) parser_clean_recipe_build = add_parser(subparsers, 'clean_recipe_build', aliases=['clean-recipe-build'], help=('Delete the build components of the given recipe. ' 'By default this will also delete built dists'), parents=[generic_parser]) parser_clean_recipe_build.add_argument('recipe', help='The recipe name') parser_clean_recipe_build.add_argument('--no-clean-dists', default=False, dest='no_clean_dists', action='store_true', help='If passed, do not delete existing dists') parser_clean_download_cache= add_parser(subparsers, 'clean_download_cache', aliases=['clean-download-cache'], help='Delete cached downloads for requirement builds', parents=[generic_parser]) parser_clean_download_cache.add_argument( 'recipes', nargs='*', help=('The recipes to clean (space-separated). If no recipe name is ' 'provided, the entire cache is cleared.')) parser_export_dist = add_parser(subparsers, 'export_dist', aliases=['export-dist'], help='Copy the named dist to the given path', parents=[generic_parser]) parser_export_dist.add_argument('output_dir', help=('The output dir to copy to')) parser_export_dist.add_argument('--symlink', action='store_true', help=('Symlink the dist instead of copying')) parser_apk = add_parser(subparsers, 'apk', help='Build an APK', parents=[generic_parser]) parser_apk.add_argument('--release', dest='build_mode', action='store_const', const='release', default='debug', help='Build the PARSER_APK. in Release mode') parser_apk.add_argument('--keystore', dest='keystore', action='store', default=None, help=('Keystore for JAR signing key, will use jarsigner ' 'default if not specified (release build only)')) parser_apk.add_argument('--signkey', dest='signkey', action='store', default=None, help='Key alias to sign PARSER_APK. with (release build only)') parser_apk.add_argument('--keystorepw', dest='keystorepw', action='store', default=None, help='Password for keystore') parser_apk.add_argument('--signkeypw', dest='signkeypw', action='store', default=None, help='Password for key alias') parser_create = add_parser(subparsers, 'create', help='Compile a set of requirements into a dist', parents=[generic_parser]) parser_archs = add_parser(subparsers, 'archs', help='List the available target architectures', parents=[generic_parser]) parser_distributions = add_parser(subparsers, 'distributions', aliases=['dists'], help='List the currently available (compiled) dists', parents=[generic_parser]) parser_delete_dist = add_parser(subparsers, 'delete_dist', aliases=['delete-dist'], help='Delete a compiled dist', parents=[generic_parser]) parser_sdk_tools = add_parser(subparsers, 'sdk_tools', aliases=['sdk-tools'], help='Run the given binary from the SDK tools dis', parents=[generic_parser]) parser_sdk_tools.add_argument( 'tool', help=('The tool binary name to run')) parser_adb = add_parser(subparsers, 'adb', help='Run adb from the given SDK', parents=[generic_parser]) parser_logcat = add_parser(subparsers, 'logcat', help='Run logcat from the given SDK', parents=[generic_parser]) parser_build_status = add_parser(subparsers, 'build_status', aliases=['build-status'], help='Print some debug information about current built components', parents=[generic_parser]) parser.add_argument('-v', '--version', action='version', version=__version__) args, unknown = parser.parse_known_args(sys.argv[1:]) args.unknown_args = unknown self.args = args if args.subparser_name is None: parser.print_help() exit(1) setup_color(args.color) if args.debug: logger.setLevel(logging.DEBUG) # strip version from requirements, and put them in environ if hasattr(args, 'requirements'): requirements = [] for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) self.ctx = Context() self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_version = args.ndk_version self.ctx.symlink_java_src = args.symlink_java_src self.ctx.java_build_tool = args.java_build_tool self._archs = split_argument_list(args.arch) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs # Each subparser corresponds to a method getattr(self, args.subparser_name.replace('-', '_'))(args)
class ToolchainCL(object): def __init__(self): argv = sys.argv self.warn_on_carriage_return_args(argv) # Buildozer used to pass these arguments in a now-invalid order # If that happens, apply this fix # This fix will be removed once a fixed buildozer is released if (len(argv) > 2 and argv[1].startswith('--color') and argv[2].startswith('--storage-dir')): argv.append(argv.pop(1)) # the --color arg argv.append(argv.pop(1)) # the --storage-dir arg parser = NoAbbrevParser( description='A packaging tool for turning Python scripts and apps ' 'into Android APKs') generic_parser = argparse.ArgumentParser( add_help=False, description='Generic arguments applied to all commands') argparse.ArgumentParser( add_help=False, description='Arguments for dist building') generic_parser.add_argument( '--debug', dest='debug', action='store_true', default=False, help='Display debug output and all build info') generic_parser.add_argument( '--color', dest='color', choices=['always', 'never', 'auto'], help='Enable or disable color output (default enabled on tty)') generic_parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') generic_parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') generic_parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help=('The Android API level to build against defaults to {} if ' 'not specified.').format(RECOMMENDED_TARGET_API)) generic_parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default=None, help=('DEPRECATED: the NDK version is now found automatically or ' 'not at all.')) generic_parser.add_argument( '--ndk-api', type=int, default=None, help=('The Android API level to compile against. This should be your ' '*minimal supported* API, not normally the same as your --android-api. ' 'Defaults to min(ANDROID_API, {}) if not specified.').format(RECOMMENDED_NDK_API)) generic_parser.add_argument( '--symlink-java-src', '--symlink_java_src', action='store_true', dest='symlink_java_src', default=False, help=('If True, symlinks the java src folder during build and dist ' 'creation. This is useful for development only, it could also' ' cause weird problems.')) default_storage_dir = user_data_dir('python-for-android') if ' ' in default_storage_dir: default_storage_dir = '~/.python-for-android' generic_parser.add_argument( '--storage-dir', dest='storage_dir', default=default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(default_storage_dir))) generic_parser.add_argument( '--arch', help='The arch to build for.', default='armeabi-v7a') # Options for specifying the Distribution generic_parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') generic_parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules. NOT NECESSARY if you are using ' 'Python 3 with --use-setup-py'), default='') generic_parser.add_argument( '--recipe-blacklist', help=('Blacklist an internal recipe from use. Allows ' 'disabling Python 3 core modules to save size'), dest="recipe_blacklist", default='') generic_parser.add_argument( '--blacklist-requirements', help=('Blacklist an internal recipe from use. Allows ' 'disabling Python 3 core modules to save size'), dest="blacklist_requirements", default='') generic_parser.add_argument( '--bootstrap', help='The bootstrap to build with. Leave unset to choose ' 'automatically.', default=None) generic_parser.add_argument( '--hook', help='Filename to a module that contains python-for-android hooks', default=None) add_boolean_option( generic_parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution') add_boolean_option( generic_parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) add_boolean_option( generic_parser, ["allow-replace-dist"], default=True, description='Whether existing dist names can be automatically replaced' ) generic_parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') generic_parser.add_argument( '--java-build-tool', dest='java_build_tool', default='auto', choices=['auto', 'ant', 'gradle'], help=('The java build tool to use when packaging the APK, defaults ' 'to automatically selecting an appropriate tool.')) add_boolean_option( generic_parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)' ) self._read_configuration() subparsers = parser.add_subparsers(dest='subparser_name', help='The command to run') def add_parser(subparsers, *args, **kwargs): """ argparse in python2 doesn't support the aliases option, so we just don't provide the aliases there. """ if 'aliases' in kwargs and sys.version_info.major < 3: kwargs.pop('aliases') return subparsers.add_parser(*args, **kwargs) add_parser( subparsers, 'recommendations', parents=[generic_parser], help='List recommended p4a dependencies') parser_recipes = add_parser( subparsers, 'recipes', parents=[generic_parser], help='List the available recipes') parser_recipes.add_argument( "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") add_parser( subparsers, 'bootstraps', help='List the available bootstraps', parents=[generic_parser]) add_parser( subparsers, 'clean_all', aliases=['clean-all'], help='Delete all builds, dists and caches', parents=[generic_parser]) add_parser( subparsers, 'clean_dists', aliases=['clean-dists'], help='Delete all dists', parents=[generic_parser]) add_parser( subparsers, 'clean_bootstrap_builds', aliases=['clean-bootstrap-builds'], help='Delete all bootstrap builds', parents=[generic_parser]) add_parser( subparsers, 'clean_builds', aliases=['clean-builds'], help='Delete all builds', parents=[generic_parser]) parser_clean = add_parser( subparsers, 'clean', help='Delete build components.', parents=[generic_parser]) parser_clean.add_argument( 'component', nargs='+', help=('The build component(s) to delete. You can pass any ' 'number of arguments from "all", "builds", "dists", ' '"distributions", "bootstrap_builds", "downloads".')) parser_clean_recipe_build = add_parser( subparsers, 'clean_recipe_build', aliases=['clean-recipe-build'], help=('Delete the build components of the given recipe. ' 'By default this will also delete built dists'), parents=[generic_parser]) parser_clean_recipe_build.add_argument( 'recipe', help='The recipe name') parser_clean_recipe_build.add_argument( '--no-clean-dists', default=False, dest='no_clean_dists', action='store_true', help='If passed, do not delete existing dists') parser_clean_download_cache = add_parser( subparsers, 'clean_download_cache', aliases=['clean-download-cache'], help='Delete cached downloads for requirement builds', parents=[generic_parser]) parser_clean_download_cache.add_argument( 'recipes', nargs='*', help='The recipes to clean (space-separated). If no recipe name is' ' provided, the entire cache is cleared.') parser_export_dist = add_parser( subparsers, 'export_dist', aliases=['export-dist'], help='Copy the named dist to the given path', parents=[generic_parser]) parser_export_dist.add_argument('output_dir', help='The output dir to copy to') parser_export_dist.add_argument( '--symlink', action='store_true', help='Symlink the dist instead of copying') parser_apk = add_parser( subparsers, 'apk', help='Build an APK', parents=[generic_parser]) # This is actually an internal argument of the build.py # (see pythonforandroid/bootstraps/common/build/build.py). # However, it is also needed before the distribution is finally # assembled for locating the setup.py / other build systems, which # is why we also add it here: parser_apk.add_argument( '--private', dest='private', help='the directory with the app source code files' + ' (containing your main.py entrypoint)', required=False, default=None) parser_apk.add_argument( '--release', dest='build_mode', action='store_const', const='release', default='debug', help='Build the PARSER_APK. in Release mode') parser_apk.add_argument( '--use-setup-py', dest="use_setup_py", action='store_true', default=False, help="Process the setup.py of a project if present. " + "(Experimental!") parser_apk.add_argument( '--ignore-setup-py', dest="ignore_setup_py", action='store_true', default=False, help="Don't run the setup.py of a project if present. " + "This may be required if the setup.py is not " + "designed to work inside p4a (e.g. by installing " + "dependencies that won't work or aren't desired " + "on Android") parser_apk.add_argument( '--keystore', dest='keystore', action='store', default=None, help=('Keystore for JAR signing key, will use jarsigner ' 'default if not specified (release build only)')) parser_apk.add_argument( '--signkey', dest='signkey', action='store', default=None, help='Key alias to sign PARSER_APK. with (release build only)') parser_apk.add_argument( '--keystorepw', dest='keystorepw', action='store', default=None, help='Password for keystore') parser_apk.add_argument( '--signkeypw', dest='signkeypw', action='store', default=None, help='Password for key alias') add_parser( subparsers, 'create', help='Compile a set of requirements into a dist', parents=[generic_parser]) add_parser( subparsers, 'archs', help='List the available target architectures', parents=[generic_parser]) add_parser( subparsers, 'distributions', aliases=['dists'], help='List the currently available (compiled) dists', parents=[generic_parser]) add_parser( subparsers, 'delete_dist', aliases=['delete-dist'], help='Delete a compiled dist', parents=[generic_parser]) parser_sdk_tools = add_parser( subparsers, 'sdk_tools', aliases=['sdk-tools'], help='Run the given binary from the SDK tools dis', parents=[generic_parser]) parser_sdk_tools.add_argument( 'tool', help='The binary tool name to run') add_parser( subparsers, 'adb', help='Run adb from the given SDK', parents=[generic_parser]) add_parser( subparsers, 'logcat', help='Run logcat from the given SDK', parents=[generic_parser]) add_parser( subparsers, 'build_status', aliases=['build-status'], help='Print some debug information about current built components', parents=[generic_parser]) parser.add_argument('-v', '--version', action='version', version=__version__) args, unknown = parser.parse_known_args(sys.argv[1:]) args.unknown_args = unknown if hasattr(args, "private") and args.private is not None: # Pass this value on to the internal bootstrap build.py: args.unknown_args += ["--private", args.private] if hasattr(args, "ignore_setup_py") and args.ignore_setup_py: args.use_setup_py = False self.args = args if args.subparser_name is None: parser.print_help() exit(1) setup_color(args.color) if args.debug: logger.setLevel(logging.DEBUG) self.ctx = Context() self.ctx.use_setup_py = getattr(args, "use_setup_py", True) have_setup_py_or_similar = False if getattr(args, "private", None) is not None: project_dir = getattr(args, "private") if (os.path.exists(os.path.join(project_dir, "setup.py")) or os.path.exists(os.path.join(project_dir, "pyproject.toml"))): have_setup_py_or_similar = True # Process requirements and put version in environ if hasattr(args, 'requirements'): requirements = [] # Add dependencies from setup.py, but only if they are recipes # (because otherwise, setup.py itself will install them later) if (have_setup_py_or_similar and getattr(args, "use_setup_py", False)): try: info("Analyzing package dependencies. MAY TAKE A WHILE.") # Get all the dependencies corresponding to a recipe: dependencies = [ dep.lower() for dep in get_dep_names_of_package( args.private, keep_version_pins=True, recursive=True, verbose=True, ) ] info("Dependencies obtained: " + str(dependencies)) all_recipes = [ recipe.lower() for recipe in set(Recipe.list_recipes(self.ctx)) ] dependencies = set(dependencies).intersection( set(all_recipes) ) # Add dependencies to argument list: if len(dependencies) > 0: if len(args.requirements) > 0: args.requirements += u"," args.requirements += u",".join(dependencies) except ValueError: # Not a python package, apparently. warning( "Processing failed, is this project a valid " "package? Will continue WITHOUT setup.py deps." ) # Parse --requirements argument list: for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) self.warn_on_deprecated_args(args) self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_api = args.ndk_api self.ctx.symlink_java_src = args.symlink_java_src self.ctx.java_build_tool = args.java_build_tool self._archs = split_argument_list(args.arch) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs # Each subparser corresponds to a method getattr(self, args.subparser_name.replace('-', '_'))(args) @staticmethod def warn_on_carriage_return_args(args): for check_arg in args: if '\r' in check_arg: warning("Argument '{}' contains a carriage return (\\r).".format(str(check_arg.replace('\r', '')))) warning("Invoking this program via scripts which use CRLF instead of LF line endings will have undefined behaviour.") def warn_on_deprecated_args(self, args): """ Print warning messages for any deprecated arguments that were passed. """ # Output warning if setup.py is present and neither --ignore-setup-py # nor --use-setup-py was specified. if getattr(args, "private", None) is not None and \ (os.path.exists(os.path.join(args.private, "setup.py")) or os.path.exists(os.path.join(args.private, "pyproject.toml")) ): if not getattr(args, "use_setup_py", False) and \ not getattr(args, "ignore_setup_py", False): warning(" **** FUTURE BEHAVIOR CHANGE WARNING ****") warning("Your project appears to contain a setup.py file.") warning("Currently, these are ignored by default.") warning("This will CHANGE in an upcoming version!") warning("") warning("To ensure your setup.py is ignored, please specify:") warning(" --ignore-setup-py") warning("") warning("To enable what will some day be the default, specify:") warning(" --use-setup-py") # NDK version is now determined automatically if args.ndk_version is not None: warning('--ndk-version is deprecated and no longer necessary, ' 'the value you passed is ignored') if 'ANDROIDNDKVER' in environ: warning('$ANDROIDNDKVER is deprecated and no longer necessary, ' 'the value you set is ignored') def hook(self, name): if not self.args.hook: return if not hasattr(self, "hook_module"): # first time, try to load the hook module self.hook_module = imp.load_source("pythonforandroid.hook", self.args.hook) if hasattr(self.hook_module, name): info("Hook: execute {}".format(name)) getattr(self.hook_module, name)(self) else: info("Hook: ignore {}".format(name)) @property def default_storage_dir(self): udd = user_data_dir('python-for-android') if ' ' in udd: udd = '~/.python-for-android' return udd @staticmethod def _read_configuration(): # search for a .p4a configuration file in the current directory if not exists(".p4a"): return info("Reading .p4a configuration") with open(".p4a") as fd: lines = fd.readlines() lines = [shlex.split(line) for line in lines if not line.startswith("#")] for line in lines: for arg in line: sys.argv.append(arg) def recipes(self, args): """ Prints recipes basic info, e.g. .. code-block:: bash python3 3.7.1 depends: ['hostpython3', 'sqlite3', 'openssl', 'libffi'] conflicts: ['python2'] optional depends: ['sqlite3', 'libffi', 'openssl'] """ ctx = self.ctx if args.compact: print(" ".join(set(Recipe.list_recipes(ctx)))) else: for name in sorted(Recipe.list_recipes(ctx)): try: recipe = Recipe.get_recipe(name, ctx) except (IOError, ValueError): warning('Recipe "{}" could not be loaded'.format(name)) except SyntaxError: import traceback traceback.print_exc() warning(('Recipe "{}" could not be loaded due to a ' 'syntax error').format(name)) version = str(recipe.version) print('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} ' '{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}' '{version:<8}{Style.RESET_ALL}'.format( recipe=recipe, Fore=Out_Fore, Style=Out_Style, version=version)) print(' {Fore.GREEN}depends: {recipe.depends}' '{Fore.RESET}'.format(recipe=recipe, Fore=Out_Fore)) if recipe.conflicts: print(' {Fore.RED}conflicts: {recipe.conflicts}' '{Fore.RESET}' .format(recipe=recipe, Fore=Out_Fore)) if recipe.opt_depends: print(' {Fore.YELLOW}optional depends: ' '{recipe.opt_depends}{Fore.RESET}' .format(recipe=recipe, Fore=Out_Fore)) def bootstraps(self, _args): """List all the bootstraps available to build with.""" for bs in Bootstrap.all_bootstraps(): bs = Bootstrap.get_bootstrap(bs, self.ctx) print('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}' .format(bs=bs, Fore=Out_Fore, Style=Out_Style)) print(' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format(bs=bs, Fore=Out_Fore)) def clean(self, args): components = args.component component_clean_methods = { 'all': self.clean_all, 'dists': self.clean_dists, 'distributions': self.clean_dists, 'builds': self.clean_builds, 'bootstrap_builds': self.clean_bootstrap_builds, 'downloads': self.clean_download_cache} for component in components: if component not in component_clean_methods: raise BuildInterruptingException(( 'Asked to clean "{}" but this argument is not ' 'recognised'.format(component))) component_clean_methods[component](args) def clean_all(self, args): """Delete all build components; the package cache, package builds, bootstrap builds and distributions.""" self.clean_dists(args) self.clean_builds(args) self.clean_download_cache(args) def clean_dists(self, _args): """Delete all compiled distributions in the internal distribution directory.""" ctx = self.ctx if exists(ctx.dist_dir): shutil.rmtree(ctx.dist_dir) def clean_bootstrap_builds(self, _args): """Delete all the bootstrap builds.""" if exists(join(self.ctx.build_dir, 'bootstrap_builds')): shutil.rmtree(join(self.ctx.build_dir, 'bootstrap_builds')) # for bs in Bootstrap.all_bootstraps(): # bs = Bootstrap.get_bootstrap(bs, self.ctx) # if bs.build_dir and exists(bs.build_dir): # info('Cleaning build for {} bootstrap.'.format(bs.name)) # shutil.rmtree(bs.build_dir) def clean_builds(self, _args): """Delete all build caches for each recipe, python-install, java code and compiled libs collection. This does *not* delete the package download cache or the final distributions. You can also use clean_recipe_build to delete the build of a specific recipe. """ ctx = self.ctx if exists(ctx.build_dir): shutil.rmtree(ctx.build_dir) if exists(ctx.python_installs_dir): shutil.rmtree(ctx.python_installs_dir) libs_dir = join(self.ctx.build_dir, 'libs_collections') if exists(libs_dir): shutil.rmtree(libs_dir) def clean_recipe_build(self, args): """Deletes the build files of the given recipe. This is intended for debug purposes. You may experience strange behaviour or problems with some recipes if their build has made unexpected state changes. If this happens, run clean_builds, or attempt to clean other recipes until things work again. """ recipe = Recipe.get_recipe(args.recipe, self.ctx) info('Cleaning build for {} recipe.'.format(recipe.name)) recipe.clean_build() if not args.no_clean_dists: self.clean_dists(args) def clean_download_cache(self, args): """ Deletes a download cache for recipes passed as arguments. If no argument is passed, it'll delete *all* downloaded caches. :: p4a clean_download_cache kivy,pyjnius This does *not* delete the build caches or final distributions. """ ctx = self.ctx if hasattr(args, 'recipes') and args.recipes: for package in args.recipes: remove_path = join(ctx.packages_path, package) if exists(remove_path): shutil.rmtree(remove_path) info('Download cache removed for: "{}"'.format(package)) else: warning('No download cache found for "{}", skipping'.format( package)) else: if exists(ctx.packages_path): shutil.rmtree(ctx.packages_path) info('Download cache removed.') else: print('No cache found at "{}"'.format(ctx.packages_path)) @require_prebuilt_dist def export_dist(self, args): """Copies a created dist to an output dir. This makes it easy to navigate to the dist to investigate it or call build.py, though you do not in general need to do this and can use the apk command instead. """ ctx = self.ctx dist = dist_from_args(ctx, args) if dist.needs_build: raise BuildInterruptingException( 'You asked to export a dist, but there is no dist ' 'with suitable recipes available. For now, you must ' ' create one first with the create argument.') if args.symlink: shprint(sh.ln, '-s', dist.dist_dir, args.output_dir) else: shprint(sh.cp, '-r', dist.dist_dir, args.output_dir) @property def _dist(self): ctx = self.ctx dist = dist_from_args(ctx, self.args) ctx.distribution = dist return dist @require_prebuilt_dist def apk(self, args): """Create an APK using the given distribution.""" ctx = self.ctx dist = self._dist # Manually fixing these arguments at the string stage is # unsatisfactory and should probably be changed somehow, but # we can't leave it until later as the build.py scripts assume # they are in the current directory. fix_args = ('--dir', '--private', '--add-jar', '--add-source', '--whitelist', '--blacklist', '--presplash', '--icon') unknown_args = args.unknown_args for i, arg in enumerate(unknown_args): argx = arg.split('=') if argx[0] in fix_args: if len(argx) > 1: unknown_args[i] = '='.join( (argx[0], realpath(expanduser(argx[1])))) elif i + 1 < len(unknown_args): unknown_args[i+1] = realpath(expanduser(unknown_args[i+1])) env = os.environ.copy() if args.build_mode == 'release': if args.keystore: env['P4A_RELEASE_KEYSTORE'] = realpath(expanduser(args.keystore)) if args.signkey: env['P4A_RELEASE_KEYALIAS'] = args.signkey if args.keystorepw: env['P4A_RELEASE_KEYSTORE_PASSWD'] = args.keystorepw if args.signkeypw: env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.signkeypw elif args.keystorepw and 'P4A_RELEASE_KEYALIAS_PASSWD' not in env: env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.keystorepw build = imp.load_source('build', join(dist.dist_dir, 'build.py')) with current_directory(dist.dist_dir): self.hook("before_apk_build") os.environ["ANDROID_API"] = str(self.ctx.android_api) build_args = build.parse_args(args.unknown_args) self.hook("after_apk_build") self.hook("before_apk_assemble") build_type = ctx.java_build_tool if build_type == 'auto': info('Selecting java build tool:') build_tools_versions = os.listdir(join(ctx.sdk_dir, 'build-tools')) build_tools_versions = [f for f in build_tools_versions if not f.startswith('.')] build_tools_versions = sorted(build_tools_versions, key=LooseVersion) build_tools_version = build_tools_versions[-1] info(('Detected highest available build tools ' 'version to be {}').format(build_tools_version)) if build_tools_version >= '25.0' and exists('gradlew'): build_type = 'gradle' info(' Building with gradle, as gradle executable is ' 'present') else: build_type = 'ant' if build_tools_version < '25.0': info((' Building with ant, as the highest ' 'build-tools-version is only {}').format( build_tools_version)) else: info(' Building with ant, as no gradle executable ' 'detected') if build_type == 'gradle': # gradle-based build env["ANDROID_NDK_HOME"] = self.ctx.ndk_dir env["ANDROID_HOME"] = self.ctx.sdk_dir gradlew = sh.Command('./gradlew') if exists('/usr/bin/dos2unix'): # .../dists/bdisttest_python3/gradlew # .../build/bootstrap_builds/sdl2-python3/gradlew # if docker on windows, gradle contains CRLF output = shprint( sh.Command('dos2unix'), gradlew._path.decode('utf8'), _tail=20, _critical=True, _env=env ) if args.build_mode == "debug": gradle_task = "assembleDebug" elif args.build_mode == "release": gradle_task = "assembleRelease" else: raise BuildInterruptingException( "Unknown build mode {} for apk()".format(args.build_mode)) output = shprint(gradlew, gradle_task, _tail=20, _critical=True, _env=env) # gradle output apks somewhere else # and don't have version in file apk_dir = join(dist.dist_dir, "build", "outputs", "apk", args.build_mode) apk_glob = "*-{}.apk" apk_add_version = True else: # ant-based build try: ant = sh.Command('ant') except sh.CommandNotFound: raise BuildInterruptingException( 'Could not find ant binary, please install it ' 'and make sure it is in your $PATH.') output = shprint(ant, args.build_mode, _tail=20, _critical=True, _env=env) apk_dir = join(dist.dist_dir, "bin") apk_glob = "*-*-{}.apk" apk_add_version = False self.hook("after_apk_assemble") info_main('# Copying APK to current directory') apk_re = re.compile(r'.*Please sign (/.*\.apk) manually$') apk_file = None for line in reversed(output.splitlines()): m = apk_re.match(line) if m: apk_file = m.groups()[0] break if not apk_file: info_main('# APK filename not found in build output. Guessing...') if args.build_mode == "release": suffixes = ("release", "release-unsigned") else: suffixes = ("debug", ) for suffix in suffixes: apks = glob.glob(join(apk_dir, apk_glob.format(suffix))) if apks: if len(apks) > 1: info('More than one built APK found... guessing you ' 'just built {}'.format(apks[-1])) apk_file = apks[-1] break else: raise BuildInterruptingException('Couldn\'t find the built APK') info_main('# Found APK file: {}'.format(apk_file)) if apk_add_version: info('# Add version number to APK') apk_name = basename(apk_file)[:-len(APK_SUFFIX)] apk_file_dest = "{}-{}-{}".format( apk_name, build_args.version, APK_SUFFIX) info('# APK renamed to {}'.format(apk_file_dest)) shprint(sh.cp, apk_file, apk_file_dest) else: shprint(sh.cp, apk_file, './') @require_prebuilt_dist def create(self, args): """Create a distribution directory if it doesn't already exist, run any recipes if necessary, and build the apk. """ pass # The decorator does everything def archs(self, _args): """List the target architectures available to be built for.""" print('{Style.BRIGHT}Available target architectures are:' '{Style.RESET_ALL}'.format(Style=Out_Style)) for arch in self.ctx.archs: print(' {}'.format(arch.arch)) def dists(self, args): """The same as :meth:`distributions`.""" self.distributions(args) def distributions(self, _args): """Lists all distributions currently available (i.e. that have already been built).""" ctx = self.ctx dists = Distribution.get_distributions(ctx) if dists: print('{Style.BRIGHT}Distributions currently installed are:' '{Style.RESET_ALL}'.format(Style=Out_Style, Fore=Out_Fore)) pretty_log_dists(dists, print) else: print('{Style.BRIGHT}There are no dists currently built.' '{Style.RESET_ALL}'.format(Style=Out_Style)) def delete_dist(self, _args): dist = self._dist if not dist.folder_exists(): info('No dist exists that matches your specifications, ' 'exiting without deleting.') return dist.delete() def sdk_tools(self, args): """Runs the android binary from the detected SDK directory, passing all arguments straight to it. This binary is used to install e.g. platform-tools for different API level targets. This is intended as a convenience function if android is not in your $PATH. """ ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_api=self.ndk_api) android = sh.Command(join(ctx.sdk_dir, 'tools', args.tool)) output = android( *args.unknown_args, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def adb(self, args): """Runs the adb binary from the detected SDK directory, passing all arguments straight to it. This is intended as a convenience function if adb is not in your $PATH. """ self._adb(args.unknown_args) def logcat(self, args): """Runs ``adb logcat`` using the adb binary from the detected SDK directory. All extra args are passed as arguments to logcat.""" self._adb(['logcat'] + args.unknown_args) def _adb(self, commands): """Call the adb executable from the SDK, passing the given commands as arguments.""" ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_api=self.ndk_api) if platform in ('win32', 'cygwin'): adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb.exe')) else: adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb')) info_notify('Starting adb...') output = adb(*commands, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def recommendations(self, args): print_recommendations() def build_status(self, _args): """Print the status of the specified build. """ print('{Style.BRIGHT}Bootstraps whose core components are probably ' 'already built:{Style.RESET_ALL}'.format(Style=Out_Style)) bootstrap_dir = join(self.ctx.build_dir, 'bootstrap_builds') if exists(bootstrap_dir): for filen in os.listdir(bootstrap_dir): print(' {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}' .format(filen=filen, Fore=Out_Fore, Style=Out_Style)) print('{Style.BRIGHT}Recipes that are probably already built:' '{Style.RESET_ALL}'.format(Style=Out_Style)) other_builds_dir = join(self.ctx.build_dir, 'other_builds') if exists(other_builds_dir): for filen in sorted(os.listdir(other_builds_dir)): name = filen.split('-')[0] dependencies = filen.split('-')[1:] recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}' '{Style.RESET_ALL}'.format( Style=Out_Style, name=name, Fore=Out_Fore)) if dependencies: recipe_str += ( ' ({Fore.BLUE}with ' + ', '.join(dependencies) + '{Fore.RESET})').format(Fore=Out_Fore) recipe_str += '{Style.RESET_ALL}'.format(Style=Out_Style) print(recipe_str)
class ToolchainCL(object): def __init__(self): argv = sys.argv # Buildozer used to pass these arguments in a now-invalid order # If that happens, apply this fix # This fix will be removed once a fixed buildozer is released if (len(argv) > 2 and argv[1].startswith('--color') and argv[2].startswith('--storage-dir')): argv.append(argv.pop(1)) # the --color arg argv.append(argv.pop(1)) # the --storage-dir arg parser = NoAbbrevParser( description=('A packaging tool for turning Python scripts and apps ' 'into Android APKs')) generic_parser = argparse.ArgumentParser( add_help=False, description=('Generic arguments applied to all commands')) dist_parser = argparse.ArgumentParser( add_help=False, description=('Arguments for dist building')) generic_parser.add_argument( '--debug', dest='debug', action='store_true', default=False, help='Display debug output and all build info') generic_parser.add_argument( '--color', dest='color', choices=['always', 'never', 'auto'], help='Enable or disable color output (default enabled on tty)') generic_parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') generic_parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') generic_parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help='The Android API level to build against.') generic_parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default='', help=('The version of the Android NDK. This is optional, ' 'we try to work it out automatically from the ndk_dir.')) generic_parser.add_argument( '--symlink-java-src', '--symlink_java_src', action='store_true', dest='symlink_java_src', default=False, help=('If True, symlinks the java src folder during build and dist ' 'creation. This is useful for development only, it could also ' 'cause weird problems.')) default_storage_dir = user_data_dir('python-for-android') if ' ' in default_storage_dir: default_storage_dir = '~/.python-for-android' generic_parser.add_argument( '--storage-dir', dest='storage_dir', default=default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(default_storage_dir))) generic_parser.add_argument( '--arch', help='The archs to build for, separated by commas.', default='armeabi') # Options for specifying the Distribution generic_parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') generic_parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules'), default='') generic_parser.add_argument( '--bootstrap', help='The bootstrap to build with. Leave unset to choose automatically.', default=None) generic_parser.add_argument( '--hook', help='Filename to a module that contain python-for-android hooks', default=None) add_boolean_option( generic_parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution:') add_boolean_option( generic_parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) generic_parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') generic_parser.add_argument( '--java-build-tool', dest='java_build_tool', default='auto', choices=['auto', 'ant', 'gradle', 'none'], help=('The java build tool to use when packaging the APK, defaults ' 'to automatically selecting an appropriate tool.')) add_boolean_option( generic_parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)') self._read_configuration() subparsers = parser.add_subparsers(dest='subparser_name', help='The command to run') def add_parser(subparsers, *args, **kwargs): ''' argparse in python2 doesn't support the aliases option, so we just don't provide the aliases there. ''' if 'aliases' in kwargs and sys.version_info.major < 3: kwargs.pop('aliases') return subparsers.add_parser(*args, **kwargs) parser_recipes = add_parser(subparsers, 'recipes', parents=[generic_parser], help='List the available recipes') parser_recipes.add_argument( "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") parser_bootstraps = add_parser( subparsers, 'bootstraps', help='List the available bootstraps', parents=[generic_parser]) parser_clean_all = add_parser( subparsers, 'clean_all', aliases=['clean-all'], help='Delete all builds, dists and caches', parents=[generic_parser]) parser_clean_dists = add_parser( subparsers, 'clean_dists', aliases=['clean-dists'], help='Delete all dists', parents=[generic_parser]) parser_clean_bootstrap_builds = add_parser( subparsers, 'clean_bootstrap_builds', aliases=['clean-bootstrap-builds'], help='Delete all bootstrap builds', parents=[generic_parser]) parser_clean_builds = add_parser( subparsers, 'clean_builds', aliases=['clean-builds'], help='Delete all builds', parents=[generic_parser]) parser_clean = add_parser(subparsers, 'clean', help='Delete build components.', parents=[generic_parser]) parser_clean.add_argument( 'component', nargs='+', help=('The build component(s) to delete. You can pass any ' 'number of arguments from "all", "builds", "dists", ' '"distributions", "bootstrap_builds", "downloads".')) parser_clean_recipe_build = add_parser(subparsers, 'clean_recipe_build', aliases=['clean-recipe-build'], help=('Delete the build components of the given recipe. ' 'By default this will also delete built dists'), parents=[generic_parser]) parser_clean_recipe_build.add_argument('recipe', help='The recipe name') parser_clean_recipe_build.add_argument('--no-clean-dists', default=False, dest='no_clean_dists', action='store_true', help='If passed, do not delete existing dists') parser_clean_download_cache= add_parser(subparsers, 'clean_download_cache', aliases=['clean-download-cache'], help='Delete cached downloads for requirement builds', parents=[generic_parser]) parser_clean_download_cache.add_argument( 'recipes', nargs='*', help=('The recipes to clean (space-separated). If no recipe name is ' 'provided, the entire cache is cleared.')) parser_export_dist = add_parser(subparsers, 'export_dist', aliases=['export-dist'], help='Copy the named dist to the given path', parents=[generic_parser]) parser_export_dist.add_argument('output_dir', help=('The output dir to copy to')) parser_export_dist.add_argument('--symlink', action='store_true', help=('Symlink the dist instead of copying')) parser_apk = add_parser(subparsers, 'apk', help='Build an APK', parents=[generic_parser]) parser_apk.add_argument('--release', dest='build_mode', action='store_const', const='release', default='debug', help='Build the PARSER_APK. in Release mode') parser_apk.add_argument('--keystore', dest='keystore', action='store', default=None, help=('Keystore for JAR signing key, will use jarsigner ' 'default if not specified (release build only)')) parser_apk.add_argument('--signkey', dest='signkey', action='store', default=None, help='Key alias to sign PARSER_APK. with (release build only)') parser_apk.add_argument('--keystorepw', dest='keystorepw', action='store', default=None, help='Password for keystore') parser_apk.add_argument('--signkeypw', dest='signkeypw', action='store', default=None, help='Password for key alias') parser_create = add_parser(subparsers, 'create', help='Compile a set of requirements into a dist', parents=[generic_parser]) parser_archs = add_parser(subparsers, 'archs', help='List the available target architectures', parents=[generic_parser]) parser_distributions = add_parser(subparsers, 'distributions', aliases=['dists'], help='List the currently available (compiled) dists', parents=[generic_parser]) parser_delete_dist = add_parser(subparsers, 'delete_dist', aliases=['delete-dist'], help='Delete a compiled dist', parents=[generic_parser]) parser_sdk_tools = add_parser(subparsers, 'sdk_tools', aliases=['sdk-tools'], help='Run the given binary from the SDK tools dis', parents=[generic_parser]) parser_sdk_tools.add_argument( 'tool', help=('The tool binary name to run')) parser_adb = add_parser(subparsers, 'adb', help='Run adb from the given SDK', parents=[generic_parser]) parser_logcat = add_parser(subparsers, 'logcat', help='Run logcat from the given SDK', parents=[generic_parser]) parser_build_status = add_parser(subparsers, 'build_status', aliases=['build-status'], help='Print some debug information about current built components', parents=[generic_parser]) parser.add_argument('-v', '--version', action='version', version=__version__) args, unknown = parser.parse_known_args(sys.argv[1:]) args.unknown_args = unknown self.args = args if args.subparser_name is None: parser.print_help() exit(1) setup_color(args.color) if args.debug: logger.setLevel(logging.DEBUG) # strip version from requirements, and put them in environ if hasattr(args, 'requirements'): requirements = [] for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) self.ctx = Context() self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_version = args.ndk_version self.ctx.symlink_java_src = args.symlink_java_src self.ctx.java_build_tool = args.java_build_tool self._archs = split_argument_list(args.arch) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs # Each subparser corresponds to a method getattr(self, args.subparser_name.replace('-', '_'))(args) def hook(self, name): if not self.args.hook: return if not hasattr(self, "hook_module"): # first time, try to load the hook module self.hook_module = imp.load_source("pythonforandroid.hook", self.args.hook) if hasattr(self.hook_module, name): info("Hook: execute {}".format(name)) getattr(self.hook_module, name)(self) else: info("Hook: ignore {}".format(name)) @property def default_storage_dir(self): udd = user_data_dir('python-for-android') if ' ' in udd: udd = '~/.python-for-android' return udd def _read_configuration(self): # search for a .p4a configuration file in the current directory if not exists(".p4a"): return info("Reading .p4a configuration") with open(".p4a") as fd: lines = fd.readlines() lines = [shlex.split(line) for line in lines if not line.startswith("#")] for line in lines: for arg in line: sys.argv.append(arg) def recipes(self, args): ctx = self.ctx if args.compact: print(" ".join(set(Recipe.list_recipes(ctx)))) else: for name in sorted(Recipe.list_recipes(ctx)): try: recipe = Recipe.get_recipe(name, ctx) except IOError: warning('Recipe "{}" could not be loaded'.format(name)) except SyntaxError: import traceback traceback.print_exc() warning(('Recipe "{}" could not be loaded due to a ' 'syntax error').format(name)) version = str(recipe.version) print('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} ' '{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}' '{version:<8}{Style.RESET_ALL}'.format( recipe=recipe, Fore=Out_Fore, Style=Out_Style, version=version)) print(' {Fore.GREEN}depends: {recipe.depends}' '{Fore.RESET}'.format(recipe=recipe, Fore=Out_Fore)) if recipe.conflicts: print(' {Fore.RED}conflicts: {recipe.conflicts}' '{Fore.RESET}' .format(recipe=recipe, Fore=Out_Fore)) if recipe.opt_depends: print(' {Fore.YELLOW}optional depends: ' '{recipe.opt_depends}{Fore.RESET}' .format(recipe=recipe, Fore=Out_Fore)) def bootstraps(self, args): '''List all the bootstraps available to build with.''' for bs in Bootstrap.list_bootstraps(): bs = Bootstrap.get_bootstrap(bs, self.ctx) print('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}' .format(bs=bs, Fore=Out_Fore, Style=Out_Style)) print(' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format(bs=bs, Fore=Out_Fore)) def clean(self, args): components = args.component component_clean_methods = {'all': self.clean_all, 'dists': self.clean_dists, 'distributions': self.clean_dists, 'builds': self.clean_builds, 'bootstrap_builds': self.clean_bootstrap_builds, 'downloads': self.clean_download_cache} for component in components: if component not in component_clean_methods: raise ValueError(( 'Asked to clean "{}" but this argument is not ' 'recognised'.format(component))) component_clean_methods[component](args) def clean_all(self, args): '''Delete all build components; the package cache, package builds, bootstrap builds and distributions.''' self.clean_dists(args) self.clean_builds(args) self.clean_download_cache(args) def clean_dists(self, args): '''Delete all compiled distributions in the internal distribution directory.''' ctx = self.ctx if exists(ctx.dist_dir): shutil.rmtree(ctx.dist_dir) def clean_bootstrap_builds(self, args): '''Delete all the bootstrap builds.''' if exists(join(self.ctx.build_dir, 'bootstrap_builds')): shutil.rmtree(join(self.ctx.build_dir, 'bootstrap_builds')) # for bs in Bootstrap.list_bootstraps(): # bs = Bootstrap.get_bootstrap(bs, self.ctx) # if bs.build_dir and exists(bs.build_dir): # info('Cleaning build for {} bootstrap.'.format(bs.name)) # shutil.rmtree(bs.build_dir) def clean_builds(self, args): '''Delete all build caches for each recipe, python-install, java code and compiled libs collection. This does *not* delete the package download cache or the final distributions. You can also use clean_recipe_build to delete the build of a specific recipe. ''' ctx = self.ctx # if exists(ctx.dist_dir): # shutil.rmtree(ctx.dist_dir) if exists(ctx.build_dir): shutil.rmtree(ctx.build_dir) if exists(ctx.python_installs_dir): shutil.rmtree(ctx.python_installs_dir) libs_dir = join(self.ctx.build_dir, 'libs_collections') if exists(libs_dir): shutil.rmtree(libs_dir) def clean_recipe_build(self, args): '''Deletes the build files of the given recipe. This is intended for debug purposes, you may experience strange behaviour or problems with some recipes (if their build has done unexpected state changes). If this happens, run clean_builds, or attempt to clean other recipes until things work again. ''' recipe = Recipe.get_recipe(args.recipe, self.ctx) info('Cleaning build for {} recipe.'.format(recipe.name)) recipe.clean_build() if not args.no_clean_dists: self.clean_dists(args) def clean_download_cache(self, args): ''' Deletes a download cache for recipes stated as arguments. If no argument is passed, it'll delete *all* downloaded cache. :: p4a clean_download_cache kivy,pyjnius This does *not* delete the build caches or final distributions. ''' ctx = self.ctx if hasattr(args, 'recipes') and args.recipes: for package in args.recipes: remove_path = join(ctx.packages_path, package) if exists(remove_path): shutil.rmtree(remove_path) info('Download cache removed for: "{}"'.format(package)) else: warning('No download cache found for "{}", skipping'.format(package)) else: if exists(ctx.packages_path): shutil.rmtree(ctx.packages_path) info('Download cache removed.') else: print('No cache found at "{}"'.format(ctx.packages_path)) @require_prebuilt_dist def export_dist(self, args): '''Copies a created dist to an output dir. This makes it easy to navigate to the dist to investigate it or call build.py, though you do not in general need to do this and can use the apk command instead. ''' ctx = self.ctx dist = dist_from_args(ctx, args) if dist.needs_build: info('You asked to export a dist, but there is no dist ' 'with suitable recipes available. For now, you must ' ' create one first with the create argument.') exit(1) if args.symlink: shprint(sh.ln, '-s', dist.dist_dir, args.output_dir) else: shprint(sh.cp, '-r', dist.dist_dir, args.output_dir) @property def _dist(self): ctx = self.ctx dist = dist_from_args(ctx, self.args) return dist @require_prebuilt_dist def apk(self, args): '''Create an APK using the given distribution.''' ctx = self.ctx dist = self._dist # Manually fixing these arguments at the string stage is # unsatisfactory and should probably be changed somehow, but # we can't leave it until later as the build.py scripts assume # they are in the current directory. fix_args = ('--dir', '--private', '--add-jar', '--add-source', '--whitelist', '--blacklist', '--presplash', '--icon') unknown_args = args.unknown_args for i, arg in enumerate(unknown_args[:-1]): argx = arg.split('=') if argx[0] in fix_args: if len(argx) > 1: unknown_args[i] = '='.join((argx[0], realpath(expanduser(argx[1])))) else: unknown_args[i+1] = realpath(expanduser(unknown_args[i+1])) env = os.environ.copy() if args.build_mode == 'release': if args.keystore: env['P4A_RELEASE_KEYSTORE'] = realpath(expanduser(args.keystore)) if args.signkey: env['P4A_RELEASE_KEYALIAS'] = args.signkey if args.keystorepw: env['P4A_RELEASE_KEYSTORE_PASSWD'] = args.keystorepw if args.signkeypw: env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.signkeypw elif args.keystorepw and 'P4A_RELEASE_KEYALIAS_PASSWD' not in env: env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.keystorepw build = imp.load_source('build', join(dist.dist_dir, 'build.py')) with current_directory(dist.dist_dir): self.hook("before_apk_build") os.environ["ANDROID_API"] = str(self.ctx.android_api) build_args = build.parse_args(args.unknown_args) self.hook("after_apk_build") self.hook("before_apk_assemble") build_type = ctx.java_build_tool if build_type == 'auto': info('Selecting java build tool:') build_tools_versions = os.listdir(join(ctx.sdk_dir, 'build-tools')) build_tools_versions = sorted(build_tools_versions, key=LooseVersion) build_tools_version = build_tools_versions[-1] info(('Detected highest available build tools ' 'version to be {}').format(build_tools_version)) if build_tools_version >= '25.0' and exists('gradlew'): build_type = 'gradle' info(' Building with gradle, as gradle executable is present') else: build_type = 'ant' if build_tools_version < '25.0': info((' Building with ant, as the highest ' 'build-tools-version is only {}').format(build_tools_version)) else: info(' Building with ant, as no gradle executable detected') elif build_type == 'gradle': # gradle-based build env["ANDROID_NDK_HOME"] = self.ctx.ndk_dir env["ANDROID_HOME"] = self.ctx.sdk_dir gradlew = sh.Command('./gradlew') if args.build_mode == "debug": gradle_task = "assembleDebug" elif args.build_mode == "release": gradle_task = "assembleRelease" else: error("Unknown build mode {} for apk()".format( args.build_mode)) exit(1) output = shprint(gradlew, gradle_task, _tail=20, _critical=True, _env=env) # gradle output apks somewhere else # and don't have version in file apk_dir = join(dist.dist_dir, "build", "outputs", "apk") apk_glob = "*-{}.apk" apk_add_version = True elif build_type == 'none': return else: # ant-based build try: ant = sh.Command('ant') except sh.CommandNotFound: error('Could not find ant binary, please install it ' 'and make sure it is in your $PATH.') exit(1) output = shprint(ant, args.build_mode, _tail=20, _critical=True, _env=env) apk_dir = join(dist.dist_dir, "bin") apk_glob = "*-*-{}.apk" apk_add_version = False self.hook("after_apk_assemble") info_main('# Copying APK to current directory') apk_re = re.compile(r'.*Package: (.*\.apk)$') apk_file = None for line in reversed(output.splitlines()): m = apk_re.match(line) if m: apk_file = m.groups()[0] break if not apk_file: info_main('# APK filename not found in build output, trying to guess') if args.build_mode == "release": suffixes = ("release", "release-unsigned") else: suffixes = ("debug", ) for suffix in suffixes: apks = glob.glob(join(apk_dir, apk_glob.format(suffix))) if apks: if len(apks) > 1: info('More than one built APK found... guessing you ' 'just built {}'.format(apks[-1])) apk_file = apks[-1] break else: raise ValueError('Couldn\'t find the built APK') info_main('# Found APK file: {}'.format(apk_file)) if apk_add_version: info('# Add version number to APK') apk_name, apk_suffix = basename(apk_file).split("-", 1) apk_file_dest = "{}-{}-{}".format( apk_name, build_args.version, apk_suffix) info('# APK renamed to {}'.format(apk_file_dest)) shprint(sh.cp, apk_file, apk_file_dest) else: shprint(sh.cp, apk_file, './') @require_prebuilt_dist def create(self, args): '''Create a distribution directory if it doesn't already exist, run any recipes if necessary, and build the apk. ''' pass # The decorator does everything def archs(self, args): '''List the target architectures available to be built for.''' print('{Style.BRIGHT}Available target architectures are:' '{Style.RESET_ALL}'.format(Style=Out_Style)) for arch in self.ctx.archs: print(' {}'.format(arch.arch)) def dists(self, args): '''The same as :meth:`distributions`.''' self.distributions(args) def distributions(self, args): '''Lists all distributions currently available (i.e. that have already been built).''' ctx = self.ctx dists = Distribution.get_distributions(ctx) if dists: print('{Style.BRIGHT}Distributions currently installed are:' '{Style.RESET_ALL}'.format(Style=Out_Style, Fore=Out_Fore)) pretty_log_dists(dists, print) else: print('{Style.BRIGHT}There are no dists currently built.' '{Style.RESET_ALL}'.format(Style=Out_Style)) def delete_dist(self, args): dist = self._dist if dist.needs_build: info('No dist exists that matches your specifications, ' 'exiting without deleting.') shutil.rmtree(dist.dist_dir) def sdk_tools(self, args): '''Runs the android binary from the detected SDK directory, passing all arguments straight to it. This binary is used to install e.g. platform-tools for different API level targets. This is intended as a convenience function if android is not in your $PATH. ''' ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_ver=self.ndk_version) android = sh.Command(join(ctx.sdk_dir, 'tools', args.tool)) output = android( *args.unknown_args, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def adb(self, args): '''Runs the adb binary from the detected SDK directory, passing all arguments straight to it. This is intended as a convenience function if adb is not in your $PATH. ''' self._adb(args.unknown_args) def logcat(self, args): '''Runs ``adb logcat`` using the adb binary from the detected SDK directory. All extra args are passed as arguments to logcat.''' self._adb(['logcat'] + args.unknown_args) def _adb(self, commands): '''Call the adb executable from the SDK, passing the given commands as arguments.''' ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_ver=self.ndk_version) if platform in ('win32', 'cygwin'): adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb.exe')) else: adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb')) info_notify('Starting adb...') output = adb(*commands, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def build_status(self, args): print('{Style.BRIGHT}Bootstraps whose core components are probably ' 'already built:{Style.RESET_ALL}'.format(Style=Out_Style)) bootstrap_dir = join(self.ctx.build_dir, 'bootstrap_builds') if exists(bootstrap_dir): for filen in os.listdir(bootstrap_dir): print(' {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}' .format(filen=filen, Fore=Out_Fore, Style=Out_Style)) print('{Style.BRIGHT}Recipes that are probably already built:' '{Style.RESET_ALL}'.format(Style=Out_Style)) other_builds_dir = join(self.ctx.build_dir, 'other_builds') if exists(other_builds_dir): for filen in sorted(os.listdir(other_builds_dir)): name = filen.split('-')[0] dependencies = filen.split('-')[1:] recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}' '{Style.RESET_ALL}'.format( Style=Out_Style, name=name, Fore=Out_Fore)) if dependencies: recipe_str += ( ' ({Fore.BLUE}with ' + ', '.join(dependencies) + '{Fore.RESET})').format(Fore=Out_Fore) recipe_str += '{Style.RESET_ALL}'.format(Style=Out_Style) print(recipe_str)
def __init__(self): argv = sys.argv # Buildozer used to pass these arguments in a now-invalid order # If that happens, apply this fix # This fix will be removed once a fixed buildozer is released if (len(argv) > 2 and argv[1].startswith('--color') and argv[2].startswith('--storage-dir')): argv.append(argv.pop(1)) # the --color arg argv.append(argv.pop(1)) # the --storage-dir arg parser = NoAbbrevParser( description=('A packaging tool for turning Python scripts and apps ' 'into Android APKs')) generic_parser = argparse.ArgumentParser( add_help=False, description=('Generic arguments applied to all commands')) dist_parser = argparse.ArgumentParser( add_help=False, description=('Arguments for dist building')) generic_parser.add_argument( '--debug', dest='debug', action='store_true', default=False, help='Display debug output and all build info') generic_parser.add_argument( '--color', dest='color', choices=['always', 'never', 'auto'], help='Enable or disable color output (default enabled on tty)') generic_parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') generic_parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') generic_parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help='The Android API level to build against.') generic_parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default='', help=('The version of the Android NDK. This is optional, ' 'we try to work it out automatically from the ndk_dir.')) generic_parser.add_argument( '--symlink-java-src', '--symlink_java_src', action='store_true', dest='symlink_java_src', default=False, help=('If True, symlinks the java src folder during build and dist ' 'creation. This is useful for development only, it could also ' 'cause weird problems.')) default_storage_dir = user_data_dir('python-for-android') if ' ' in default_storage_dir: default_storage_dir = '~/.python-for-android' generic_parser.add_argument( '--storage-dir', dest='storage_dir', default=default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(default_storage_dir))) generic_parser.add_argument( '--arch', help='The archs to build for, separated by commas.', default='armeabi') # Options for specifying the Distribution generic_parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') generic_parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules'), default='') generic_parser.add_argument( '--bootstrap', help='The bootstrap to build with. Leave unset to choose automatically.', default=None) generic_parser.add_argument( '--hook', help='Filename to a module that contain python-for-android hooks', default=None) add_boolean_option( generic_parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution:') add_boolean_option( generic_parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) generic_parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') generic_parser.add_argument( '--java-build-tool', dest='java_build_tool', default='auto', choices=['auto', 'ant', 'gradle', 'none'], help=('The java build tool to use when packaging the APK, defaults ' 'to automatically selecting an appropriate tool.')) add_boolean_option( generic_parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)') self._read_configuration() subparsers = parser.add_subparsers(dest='subparser_name', help='The command to run') def add_parser(subparsers, *args, **kwargs): ''' argparse in python2 doesn't support the aliases option, so we just don't provide the aliases there. ''' if 'aliases' in kwargs and sys.version_info.major < 3: kwargs.pop('aliases') return subparsers.add_parser(*args, **kwargs) parser_recipes = add_parser(subparsers, 'recipes', parents=[generic_parser], help='List the available recipes') parser_recipes.add_argument( "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") parser_bootstraps = add_parser( subparsers, 'bootstraps', help='List the available bootstraps', parents=[generic_parser]) parser_clean_all = add_parser( subparsers, 'clean_all', aliases=['clean-all'], help='Delete all builds, dists and caches', parents=[generic_parser]) parser_clean_dists = add_parser( subparsers, 'clean_dists', aliases=['clean-dists'], help='Delete all dists', parents=[generic_parser]) parser_clean_bootstrap_builds = add_parser( subparsers, 'clean_bootstrap_builds', aliases=['clean-bootstrap-builds'], help='Delete all bootstrap builds', parents=[generic_parser]) parser_clean_builds = add_parser( subparsers, 'clean_builds', aliases=['clean-builds'], help='Delete all builds', parents=[generic_parser]) parser_clean = add_parser(subparsers, 'clean', help='Delete build components.', parents=[generic_parser]) parser_clean.add_argument( 'component', nargs='+', help=('The build component(s) to delete. You can pass any ' 'number of arguments from "all", "builds", "dists", ' '"distributions", "bootstrap_builds", "downloads".')) parser_clean_recipe_build = add_parser(subparsers, 'clean_recipe_build', aliases=['clean-recipe-build'], help=('Delete the build components of the given recipe. ' 'By default this will also delete built dists'), parents=[generic_parser]) parser_clean_recipe_build.add_argument('recipe', help='The recipe name') parser_clean_recipe_build.add_argument('--no-clean-dists', default=False, dest='no_clean_dists', action='store_true', help='If passed, do not delete existing dists') parser_clean_download_cache= add_parser(subparsers, 'clean_download_cache', aliases=['clean-download-cache'], help='Delete cached downloads for requirement builds', parents=[generic_parser]) parser_clean_download_cache.add_argument( 'recipes', nargs='*', help=('The recipes to clean (space-separated). If no recipe name is ' 'provided, the entire cache is cleared.')) parser_export_dist = add_parser(subparsers, 'export_dist', aliases=['export-dist'], help='Copy the named dist to the given path', parents=[generic_parser]) parser_export_dist.add_argument('output_dir', help=('The output dir to copy to')) parser_export_dist.add_argument('--symlink', action='store_true', help=('Symlink the dist instead of copying')) parser_apk = add_parser(subparsers, 'apk', help='Build an APK', parents=[generic_parser]) parser_apk.add_argument('--release', dest='build_mode', action='store_const', const='release', default='debug', help='Build the PARSER_APK. in Release mode') parser_apk.add_argument('--keystore', dest='keystore', action='store', default=None, help=('Keystore for JAR signing key, will use jarsigner ' 'default if not specified (release build only)')) parser_apk.add_argument('--signkey', dest='signkey', action='store', default=None, help='Key alias to sign PARSER_APK. with (release build only)') parser_apk.add_argument('--keystorepw', dest='keystorepw', action='store', default=None, help='Password for keystore') parser_apk.add_argument('--signkeypw', dest='signkeypw', action='store', default=None, help='Password for key alias') parser_create = add_parser(subparsers, 'create', help='Compile a set of requirements into a dist', parents=[generic_parser]) parser_archs = add_parser(subparsers, 'archs', help='List the available target architectures', parents=[generic_parser]) parser_distributions = add_parser(subparsers, 'distributions', aliases=['dists'], help='List the currently available (compiled) dists', parents=[generic_parser]) parser_delete_dist = add_parser(subparsers, 'delete_dist', aliases=['delete-dist'], help='Delete a compiled dist', parents=[generic_parser]) parser_sdk_tools = add_parser(subparsers, 'sdk_tools', aliases=['sdk-tools'], help='Run the given binary from the SDK tools dis', parents=[generic_parser]) parser_sdk_tools.add_argument( 'tool', help=('The tool binary name to run')) parser_adb = add_parser(subparsers, 'adb', help='Run adb from the given SDK', parents=[generic_parser]) parser_logcat = add_parser(subparsers, 'logcat', help='Run logcat from the given SDK', parents=[generic_parser]) parser_build_status = add_parser(subparsers, 'build_status', aliases=['build-status'], help='Print some debug information about current built components', parents=[generic_parser]) parser.add_argument('-v', '--version', action='version', version=__version__) args, unknown = parser.parse_known_args(sys.argv[1:]) args.unknown_args = unknown self.args = args if args.subparser_name is None: parser.print_help() exit(1) setup_color(args.color) if args.debug: logger.setLevel(logging.DEBUG) # strip version from requirements, and put them in environ if hasattr(args, 'requirements'): requirements = [] for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) self.ctx = Context() self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_version = args.ndk_version self.ctx.symlink_java_src = args.symlink_java_src self.ctx.java_build_tool = args.java_build_tool self._archs = split_argument_list(args.arch) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs # Each subparser corresponds to a method getattr(self, args.subparser_name.replace('-', '_'))(args)
class ToolchainCL(object): def __init__(self): parser = argparse.ArgumentParser( description="Tool for managing the Android / Python toolchain", usage="""toolchain <command> [<args>] Available commands: adb Runs adb binary from the detected SDK dir apk Create an APK using the given distribution bootstraps List all the bootstraps available to build with. build_status Informations about the current build create Build an android project with all recipes clean_all Delete all build components clean_builds Delete all build caches clean_dists Delete all compiled distributions clean_download_cache Delete any downloaded recipe packages clean_recipe_build Delete the build files of a recipe distributions List all distributions export_dist Copies a created dist to an output directory logcat Runs logcat from the detected SDK dir print_context_info Prints debug informations recipes List all the available recipes sdk_tools Runs android binary from the detected SDK dir symlink_dist Symlinks a created dist to an output directory Planned commands: build_dist """) parser.add_argument("command", help="Command to run") # General options parser.add_argument( '--debug', dest='debug', action='store_true', help='Display debug output and all build info') parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help='The Android API level to build against.') parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default='', help=('The version of the Android NDK. This is optional, ' 'we try to work it out automatically from the ndk_dir.')) parser.add_argument( '--storage-dir', dest='storage_dir', default=self.default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(self.default_storage_dir))) # AND: This option doesn't really fit in the other categories, the # arg structure needs a rethink parser.add_argument( '--arch', help='The archs to build for, separated by commas.', default='armeabi') # Options for specifying the Distribution parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules'), default='') add_boolean_option( parser, ["allow-download"], default=False, description='Whether to allow binary dist download:') add_boolean_option( parser, ["allow-build"], default=True, description='Whether to allow compilation of a new distribution:') add_boolean_option( parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution:') parser.add_argument( '--extra-dist-dirs', '--extra_dist_dirs', dest='extra_dist_dirs', default='', help='Directories in which to look for distributions') add_boolean_option( parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') add_boolean_option( parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)') self._read_configuration() args, unknown = parser.parse_known_args(sys.argv[1:]) self.dist_args = args # strip version from requirements, and put them in environ requirements = [] for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) if args.debug: logger.setLevel(logging.DEBUG) self.ctx = Context() self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_version = args.ndk_version self._archs = split_argument_list(args.arch) # AND: Fail nicely if the args aren't handled yet if args.extra_dist_dirs: warning('Received --extra_dist_dirs but this arg currently is not ' 'handled, exiting.') exit(1) if args.allow_download: warning('Received --allow_download but this arg currently is not ' 'handled, exiting.') exit(1) # if args.allow_build: # warning('Received --allow_build but this arg currently is not ' # 'handled, exiting.') # exit(1) if not hasattr(self, args.command): print('Unrecognized command') parser.print_help() exit(1) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs getattr(self, args.command)(unknown) @property def default_storage_dir(self): udd = user_data_dir('python-for-android') if ' ' in udd: udd = '~/.python-for-android' return udd def _read_configuration(self): # search for a .p4a configuration file in the current directory if not exists(".p4a"): return info("Reading .p4a configuration") with open(".p4a") as fd: lines = fd.readlines() lines = [shlex.split(line) for line in lines if not line.startswith("#")] for line in lines: for arg in line: sys.argv.append(arg) def recipes(self, args): parser = argparse.ArgumentParser( description="List all the available recipes") parser.add_argument( "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") add_boolean_option( parser, ["color"], default=True, description='Whether the output should be colored:') args = parser.parse_args(args) Fore = Out_Fore Style = Out_Style if not args.color: Fore = Null_Fore Style = Null_Style ctx = self.ctx if args.compact: print(" ".join(set(Recipe.list_recipes(ctx)))) else: for name in sorted(Recipe.list_recipes(ctx)): recipe = Recipe.get_recipe(name, ctx) version = str(recipe.version) print('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} ' '{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}' '{version:<8}{Style.RESET_ALL}'.format( recipe=recipe, Fore=Fore, Style=Style, version=version)) print(' {Fore.GREEN}depends: {recipe.depends}' '{Fore.RESET}'.format(recipe=recipe, Fore=Fore)) if recipe.conflicts: print(' {Fore.RED}conflicts: {recipe.conflicts}' '{Fore.RESET}' .format(recipe=recipe, Fore=Fore)) if recipe.opt_depends: print(' {Fore.YELLOW}optional depends: ' '{recipe.opt_depends}{Fore.RESET}' .format(recipe=recipe, Fore=Fore)) def bootstraps(self, args): '''List all the bootstraps available to build with.''' for bs in Bootstrap.list_bootstraps(): bs = Bootstrap.get_bootstrap(bs, self.ctx) print('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}' .format(bs=bs, Fore=Out_Fore, Style=Out_Style)) print(' {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}' .format(bs=bs, Fore=Out_Fore)) def clean_all(self, args): '''Delete all build components; the package cache, package builds, bootstrap builds and distributions.''' parser = argparse.ArgumentParser( description="Clean the build cache, downloads and dists") parsed_args = parser.parse_args(args) self.clean_dists(args) self.clean_builds(args) self.clean_download_cache(args) def clean_dists(self, args): '''Delete all compiled distributions in the internal distribution directory.''' parser = argparse.ArgumentParser( description="Delete any distributions that have been built.") args = parser.parse_args(args) ctx = self.ctx if exists(ctx.dist_dir): shutil.rmtree(ctx.dist_dir) def clean_builds(self, args): '''Delete all build caches for each recipe, python-install, java code and compiled libs collection. This does *not* delete the package download cache or the final distributions. You can also use clean_recipe_build to delete the build of a specific recipe. ''' parser = argparse.ArgumentParser( description="Delete all build files (but not download caches)") args = parser.parse_args(args) ctx = self.ctx # if exists(ctx.dist_dir): # shutil.rmtree(ctx.dist_dir) if exists(ctx.build_dir): shutil.rmtree(ctx.build_dir) if exists(ctx.python_installs_dir): shutil.rmtree(ctx.python_installs_dir) libs_dir = join(self.ctx.build_dir, 'libs_collections') if exists(libs_dir): shutil.rmtree(libs_dir) def clean_recipe_build(self, args): '''Deletes the build files of the given recipe. This is intended for debug purposes, you may experience strange behaviour or problems with some recipes (if their build has done unexpected state changes). If this happens, run clean_builds, or attempt to clean other recipes until things work again. ''' parser = argparse.ArgumentParser( description="Delete all build files for the given recipe name.") parser.add_argument('recipe', help='The recipe name') args = parser.parse_args(args) recipe = Recipe.get_recipe(args.recipe, self.ctx) info('Cleaning build for {} recipe.'.format(recipe.name)) recipe.clean_build() def clean_download_cache(self, args): ''' Deletes any downloaded recipe packages. This does *not* delete the build caches or final distributions. ''' parser = argparse.ArgumentParser( description="Delete all download caches") args = parser.parse_args(args) ctx = self.ctx if exists(ctx.packages_path): shutil.rmtree(ctx.packages_path) @require_prebuilt_dist def export_dist(self, args): '''Copies a created dist to an output dir. This makes it easy to navigate to the dist to investigate it or call build.py, though you do not in general need to do this and can use the apk command instead. ''' parser = argparse.ArgumentParser( description='Copy a created dist to a given directory') parser.add_argument('--output', help=('The output dir to copy to'), required=True) args = parser.parse_args(args) ctx = self.ctx dist = dist_from_args(ctx, self.dist_args) if dist.needs_build: info('You asked to export a dist, but there is no dist ' 'with suitable recipes available. For now, you must ' ' create one first with the create argument.') exit(1) shprint(sh.cp, '-r', dist.dist_dir, args.output) @require_prebuilt_dist def symlink_dist(self, args): '''Symlinks a created dist to an output dir. This makes it easy to navigate to the dist to investigate it or call build.py, though you do not in general need to do this and can use the apk command instead. ''' parser = argparse.ArgumentParser( description='Symlink a created dist to a given directory') parser.add_argument('--output', help=('The output dir to copy to'), required=True) args = parser.parse_args(args) ctx = self.ctx dist = dist_from_args(ctx, self.dist_args) if dist.needs_build: info('You asked to symlink a dist, but there is no dist ' 'with suitable recipes available. For now, you must ' 'create one first with the create argument.') exit(1) shprint(sh.ln, '-s', dist.dist_dir, args.output) # def _get_dist(self): # ctx = self.ctx # dist = dist_from_args(ctx, self.dist_args) @property def _dist(self): ctx = self.ctx dist = dist_from_args(ctx, self.dist_args) return dist @require_prebuilt_dist def apk(self, args): '''Create an APK using the given distribution.''' ap = argparse.ArgumentParser( description='Build an APK') ap.add_argument('--release', dest='build_mode', action='store_const', const='release', default='debug', help='Build the APK in Release mode') ap.add_argument('--keystore', dest='keystore', action='store', default=None, help=('Keystore for JAR signing key, will use jarsigner ' 'default if not specified (release build only)')) ap.add_argument('--signkey', dest='signkey', action='store', default=None, help='Key alias to sign APK with (release build only)') ap.add_argument('--keystorepw', dest='keystorepw', action='store', default=None, help='Password for keystore') ap.add_argument('--signkeypw', dest='signkeypw', action='store', default=None, help='Password for key alias') apk_args, args = ap.parse_known_args(args) ctx = self.ctx dist = self._dist # Manually fixing these arguments at the string stage is # unsatisfactory and should probably be changed somehow, but # we can't leave it until later as the build.py scripts assume # they are in the current directory. fix_args = ('--dir', '--private', '--add-jar', '--add-source', '--whitelist', '--blacklist', '--presplash', '--icon') for i, arg in enumerate(args[:-1]): argx = arg.split('=') if argx[0] in fix_args: if len(argx) > 1: args[i] = '='.join((argx[0], realpath(expanduser(argx[1])))) else: args[i+1] = realpath(expanduser(args[i+1])) env = os.environ.copy() if apk_args.build_mode == 'release': if apk_args.keystore: env['P4A_RELEASE_KEYSTORE'] = realpath(expanduser(apk_args.keystore)) if apk_args.signkey: env['P4A_RELEASE_KEYALIAS'] = apk_args.signkey if apk_args.keystorepw: env['P4A_RELEASE_KEYSTORE_PASSWD'] = apk_args.keystorepw if apk_args.signkeypw: env['P4A_RELEASE_KEYALIAS_PASSWD'] = apk_args.signkeypw elif apk_args.keystorepw and 'P4A_RELEASE_KEYALIAS_PASSWD' not in env: env['P4A_RELEASE_KEYALIAS_PASSWD'] = apk_args.keystorepw build = imp.load_source('build', join(dist.dist_dir, 'build.py')) with current_directory(dist.dist_dir): build_args = build.parse_args(args) output = shprint(sh.ant, apk_args.build_mode, _tail=20, _critical=True, _env=env) info_main('# Copying APK to current directory') apk_re = re.compile(r'.*Package: (.*\.apk)$') apk_file = None for line in reversed(output.splitlines()): m = apk_re.match(line) if m: apk_file = m.groups()[0] break if not apk_file: info_main('# APK filename not found in build output, trying to guess') apks = glob.glob(join(dist.dist_dir, 'bin', '*-*-{}.apk'.format(apk_args.build_mode))) if len(apks) == 0: raise ValueError('Couldn\'t find the built APK') if len(apks) > 1: info('More than one built APK found...guessing you ' 'just built {}'.format(apks[-1])) apk_file = apks[-1] info_main('# Found APK file: {}'.format(apk_file)) shprint(sh.cp, apk_file, './') @require_prebuilt_dist def create(self, args): '''Create a distribution directory if it doesn't already exist, run any recipes if necessary, and build the apk. ''' pass # The decorator does this for us # ctx = self.ctx # dist = dist_from_args(ctx, self.dist_args) # if not dist.needs_build: # info('You asked to create a distribution, but a dist with ' # 'this name already exists. If you don\'t want to use ' # 'it, you must delete it and rebuild, or create your ' # 'new dist with a different name.') # exit(1) # info('Ready to create dist {}, contains recipes {}'.format( # dist.name, ', '.join(dist.recipes))) # build_dist_from_args(ctx, dist, args) def print_context_info(self, args): '''Prints some debug information about which system paths python-for-android will internally use for package building, along with information about where the Android SDK and NDK will be called from.''' ctx = self.ctx for attribute in ('root_dir', 'build_dir', 'dist_dir', 'libs_dir', 'ccache', 'cython', 'sdk_dir', 'ndk_dir', 'ndk_platform', 'ndk_ver', 'android_api'): print('{} is {}'.format(attribute, getattr(ctx, attribute))) def archs(self, args): '''List the target architectures available to be built for.''' print('{Style.BRIGHT}Available target architectures are:' '{Style.RESET_ALL}'.format(Style=Out_Style)) for arch in self.ctx.archs: print(' {}'.format(arch.arch)) def dists(self, args): '''The same as :meth:`distributions`.''' self.distributions(args) def distributions(self, args): '''Lists all distributions currently available (i.e. that have already been built).''' ctx = self.ctx dists = Distribution.get_distributions(ctx) if dists: print('{Style.BRIGHT}Distributions currently installed are:' '{Style.RESET_ALL}'.format(Style=Out_Style, Fore=Out_Fore)) pretty_log_dists(dists, print) else: print('{Style.BRIGHT}There are no dists currently built.' '{Style.RESET_ALL}'.format(Style=Out_Style)) def delete_dist(self, args): dist = self._dist if dist.needs_build: info('No dist exists that matches your specifications, ' 'exiting without deleting.') shutil.rmtree(dist.dist_dir) def sdk_tools(self, args): '''Runs the android binary from the detected SDK directory, passing all arguments straight to it. This binary is used to install e.g. platform-tools for different API level targets. This is intended as a convenience function if android is not in your $PATH. ''' parser = argparse.ArgumentParser( description='Run a binary from the /path/to/sdk/tools directory') parser.add_argument('tool', help=('The tool binary name to run')) args, unknown = parser.parse_known_args(args) ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_ver=self.ndk_version) android = sh.Command(join(ctx.sdk_dir, 'tools', args.tool)) output = android( *unknown, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def adb(self, args): '''Runs the adb binary from the detected SDK directory, passing all arguments straight to it. This is intended as a convenience function if adb is not in your $PATH. ''' ctx = self.ctx ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir, user_ndk_dir=self.ndk_dir, user_android_api=self.android_api, user_ndk_ver=self.ndk_version) if platform in ('win32', 'cygwin'): adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb.exe')) else: adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb')) info_notify('Starting adb...') output = adb(args, _iter=True, _out_bufsize=1, _err_to_out=True) for line in output: sys.stdout.write(line) sys.stdout.flush() def logcat(self, args): '''Runs ``adb logcat`` using the adb binary from the detected SDK directory. All extra args are passed as arguments to logcat.''' self.adb(['logcat'] + args) def build_status(self, args): print('{Style.BRIGHT}Bootstraps whose core components are probably ' 'already built:{Style.RESET_ALL}'.format(Style=Out_Style)) for filen in os.listdir(join(self.ctx.build_dir, 'bootstrap_builds')): print(' {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}' .format(filen=filen, Fore=Out_Fore, Style=Out_Style)) print('{Style.BRIGHT}Recipes that are probably already built:' '{Style.RESET_ALL}'.format(Style=Out_Style)) if exists(join(self.ctx.build_dir, 'other_builds')): for filen in sorted( os.listdir(join(self.ctx.build_dir, 'other_builds'))): name = filen.split('-')[0] dependencies = filen.split('-')[1:] recipe_str = (' {Style.BRIGHT}{Fore.GREEN}{name}' '{Style.RESET_ALL}'.format( Style=Out_Style, name=name, Fore=Out_Fore)) if dependencies: recipe_str += ( ' ({Fore.BLUE}with ' + ', '.join(dependencies) + '{Fore.RESET})').format(Fore=Out_Fore) recipe_str += '{Style.RESET_ALL}'.format(Style=Out_Style) print(recipe_str)
def __init__(self): argv = sys.argv self.warn_on_carriage_return_args(argv) # Buildozer used to pass these arguments in a now-invalid order # If that happens, apply this fix # This fix will be removed once a fixed buildozer is released if (len(argv) > 2 and argv[1].startswith('--color') and argv[2].startswith('--storage-dir')): argv.append(argv.pop(1)) # the --color arg argv.append(argv.pop(1)) # the --storage-dir arg parser = NoAbbrevParser( description='A packaging tool for turning Python scripts and apps ' 'into Android APKs') generic_parser = argparse.ArgumentParser( add_help=False, description='Generic arguments applied to all commands') argparse.ArgumentParser( add_help=False, description='Arguments for dist building') generic_parser.add_argument( '--debug', dest='debug', action='store_true', default=False, help='Display debug output and all build info') generic_parser.add_argument( '--color', dest='color', choices=['always', 'never', 'auto'], help='Enable or disable color output (default enabled on tty)') generic_parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') generic_parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') generic_parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help=('The Android API level to build against defaults to {} if ' 'not specified.').format(RECOMMENDED_TARGET_API)) generic_parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default=None, help=('DEPRECATED: the NDK version is now found automatically or ' 'not at all.')) generic_parser.add_argument( '--ndk-api', type=int, default=None, help=('The Android API level to compile against. This should be your ' '*minimal supported* API, not normally the same as your --android-api. ' 'Defaults to min(ANDROID_API, {}) if not specified.').format(RECOMMENDED_NDK_API)) generic_parser.add_argument( '--symlink-java-src', '--symlink_java_src', action='store_true', dest='symlink_java_src', default=False, help=('If True, symlinks the java src folder during build and dist ' 'creation. This is useful for development only, it could also' ' cause weird problems.')) default_storage_dir = user_data_dir('python-for-android') if ' ' in default_storage_dir: default_storage_dir = '~/.python-for-android' generic_parser.add_argument( '--storage-dir', dest='storage_dir', default=default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(default_storage_dir))) generic_parser.add_argument( '--arch', help='The arch to build for.', default='armeabi-v7a') # Options for specifying the Distribution generic_parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') generic_parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules. NOT NECESSARY if you are using ' 'Python 3 with --use-setup-py'), default='') generic_parser.add_argument( '--recipe-blacklist', help=('Blacklist an internal recipe from use. Allows ' 'disabling Python 3 core modules to save size'), dest="recipe_blacklist", default='') generic_parser.add_argument( '--blacklist-requirements', help=('Blacklist an internal recipe from use. Allows ' 'disabling Python 3 core modules to save size'), dest="blacklist_requirements", default='') generic_parser.add_argument( '--bootstrap', help='The bootstrap to build with. Leave unset to choose ' 'automatically.', default=None) generic_parser.add_argument( '--hook', help='Filename to a module that contains python-for-android hooks', default=None) add_boolean_option( generic_parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution') add_boolean_option( generic_parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) add_boolean_option( generic_parser, ["allow-replace-dist"], default=True, description='Whether existing dist names can be automatically replaced' ) generic_parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') generic_parser.add_argument( '--java-build-tool', dest='java_build_tool', default='auto', choices=['auto', 'ant', 'gradle'], help=('The java build tool to use when packaging the APK, defaults ' 'to automatically selecting an appropriate tool.')) add_boolean_option( generic_parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)' ) self._read_configuration() subparsers = parser.add_subparsers(dest='subparser_name', help='The command to run') def add_parser(subparsers, *args, **kwargs): """ argparse in python2 doesn't support the aliases option, so we just don't provide the aliases there. """ if 'aliases' in kwargs and sys.version_info.major < 3: kwargs.pop('aliases') return subparsers.add_parser(*args, **kwargs) add_parser( subparsers, 'recommendations', parents=[generic_parser], help='List recommended p4a dependencies') parser_recipes = add_parser( subparsers, 'recipes', parents=[generic_parser], help='List the available recipes') parser_recipes.add_argument( "--compact", action="store_true", default=False, help="Produce a compact list suitable for scripting") add_parser( subparsers, 'bootstraps', help='List the available bootstraps', parents=[generic_parser]) add_parser( subparsers, 'clean_all', aliases=['clean-all'], help='Delete all builds, dists and caches', parents=[generic_parser]) add_parser( subparsers, 'clean_dists', aliases=['clean-dists'], help='Delete all dists', parents=[generic_parser]) add_parser( subparsers, 'clean_bootstrap_builds', aliases=['clean-bootstrap-builds'], help='Delete all bootstrap builds', parents=[generic_parser]) add_parser( subparsers, 'clean_builds', aliases=['clean-builds'], help='Delete all builds', parents=[generic_parser]) parser_clean = add_parser( subparsers, 'clean', help='Delete build components.', parents=[generic_parser]) parser_clean.add_argument( 'component', nargs='+', help=('The build component(s) to delete. You can pass any ' 'number of arguments from "all", "builds", "dists", ' '"distributions", "bootstrap_builds", "downloads".')) parser_clean_recipe_build = add_parser( subparsers, 'clean_recipe_build', aliases=['clean-recipe-build'], help=('Delete the build components of the given recipe. ' 'By default this will also delete built dists'), parents=[generic_parser]) parser_clean_recipe_build.add_argument( 'recipe', help='The recipe name') parser_clean_recipe_build.add_argument( '--no-clean-dists', default=False, dest='no_clean_dists', action='store_true', help='If passed, do not delete existing dists') parser_clean_download_cache = add_parser( subparsers, 'clean_download_cache', aliases=['clean-download-cache'], help='Delete cached downloads for requirement builds', parents=[generic_parser]) parser_clean_download_cache.add_argument( 'recipes', nargs='*', help='The recipes to clean (space-separated). If no recipe name is' ' provided, the entire cache is cleared.') parser_export_dist = add_parser( subparsers, 'export_dist', aliases=['export-dist'], help='Copy the named dist to the given path', parents=[generic_parser]) parser_export_dist.add_argument('output_dir', help='The output dir to copy to') parser_export_dist.add_argument( '--symlink', action='store_true', help='Symlink the dist instead of copying') parser_apk = add_parser( subparsers, 'apk', help='Build an APK', parents=[generic_parser]) # This is actually an internal argument of the build.py # (see pythonforandroid/bootstraps/common/build/build.py). # However, it is also needed before the distribution is finally # assembled for locating the setup.py / other build systems, which # is why we also add it here: parser_apk.add_argument( '--private', dest='private', help='the directory with the app source code files' + ' (containing your main.py entrypoint)', required=False, default=None) parser_apk.add_argument( '--release', dest='build_mode', action='store_const', const='release', default='debug', help='Build the PARSER_APK. in Release mode') parser_apk.add_argument( '--use-setup-py', dest="use_setup_py", action='store_true', default=False, help="Process the setup.py of a project if present. " + "(Experimental!") parser_apk.add_argument( '--ignore-setup-py', dest="ignore_setup_py", action='store_true', default=False, help="Don't run the setup.py of a project if present. " + "This may be required if the setup.py is not " + "designed to work inside p4a (e.g. by installing " + "dependencies that won't work or aren't desired " + "on Android") parser_apk.add_argument( '--keystore', dest='keystore', action='store', default=None, help=('Keystore for JAR signing key, will use jarsigner ' 'default if not specified (release build only)')) parser_apk.add_argument( '--signkey', dest='signkey', action='store', default=None, help='Key alias to sign PARSER_APK. with (release build only)') parser_apk.add_argument( '--keystorepw', dest='keystorepw', action='store', default=None, help='Password for keystore') parser_apk.add_argument( '--signkeypw', dest='signkeypw', action='store', default=None, help='Password for key alias') add_parser( subparsers, 'create', help='Compile a set of requirements into a dist', parents=[generic_parser]) add_parser( subparsers, 'archs', help='List the available target architectures', parents=[generic_parser]) add_parser( subparsers, 'distributions', aliases=['dists'], help='List the currently available (compiled) dists', parents=[generic_parser]) add_parser( subparsers, 'delete_dist', aliases=['delete-dist'], help='Delete a compiled dist', parents=[generic_parser]) parser_sdk_tools = add_parser( subparsers, 'sdk_tools', aliases=['sdk-tools'], help='Run the given binary from the SDK tools dis', parents=[generic_parser]) parser_sdk_tools.add_argument( 'tool', help='The binary tool name to run') add_parser( subparsers, 'adb', help='Run adb from the given SDK', parents=[generic_parser]) add_parser( subparsers, 'logcat', help='Run logcat from the given SDK', parents=[generic_parser]) add_parser( subparsers, 'build_status', aliases=['build-status'], help='Print some debug information about current built components', parents=[generic_parser]) parser.add_argument('-v', '--version', action='version', version=__version__) args, unknown = parser.parse_known_args(sys.argv[1:]) args.unknown_args = unknown if hasattr(args, "private") and args.private is not None: # Pass this value on to the internal bootstrap build.py: args.unknown_args += ["--private", args.private] if hasattr(args, "ignore_setup_py") and args.ignore_setup_py: args.use_setup_py = False self.args = args if args.subparser_name is None: parser.print_help() exit(1) setup_color(args.color) if args.debug: logger.setLevel(logging.DEBUG) self.ctx = Context() self.ctx.use_setup_py = getattr(args, "use_setup_py", True) have_setup_py_or_similar = False if getattr(args, "private", None) is not None: project_dir = getattr(args, "private") if (os.path.exists(os.path.join(project_dir, "setup.py")) or os.path.exists(os.path.join(project_dir, "pyproject.toml"))): have_setup_py_or_similar = True # Process requirements and put version in environ if hasattr(args, 'requirements'): requirements = [] # Add dependencies from setup.py, but only if they are recipes # (because otherwise, setup.py itself will install them later) if (have_setup_py_or_similar and getattr(args, "use_setup_py", False)): try: info("Analyzing package dependencies. MAY TAKE A WHILE.") # Get all the dependencies corresponding to a recipe: dependencies = [ dep.lower() for dep in get_dep_names_of_package( args.private, keep_version_pins=True, recursive=True, verbose=True, ) ] info("Dependencies obtained: " + str(dependencies)) all_recipes = [ recipe.lower() for recipe in set(Recipe.list_recipes(self.ctx)) ] dependencies = set(dependencies).intersection( set(all_recipes) ) # Add dependencies to argument list: if len(dependencies) > 0: if len(args.requirements) > 0: args.requirements += u"," args.requirements += u",".join(dependencies) except ValueError: # Not a python package, apparently. warning( "Processing failed, is this project a valid " "package? Will continue WITHOUT setup.py deps." ) # Parse --requirements argument list: for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) self.warn_on_deprecated_args(args) self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_api = args.ndk_api self.ctx.symlink_java_src = args.symlink_java_src self.ctx.java_build_tool = args.java_build_tool self._archs = split_argument_list(args.arch) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs # Each subparser corresponds to a method getattr(self, args.subparser_name.replace('-', '_'))(args)
def __init__(self): parser = argparse.ArgumentParser( description="Tool for managing the Android / Python toolchain", usage="""toolchain <command> [<args>] Available commands: adb Runs adb binary from the detected SDK dir apk Create an APK using the given distribution bootstraps List all the bootstraps available to build with. build_status Informations about the current build create Build an android project with all recipes clean_all Delete all build components clean_builds Delete all build caches clean_dists Delete all compiled distributions clean_download_cache Delete any downloaded recipe packages clean_recipe_build Delete the build files of a recipe distributions List all distributions export_dist Copies a created dist to an output directory logcat Runs logcat from the detected SDK dir print_context_info Prints debug informations recipes List all the available recipes sdk_tools Runs android binary from the detected SDK dir symlink_dist Symlinks a created dist to an output directory Planned commands: build_dist """) parser.add_argument("command", help="Command to run") # General options parser.add_argument( '--debug', dest='debug', action='store_true', help='Display debug output and all build info') parser.add_argument( '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='', help='The filepath where the Android SDK is installed') parser.add_argument( '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='', help='The filepath where the Android NDK is installed') parser.add_argument( '--android-api', '--android_api', dest='android_api', default=0, type=int, help='The Android API level to build against.') parser.add_argument( '--ndk-version', '--ndk_version', dest='ndk_version', default='', help=('The version of the Android NDK. This is optional, ' 'we try to work it out automatically from the ndk_dir.')) parser.add_argument( '--storage-dir', dest='storage_dir', default=self.default_storage_dir, help=('Primary storage directory for downloads and builds ' '(default: {})'.format(self.default_storage_dir))) # AND: This option doesn't really fit in the other categories, the # arg structure needs a rethink parser.add_argument( '--arch', help='The archs to build for, separated by commas.', default='armeabi') # Options for specifying the Distribution parser.add_argument( '--dist-name', '--dist_name', help='The name of the distribution to use or create', default='') parser.add_argument( '--requirements', help=('Dependencies of your app, should be recipe names or ' 'Python modules'), default='') add_boolean_option( parser, ["allow-download"], default=False, description='Whether to allow binary dist download:') add_boolean_option( parser, ["allow-build"], default=True, description='Whether to allow compilation of a new distribution:') add_boolean_option( parser, ["force-build"], default=False, description='Whether to force compilation of a new distribution:') parser.add_argument( '--extra-dist-dirs', '--extra_dist_dirs', dest='extra_dist_dirs', default='', help='Directories in which to look for distributions') add_boolean_option( parser, ["require-perfect-match"], default=False, description=('Whether the dist recipes must perfectly match ' 'those requested')) parser.add_argument( '--local-recipes', '--local_recipes', dest='local_recipes', default='./p4a-recipes', help='Directory to look for local recipes') add_boolean_option( parser, ['copy-libs'], default=False, description='Copy libraries instead of using biglink (Android 4.3+)') self._read_configuration() args, unknown = parser.parse_known_args(sys.argv[1:]) self.dist_args = args # strip version from requirements, and put them in environ requirements = [] for requirement in split_argument_list(args.requirements): if "==" in requirement: requirement, version = requirement.split(u"==", 1) os.environ["VERSION_{}".format(requirement)] = version info('Recipe {}: version "{}" requested'.format( requirement, version)) requirements.append(requirement) args.requirements = u",".join(requirements) if args.debug: logger.setLevel(logging.DEBUG) self.ctx = Context() self.storage_dir = args.storage_dir self.ctx.setup_dirs(self.storage_dir) self.sdk_dir = args.sdk_dir self.ndk_dir = args.ndk_dir self.android_api = args.android_api self.ndk_version = args.ndk_version self._archs = split_argument_list(args.arch) # AND: Fail nicely if the args aren't handled yet if args.extra_dist_dirs: warning('Received --extra_dist_dirs but this arg currently is not ' 'handled, exiting.') exit(1) if args.allow_download: warning('Received --allow_download but this arg currently is not ' 'handled, exiting.') exit(1) # if args.allow_build: # warning('Received --allow_build but this arg currently is not ' # 'handled, exiting.') # exit(1) if not hasattr(self, args.command): print('Unrecognized command') parser.print_help() exit(1) self.ctx.local_recipes = args.local_recipes self.ctx.copy_libs = args.copy_libs getattr(self, args.command)(unknown)