forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flatpak packages operations & facts (pyinfra-dev#1129)
* added flatpak support * removed unused variable * fixing mypy issues * fixed local failing tests * added non-interactive to the commands asd\ * linting problem fix
- Loading branch information
1 parent
a509f95
commit e57e779
Showing
9 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
|
||
from pyinfra.api import FactBase | ||
|
||
|
||
class FlatpakBaseFact(FactBase): | ||
abstract = True | ||
|
||
def requires_command(self, *args, **kwargs) -> str: | ||
return "flatpak" | ||
|
||
|
||
class FlatpakPackage(FlatpakBaseFact): | ||
""" | ||
Returns information for an installed flatpak package | ||
.. code:: python | ||
{ | ||
"id": "org.signal.Signal", | ||
"ref": "app/org.signal.Signal/x86_64/stable", | ||
"version": "7.12.0" | ||
} | ||
""" | ||
|
||
default = dict | ||
_regexes = { | ||
"id": "^[ ]+ID:[ ]+(.*)$", | ||
"ref": r"^[ ]+Ref:[ ]+(.*)$", | ||
"version": r"^[ ]+Version:[ ]+([\w\d.-]+).*$", | ||
} | ||
|
||
def command(self, package): | ||
return f"flatpak info {package}" | ||
|
||
def process(self, output): | ||
data = {} | ||
for line in output: | ||
for regex_name, regex in self._regexes.items(): | ||
matches = re.match(regex, line) | ||
if matches: | ||
data[regex_name] = matches.group(1) | ||
|
||
return data | ||
|
||
|
||
class FlatpakPackages(FlatpakBaseFact): | ||
""" | ||
Returns a list of installed flatpak packages: | ||
.. code:: python | ||
[ | ||
"org.gnome.Platform", | ||
"org.kde.Platform", | ||
"org.kde.Sdk", | ||
"org.libreoffice.LibreOffice", | ||
"org.videolan.VLC" | ||
] | ||
""" | ||
|
||
default = list | ||
|
||
def command(self): | ||
return "flatpak list --columns=application" | ||
|
||
def process(self, output): | ||
return [flatpak for flatpak in output[1:]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Manage flatpak packages. See https://www.flatpak.org/ | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from pyinfra import host | ||
from pyinfra.api import operation | ||
from pyinfra.facts.flatpak import FlatpakPackages | ||
|
||
|
||
@operation() | ||
def package( | ||
packages: str | list[str] | None = None, | ||
present=True, | ||
): | ||
""" | ||
Install/remove a flatpak package | ||
+ packages: List of packages | ||
+ present: whether the package should be installed | ||
**Examples:** | ||
.. code:: python | ||
# Install vlc flatpak | ||
flatpak.package( | ||
name="Install vlc", | ||
packages="org.videolan.VLC", | ||
) | ||
# Install multiple flatpaks | ||
flatpak.package( | ||
name="Install vlc and kodi", | ||
packages=["org.videolan.VLC", "tv.kodi.Kodi"], | ||
) | ||
# Remove vlc | ||
flatpak.package( | ||
name="Remove vlc", | ||
packages="org.videolan.VLC", | ||
present=False, | ||
) | ||
""" | ||
|
||
if packages is None: | ||
return | ||
|
||
if isinstance(packages, str): | ||
packages = [packages] | ||
|
||
flatpak_packages = host.get_fact(FlatpakPackages) | ||
|
||
install_packages = [] | ||
remove_packages = [] | ||
|
||
for package in packages: | ||
# it's installed | ||
if package in flatpak_packages: | ||
if not present: | ||
# we don't want it | ||
remove_packages.append(package) | ||
|
||
# it's not installed | ||
if package not in flatpak_packages: | ||
# we want it | ||
if present: | ||
install_packages.append(package) | ||
|
||
# we don't want it | ||
else: | ||
host.noop(f"flatpak package {package} is not installed") | ||
|
||
if install_packages: | ||
yield " ".join(["flatpak", "install", "--noninteractive"] + install_packages) | ||
|
||
if remove_packages: | ||
yield " ".join(["flatpak", "uninstall", "--noninteractive"] + remove_packages) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"arg": "tv.kodi.Kodi", | ||
"command": "flatpak info tv.kodi.Kodi", | ||
"requires_command": "flatpak", | ||
"output": [ | ||
"", | ||
"Kodi - Ultimate entertainment center", | ||
"", | ||
" ID: tv.kodi.Kodi", | ||
" Ref: app/tv.kodi.Kodi/x86_64/stable", | ||
" Arch: x86_64", | ||
" Branch: stable", | ||
" Version: 21.0-Omega", | ||
" License: GPL-2.0-only GPL-2.0-or-later LGPL-2.1-or-later MIT BSD-3-Clause BSD-4-Clause", | ||
" Origin: flathub", | ||
" Collection: org.flathub.Stable", | ||
"Installation: system", | ||
" Installed: 514.7 MB", | ||
" Runtime: org.freedesktop.Platform/x86_64/23.08", | ||
" Sdk: org.freedesktop.Sdk/x86_64/23.08", | ||
"", | ||
" Commit: 03934475318a3a17476eed0f73e2d8339194373109f324868fc22484cbd7276e", | ||
" Parent: cb0195ac49050d0e16c840244de4c99061453102a270f6f930f3186a25bcd562", | ||
" Subject: Update addons (787f64c4)", | ||
" Date: 2024-06-01 18:22:30 +0000" | ||
], | ||
"fact": { | ||
"id": "tv.kodi.Kodi", | ||
"ref": "app/tv.kodi.Kodi/x86_64/stable", | ||
"version": "21.0-Omega" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"arg": "not_found.test.test", | ||
"command": "flatpak info not_found.test.test", | ||
"requires_command": "flatpak", | ||
"output": [ | ||
"error: not_found.test.test/*unspecified*/*unspecified* not installed" | ||
], | ||
"fact": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"arg": "org.videolan.VLC", | ||
"command": "flatpak info org.videolan.VLC", | ||
"requires_command": "flatpak", | ||
"output": [ | ||
"", | ||
"VLC - VLC media player, the open-source multimedia player", | ||
"", | ||
" ID: org.videolan.VLC", | ||
" Ref: app/org.videolan.VLC/x86_64/stable", | ||
" Arch: x86_64", | ||
" Branch: stable", | ||
" Version: 3.0.21", | ||
" License: GPL-2.0+", | ||
" Origin: flathub", | ||
" Collection: org.flathub.Stable", | ||
"Installation: system", | ||
" Installed: 98.9 MB", | ||
" Runtime: org.kde.Platform/x86_64/5.15-23.08", | ||
" Sdk: org.kde.Sdk/x86_64/5.15-23.08", | ||
"", | ||
" Commit: 00fce8e80caa0b5a74e4fd8baeda39e5450f406e9992586a578ec991258c927c", | ||
" Parent: 4905cf9d41d96f96b967ea4150a7f5d06930158f125fd41603936ddcd006aaaa", | ||
" Subject: Update VLC to 3.0.21 and some deps (c6a6bb62)", | ||
" Date: 2024-06-09 13:21:31 +0000" | ||
], | ||
"fact": { | ||
"id": "org.videolan.VLC", | ||
"ref": "app/org.videolan.VLC/x86_64/stable", | ||
"version": "3.0.21" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"command": "flatpak list --columns=application", | ||
"requires_command": "flatpak", | ||
"output": [ | ||
"Application ID", | ||
"com.bitwarden.desktop", | ||
"com.mattermost.Desktop", | ||
"org.videolan.VLC", | ||
"tv.kodi.Kodi" | ||
], | ||
"fact": [ | ||
"com.bitwarden.desktop", | ||
"com.mattermost.Desktop", | ||
"org.videolan.VLC", | ||
"tv.kodi.Kodi" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"kwargs": { | ||
"packages": ["org.videolan.VLC"] | ||
}, | ||
"facts": { | ||
"flatpak.FlatpakPackage": {}, | ||
"flatpak.FlatpakPackages": [] | ||
}, | ||
"commands": ["flatpak install --noninteractive org.videolan.VLC"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"kwargs": { | ||
"packages": ["org.videolan.VLC", "tv.kodi.Kodi"] | ||
}, | ||
"facts": { | ||
"flatpak.FlatpakPackage": { | ||
"package=org.videolan.VLC": { | ||
"id": "org.videolan.VLC", | ||
"ref": "app/org.videolan.VLC/x86_64/stable", | ||
"version": "3.0.21" | ||
} | ||
}, | ||
"flatpak.FlatpakPackages": ["org.videolan.VLC"] | ||
}, | ||
"commands": ["flatpak install --noninteractive tv.kodi.Kodi"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"kwargs": { | ||
"packages": ["org.videolan.VLC", "tv.kodi.Kodi"], | ||
"present": false | ||
}, | ||
"facts": { | ||
"flatpak.FlatpakPackage": {}, | ||
"flatpak.FlatpakPackages": ["org.videolan.VLC"] | ||
}, | ||
"commands": ["flatpak uninstall --noninteractive org.videolan.VLC"], | ||
"noop_description": "flatpak package tv.kodi.Kodi is not installed" | ||
} |