Skip to content

Commit

Permalink
Rename all name arguments to Fact#command methods
Browse files Browse the repository at this point in the history
  • Loading branch information
martenlienen committed May 30, 2024
1 parent 0abd585 commit 8cb913d
Show file tree
Hide file tree
Showing 50 changed files with 94 additions and 101 deletions.
7 changes: 5 additions & 2 deletions pyinfra/facts/deb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import shlex

from pyinfra.api import FactBase

Expand Down Expand Up @@ -54,8 +55,10 @@ class DebPackage(FactBase):

requires_command = "dpkg"

def command(self, name):
return "! test -e {0} && (dpkg -s {0} 2>/dev/null || true) || dpkg -I {0}".format(name)
def command(self, package):
return "! test -e {0} && (dpkg -s {0} 2>/dev/null || true) || dpkg -I {0}".format(
shlex.quote(package)
)

def process(self, output):
data = {}
Expand Down
6 changes: 4 additions & 2 deletions pyinfra/facts/pacman.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import shlex

from pyinfra.api import FactBase

from .util.packaging import parse_packages
Expand All @@ -21,9 +23,9 @@ class PacmanUnpackGroup(FactBase):

default = list

def command(self, name):
def command(self, package):
# Accept failure here (|| true) for invalid/unknown packages
return 'pacman -S --print-format "%n" {0} || true'.format(name)
return 'pacman -S --print-format "%n" {0} || true'.format(shlex.quote(package))

def process(self, output):
return output
Expand Down
19 changes: 10 additions & 9 deletions pyinfra/facts/rpm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import shlex

from pyinfra.api import FactBase

