From 65b3f523f9a193ad7dd6edac9fb196fc986a9822 Mon Sep 17 00:00:00 2001 From: Adrien Banlin Date: Mon, 12 Jun 2023 10:49:48 +0200 Subject: [PATCH] networkinterfaces: make allow/auto behave the same This change goal is to make auto behave like allow-auto. This commit will also provide the interfaces range capability to any other allow-class names. --- ifupdown2/ifupdown/networkinterfaces.py | 40 +++++++++++-------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/ifupdown2/ifupdown/networkinterfaces.py b/ifupdown2/ifupdown/networkinterfaces.py index ffa62ab..f4d1008 100644 --- a/ifupdown2/ifupdown/networkinterfaces.py +++ b/ifupdown2/ifupdown/networkinterfaces.py @@ -142,6 +142,20 @@ class networkInterfaces(): return 1 return 0 + def _add_ifaces_to_class(self, classname, ifaces): + if classname not in self.allow_classes: + self.allow_classes[classname] = [] + + # This is a specific uses case: everything is considered auto if all + # is being given to the auto or allow-auto classe. + if classname == 'auto' and 'all' in ifaces: + self.auto_all = True + return # nothing is to be done. + + for ifname in ifaces: + ifnames = utils.expand_iface_range(ifname) or [ifname] + self.allow_classes[classname].extend(ifnames) + def process_allow(self, lines, cur_idx, lineno): allow_line = lines[cur_idx] @@ -152,12 +166,7 @@ class networkInterfaces(): allow_class = words[0].split('-')[1] ifacenames = words[1:] - - if self.allow_classes.get(allow_class): - for i in ifacenames: - self.allow_classes[allow_class].append(i) - else: - self.allow_classes[allow_class] = ifacenames + self._add_ifaces_to_class(allow_class, ifacenames) return 0 def process_source(self, lines, cur_idx, lineno): @@ -201,25 +210,12 @@ class networkInterfaces(): def process_auto(self, lines, cur_idx, lineno): auto_ifaces = re.split(self._ws_split_regex, lines[cur_idx])[1:] + if not auto_ifaces: self._parse_error(self._currentfile, lineno, 'invalid auto line \'%s\''%lines[cur_idx]) - return 0 - for a in auto_ifaces: - if a == 'all': - self.auto_all = True - break - r = utils.parse_iface_range(a) - if r: - if len(r) == 3: - # eg swp1.[2-4], r = "swp1.", 2, 4) - for i in range(r[1], r[2]+1): - self.auto_ifaces.append('%s%d' %(r[0], i)) - elif len(r) == 4: - for i in range(r[1], r[2]+1): - # eg swp[2-4].100, r = ("swp", 2, 4, ".100") - self.auto_ifaces.append('%s%d%s' %(r[0], i, r[3])) - self.auto_ifaces.append(a) + else: + self._add_ifaces_to_class('auto', auto_ifaces) return 0 def _add_to_iface_config(self, ifacename, iface_config, attrname,