Skip to content

Commit f74c546

Browse files
committed
Made Python 2 check more robust
1 parent 32c707d commit f74c546

File tree

7 files changed

+58
-28
lines changed

7 files changed

+58
-28
lines changed

pythonforandroid/bdistapk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def finalize_options(self):
7575
def run(self):
7676
self.prepare_build_dir()
7777

78-
from pythonforandroid.toolchain import main
78+
from pythonforandroid.entrypoints import main
7979
sys.argv[1] = 'apk'
8080
main()
8181

pythonforandroid/entrypoints.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pythonforandroid.recommendations import check_python_version
2+
from pythonforandroid.util import BuildInterruptingException, handle_build_exception
3+
4+
def main():
5+
"""
6+
Main entrypoint for running python-for-android as a script.
7+
"""
8+
9+
try:
10+
# Check the Python version before importing anything heavier than
11+
# the util functions. This lets us provide a nice message about
12+
# incompatibility rather than having the interpreter crash if it
13+
# reaches unsupported syntax from a newer Python version.
14+
check_python_version()
15+
16+
from pythonforandroid.toolchain import ToolchainCL
17+
ToolchainCL()
18+
except BuildInterruptingException as exc:
19+
handle_build_exception(exc)

pythonforandroid/logger.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from math import log10
77
from collections import defaultdict
88
from colorama import Style as Colo_Style, Fore as Colo_Fore
9+
10+
# six import left for Python 2 compatibility during initial Python version check
911
import six
1012

1113
# This codecs change fixes a bug with log output, but crashes under python3

pythonforandroid/recommendations.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Simple functions for checking dependency versions."""
22

3+
import sys
34
from distutils.version import LooseVersion
45
from os.path import join
6+
57
from pythonforandroid.logger import info, warning
68
from pythonforandroid.util import BuildInterruptingException
79

@@ -93,7 +95,6 @@ def check_target_api(api, arch):
9395
RECOMMENDED_NDK_API = 21
9496
OLD_NDK_API_MESSAGE = ('NDK API less than {} is not supported'.format(MIN_NDK_API))
9597

96-
9798
def check_ndk_api(ndk_api, android_api):
9899
"""Warn if the user's NDK is too high or low."""
99100
if ndk_api > android_api:
@@ -105,3 +106,28 @@ def check_ndk_api(ndk_api, android_api):
105106

106107
if ndk_api < MIN_NDK_API:
107108
warning(OLD_NDK_API_MESSAGE)
109+
110+
MIN_PYTHON_MAJOR_VERSION = 3
111+
MIN_PYTHON_MINOR_VERSION = 6
112+
MIN_PYTHON_VERSION = LooseVersion('{major}.{minor}'.format(major=MIN_PYTHON_MAJOR_VERSION,
113+
minor=MIN_PYTHON_MINOR_VERSION))
114+
PY2_ERROR_TEXT = (
115+
'python-for-android no longer supports running under Python 2. Either upgrade to '
116+
'Python {min_version} (recommended), or revert to python-for-android 2019.07.08. Note that '
117+
'you *can* still target Python 2 on Android by including python2 in your requirements.').format(
118+
min_version=MIN_PYTHON_VERSION)
119+
120+
PY_MINOR_VERSION_ERROR_TEXT = (
121+
'Your Python version {user_major}.{user_minor} is not supported by python-for-android, '
122+
'please upgrade to {min_version} or higher.'
123+
).format(
124+
user_major=sys.version_info.major,
125+
user_minor=sys.version_info.minor,
126+
min_version=MIN_PYTHON_VERSION)
127+
128+
def check_python_version():
129+
if sys.version_info.major < MIN_PYTHON_MAJOR_VERSION:
130+
raise BuildInterruptingException(PY2_ERROR_TEXT)
131+
132+
if sys.version_info.minor < MIN_PYTHON_MINOR_VERSION:
133+
raise BuildInterruptingException(PY_MINOR_VERSION_ERROR_TEXT)