Expand All @@ -19,7 +20,7 @@ class RpmPackages(FactBase):
}
"""

command = 'rpm --queryformat "{0}" -qa'.format(rpm_query_format)
command = "rpm --queryformat {0} -qa".format(shlex.quote(rpm_query_format))
requires_command = "rpm"

default = dict
Expand All @@ -42,12 +43,12 @@ class RpmPackage(FactBase):

requires_command = "rpm"

def command(self, name):
def command(self, package):
return (
'rpm --queryformat "{0}" -q {1} || '
"rpm --queryformat {0} -q {1} || "
"! test -e {1} || "
'rpm --queryformat "{0}" -qp {1} 2> /dev/null'
).format(rpm_query_format, name)
"rpm --queryformat {0} -qp {1} 2> /dev/null"
).format(shlex.quote(rpm_query_format), shlex.quote(package))

def process(self, output):
for line in output:
Expand All @@ -69,11 +70,11 @@ class RpmPackageProvides(FactBase):
requires_command = "repoquery"

@staticmethod
def command(name):
def command(package):
# Accept failure here (|| true) for invalid/unknown packages
return 'repoquery --queryformat "{0}" --whatprovides {1} || true'.format(
rpm_query_format,
name,
return "repoquery --queryformat {0} --whatprovides {1} || true".format(
shlex.quote(rpm_query_format),
shlex.quote(package),
)

@staticmethod
Expand Down
10 changes: 3 additions & 7 deletions pyinfra/operations/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,14 @@ def deb(src, present=True, force=False):
src = temp_filename

# Check for file .deb information (if file is present)
info = host.get_fact(DebPackage, name=src)
info = host.get_fact(DebPackage, package=src)
current_packages = host.get_fact(DebPackages)

exists = False

# We have deb info! Check against installed packages
if info:
if (
info["name"] in current_packages
and info.get("version") in current_packages[info["name"]]
):
exists = True
if info and info.get("version") in current_packages.get(info.get("name"), {}):
exists = True

# Package does not exist and we want?
if present:
Expand Down
5 changes: 1 addition & 4 deletions pyinfra/operations/dnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,5 @@ def packages(
upgrade_command="dnf update -y",
version_join="=",
latest=latest,
expand_package_fact=lambda package: host.get_fact(
RpmPackageProvides,
name=package,
),
expand_package_fact=lambda package: host.get_fact(RpmPackageProvides, package=package),
)
5 changes: 1 addition & 4 deletions pyinfra/operations/pacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,5 @@ def packages(
present,
install_command="pacman --noconfirm -S",
uninstall_command="pacman --noconfirm -R",
expand_package_fact=lambda package: host.get_fact(
PacmanUnpackGroup,
name=package,
),
expand_package_fact=lambda package: host.get_fact(PacmanUnpackGroup, package=package),
)
4 changes: 2 additions & 2 deletions pyinfra/operations/util/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ def ensure_rpm(state, host, files, source, present, package_manager_command):
source = temp_filename

# Check for file .rpm information
info = host.get_fact(RpmPackage, name=source)
info = host.get_fact(RpmPackage, package=source)
exists = False

# We have info!
if info:
current_package = host.get_fact(RpmPackage, name=info["name"])
current_package = host.get_fact(RpmPackage, package=info["name"])
if current_package and current_package["version"] == info["version"]:
exists = True

Expand Down
5 changes: 1 addition & 4 deletions pyinfra/operations/yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,5 @@ def packages(
upgrade_command="yum update -y",
version_join="=",
latest=latest,
expand_package_fact=lambda package: host.get_fact(
RpmPackageProvides,
name=package,
),
expand_package_fact=lambda package: host.get_fact(RpmPackageProvides, package=package),
)
2 changes: 1 addition & 1 deletion tests/facts/rpm.RpmPackage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "somepackage",
"command": "rpm --queryformat \"%{NAME} %{VERSION}-%{RELEASE}\\n\" -q somepackage || ! test -e somepackage || rpm --queryformat \"%{NAME} %{VERSION}-%{RELEASE}\\n\" -qp somepackage 2> /dev/null",
"command": "rpm --queryformat '%{NAME} %{VERSION}-%{RELEASE}\\n' -q somepackage || ! test -e somepackage || rpm --queryformat '%{NAME} %{VERSION}-%{RELEASE}\\n' -qp somepackage 2> /dev/null",
"requires_command": "rpm",
"output": [
"pydash 3.48.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/rpm.RpmPackageProvides/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "vim",
"command": "repoquery --queryformat \"%{NAME} %{VERSION}-%{RELEASE}\\n\" --whatprovides vim || true",
"command": "repoquery --queryformat '%{NAME} %{VERSION}-%{RELEASE}\\n' --whatprovides vim || true",
"requires_command": "repoquery",
"output": [
"vim-enhanced 8.0.1763-15.el8"
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/rpm.RpmPackages/packages.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"command": "rpm --queryformat \"%{NAME} %{VERSION}-%{RELEASE}\\n\" -qa",
"command": "rpm --queryformat '%{NAME} %{VERSION}-%{RELEASE}\\n' -qa",
"requires_command": "rpm",
"output": [
"pydash 3.48.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/apt.deb/add_existing.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"path=_tempfile_": ""
},
"deb.DebPackage": {
"name=_tempfile_": {
"package=_tempfile_": {
"name": "test",
"version": 0
}
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/apt.deb/remove_existing.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"path=_tempfile_": ""
},
"deb.DebPackage": {
"name=_tempfile_": {
"package=_tempfile_": {
"name": "test",
"version": 0
}
Expand Down
6 changes: 3 additions & 3 deletions tests/operations/dnf.packages/add_packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"something": [""]
},
"rpm.RpmPackageProvides": {
"name=git": null,
"name=python-devel": null,
"name=something": null
"package=git": null,
"package=python-devel": null,
"package=something": null
}
},
"commands": [
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/dnf.packages/install_with_args.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"facts": {
"rpm.RpmPackages": {},
"rpm.RpmPackageProvides": {
"name=docker-ce": null
"package=docker-ce": null
}
},
"commands": [
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/dnf.packages/install_with_nobest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"facts": {
"rpm.RpmPackages": {},
"rpm.RpmPackageProvides": {
"name=docker-ce": null
"package=docker-ce": null
}
},
"commands": [
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/dnf.packages/noop_add_package_alias.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"vim-enhanced": ["abc"]
},
"rpm.RpmPackageProvides": {
"name=vim": [["vim-enhanced", "abc"]]
"package=vim": [["vim-enhanced", "abc"]]
}
},
"commands": [],
Expand Down
6 changes: 3 additions & 3 deletions tests/operations/dnf.packages/remove_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"vim-common": ["8.2.1081-1.fc32"]
},
"rpm.RpmPackageProvides": {
"name=git": null,
"name=vim-enhanced": null,
"name=vim-common": null
"package=git": null,
"package=vim-enhanced": null,
"package=vim-common": null
}
},
"commands": [
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/dnf.packages/uninstall_with_args.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"git": [""]
},
"rpm.RpmPackageProvides": {
"name=git": null
"package=git": null
}
},
"commands": [
Expand Down
4 changes: 2 additions & 2 deletions tests/operations/dnf.rpm/add.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"facts": {
"rpm.RpmPackages": {},
"rpm.RpmPackage": {
"name=something.rpm": {
"package=something.rpm": {
"name": "something",
"version": "abc"
},
"name=something": null
"package=something": null
}
},
"commands": [
Expand Down
4 changes: 2 additions & 2 deletions tests/operations/dnf.rpm/add_existing.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"args": ["something.rpm"],
"facts": {
"rpm.RpmPackage": {
"name=something.rpm": {
"package=something.rpm": {
"name": "something",
"version": "1.1"
},
"name=something": {
"package=something": {
"version": "1.1"
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/operations/dnf.rpm/add_existing_upgrade.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"args": ["something.rpm"],
"facts": {
"rpm.RpmPackage": {
"name=something.rpm": {
"package=something.rpm": {
"name": "something",
"version": "1.1"
},
"name=something": {
"package=something": {
"version": "1.0"
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/dnf.rpm/add_url.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"path=_tempfile_.rpm": null
},
"rpm.RpmPackage": {
"name=_tempfile_.rpm": null
"package=_tempfile_.rpm": null
},
"server.Which": {
"command=curl": "yes"
Expand Down
4 changes: 2 additions & 2 deletions tests/operations/dnf.rpm/remove.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
},
"facts": {
"rpm.RpmPackage": {
"name=something.rpm": {
"package=something.rpm": {
"name": "something",
"version": "1.1"
},
"name=something": {
"package=something": {
"version": "1.1"
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/operations/pacman.packages/add_packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"args": [["curl", "xorg-fonts"]],
"facts": {
"pacman.PacmanUnpackGroup": {
"name=curl": ["curl"],
"name=xorg-fonts": ["xorg-font-util", "xorg-fonts-encodings"]
"package=curl": ["curl"],
"package=xorg-fonts": ["xorg-font-util", "xorg-fonts-encodings"]
},
"pacman.PacmanPackages": {}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"args": [["xorg-fonts"]],
"facts": {
"pacman.PacmanUnpackGroup": {
"name=xorg-fonts": ["xorg-font-util", "xorg-font-encodings"]
"package=xorg-fonts": ["xorg-font-util", "xorg-font-encodings"]
},
"pacman.PacmanPackages": {}
},
Expand Down
4 changes: 2 additions & 2 deletions tests/operations/pacman.packages/remove_packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
},
"facts": {
"pacman.PacmanUnpackGroup": {
"name=curl": ["curl"],
"name=i-dont-exist": []
"package=curl": ["curl"],
"package=i-dont-exist": []
},
"pacman.PacmanPackages": {
"curl": "1"
Expand Down
6 changes: 3 additions & 3 deletions tests/operations/pacman.packages/remove_packages_expand.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"facts": {
"pacman.PacmanUnpackGroup": {
"name=curl": ["curl"],
"name=i-dont-exist": [],
"name=xorg-fonts": ["xorg-font-util", "xorg-font-encodings"]
"package=curl": ["curl"],
"package=i-dont-exist": [],
"package=xorg-fonts": ["xorg-font-util", "xorg-font-encodings"]
},
"pacman.PacmanPackages": {
"curl": ["1"],
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/pacman.packages/upgrade_update.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"facts": {
"pacman.PacmanUnpackGroup": {
"name=curl": ["curl"]
"package=curl": ["curl"]
},
"pacman.PacmanPackages": {
"curl": ["1"]
Expand Down
6 changes: 3 additions & 3 deletions tests/operations/yum.packages/add_packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"something": [""]
},
"rpm.RpmPackageProvides": {
"name=git": null,
"name=python-devel": null,
"name=something": null
"package=git": null,
"package=python-devel": null,
"package=something": null
}
},
"commands": [
Expand Down
2 changes: 1 addition & 1 deletion tests/operations/yum.packages/add_packages_alias.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"facts": {
"rpm.RpmPackages": {},
"rpm.RpmPackageProvides": {
"name=vim": [["vim-enhanced", "1.2.3"]]
"package=vim": [["vim-enhanced", "1.2.3"]]
}
},
"commands": [
Expand Down
Loading

0 comments on commit 8cb913d

Please sign in to comment.