Skip to content

Commit 04468c2

Browse files
authored
operations.util.packaging: extend PkgInfo for winget
1 parent fc5ece9 commit 04468c2

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/pyinfra/operations/util/packaging.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
from pyinfra.operations import files
1616

1717

18+
def default_inst_vers_format_fn(name: str, operator: str, version: str):
19+
return "{name}{operator}{version}".format(name=name, operator=operator, version=version)
20+
21+
1822
class PkgInfo(NamedTuple):
1923
name: str
2024
version: str
2125
operator: str
2226
url: str
27+
inst_vers_format_fn: Callable = default_inst_vers_format_fn
2328
"""
2429
The key packaging information needed: version, operator and url are optional.
2530
"""
@@ -34,13 +39,22 @@ def has_version(self) -> bool:
3439

3540
@property
3641
def inst_vers(self) -> str:
37-
return (
38-
self.url
39-
if self.url != ""
40-
else (
41-
self.operator.join([self.name, self.version]) if self.version != "" else self.name
42-
)
43-
)
42+
"""String that represents how a program can be installed.
43+
44+
- If self.url exists, then url is always returned.
45+
- If self.version exists, then inst_vers_format_fn is used
46+
to create the string. The default template is '{name}{operator}{version}'.
47+
- Otherwise, self.name is returned.
48+
49+
Note, the result string will be quoted, so input is shell safe.
50+
"""
51+
52+
if self.url:
53+
return shlex.quote(self.url)
54+
55+
if self.version:
56+
return shlex.quote(self.inst_vers_format_fn(self.name, self.operator, self.version))
57+
return shlex.quote(self.name)
4458

4559
@classmethod
4660
def from_possible_pair(cls, s: str, join: str | None) -> PkgInfo:
@@ -194,7 +208,6 @@ def ensure_packages(
194208
host.noop(f"package {pkg} is installed ({','.join(current_packages[pkg])})")
195209
else:
196210
host.noop(f"package {package.name} is installed")
197-
198211
if present is False:
199212
for package in packages:
200213
has_package, expanded_packages = _has_package(
@@ -209,10 +222,10 @@ def ensure_packages(
209222

210223
if diff_packages:
211224
command = install_command if present else uninstall_command
212-
yield f"{command} {' '.join([shlex.quote(pkg) for pkg in diff_packages])}"
225+
yield f"{command} {' '.join([pkg for pkg in diff_packages])}"
213226

214227
if latest and upgrade_command and upgrade_packages:
215-
yield f"{upgrade_command} {' '.join([shlex.quote(pkg) for pkg in upgrade_packages])}"
228+
yield f"{upgrade_command} {' '.join([pkg for pkg in upgrade_packages])}"
216229

217230

218231
def ensure_rpm(state: State, host: Host, source: str, present: bool, package_manager_command: str):

0 commit comments

Comments
 (0)