From 22b7dfee1ddc5f435f8bf016cbf83980a45b4374 Mon Sep 17 00:00:00 2001 From: Andre Miras Date: Mon, 30 Mar 2020 23:10:49 +0200 Subject: [PATCH] Minor fixes and cleanup - remove `object` inheritance - remove `__future__` imports - remove `six` checks and conditional imports - flat out a few for loops - fix `dir` is a reserved keyword - few more nitpick fixes --- pythonforandroid/archs.py | 3 +- pythonforandroid/bdistapk.py | 1 - pythonforandroid/bootstrap.py | 13 ++-- .../bootstraps/common/build/build.py | 2 - pythonforandroid/build.py | 61 ++++++++----------- pythonforandroid/distribution.py | 2 +- pythonforandroid/graph.py | 2 +- pythonforandroid/logger.py | 21 ++----- pythonforandroid/recipe.py | 31 ++++------ pythonforandroid/recipes/android/__init__.py | 1 - pythonforandroid/toolchain.py | 3 +- pythonforandroid/tools/biglink | 1 - pythonforandroid/tools/liblink | 26 +------- pythonforandroid/util.py | 6 +- tests/test_bootstrap.py | 11 ++-- 15 files changed, 58 insertions(+), 126 deletions(-) diff --git a/pythonforandroid/archs.py b/pythonforandroid/archs.py index fb991eacf0..f1c8e1121b 100644 --- a/pythonforandroid/archs.py +++ b/pythonforandroid/archs.py @@ -8,7 +8,7 @@ from pythonforandroid.util import BuildInterruptingException, build_platform -class Arch(object): +class Arch: toolchain_prefix = None '''The prefix for the toolchain dir in the NDK.''' @@ -46,7 +46,6 @@ class Arch(object): ] def __init__(self, ctx): - super().__init__() self.ctx = ctx # Allows injecting additional linker paths used by any recipe. diff --git a/pythonforandroid/bdistapk.py b/pythonforandroid/bdistapk.py index ea1b78ca2f..d22c9947a4 100644 --- a/pythonforandroid/bdistapk.py +++ b/pythonforandroid/bdistapk.py @@ -1,4 +1,3 @@ -from __future__ import print_function from setuptools import Command import sys diff --git a/pythonforandroid/bootstrap.py b/pythonforandroid/bootstrap.py index e85dc74732..75b82e555f 100755 --- a/pythonforandroid/bootstrap.py +++ b/pythonforandroid/bootstrap.py @@ -63,7 +63,7 @@ def rank_bootstrap(bootstrap): return 1 -class Bootstrap(object): +class Bootstrap: '''An Android project template, containing recipe stuff for compilation and templated fields for APK info. ''' @@ -294,15 +294,15 @@ def distribute_libs(self, arch, src_dirs, wildcard='*', dest_dir="libs"): tgt_dir = join(dest_dir, arch.arch) ensure_dir(tgt_dir) for src_dir in src_dirs: - for lib in glob.glob(join(src_dir, wildcard)): - shprint(sh.cp, '-a', lib, tgt_dir) + libs = glob.glob(join(src_dir, wildcard)) + shprint(sh.cp, '-a', *libs, tgt_dir) def distribute_javaclasses(self, javaclass_dir, dest_dir="src"): '''Copy existing javaclasses from build dir to current dist dir.''' info('Copying java files') ensure_dir(dest_dir) - for filename in glob.glob(javaclass_dir): - shprint(sh.cp, '-a', filename, dest_dir) + filenames = glob.glob(javaclass_dir) + shprint(sh.cp, '-a', *filenames, dest_dir) def distribute_aars(self, arch): '''Process existing .aar bundles and copy to current dist dir.''' @@ -335,8 +335,7 @@ def _unpack_aar(self, aar, arch): debug(" to {}".format(so_tgt_dir)) ensure_dir(so_tgt_dir) so_files = glob.glob(join(so_src_dir, '*.so')) - for f in so_files: - shprint(sh.cp, '-a', f, so_tgt_dir) + shprint(sh.cp, '-a', *so_files, so_tgt_dir) def strip_libraries(self, arch): info('Stripping libraries') diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index fea791db8d..cb859cf832 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 -from __future__ import print_function - import json from os.path import ( dirname, join, isfile, realpath, diff --git a/pythonforandroid/build.py b/pythonforandroid/build.py index f19ddc9e96..6558e29c56 100644 --- a/pythonforandroid/build.py +++ b/pythonforandroid/build.py @@ -1,5 +1,3 @@ -from __future__ import print_function - from os.path import ( abspath, join, realpath, dirname, expanduser, exists, split, isdir @@ -79,7 +77,7 @@ def get_available_apis(sdk_dir): return apis -class Context(object): +class Context: '''A build context. If anything will be built, an instance this class will be instantiated and used to hold all the build state.''' @@ -132,34 +130,33 @@ def templates_dir(self): @property def libs_dir(self): # Was previously hardcoded as self.build_dir/libs - dir = join(self.build_dir, 'libs_collections', - self.bootstrap.distribution.name) - ensure_dir(dir) - return dir + directory = join(self.build_dir, 'libs_collections', + self.bootstrap.distribution.name) + ensure_dir(directory) + return directory @property def javaclass_dir(self): # Was previously hardcoded as self.build_dir/java - dir = join(self.build_dir, 'javaclasses', - self.bootstrap.distribution.name) - ensure_dir(dir) - return dir + directory = join(self.build_dir, 'javaclasses', + self.bootstrap.distribution.name) + ensure_dir(directory) + return directory @property def aars_dir(self): - dir = join(self.build_dir, 'aars', self.bootstrap.distribution.name) - ensure_dir(dir) - return dir + directory = join(self.build_dir, 'aars', self.bootstrap.distribution.name) + ensure_dir(directory) + return directory @property def python_installs_dir(self): - dir = join(self.build_dir, 'python-installs') - ensure_dir(dir) - return dir + directory = join(self.build_dir, 'python-installs') + ensure_dir(directory) + return directory def get_python_install_dir(self): - dir = join(self.python_installs_dir, self.bootstrap.distribution.name) - return dir + return join(self.python_installs_dir, self.bootstrap.distribution.name) def setup_dirs(self, storage_dir): '''Calculates all the storage and build dirs, and makes sure @@ -264,7 +261,7 @@ def prepare_build_environment(self, possible_dirs = glob.glob(expanduser(join( '~', '.buildozer', 'android', 'platform', 'android-sdk-*'))) possible_dirs = [d for d in possible_dirs if not - (d.endswith('.bz2') or d.endswith('.gz'))] + d.endswith(('.bz2', '.gz'))] if possible_dirs: info('Found possible SDK dirs in buildozer dir: {}'.format( ', '.join([d.split(os.sep)[-1] for d in possible_dirs]))) @@ -427,15 +424,13 @@ def prepare_build_environment(self, for executable in ("pkg-config", "autoconf", "automake", "libtoolize", "tar", "bzip2", "unzip", "make", "gcc", "g++"): if not sh.which(executable): - warning("Missing executable: {} is not installed".format( - executable)) + warning(f"Missing executable: {executable} is not installed") if not ok: raise BuildInterruptingException( 'python-for-android cannot continue due to the missing executables above') def __init__(self): - super().__init__() self.include_dirs = [] self._build_env_prepared = False @@ -605,23 +600,16 @@ def build_recipes(build_order, python_modules, ctx, project_dir, ignore_setup_py=ignore_project_setup_py ) - return - def project_has_setup_py(project_dir): - if project_dir is not None and \ - (os.path.exists(os.path.join(project_dir, - "setup.py")) or - os.path.exists(os.path.join(project_dir, - "pyproject.toml")) - ): - return True - return False + return (project_dir is not None and + (exists(join(project_dir, "setup.py")) or + exists(join(project_dir, "pyproject.toml")) + )) def run_setuppy_install(ctx, project_dir, env=None): - if env is None: - env = dict() + env = env or {} with current_directory(project_dir): info('got setup.py or similar, running project install. ' + @@ -1076,5 +1064,4 @@ def copylibs_function(soname, objs_paths, extra_link_dirs=[], env=None): '\n\t'.join(needed_libs)) print('Copying libraries') - for lib in sofiles: - shprint(sh.cp, lib, dest) + shprint(sh.cp, *sofiles, dest) diff --git a/pythonforandroid/distribution.py b/pythonforandroid/distribution.py index 379cd90852..8607766eba 100644 --- a/pythonforandroid/distribution.py +++ b/pythonforandroid/distribution.py @@ -7,7 +7,7 @@ from shutil import rmtree -class Distribution(object): +class Distribution: '''State container for information about a distribution (i.e. an Android project). diff --git a/pythonforandroid/graph.py b/pythonforandroid/graph.py index 2e98e8ccda..567c77fa3b 100644 --- a/pythonforandroid/graph.py +++ b/pythonforandroid/graph.py @@ -160,7 +160,7 @@ def obvious_conflict_checker(ctx, name_tuples, blacklist=None): current_to_be_added = list(to_be_added) to_be_added = [] for (added_tuple, adding_recipe) in current_to_be_added: - assert(type(added_tuple) == tuple) + assert type(added_tuple) == tuple if len(added_tuple) > 1: # No obvious commitment in what to add, don't check it itself # but throw it into deps for later comparing against diff --git a/pythonforandroid/logger.py b/pythonforandroid/logger.py index 193356a2a8..8f0b15ca31 100644 --- a/pythonforandroid/logger.py +++ b/pythonforandroid/logger.py @@ -7,19 +7,6 @@ from collections import defaultdict from colorama import Style as Colo_Style, Fore as Colo_Fore -# six import left for Python 2 compatibility during initial Python version check -import six - -# This codecs change fixes a bug with log output, but crashes under python3 -if not six.PY3: - import codecs - stdout = codecs.getwriter('utf8')(stdout) - stderr = codecs.getwriter('utf8')(stderr) - -if six.PY2: - unistr = unicode # noqa F821 -else: - unistr = str # monkey patch to show full output sh.ErrorReturnCode.truncate_cap = 999999 @@ -61,7 +48,7 @@ def format(self, record): error = logger.error -class colorama_shim(object): +class colorama_shim: def __init__(self, real): self._dict = defaultdict(str) @@ -114,12 +101,12 @@ def shorten_string(string, max_width): return string visible = max_width - 16 - int(log10(string_len)) # expected suffix len "...(and XXXXX more)" - if not isinstance(string, unistr): - visstring = unistr(string[:visible], errors='ignore') + if not isinstance(string, str): + visstring = str(string[:visible], errors='ignore') else: visstring = string[:visible] return u''.join((visstring, u'...(and ', - unistr(string_len - visible), u' more)')) + str(string_len - visible), u' more)')) def get_console_width(): diff --git a/pythonforandroid/recipe.py b/pythonforandroid/recipe.py index 2723cda3f1..7a429de6ca 100644 --- a/pythonforandroid/recipe.py +++ b/pythonforandroid/recipe.py @@ -1,7 +1,7 @@ from os.path import basename, dirname, exists, isdir, isfile, join, realpath, split import glob from shutil import rmtree -from six import PY2, with_metaclass +from six import with_metaclass import hashlib from re import match @@ -22,26 +22,17 @@ def import_recipe(module, filename): - if PY2: - import imp - import warnings - with warnings.catch_warnings(): - # ignores warnings raised by hierarchical module names - # (names containing dots) on Python 2 - warnings.simplefilter("ignore", RuntimeWarning) - return imp.load_source(module, filename) + # Python 3.5+ + import importlib.util + if hasattr(importlib.util, 'module_from_spec'): + spec = importlib.util.spec_from_file_location(module, filename) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod else: - # Python 3.5+ - import importlib.util - if hasattr(importlib.util, 'module_from_spec'): - spec = importlib.util.spec_from_file_location(module, filename) - mod = importlib.util.module_from_spec(spec) - spec.loader.exec_module(mod) - return mod - else: - # Python 3.3 and 3.4: - from importlib.machinery import SourceFileLoader - return SourceFileLoader(module, filename).load_module() + # Python 3.3 and 3.4: + from importlib.machinery import SourceFileLoader + return SourceFileLoader(module, filename).load_module() class RecipeMeta(type): diff --git a/pythonforandroid/recipes/android/__init__.py b/pythonforandroid/recipes/android/__init__.py index 7fe3e3517d..6d196fe321 100644 --- a/pythonforandroid/recipes/android/__init__.py +++ b/pythonforandroid/recipes/android/__init__.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals from pythonforandroid.recipe import CythonRecipe, IncludedFilesBehaviour from pythonforandroid.util import current_directory from pythonforandroid import logger diff --git a/pythonforandroid/toolchain.py b/pythonforandroid/toolchain.py index b4236d8860..592bf1cc3b 100644 --- a/pythonforandroid/toolchain.py +++ b/pythonforandroid/toolchain.py @@ -6,7 +6,6 @@ This module defines the entry point for command line and programmatic use. """ -from __future__ import print_function from os import environ from pythonforandroid import __version__ from pythonforandroid.pythonpackage import get_dep_names_of_package @@ -235,7 +234,7 @@ def _get_option_tuples(self, option_string): return [] -class ToolchainCL(object): +class ToolchainCL: def __init__(self): diff --git a/pythonforandroid/tools/biglink b/pythonforandroid/tools/biglink index e2652d52c9..8a8e561fba 100755 --- a/pythonforandroid/tools/biglink +++ b/pythonforandroid/tools/biglink @@ -1,6 +1,5 @@ #!/usr/bin/env python -from __future__ import print_function import os import sys import subprocess diff --git a/pythonforandroid/tools/liblink b/pythonforandroid/tools/liblink index 182eff7a6c..de837e6ca5 100755 --- a/pythonforandroid/tools/liblink +++ b/pythonforandroid/tools/liblink @@ -1,6 +1,5 @@ #!/usr/bin/env python -from __future__ import print_function import sys import subprocess from os import environ @@ -22,7 +21,7 @@ while i < len(sys.argv): i += 1 continue - if opt.startswith("-l") or opt.startswith("-L"): + if opt.startswith(("-l", "-L")): libs.append(opt) continue @@ -34,27 +33,8 @@ while i < len(sys.argv): i += 1 continue - if opt.startswith("-I") or opt.startswith('-isystem'): - continue - - if opt.startswith("-m"): - continue - - if opt.startswith("-f"): - continue - - if opt.startswith("-O"): - continue - - if opt.startswith("-g"): - continue - - if opt.startswith("-D"): - continue - - if opt.startswith("-R"): - # for -rpath, not implemented yet. - continue + if opt.startswith( + ("-I", "-isystem", "-m", "-f", "-O", "-g", "-D", "-R")): if opt.startswith("-"): print(sys.argv) diff --git a/pythonforandroid/util.py b/pythonforandroid/util.py index d4719ba160..80a607f5fe 100644 --- a/pythonforandroid/util.py +++ b/pythonforandroid/util.py @@ -6,11 +6,7 @@ from fnmatch import fnmatch from tempfile import mkdtemp -# This Python version workaround left for compatibility during initial version check -try: # Python 3 - from urllib.request import FancyURLopener -except ImportError: # Python 2 - from urllib import FancyURLopener +from urllib.request import FancyURLopener from pythonforandroid.logger import (logger, Err_Fore, error, info) diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index d3bc9a19d4..583cefb81c 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -493,12 +493,11 @@ def reset_mocks(): libs_dir = os.path.join("libs", arch.arch) # we expect two calls to glob/copy command via shprint self.assertEqual(len(mock_glob.call_args_list), 2) - self.assertEqual(len(mock_shprint.call_args_list), 2) - for i, lib in enumerate(mock_glob.return_value): - self.assertEqual( - mock_shprint.call_args_list[i], - mock.call(sh.cp, "-a", lib, libs_dir), - ) + self.assertEqual(len(mock_shprint.call_args_list), 1) + self.assertEqual( + mock_shprint.call_args_list, + [mock.call(sh.cp, "-a", *mock_glob.return_value, libs_dir)] + ) mock_build_dir.assert_called() mock_bs_dir.assert_called_once_with(libs_dir) reset_mocks()