Skip to content

Commit 6688562

Browse files
committed
[recommendations] Refactor important log messages
So it will be easier for us to maintain the tests for recommendations module
1 parent 6a8b3cb commit 6688562

File tree

2 files changed

+98
-59
lines changed

2 files changed

+98
-59
lines changed

pythonforandroid/recommendations.py

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,38 @@
99
MIN_NDK_VERSION = 17
1010
MAX_NDK_VERSION = 17
1111

12-
RECOMMENDED_NDK_VERSION = '17c'
12+
RECOMMENDED_NDK_VERSION = "17c"
13+
NDK_DOWNLOAD_URL = "https://developer.android.com/ndk/downloads/"
14+
15+
# Important log messages
1316
NEW_NDK_MESSAGE = 'Newer NDKs may not be fully supported by p4a.'
14-
NDK_DOWNLOAD_URL = 'https://developer.android.com/ndk/downloads/'
17+
UNKNOWN_NDK_MESSAGE = (
18+
'Could not determine NDK version, no source.properties in the NDK dir'
19+
)
20+
PARSE_ERROR_NDK_MESSAGE = (
21+
'Could not parse $NDK_DIR/source.properties, not checking NDK version'
22+
)
23+
READ_ERROR_NDK_MESSAGE = (
24+
'Unable to read the NDK version from the given directory {ndk_dir}'
25+
)
26+
ENSURE_RIGHT_NDK_MESSAGE = (
27+
'Make sure your NDK version is greater than {min_supported}. If you get '
28+
'build errors, download the recommended NDK {rec_version} from {ndk_url}'
29+
)
30+
NDK_LOWER_THAN_SUPPORTED_MESSAGE = (
31+
'The minimum supported NDK version is {min_supported}. '
32+
'You can download it from {ndk_url}'
33+
)
34+
UNSUPPORTED_NDK_API_FOR_ARMEABI_MESSAGE = (
35+
'Asked to build for armeabi architecture with API '
36+
'{req_ndk_api}, but API {max_ndk_api} or greater does not support armeabi'
37+
)
38+
CURRENT_NDK_VERSION_MESSAGE = (
39+
'Found NDK version {ndk_version}'
40+
)
41+
RECOMMENDED_NDK_VERSION_MESSAGE = (
42+
'Maximum recommended NDK version is {recommended_ndk_version}'
43+
)
1544

1645

1746
def check_ndk_version(ndk_dir):
@@ -29,14 +58,9 @@ def check_ndk_version(ndk_dir):
2958
version = read_ndk_version(ndk_dir)
3059

