Skip to content

Commit

Permalink
Add flatpak packages operations & facts (pyinfra-dev#1129)
Browse files Browse the repository at this point in the history
* 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
JustScreaMy authored Aug 10, 2024
1 parent a509f95 commit e57e779
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pyinfra/facts/flatpak.py
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:]]
79 changes: 79 additions & 0 deletions pyinfra/operations/flatpak.py
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)
32 changes: 32 additions & 0 deletions tests/facts/flatpak.FlatpakPackage/kodi.json
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"
}
}
9 changes: 9 additions & 0 deletions tests/facts/flatpak.FlatpakPackage/not_found.json
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": {}
}
32 changes: 32 additions & 0 deletions tests/facts/flatpak.FlatpakPackage/vlc.json
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"
}
}
17 changes: 17 additions & 0 deletions tests/facts/flatpak.FlatpakPackages/packages.json
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"
]
}
10 changes: 10 additions & 0 deletions tests/operations/flatpak.package/install_package.json
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"]
}
16 changes: 16 additions & 0 deletions tests/operations/flatpak.package/install_packages.json
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"]
}
12 changes: 12 additions & 0 deletions tests/operations/flatpak.package/remove_packages.json
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"
}

0 comments on commit e57e779

Please sign in to comment.