]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/actions/initializer.py
tools: Drop requests library dependency
[waydroid.git] / tools / actions / initializer.py
1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 import logging
4 import os
5 import requests
6 from tools import helpers
7 import tools.config
8
9
10 def get_vendor_type(args):
11 vndk_str = helpers.props.host_get(args, "ro.vndk.version")
12 ret = "MAINLINE"
13 if vndk_str != "":
14 vndk = int(vndk_str)
15 if vndk > 19:
16 ret = "HALIUM_" + str(vndk - 19)
17
18 return ret
19
20 def setup_config(args):
21 cfg = tools.config.load(args)
22 args.arch = helpers.arch.host()
23 cfg["waydroid"]["arch"] = args.arch
24
25 preinstalled_images = tools.config.defaults["preinstalled_images_path"]
26 if not args.images_path:
27 if os.path.isdir(preinstalled_images):
28 if os.path.isfile(preinstalled_images + "/system.img") and os.path.isfile(preinstalled_images + "/vendor.img"):
29 args.images_path = preinstalled_images
30 else:
31 logging.error("Missing system or vendor on preinstalled images dir, fallback to default")
32 if not args.images_path:
33 args.images_path = tools.config.defaults["images_path"]
34 cfg["waydroid"]["images_path"] = args.images_path
35
36 channels_cfg = tools.config.load_channels()
37 if not args.system_channel:
38 args.system_channel = channels_cfg["channels"]["system_channel"]
39 if not args.vendor_channel:
40 args.vendor_channel = channels_cfg["channels"]["vendor_channel"]
41 if not args.rom_type:
42 args.rom_type = channels_cfg["channels"]["rom_type"]
43 if not args.system_type:
44 args.system_type = channels_cfg["channels"]["system_type"]
45
46 args.system_ota = args.system_channel + "/" + args.rom_type + \
47 "/waydroid_" + args.arch + "/" + args.system_type + ".json"
48 system_request = helpers.http.retrieve(args.system_ota)
49 if system_request[0] != 200:
50 if args.images_path != preinstalled_images:
51 raise ValueError(
52 "Failed to get system OTA channel: {}, error: {}".format(args.system_ota, system_request[0]))
53 else:
54 args.system_ota = "None"
55
56 device_codename = helpers.props.host_get(args, "ro.product.device")
57 args.vendor_type = None
58 for vendor in [device_codename, get_vendor_type(args)]:
59 vendor_ota = args.vendor_channel + "/waydroid_" + \
60 args.arch + "/" + vendor + ".json"
61 vendor_request = helpers.http.retrieve(vendor_ota)
62 if vendor_request[0] == 200:
63 args.vendor_type = vendor
64 args.vendor_ota = vendor_ota
65 break
66
67 if not args.vendor_type:
68 if args.images_path != preinstalled_images:
69 raise ValueError(
70 "Failed to get vendor OTA channel: {}".format(vendor_ota))
71 else:
72 args.vendor_ota = "None"
73 args.vendor_type = get_vendor_type(args)
74
75 cfg["waydroid"]["vendor_type"] = args.vendor_type
76 cfg["waydroid"]["system_ota"] = args.system_ota
77 cfg["waydroid"]["vendor_ota"] = args.vendor_ota
78 helpers.drivers.setupBinderNodes(args)
79 cfg["waydroid"]["binder"] = args.BINDER_DRIVER
80 cfg["waydroid"]["vndbinder"] = args.VNDBINDER_DRIVER
81 cfg["waydroid"]["hwbinder"] = args.HWBINDER_DRIVER
82 tools.config.save(args, cfg)
83
84 def init(args):
85 if not os.path.isfile(args.config) or args.force:
86 setup_config(args)
87 status = "STOPPED"
88 if os.path.exists(tools.config.defaults["lxc"] + "/waydroid"):
89 status = helpers.lxc.status(args)
90 if status != "STOPPED":
91 logging.info("Stopping container")
92 helpers.lxc.stop(args)
93 helpers.images.umount_rootfs(args)
94 if args.images_path != tools.config.defaults["preinstalled_images_path"]:
95 helpers.images.get(args)
96 if not os.path.isdir(tools.config.defaults["rootfs"]):
97 os.mkdir(tools.config.defaults["rootfs"])
98 helpers.lxc.setup_host_perms(args)
99 helpers.lxc.set_lxc_config(args)
100 helpers.lxc.make_base_props(args)
101 if status != "STOPPED":
102 logging.info("Starting container")
103 helpers.images.mount_rootfs(args, args.images_path)
104 helpers.lxc.start(args)
105 else:
106 logging.info("Already initialized")