3160
if version is None:
61+
warning(READ_ERROR_NDK_MESSAGE.format(ndk_dir=ndk_dir))
3262
warning(
33-
'Unable to read the NDK version from the given directory '
34-
'{}'.format(ndk_dir)
35-
)
36-
warning(
37-
"Make sure your NDK version is greater than {min_supported}. "
38-
"If you get build errors, download the recommended NDK "
39-
"{rec_version} from {ndk_url}".format(
63+
ENSURE_RIGHT_NDK_MESSAGE.format(
4064
min_supported=MIN_NDK_VERSION,
4165
rec_version=RECOMMENDED_NDK_VERSION,
4266
ndk_url=NDK_DOWNLOAD_URL,
@@ -60,13 +84,12 @@ def check_ndk_version(ndk_dir):
6084
major_version=major_version, letter_version=letter_version
6185
)
6286

63-
info('Found NDK version {}'.format(string_version))
87+
info(CURRENT_NDK_VERSION_MESSAGE.format(ndk_version=string_version))
6488

6589
if major_version < MIN_NDK_VERSION:
6690
raise BuildInterruptingException(
67-
'The minimum supported NDK version is {min_supported}. You can '
68-
'download it from {ndk_url}'.format(
69-
min_supported=MIN_NDK_VERSION, ndk_url=NDK_DOWNLOAD_URL,
91+
NDK_LOWER_THAN_SUPPORTED_MESSAGE.format(
92+
min_supported=MIN_NDK_VERSION, ndk_url=NDK_DOWNLOAD_URL
7093
),
7194
instructions=(
7295
'Please, go to the android NDK page ({ndk_url}) and download a'
@@ -79,8 +102,8 @@ def check_ndk_version(ndk_dir):
79102
)
80103
elif major_version > MAX_NDK_VERSION:
81104
warning(
82-
'Maximum recommended NDK version is {}'.format(
83-
RECOMMENDED_NDK_VERSION
105+
RECOMMENDED_NDK_VERSION_MESSAGE.format(
106+
recommended_ndk_version=RECOMMENDED_NDK_VERSION
84107
)
85108
)
86109
warning(NEW_NDK_MESSAGE)
@@ -92,16 +115,14 @@ def read_ndk_version(ndk_dir):
92115
with open(join(ndk_dir, 'source.properties')) as fileh:
93116
ndk_data = fileh.read()
94117
except IOError:
95-
info('Could not determine NDK version, no source.properties '
96-
'in the NDK dir')
118+
info(UNKNOWN_NDK_MESSAGE)
97119
return
98120

99121
for line in ndk_data.split('\n'):
100122
if line.startswith('Pkg.Revision'):
101123
break
102124
else:
103-
info('Could not parse $NDK_DIR/source.properties, not checking '
104-
'NDK version')
125+
info(PARSE_ERROR_NDK_MESSAGE)
105126
return
106127

107128
# Line should have the form "Pkg.Revision = ..."
@@ -130,9 +151,9 @@ def check_target_api(api, arch):
130151

131152
if api >= ARMEABI_MAX_TARGET_API and arch == 'armeabi':
132153
raise BuildInterruptingException(
133-
'Asked to build for armeabi architecture with API '
134-
'{}, but API {} or greater does not support armeabi'.format(
135-
api, ARMEABI_MAX_TARGET_API),
154+
UNSUPPORTED_NDK_API_FOR_ARMEABI_MESSAGE.format(
155+
req_ndk_api=api, max_ndk_api=ARMEABI_MAX_TARGET_API
156+
),
136157
instructions='You probably want to build with --arch=armeabi-v7a instead')
137158

138159
if api < MIN_TARGET_API:
@@ -143,14 +164,19 @@ def check_target_api(api, arch):
143164
MIN_NDK_API = 21
144165
RECOMMENDED_NDK_API = 21
145166
OLD_NDK_API_MESSAGE = ('NDK API less than {} is not supported'.format(MIN_NDK_API))
167+
TARGET_NDK_API_GREATER_THAN_TARGET_API_MESSAGE = (
168+
'Target NDK API is {ndk_api}, '
169+
'higher than the target Android API {android_api}.'
170+
)
146171

147172

148173
def check_ndk_api(ndk_api, android_api):
149174
"""Warn if the user's NDK is too high or low."""
150175
if ndk_api > android_api:
151176
raise BuildInterruptingException(
152-
'Target NDK API is {}, higher than the target Android API {}.'.format(
153-
ndk_api, android_api),
177+
TARGET_NDK_API_GREATER_THAN_TARGET_API_MESSAGE.format(
178+
ndk_api=ndk_api, android_api=android_api
179+
),
154180
instructions=('The NDK API is a minimum supported API number and must be lower '
155181
'than the target Android API'))
156182

tests/test_recommendations.py

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@
2121
NDK_DOWNLOAD_URL,
2222
ARMEABI_MAX_TARGET_API,
2323
MIN_TARGET_API,
24+
UNKNOWN_NDK_MESSAGE,
25+
PARSE_ERROR_NDK_MESSAGE,
26+
READ_ERROR_NDK_MESSAGE,
27+
ENSURE_RIGHT_NDK_MESSAGE,
28+
NDK_LOWER_THAN_SUPPORTED_MESSAGE,
29+
UNSUPPORTED_NDK_API_FOR_ARMEABI_MESSAGE,
30+
CURRENT_NDK_VERSION_MESSAGE,
31+
RECOMMENDED_NDK_VERSION_MESSAGE,
32+
TARGET_NDK_API_GREATER_THAN_TARGET_API_MESSAGE,
33+
OLD_NDK_API_MESSAGE,
34+
NEW_NDK_MESSAGE,
35+
OLD_API_MESSAGE,
2436
)
2537
from pythonforandroid.util import BuildInterruptingException
38+
2639
running_in_py2 = int(py_version[0]) < 3
2740

2841

@@ -45,15 +58,17 @@ def test_check_ndk_version_greater_than_recommended(self, mock_read_ndk):
4558
self.assertEqual(
4659
cm.output,
4760
[
48-
"INFO:p4a:[INFO]: Found NDK version {ndk_current}".format(
49-
ndk_current=MAX_NDK_VERSION + 1
61+
"INFO:p4a:[INFO]: {}".format(
62+
CURRENT_NDK_VERSION_MESSAGE.format(
63+
ndk_version=MAX_NDK_VERSION + 1
64+
)
5065
),
51-
"WARNING:p4a:[WARNING]:"
52-
" Maximum recommended NDK version is {ndk_recommended}".format(
53-
ndk_recommended=RECOMMENDED_NDK_VERSION
66+
"WARNING:p4a:[WARNING]: {}".format(
67+
RECOMMENDED_NDK_VERSION_MESSAGE.format(
68+
recommended_ndk_version=RECOMMENDED_NDK_VERSION
69+
)
5470
),
55-
"WARNING:p4a:[WARNING]:"
56-
" Newer NDKs may not be fully supported by p4a.",
71+
"WARNING:p4a:[WARNING]: {}".format(NEW_NDK_MESSAGE),
5772
],
5873
)
5974

@@ -64,9 +79,8 @@ def test_check_ndk_version_lower_than_recommended(self, mock_read_ndk):
6479
check_ndk_version(self.ndk_dir)
6580
self.assertEqual(
6681
e.exception.args[0],
67-
"Unsupported NDK version detected {ndk_current}"
68-
"\n* Note: Minimum supported NDK version is {ndk_min}".format(
69-
ndk_current=MIN_NDK_VERSION - 1, ndk_min=MIN_NDK_VERSION
82+
NDK_LOWER_THAN_SUPPORTED_MESSAGE.format(
83+
min_supported=MIN_NDK_VERSION, ndk_url=NDK_DOWNLOAD_URL
7084
),
7185
)
7286
mock_read_ndk.assert_called_once_with(self.ndk_dir)
@@ -83,16 +97,17 @@ def test_check_ndk_version_error(self):
8397
self.assertEqual(
8498
cm.output,
8599
[
86-
"INFO:p4a:[INFO]: Could not determine NDK version, "
87-
"no source.properties in the NDK dir",
88-
"WARNING:p4a:[WARNING]: Unable to read the ndk version, "
89-
"assuming that you are using an NDK greater than 17 (the "
90-
"minimum ndk required to use p4a successfully).\n"
91-
"Note: If you got build errors, consider to download the "
92-
"recommended ndk version which is 17c and try it again (after "
93-
"removing all the files generated with this build). To "
94-
"download the android NDK visit the following "
95-
"page: {download_url}".format(download_url=NDK_DOWNLOAD_URL),
100+
"INFO:p4a:[INFO]: {}".format(UNKNOWN_NDK_MESSAGE),
101+
"WARNING:p4a:[WARNING]: {}".format(
102+
READ_ERROR_NDK_MESSAGE.format(ndk_dir=self.ndk_dir)
103+
),
104+
"WARNING:p4a:[WARNING]: {}".format(
105+
ENSURE_RIGHT_NDK_MESSAGE.format(
106+
min_supported=MIN_NDK_VERSION,
107+
rec_version=RECOMMENDED_NDK_VERSION,
108+
ndk_url=NDK_DOWNLOAD_URL,
109+
)
110+
),
96111
],
97112
)
98113

@@ -119,10 +134,7 @@ def test_read_ndk_version_error(self, mock_open_src_prop):
119134
version = read_ndk_version(self.ndk_dir)
120135
self.assertEqual(
121136
cm.output,
122-
[
123-
"INFO:p4a:[INFO]: Could not parse "
124-
"$NDK_DIR/source.properties, not checking NDK version"
125-
],
137+
["INFO:p4a:[INFO]: {}".format(PARSE_ERROR_NDK_MESSAGE)],
126138
)
127139
mock_open_src_prop.assert_called_once_with(
128140
join(self.ndk_dir, "source.properties")
@@ -135,10 +147,9 @@ def test_check_target_api_error_arch_armeabi(self):
135147
check_target_api(RECOMMENDED_TARGET_API, "armeabi")
136148
self.assertEqual(
137149
e.exception.args[0],
138-
"Asked to build for armeabi architecture with API {ndk_api}, but "
139-
"API {max_target_api} or greater does not support armeabi".format(
140-
ndk_api=RECOMMENDED_TARGET_API,
141-
max_target_api=ARMEABI_MAX_TARGET_API,
150+
UNSUPPORTED_NDK_API_FOR_ARMEABI_MESSAGE.format(
151+
req_ndk_api=RECOMMENDED_TARGET_API,
152+
max_ndk_api=ARMEABI_MAX_TARGET_API,
142153
),
143154
)
144155

@@ -151,10 +162,9 @@ def test_check_target_api_warning_target_api(self):
151162
cm.output,
152163
[
153164
"WARNING:p4a:[WARNING]: Target API 25 < 26",
154-
"WARNING:p4a:[WARNING]: Target APIs lower than 26 are no "
155-
"longer supported on Google Play, and are not recommended. "
156-
"Note that the Target API can be higher than your device "
157-
"Android version, and should usually be as high as possible.",
165+
"WARNING:p4a:[WARNING]: {old_api_msg}".format(
166+
old_api_msg=OLD_API_MESSAGE
167+
),
158168
],
159169
)
160170

@@ -169,8 +179,7 @@ def test_check_ndk_api_error_android_api(self):
169179
check_ndk_api(ndk_api, android_api)
170180
self.assertEqual(
171181
e.exception.args[0],
172-
"Target NDK API is {ndk_api}, higher than the target Android "
173-
"API {android_api}.".format(
182+
TARGET_NDK_API_GREATER_THAN_TARGET_API_MESSAGE.format(
174183
ndk_api=ndk_api, android_api=android_api
175184
),
176185
)
@@ -187,5 +196,9 @@ def test_check_ndk_api_warning_old_ndk(self):
187196
check_ndk_api(ndk_api, android_api)
188197
self.assertEqual(
189198
cm.output,
190-
["WARNING:p4a:[WARNING]: NDK API less than 21 is not supported"],
199+
[
200+
"WARNING:p4a:[WARNING]: {}".format(
201+
OLD_NDK_API_MESSAGE.format(MIN_NDK_API)
202+
)
203+
],
191204
)

0 commit comments

Comments
 (0)