-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_device_recipe.py
executable file
·260 lines (218 loc) · 6.72 KB
/
generate_device_recipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import yaml
import json
import datetime
from itertools import chain
SUITABLE_FILES = [
# Used for community ports
"community_devices.yml",
# Droidian official devices and generic rootfs
"devices.yml",
]
IS_COMMUNITY_PORT = False
BUILDER_MAIN_DIRECTORY = os.path.abspath(os.path.dirname(sys.argv[0]))
BUILDER_GENERATED_DIRECTORY = os.path.join(BUILDER_MAIN_DIRECTORY, "generated")
BUILDER_OUT_DIRECTORY = os.path.join(BUILDER_MAIN_DIRECTORY, "out")
TEMPLATE = """
{{- $product := or .product "%(product)s" -}}
{{- $architecture := or .architecture "%(architecture)s" -}}
{{- $suffix := or .suffix "%(suffix)s" -}}
{{- $edition := or .edition "%(edition)s" -}}
{{- $variant := or .variant "%(variant)s" -}}
{{- $apilevel := or .apilevel %(apilevel)d -}}
{{- $version := or .version "%(version)s" -}}
{{- $mtype := or .mtype "%(mtype)s" -}}
{{- $image := or .image (printf "droidian-%%s-%%s-%%s-%%s-api%%d-%%s-%%s_%%s.zip" $mtype $edition $variant $product $apilevel $architecture $version $suffix) -}}
{{- $output_type := or .output_type "%(output_type)s" -}}
{{- $use_internal_repository := or .use_internal_repository "%(use_internal_repository)s" -}}
{{- $droidian_version := or .droidian_version "%(version)s" -}}
architecture: {{ $architecture }}
actions:
- action: run
description: Do nothing
chroot: false
command: echo "Doing nothing!"
"""
TEMPLATE_ONLY_STABLE = """
{{ if ne $version "next" }}
"""
TEMPLATE_ONLY_NIGHTLY = """
{{ if eq $version "next" }}
"""
TEMPLATE_END = """
{{end}}
"""
TEMPLATE_ENTRYPOINT = """
- action: recipe
description: Build Droidian
recipe: ../rootfs-templates/device.yaml
variables:
architecture: {{ $architecture }}
suffix: {{ $suffix }}
edition: {{ $edition }}
variant: {{ $variant }}
apilevel: {{ $apilevel }}
version: {{ $version }}
image: {{ $image }}
output_type: {{ $output_type }}
use_internal_repository: {{ $use_internal_repository }}
droidian_version: {{ $droidian_version }}
"""
TEMPLATE_BUNDLE = """
- action: recipe
description: Generate %(name)s feature bundle
recipe: ../rootfs-templates/recipes/sideload-create.yaml
variables:
architecture: %(architecture)s
device_arch: %(architecture)s
sideload_name: %(name)s-api%(apilevel)d-%(architecture)s_{{ $suffix }}
packages: %(packages)s
"""
TEMPLATE_IMAGE_ADAPTATION = """
- action: recipe
description: Install device adaptation
recipe: ../rootfs-templates/recipes/adaptation.yaml
variables:
architecture: %(architecture)s
packages: %(packages)s
"""
def get_matrix(contents):
"""
Returns a JSON containing all the possible jobs.
"""
get_list = lambda x: x if isinstance(x, list) else [x]
return list(
chain(
*[
[
{
"job_name" : "%s (%s/%s edition) - %s (api%d)" % (product, edition, variant, arch, apilevel),
"product" : product,
"arch" : arch,
"edition" : edition,
"variant" : variant,
"apilevel" : apilevel,
}
for arch in get_list(config.get("arch", ["arm64"]))
for edition in get_list(config.get("edition", ["phosh"]))
for variant in get_list(config.get("variant", ["standard"]))
for apilevel in get_list(config.get("apilevel", [28]))
]
for product, config in contents.items()
if not product.startswith(".")
]
)
)
def generate_recipe_for_product(contents, product, arch, edition, variant, apilevel):
"""
Generates a debos recipe for the given product
"""
config = contents[product]
if not os.path.exists(BUILDER_GENERATED_DIRECTORY):
os.makedirs(BUILDER_GENERATED_DIRECTORY)
if not os.path.exists(BUILDER_OUT_DIRECTORY):
os.makedirs(BUILDER_OUT_DIRECTORY)
template_config = {
"product" : product,
"architecture" : arch,
"edition" : edition,
"variant" : variant,
"apilevel" : int(apilevel),
"mtype" : "OFFICIAL" if not IS_COMMUNITY_PORT else "UNOFFICIAL",
"version" : os.environ.get("DROIDIAN_VERSION", "next"),
"suffix" : datetime.datetime.utcnow().strftime("%Y%m%d"),
"output_type" : config["type"],
"use_internal_repository" : "yes" if config.get("use_internal_repository", False) else "no",
}
# TODO: perhaps use pyyaml?
with open(os.path.join(BUILDER_GENERATED_DIRECTORY, "product.yaml"), "w") as f:
f.write(TEMPLATE % template_config)
if config["type"] == "rootfs":
if config.get("bundles", []):
for bundle in config["bundles"]:
write_end = False
if bundle["arch"] in ("any", arch) and bundle.get("apilevel", 28) in ("any", int(apilevel)):
if bundle.get("only_stable", False):
f.write(TEMPLATE_ONLY_STABLE)
write_end = True
elif bundle.get("only_nightly", False):
f.write(TEMPLATE_ONLY_NIGHTLY)
write_end = True
f.write(
TEMPLATE_BUNDLE % {
"name" : bundle["name"],
"architecture" : arch,
"packages" : " ".join(bundle["packages"]),
"apilevel" : int(apilevel),
}
)
if write_end:
f.write(TEMPLATE_END)
if config.get("packages", []):
f.write(
TEMPLATE_IMAGE_ADAPTATION % {
"architecture" : arch,
"packages" : " ".join(config["packages"])
}
)
elif config["type"] == "image":
f.write(
TEMPLATE_IMAGE_ADAPTATION % {
"architecture" : arch,
"packages" : " ".join(config["packages"])
}
)
with open(os.path.join(BUILDER_GENERATED_DIRECTORY, "droidian.yaml"), "w") as f:
f.write(TEMPLATE % template_config)
f.write(TEMPLATE_ENTRYPOINT)
def prompt_product(contents):
"""
Interactive prompt to generate_recipe_for_product().
"""
available = get_matrix(contents)
print(
"Available products:\n\n%s\n" %
"\n".join(
[
" %d) %s" % (i+1, available[i]["job_name"])
for i in range(0, len(available))
]
)
)
choice = input("Please choose a product: ")
try:
choice = int(choice) - 1
generate_recipe_for_product(
contents,
available[choice]["product"],
available[choice]["arch"],
available[choice]["edition"],
available[choice]["variant"],
available[choice]["apilevel"],
)
except:
raise
if __name__ == "__main__":
contents = {}
for candidate in SUITABLE_FILES:
path = os.path.join(BUILDER_MAIN_DIRECTORY, candidate)
if not os.path.exists(path):
continue
with open(path, "r") as f:
contents = yaml.safe_load(f)
if candidate == "community_devices.yml":
IS_COMMUNITY_PORT = True
break
argc = len(sys.argv)
if argc == 6:
generate_recipe_for_product(contents, *sys.argv[1:])
elif argc == 1:
prompt_product(contents)
elif "--matrix" in sys.argv:
print(json.dumps(get_matrix(contents)))
else:
sys.stderr.write("USAGE: generate_device_recipe.py [--matrix]|(<product_name> <arch> <edition> <variant> <apilevel>)\n")
sys.exit(1)