diff --git a/pmb/config/init.py b/pmb/config/init.py index 278b0503..3488f1e0 100644 --- a/pmb/config/init.py +++ b/pmb/config/init.py @@ -217,14 +217,41 @@ def ask_for_device_nonfree(args, device): def ask_for_device(args): - devices = sorted(pmb.helpers.devices.list_codenames(args)) - logging.info("Target device (either an existing one, or a new one for" - " porting).") - logging.info("Available (" + str(len(devices)) + "): " + - ", ".join(devices)) + vendors = sorted(pmb.helpers.devices.list_vendors(args)) + logging.info("Choose your target device vendor (either an " + "existing one, or a new one for porting).") + logging.info("Available vendors (" + str(len(vendors)) + "): " + + ", ".join(vendors)) + + current_vendor = None + current_codename = None + if args.device: + current_vendor = args.device.split("-", 1)[0] + current_codename = args.device.split("-", 1)[1] + while True: - device = pmb.helpers.cli.ask(args, "Device", None, args.device, False, - "[a-z0-9]+-[a-z0-9]+") + vendor = pmb.helpers.cli.ask(args, "Vendor", None, current_vendor, + False, r"[a-z0-9]+") + + new_vendor = vendor not in vendors + if new_vendor: + logging.info(f"The specified vendor ({vendor}) could not be found " + f"in existing ports, do you want to start a new port?") + if not pmb.helpers.cli.confirm(args, default=True): + continue + else: + devices = sorted(pmb.helpers.devices.list_codenames(args, vendor)) + # Remove "vendor-" prefixes from device list + codenames = [x.split('-', 1)[1] for x in devices] + logging.info("Available codenames (" + str(len(codenames)) + "): " + + ", ".join(codenames)) + + if current_vendor != vendor: + current_codename = '' + codename = pmb.helpers.cli.ask(args, "Device codename", None, + current_codename, False, r"[a-z0-9]+") + + device = vendor + '-' + codename device_exists = os.path.exists(args.aports + "/device/device-" + device + "/deviceinfo") if not device_exists: @@ -236,8 +263,11 @@ def ask_for_device(args): logging.info("You are about to do a new device port for '" + device + "'.") if not pmb.helpers.cli.confirm(args, default=True): + current_vendor = vendor continue + # New port creation confirmed + logging.info(f"Generating new aports for: {device}...") pmb.aportgen.generate(args, "device-" + device) pmb.aportgen.generate(args, "linux-" + device) break diff --git a/pmb/helpers/devices.py b/pmb/helpers/devices.py index a2b4964d..2e277842 100644 --- a/pmb/helpers/devices.py +++ b/pmb/helpers/devices.py @@ -21,15 +21,29 @@ import glob import pmb.parse -def list_codenames(args): +def list_codenames(args, vendor=None): """ Get all devices, for which aports are available + :param vendor: vendor name to choose devices from, or None for all vendors :returns: ["first-device", "second-device", ...] """ ret = [] for path in glob.glob(args.aports + "/device/device-*"): device = os.path.basename(path).split("-", 1)[1] - ret += [device] + if (vendor is None) or device.startswith(vendor + '-'): + ret.append(device) + return ret + + +def list_vendors(args): + """ + Get all device vendors, for which aports are available + :returns: {"vendor1", "vendor2", ...} + """ + ret = set() + for path in glob.glob(args.aports + "/device/device-*"): + vendor = os.path.basename(path).split("-", 2)[1] + ret.add(vendor) return ret diff --git a/test/test_questions.py b/test/test_questions.py index e5660425..69b9892b 100644 --- a/test/test_questions.py +++ b/test/test_questions.py @@ -134,18 +134,26 @@ def test_questions_device(args, monkeypatch): # Existing device (without non-free components so we have defaults there) func = pmb.config.init.ask_for_device nonfree = {"firmware": True, "userland": False} - fake_answers(monkeypatch, ["lg-mako"]) + fake_answers(monkeypatch, ["lg", "mako"]) kernel = args.kernel assert func(args) == ("lg-mako", True, kernel, nonfree) - # Non-existing device, go back, existing device - fake_answers(monkeypatch, ["whoops-typo", "n", "lg-mako"]) + # Non-existing vendor, go back, existing vendor+device + fake_answers(monkeypatch, ["whoops", "n", "lg", "mako"]) assert func(args) == ("lg-mako", True, kernel, nonfree) - # New device - fake_answers(monkeypatch, ["new-device", "y"]) + # Existing vendor, new device, go back, existing vendor+device + fake_answers(monkeypatch, ["lg", "nonexistent", "n", "lg", "mako"]) + assert func(args) == ("lg-mako", True, kernel, nonfree) + + # New vendor and new device (new port) + fake_answers(monkeypatch, ["new", "y", "device", "y"]) assert func(args) == ("new-device", False, kernel, nonfree) + # Existing vendor, new device (new port) + fake_answers(monkeypatch, ["lg", "nonexistent", "y"]) + assert func(args) == ("lg-nonexistent", False, kernel, nonfree) + def test_questions_device_kernel(args, monkeypatch): # Prepare args