|
1 |
| -from os import uname |
2 |
| -from distutils.version import LooseVersion |
| 1 | +""" |
| 2 | + Helper functions for recipes. |
3 | 3 |
|
| 4 | + Recipes must supply a list of patches. |
4 | 5 |
|
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 |
9 | 9 |
|
| 10 | + This library provides some helpful conditionals and mechanisms to |
| 11 | + join multiple conditionals. |
10 | 12 |
|
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 |
15 | 26 |
|
16 | 27 |
|
17 | 28 | 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 | + """ |
21 | 32 |
|
| 33 | + def check(arch, recipe): |
| 34 | + return uname().system.lower() == platform.lower() |
| 35 | + |
| 36 | + return check |
22 | 37 |
|
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") |
25 | 42 |
|
26 | 43 |
|
27 | 44 | 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): |
29 | 51 | return arch.arch == xarch
|
30 |
| - return is_x |
31 | 52 |
|
| 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 |
32 | 66 |
|
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): |
35 | 70 | return recipe.ctx.android_api > apiver
|
36 |
| - return is_x |
| 71 | + |
| 72 | + return check |
37 | 73 |
|
38 | 74 |
|
39 |
| -def is_api_gte(apiver): |
40 |
| - def is_x(recipe, **kwargs): |
| 75 | +def is_api_gte(apiver: int): |
| 76 | + def check(arch, recipe): |
41 | 77 | return recipe.ctx.android_api >= apiver
|
42 |
| - return is_x |
| 78 | + |
| 79 | + return check |
43 | 80 |
|
44 | 81 |
|
45 |
| -def is_api_lt(apiver): |
46 |
| - def is_x(recipe, **kwargs): |
| 82 | +def is_api_lt(apiver: int): |
| 83 | + def check(arch, recipe): |
47 | 84 | return recipe.ctx.android_api < apiver
|
48 |
| - return is_x |
| 85 | + |
| 86 | + return check |
49 | 87 |
|
50 | 88 |
|
51 |
| -def is_api_lte(apiver): |
52 |
| - def is_x(recipe, **kwargs): |
| 89 | +def is_api_lte(apiver: int): |
| 90 | + def check(arch, recipe): |
53 | 91 | return recipe.ctx.android_api <= apiver
|
54 |
| - return is_x |
55 |
| - |
56 | 92 |
|
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 |
61 | 94 |
|
62 | 95 |
|
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: |
67 | 97 |
|
68 | 98 |
|
69 | 99 | 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): |
71 | 106 | 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" |
73 | 116 |
|
74 | 117 |
|
75 | 118 | 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 |
78 | 125 |
|
79 | 126 |
|
80 | 127 | 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 |
84 | 134 |
|
85 | 135 |
|
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