Skip to content

Commit 32817fb

Browse files
authored
Cleanup patching.py (#2868)
Major changes to comments, param names, function ordering. Removed deprecated LooseVersion Move os.uname to platform.uname Added win32 check Windows fix
1 parent 4755bcc commit 32817fb

File tree

1 file changed

+138
-49
lines changed

1 file changed

+138
-49
lines changed

pythonforandroid/patching.py

Lines changed: 138 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,178 @@
1-
from os import uname
2-
from distutils.version import LooseVersion
1+
"""
2+
Helper functions for recipes.
33
4+
Recipes must supply a list of patches.
45
5-
def check_all(*callables):
6-
def check(**kwargs):
7-
return all(c(**kwargs) for c in callables)
8-
return check
6+
Patches consist of a filename and an optional conditional, which is
7+
any function of the form:
8+
def patch_check(arch: string, recipe : Recipe) -> bool
99
10+
This library provides some helpful conditionals and mechanisms to
11+
join multiple conditionals.
1012
11-
def check_any(*callables):
12-
def check(**kwargs):
13-
return any(c(**kwargs) for c in callables)
14-
return check
13+
Example:
14+
patches = [
15+
("linux_or_darwin_only.patch",
16+
check_any(is_linux, is_darwin),
17+
("recent_android_API.patch",
18+
is_apt_gte(27)),
19+
]
20+
"""
21+
from platform import uname
22+
from packaging.version import Version
23+
24+
25+
# Platform checks
1526

1627

1728
def is_platform(platform):
18-
def is_x(**kwargs):
19-
return uname()[0] == platform
20-
return is_x
29+
"""
30+
Returns true if the host platform matches the parameter given.
31+
"""
2132

33+
def check(arch, recipe):
34+
return uname().system.lower() == platform.lower()
35+
36+
return check
2237

23-
is_linux = is_platform('Linux')
24-
is_darwin = is_platform('Darwin')
38+
39+
is_linux = is_platform("Linux")
40+
is_darwin = is_platform("Darwin")
41+
is_windows = is_platform("Windows")
2542

2643

2744
def is_arch(xarch):
28-
def is_x(arch, **kwargs):
45+
"""
46+
Returns true if the target architecture platform matches the parameter
47+
given.
48+
"""
49+
50+
def check(arch):
2951
return arch.arch == xarch
30-
return is_x
3152

53+
return check
54+
55+
56+
# Android API comparisons:
57+
# Return true if the Android API level being targeted
58+
# is equal (or >, >=, <, <= as appropriate) the given parameter
59+
60+
61+
def is_api(apiver: int):
62+
def check(arch, recipe):
63+
return recipe.ctx.android_api == apiver
64+
65+
return check
3266

33-
def is_api_gt(apiver):
34-
def is_x(recipe, **kwargs):
67+
68+
def is_api_gt(apiver: int):
69+
def check(arch, recipe):
3570
return recipe.ctx.android_api > apiver
36-
return is_x
71+
72+
return check
3773

3874

39-
def is_api_gte(apiver):
40-
def is_x(recipe, **kwargs):
75+
def is_api_gte(apiver: int):
76+
def check(arch, recipe):
4177
return recipe.ctx.android_api >= apiver
42-
return is_x
78+
79+
return check
4380

4481

45-
def is_api_lt(apiver):
46-
def is_x(recipe, **kwargs):
82+
def is_api_lt(apiver: int):
83+
def check(arch, recipe):
4784
return recipe.ctx.android_api < apiver
48-
return is_x
85+
86+
return check
4987

5088

51-
def is_api_lte(apiver):
52-
def is_x(recipe, **kwargs):
89+
def is_api_lte(apiver: int):
90+
def check(arch, recipe):
5391
return recipe.ctx.android_api <= apiver
54-
return is_x
55-
5692

57-
def is_api(apiver):
58-
def is_x(recipe, **kwargs):
59-
return recipe.ctx.android_api == apiver
60-
return is_x
93+
return check
6194

6295

63-
def will_build(recipe_name):
64-
def will(recipe, **kwargs):
65-
return recipe_name in recipe.ctx.recipe_build_order
66-
return will
96+
# Android API comparisons:
6797

6898

6999
def is_ndk(ndk):
70-
def is_x(recipe, **kwargs):
100+
"""
101+
Return true if the Minimum Supported Android NDK level being targeted
102+
is equal the given parameter (which should be an AndroidNDK instance)
103+
"""
104+
105+
def check(arch, recipe):
71106
return recipe.ctx.ndk == ndk
72-
return is_x
107+
108+
return check
109+
110+
111+
# Recipe Version comparisons:
112+
# These compare the Recipe's version with the provided string (or
113+
# Packaging.Version).
114+
#
115+
# Warning: Both strings must conform to PEP 440 - e.g. "3.2.1" or "1.0rc1"
73116

74117

75118
def is_version_gt(version):
76-
def is_x(recipe, **kwargs):
77-
return LooseVersion(recipe.version) > version
119+
"""Return true if the Recipe's version is greater"""
120+
121+
def check(arch, recipe):
122+
return Version(recipe.version) > Version(version)
123+
124+
return check
78125

79126

80127
def is_version_lt(version):
81-
def is_x(recipe, **kwargs):
82-
return LooseVersion(recipe.version) < version
83-
return is_x
128+
"""Return true if the Recipe's version is less than"""
129+
130+
def check(arch, recipe):
131+
return Version(recipe.version) < Version(version)
132+
133+
return check
84134

85135

86-
def version_starts_with(version):
87-
def is_x(recipe, **kwargs):
88-
return recipe.version.startswith(version)
89-
return is_x
136+
def version_starts_with(version_prefix):
137+
def check(arch, recipe):
138+
return recipe.version.startswith(version_prefix)
139+
140+
return check
141+
142+
143+
# Will Build
144+
145+
146+
def will_build(recipe_name):
147+
"""Return true if the recipe with this name is planned to be included in
148+
the distribution."""
149+
150+
def check(arch, recipe):
151+
return recipe_name in recipe.ctx.recipe_build_order
152+
153+
return check
154+
155+
156+
# Conjunctions
157+
158+
159+
def check_all(*patch_checks):
160+
"""
161+
Given a collection of patch_checks as params, return if all returned true.
162+
"""
163+
164+
def check(arch, recipe):
165+
return all(patch_check(arch, recipe) for patch_check in patch_checks)
166+
167+
return check
168+
169+
170+
def check_any(*patch_checks):
171+
"""
172+
Given a collection of patch_checks as params, return if any returned true.
173+
"""
174+
175+
def check(arch, recipe):
176+
return any(patch_check(arch, recipe) for patch_check in patch_checks)
177+
178+
return check

0 commit comments

Comments
 (0)