From ae38bc33cf38e4d08611ba706a2351a5ebc00f59 Mon Sep 17 00:00:00 2001 From: Nick Mills-Barrett Date: Sun, 17 Nov 2024 13:47:19 +0000 Subject: [PATCH] Update fact names and operations for 3.x --- .typos.toml | 1 + pyinfra/facts/opkg.py | 53 ++++++++++++------- pyinfra/operations/opkg.py | 24 +++------ .../opkg_conf.json | 0 .../opkg_feeds.json | 0 .../opkg_installable_architectures.json | 0 .../opkg_packages.json | 0 .../opkg_upgradeable_packages.json | 0 .../opkg.packages/add_existing_package.json | 2 +- .../opkg.packages/add_multiple_packages.json | 2 +- .../opkg.packages/add_one_package.json | 2 +- .../add_with_unallowed_pinning.json | 2 +- .../list_of_nulls_package_list.json | 2 +- .../opkg.packages/null_package_list.json | 2 +- .../remove_existing_package.json | 2 +- ...e_existing_package_and_require_update.json | 4 +- .../remove_not_existing_package.json | 2 +- .../opkg.packages/update_then_add_one.json | 4 +- .../operations/opkg.update/first_update.json | 4 +- .../opkg_second_update_forced.json | 10 ---- .../operations/opkg.update/second_update.json | 9 ---- .../opkg.update/second_update_with_force.json | 10 ---- tests/words.txt | 1 + 23 files changed, 58 insertions(+), 78 deletions(-) rename tests/facts/{opkg.Conf => opkg.OpkgConf}/opkg_conf.json (100%) rename tests/facts/{opkg.Feeds => opkg.OpkgFeeds}/opkg_feeds.json (100%) rename tests/facts/{opkg.InstallableArchitectures => opkg.OpkgInstallableArchitectures}/opkg_installable_architectures.json (100%) rename tests/facts/{opkg.Packages => opkg.OpkgPackages}/opkg_packages.json (100%) rename tests/facts/{opkg.UpgradeablePackages => opkg.OpkgUpgradeablePackages}/opkg_upgradeable_packages.json (100%) delete mode 100644 tests/operations/opkg.update/opkg_second_update_forced.json delete mode 100644 tests/operations/opkg.update/second_update.json delete mode 100644 tests/operations/opkg.update/second_update_with_force.json diff --git a/.typos.toml b/.typos.toml index 186698e11..15e19eaa8 100644 --- a/.typos.toml +++ b/.typos.toml @@ -1,6 +1,7 @@ [files] extend-exclude = [ "tests/facts/apt.SimulateOperationWillChange/upgrade.json", + "tests/facts/opkg.OpkgPackages/opkg_packages.json", "tests/words.txt", ] diff --git a/pyinfra/facts/opkg.py b/pyinfra/facts/opkg.py index 0c6594dfe..b16927062 100644 --- a/pyinfra/facts/opkg.py +++ b/pyinfra/facts/opkg.py @@ -8,6 +8,7 @@ see https://openwrt.org/docs/guide-user/additional-software/opkg """ + import re from typing import Dict, NamedTuple, Union @@ -15,16 +16,16 @@ from pyinfra.api import FactBase from pyinfra.facts.util.packaging import parse_packages -# TODO - change NamedTuple to dataclass but need to figure out how to get json serialization +# TODO - change NamedTuple to dataclass Opkgbut need to figure out how to get json serialization # to work without changing core code -class PkgUpgradeInfo(NamedTuple): +class OpkgPkgUpgradeInfo(NamedTuple): installed: str available: str -class ConfInfo(NamedTuple): +class OpkgConfInfo(NamedTuple): paths: Dict[str, str] # list of paths, e.g. {'root':'/', 'ram':'/tmp} list_dir: str # where package lists are stored, e.g. /var/opkg-lists options: Dict[ @@ -33,13 +34,13 @@ class ConfInfo(NamedTuple): arch_cfg: Dict[str, int] # priorities for architectures -class FeedInfo(NamedTuple): +class OpkgFeedInfo(NamedTuple): url: str # url for the feed fmt: str # format of the feed, e.g. "src/gz" kind: str # whether it comes from the 'distribution' or is 'custom' -class Conf(FactBase): +class OpkgConf(FactBase): """ Returns a NamedTuple with the current configuration: @@ -77,9 +78,13 @@ class Conf(FactBase): """, re.X, ) - conf_file = "/etc/opkg.conf" - command = f"cat {conf_file}" - default = lambda x: ConfInfo({}, "", {}, {}) # noqa: + + @staticmethod + def default(): + return OpkgConfInfo({}, "", {}, {}) + + def command(self) -> str: + return "cat /etc/opkg.conf" def process(self, output): dest, lists_dir, options, arch_cfg = {}, "", {}, {} @@ -97,10 +102,10 @@ def process(self, output): elif match.group("option") is not None: options[match.group("option")] = match.group("value") or True - return ConfInfo(dest, lists_dir, options, arch_cfg) + return OpkgConfInfo(dest, lists_dir, options, arch_cfg) -class Feeds(FactBase): +class OpkgFeeds(FactBase): """ Returns a dictionary containing the information for the distribution-provided and custom opkg feeds: @@ -120,9 +125,11 @@ class Feeds(FactBase): regex = re.compile( r"^(CUSTOM)|(?:\s*(?P[\w/]+)\s+(?P[\w]+)\s+(?P[\w./:]+))?(?:\s*#.*)?$" ) - command = "cat /etc/opkg/distfeeds.conf; echo CUSTOM; cat /etc/opkg/customfeeds.conf" default = dict + def command(self) -> str: + return "cat /etc/opkg/distfeeds.conf; echo CUSTOM; cat /etc/opkg/customfeeds.conf" + def process(self, output): feeds, kind = {}, "distribution" for line in output: @@ -133,12 +140,14 @@ def process(self, output): elif match.group(0) == "CUSTOM": kind = "custom" elif match.group("name") is not None: - feeds[match.group("name")] = FeedInfo(match.group("url"), match.group("fmt"), kind) + feeds[match.group("name")] = OpkgFeedInfo( + match.group("url"), match.group("fmt"), kind + ) return feeds -class InstallableArchitectures(FactBase): +class OpkgInstallableArchitectures(FactBase): """ Returns a dictionary containing the currently installable architectures for this system along with their priority: @@ -153,9 +162,11 @@ class InstallableArchitectures(FactBase): """ regex = re.compile(r"^(?:\s*arch\s+(?P[\w]+)\s+(?P\d+))?(\s*#.*)?$") - command = "/bin/opkg print-architecture" default = dict + def command(self) -> str: + return "/bin/opkg print-architecture" + def process(self, output): arch_list = {} for line in output: @@ -169,7 +180,7 @@ def process(self, output): return arch_list -class Packages(FactBase): +class OpkgPackages(FactBase): """ Returns a dict of installed opkg packages: @@ -181,15 +192,17 @@ class Packages(FactBase): } """ - command = "/bin/opkg list-installed" regex = r"^([a-zA-Z0-9][\w\-\.]*)\s-\s([\w\-\.]+)" default = dict + def command(self) -> str: + return "/bin/opkg list-installed" + def process(self, output): return parse_packages(self.regex, sorted(output)) -class UpgradeablePackages(FactBase): +class OpkgUpgradeablePackages(FactBase): """ Returns a dict of installed and upgradable opkg packages: @@ -201,17 +214,19 @@ class UpgradeablePackages(FactBase): } """ - command = "/bin/opkg list-upgradable" # yes, really spelled that way regex = re.compile(r"^([a-zA-Z0-9][\w\-.]*)\s-\s([\w\-.]+)\s-\s([\w\-.]+)") default = dict use_default_on_error = True + def command(self) -> str: + return "/bin/opkg list-upgradable" # yes, really spelled that way + def process(self, output): result = {} for line in output: match = self.regex.match(line) if match and len(match.groups()) == 3: - result[match.group(1)] = PkgUpgradeInfo(match.group(2), match.group(3)) + result[match.group(1)] = OpkgPkgUpgradeInfo(match.group(2), match.group(3)) else: logger.warning(f"Opkg: could not list-upgradable line '{line}'") diff --git a/pyinfra/operations/opkg.py b/pyinfra/operations/opkg.py index d60ed7f3c..abbeb3eb5 100644 --- a/pyinfra/operations/opkg.py +++ b/pyinfra/operations/opkg.py @@ -6,37 +6,30 @@ see https://openwrt.org/docs/guide-user/additional-software/opkg OpenWrt recommends against upgrading all packages thus there is no ``opkg.upgrade`` function """ + from typing import List, Union from pyinfra import host from pyinfra.api import StringCommand, operation -from pyinfra.facts.opkg import Packages +from pyinfra.facts.opkg import OpkgPackages from pyinfra.operations.util.packaging import ensure_packages EQUALS = "=" -UPDATE_ENTRY = "$$updated$$" # use $ as not allowed in package names (TODO - need reference) @operation(is_idempotent=False) -def update(force: bool = False): +def update(): """ - Update the local opkg information. Unless force is set, will only run - once per host per invocation of pyinfra. - - + force - refresh the package list even if already done + Update the local opkg information. """ - if force or (UPDATE_ENTRY not in host.get_fact(Packages)): - host.get_fact(Packages).update({UPDATE_ENTRY: [UPDATE_ENTRY]}) - yield StringCommand("opkg update > /dev/null 2>&1") - else: - host.noop("opkg packages already updated and not forced") + yield StringCommand("opkg update") _update = update -@operation +@operation() def packages( packages: Union[str, List[str]] = "", present: bool = True, @@ -81,16 +74,15 @@ def packages( raise ValueError(f"opkg does not support version pinning but found for: '{have_equals}'") if update: - yield from _update() + yield from _update._inner() yield from ensure_packages( host, pkg_list, - host.get_fact(Packages), + host.get_fact(OpkgPackages), present, install_command="opkg install", upgrade_command="opkg upgrade", uninstall_command="opkg remove", latest=latest, - # lower=True, # FIXME - does ensure_packages always do this or ? ) diff --git a/tests/facts/opkg.Conf/opkg_conf.json b/tests/facts/opkg.OpkgConf/opkg_conf.json similarity index 100% rename from tests/facts/opkg.Conf/opkg_conf.json rename to tests/facts/opkg.OpkgConf/opkg_conf.json diff --git a/tests/facts/opkg.Feeds/opkg_feeds.json b/tests/facts/opkg.OpkgFeeds/opkg_feeds.json similarity index 100% rename from tests/facts/opkg.Feeds/opkg_feeds.json rename to tests/facts/opkg.OpkgFeeds/opkg_feeds.json diff --git a/tests/facts/opkg.InstallableArchitectures/opkg_installable_architectures.json b/tests/facts/opkg.OpkgInstallableArchitectures/opkg_installable_architectures.json similarity index 100% rename from tests/facts/opkg.InstallableArchitectures/opkg_installable_architectures.json rename to tests/facts/opkg.OpkgInstallableArchitectures/opkg_installable_architectures.json diff --git a/tests/facts/opkg.Packages/opkg_packages.json b/tests/facts/opkg.OpkgPackages/opkg_packages.json similarity index 100% rename from tests/facts/opkg.Packages/opkg_packages.json rename to tests/facts/opkg.OpkgPackages/opkg_packages.json diff --git a/tests/facts/opkg.UpgradeablePackages/opkg_upgradeable_packages.json b/tests/facts/opkg.OpkgUpgradeablePackages/opkg_upgradeable_packages.json similarity index 100% rename from tests/facts/opkg.UpgradeablePackages/opkg_upgradeable_packages.json rename to tests/facts/opkg.OpkgUpgradeablePackages/opkg_upgradeable_packages.json diff --git a/tests/operations/opkg.packages/add_existing_package.json b/tests/operations/opkg.packages/add_existing_package.json index 17c7e74e9..6d8c67360 100644 --- a/tests/operations/opkg.packages/add_existing_package.json +++ b/tests/operations/opkg.packages/add_existing_package.json @@ -4,7 +4,7 @@ "update": false }, "facts": { - "opkg.Packages": {"curl": ["7.66.0-2"]} + "opkg.OpkgPackages": {"curl": ["7.66.0-2"]} }, "commands": [ ], diff --git a/tests/operations/opkg.packages/add_multiple_packages.json b/tests/operations/opkg.packages/add_multiple_packages.json index 520c06498..ba3eb5ee4 100644 --- a/tests/operations/opkg.packages/add_multiple_packages.json +++ b/tests/operations/opkg.packages/add_multiple_packages.json @@ -4,7 +4,7 @@ "update": false }, "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [ "opkg install curl wget" diff --git a/tests/operations/opkg.packages/add_one_package.json b/tests/operations/opkg.packages/add_one_package.json index 3f11acd6b..53f774103 100644 --- a/tests/operations/opkg.packages/add_one_package.json +++ b/tests/operations/opkg.packages/add_one_package.json @@ -4,7 +4,7 @@ "update": false }, "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [ "opkg install curl" diff --git a/tests/operations/opkg.packages/add_with_unallowed_pinning.json b/tests/operations/opkg.packages/add_with_unallowed_pinning.json index da91cecd4..ae6c98db0 100644 --- a/tests/operations/opkg.packages/add_with_unallowed_pinning.json +++ b/tests/operations/opkg.packages/add_with_unallowed_pinning.json @@ -1,7 +1,7 @@ { "args": ["curl=7.66.0-2"], "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [ ], "exception": { diff --git a/tests/operations/opkg.packages/list_of_nulls_package_list.json b/tests/operations/opkg.packages/list_of_nulls_package_list.json index ac63a624d..70ecfdd57 100644 --- a/tests/operations/opkg.packages/list_of_nulls_package_list.json +++ b/tests/operations/opkg.packages/list_of_nulls_package_list.json @@ -1,7 +1,7 @@ { "args": ["", "", ""], "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [], "noop_description": "empty or invalid package list provided to opkg.packages", diff --git a/tests/operations/opkg.packages/null_package_list.json b/tests/operations/opkg.packages/null_package_list.json index 42e795cac..1d8ee123d 100644 --- a/tests/operations/opkg.packages/null_package_list.json +++ b/tests/operations/opkg.packages/null_package_list.json @@ -1,7 +1,7 @@ { "args": [], "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [], "noop_description": "empty or invalid package list provided to opkg.packages", diff --git a/tests/operations/opkg.packages/remove_existing_package.json b/tests/operations/opkg.packages/remove_existing_package.json index c56aee400..e7adb868e 100644 --- a/tests/operations/opkg.packages/remove_existing_package.json +++ b/tests/operations/opkg.packages/remove_existing_package.json @@ -5,7 +5,7 @@ "update": false }, "facts": { - "opkg.Packages": {"curl": ["7.66.0-2"]} + "opkg.OpkgPackages": {"curl": ["7.66.0-2"]} }, "commands": [ "opkg remove curl" diff --git a/tests/operations/opkg.packages/remove_existing_package_and_require_update.json b/tests/operations/opkg.packages/remove_existing_package_and_require_update.json index 6b9ba3f80..d083fe4af 100644 --- a/tests/operations/opkg.packages/remove_existing_package_and_require_update.json +++ b/tests/operations/opkg.packages/remove_existing_package_and_require_update.json @@ -5,10 +5,10 @@ "update": true }, "facts": { - "opkg.Packages": {"curl": ["7.66.0-2"]} + "opkg.OpkgPackages": {"curl": ["7.66.0-2"]} }, "commands": [ - "opkg update > /dev/null 2>&1", + "opkg update", "opkg remove curl" ] } diff --git a/tests/operations/opkg.packages/remove_not_existing_package.json b/tests/operations/opkg.packages/remove_not_existing_package.json index cc783d2c1..b68ce53d8 100644 --- a/tests/operations/opkg.packages/remove_not_existing_package.json +++ b/tests/operations/opkg.packages/remove_not_existing_package.json @@ -5,7 +5,7 @@ "update": false }, "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [ ], diff --git a/tests/operations/opkg.packages/update_then_add_one.json b/tests/operations/opkg.packages/update_then_add_one.json index 539e0725b..907b23e9a 100644 --- a/tests/operations/opkg.packages/update_then_add_one.json +++ b/tests/operations/opkg.packages/update_then_add_one.json @@ -2,10 +2,10 @@ "args": ["curl"], "kwargs": {"update": true}, "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [ - "opkg update > /dev/null 2>&1", + "opkg update", "opkg install curl" ] } diff --git a/tests/operations/opkg.update/first_update.json b/tests/operations/opkg.update/first_update.json index c4ece2ed1..ea2e6b3ba 100644 --- a/tests/operations/opkg.update/first_update.json +++ b/tests/operations/opkg.update/first_update.json @@ -1,9 +1,9 @@ { "args": [], "facts": { - "opkg.Packages": {} + "opkg.OpkgPackages": {} }, "commands": [ - "opkg update > /dev/null 2>&1" + "opkg update" ] } diff --git a/tests/operations/opkg.update/opkg_second_update_forced.json b/tests/operations/opkg.update/opkg_second_update_forced.json deleted file mode 100644 index 76bd6d894..000000000 --- a/tests/operations/opkg.update/opkg_second_update_forced.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "args": [], - "kwargs": {"force": true}, - "facts": { - "opkg.Packages": {"$$updated$$": ["$$updated$$"]} - }, - "commands": [ - "opkg update > /dev/null 2>&1" - ] -} diff --git a/tests/operations/opkg.update/second_update.json b/tests/operations/opkg.update/second_update.json deleted file mode 100644 index 4cfa4a8bb..000000000 --- a/tests/operations/opkg.update/second_update.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "args": [], - "facts": { - "opkg.Packages": {"$$updated$$": ["$$updated$$"]} - }, - "commands": [ - ], - "noop_description": "opkg packages already updated and not forced" -} diff --git a/tests/operations/opkg.update/second_update_with_force.json b/tests/operations/opkg.update/second_update_with_force.json deleted file mode 100644 index 76bd6d894..000000000 --- a/tests/operations/opkg.update/second_update_with_force.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "args": [], - "kwargs": {"force": true}, - "facts": { - "opkg.Packages": {"$$updated$$": ["$$updated$$"]} - }, - "commands": [ - "opkg update > /dev/null 2>&1" - ] -} diff --git a/tests/words.txt b/tests/words.txt index a407f15ce..d6701aa3a 100644 --- a/tests/words.txt +++ b/tests/words.txt @@ -442,3 +442,4 @@ yaml yarn zfill zypper +usign