pythonforandroid/toolchain.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
from pythonforandroid import __version__
1212
from pythonforandroid.pythonpackage import get_dep_names_of_package
1313
from pythonforandroid.recommendations import (
14-
RECOMMENDED_NDK_API, RECOMMENDED_TARGET_API)
14+
RECOMMENDED_NDK_API, RECOMMENDED_TARGET_API, check_python_version)
1515
from pythonforandroid.util import BuildInterruptingException, handle_build_exception
16+
from pythonforandroid.entrypoints import main
1617

1718

1819
def check_python_dependencies():
@@ -100,14 +101,6 @@ def check_python_dependencies():
100101
toolchain_dir = dirname(__file__)
101102
sys.path.insert(0, join(toolchain_dir, "tools", "external"))
102103

103-
def check_python_version():
104-
py2_text = (
105-
'python-for-android no longer supports running under Python 2. Either upgrade to '
106-
'Python 3 (recommended), or revert to python-for-android 2019.07.08. Note that '
107-
'you *can* still target Python 2 on Android by including python2 in your requirements.')
108-
if sys.version_info.major < 3:
109-
raise BuildInterruptingException(py2_text)
110-
111104
def add_boolean_option(parser, names, no_names=None,
112105
default=True, dest=None, description=None):
113106
group = parser.add_argument_group(description=description)
@@ -576,8 +569,6 @@ def add_parser(subparsers, *args, **kwargs):
576569
args, unknown = parser.parse_known_args(sys.argv[1:])
577570
args.unknown_args = unknown
578571

579-
check_python_version()
580-
581572
if hasattr(args, "private") and args.private is not None:
582573
# Pass this value on to the internal bootstrap build.py:
583574
args.unknown_args += ["--private", args.private]
@@ -1188,12 +1179,5 @@ def build_status(self, _args):
11881179
print(recipe_str)
11891180

11901181

1191-
def main():
1192-
try:
1193-
ToolchainCL()
1194-
except BuildInterruptingException as exc:
1195-
handle_build_exception(exc)
1196-
1197-
11981182
if __name__ == "__main__":
11991183
main()

pythonforandroid/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import sys
77
from fnmatch import fnmatch
88
from tempfile import mkdtemp
9-
try:
9+
10+
# This Python version workaround left for compatibility during initial version check
11+
try: # Python 3
1012
from urllib.request import FancyURLopener
11-
except ImportError:
13+
except ImportError: # Python 2
1214
from urllib import FancyURLopener
1315

1416
from pythonforandroid.logger import (logger, Err_Fore, error, info)
1517

16-
IS_PY3 = sys.version_info[0] >= 3
17-
1818

1919
class WgetDownloader(FancyURLopener):
2020
version = ('Wget/1.17.1')

setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ def recursively_include(results, directory, patterns):
9494
install_requires=install_reqs,
9595
entry_points={
9696
'console_scripts': [
97-
'python-for-android = pythonforandroid.toolchain:main',
98-
'p4a = pythonforandroid.toolchain:main',
97+
'python-for-android = pythonforandroid.entrypoints:main',
98+
'p4a = pythonforandroid.entrypoints:main',
9999
],
100100
'distutils.commands': [
101101
'apk = pythonforandroid.bdistapk:BdistAPK',
102102
],
103103
},
104104
classifiers = [
105-
'Development Status :: 4 - Beta',
105+
'Development Status :: 5 - Production/Stable',
106106
'Intended Audience :: Developers',
107107
'License :: OSI Approved :: MIT License',
108108
'Operating System :: Microsoft :: Windows',
@@ -111,7 +111,6 @@ def recursively_include(results, directory, patterns):
111111
'Operating System :: MacOS :: MacOS X',
112112
'Operating System :: Android',
113113
'Programming Language :: C',
114-
'Programming Language :: Python :: 2',
115114
'Programming Language :: Python :: 3',
116115
'Topic :: Software Development',
117116
'Topic :: Utilities',

0 commit comments

Comments
 (0)