[debian] initial Debian package management support

This commit is contained in:
Vinicius Moreira
2022-03-10 17:11:19 -03:00
parent 9d88355b5c
commit f6c8ad9553
70 changed files with 4011 additions and 45 deletions

View File

@@ -0,0 +1,3 @@
import os
DEBIAN_TESTS_DIR = os.path.dirname(os.path.abspath(__file__))

View File

@@ -0,0 +1,17 @@
{
"firefox": {
"categories": [
"GNOME",
"GTK",
"Network",
"WebBrowser"
],
"exe_path": "firefox %u",
"icon_path": "firefox"
},
"synaptic": {
"exe_path": "synaptic-pkexec",
"icon_path": "synaptic"
}
}

View File

@@ -0,0 +1,169 @@
from unittest import TestCase
from unittest.mock import Mock, patch
from bauh import __app_name__
from bauh.commons import system
from bauh.commons.system import USE_GLOBAL_INTERPRETER
from bauh.gems.debian.aptitude import Aptitude, map_package_name
from bauh.gems.debian.model import DebianPackage
class MapPackageNameTest(TestCase):
def test__it_must_return_the_same_input_when_no_colon(self):
self.assertEqual('my_package', map_package_name('my_package'))
def test__it_must_return_the_only_the_word_before_the_first_colon(self):
self.assertEqual('my_package', map_package_name('my_package:i386'))
def test__it_must_return_a_name_with_colon_when_several_colons_are_present(self):
self.assertEqual('my_package:i386', map_package_name('my_package:i386:amd64'))
class AptitudeTest(TestCase):
def setUp(self):
self.aptitude = Aptitude(Mock())
@patch(f'{__app_name__}.gems.debian.aptitude.system.execute', return_value=(0, """
gimp-cbmplugs^<none>^1.2.2-1build1^Distro Developers <distro-devel-discuss@lists.distro.com>^universe/graphics^plugins for The GIMP to import/export Commodore 64 files
gimp-gmic^2.4.5-1.0^2.4.5-1.1^Distro Developers <distro-devel-discuss@lists.distro.com>^plugin^GREYC's Magic for Image Computing - GIMP Plugin
gimp-gutenprint^5.3.3-4^5.3.3-4^Distro Developers <distro-devel-discuss@lists.distro.com>^plugin^print plugin for the GIMP
gimp-help^<none>^<none>^<none>^^
"""))
def test_search__must_return_installed_and_not_installed_packages_with_updates(self, execute: Mock):
query = 'gimp'
res = [p for p in self.aptitude.search(query=query)]
execute.assert_called_once_with(f"aptitude search {query} -q -F '%p^%v^%V^%m^%s^%d' --disable-columns",
shell=True, custom_env=system.gen_env(USE_GLOBAL_INTERPRETER, lang=''))
exp = [
DebianPackage(name='gimp-cbmplugs', version='1.2.2-1build1', latest_version='1.2.2-1build1',
maintainer='Distro Developers',
description='plugins for The GIMP to import/export Commodore 64 files',
categories=('graphics',),
installed=False, update=False),
DebianPackage(name='gimp-gmic', version='2.4.5-1.0', latest_version='2.4.5-1.1',
maintainer='Distro Developers',
description="GREYC's Magic for Image Computing - GIMP Plugin",
categories=('plugin',),
installed=True, update=True),
DebianPackage(name='gimp-gutenprint', version='5.3.3-4', latest_version='5.3.3-4',
maintainer='Distro Developers',
description="print plugin for the GIMP",
categories=('plugin',),
installed=True, update=False),
]
self.assertEqual([p.__dict__ for p in exp], [p.__dict__ for p in res])
@patch(f'{__app_name__}.gems.debian.aptitude.system.execute', return_value=(0, """
Package: firefox
Version: 97.0+distro1+una
State: installed (95.0.1+distro1.1+una), upgrade available (97.0+distro1+una)
Automatically installed: no
Priority: optional
Section: web
Maintainer: Distro Dev <root@distro.com>
Architecture: amd64
Uncompressed Size: 236 M
PreDepends: distro-system-adjustments (>= 2021.12.16)
Breaks: firefox-dbg (< 95.0.1+distro1+una), firefox-dev (< 95.0.1+distro1+una), firefox-geckodriver (< 95.0.1+distro1+una), firefox-mozsymbols (< 95.0.1+distro1+una)
Replaces: firefox-dbg (< 95.0.1+distro1+una), firefox-dev (< 95.0.1+distro1+una), firefox-geckodriver (< 95.0.1+distro1+una), firefox-mozsymbols (< 95.0.1+distro1+una)
Provides: gnome-www-browser, www-browser
Description: The Firefox web browser
The Mozilla Firefox Web Browser.
Package: gcc
Version: 4:9.3.0-1distro2
State: installed
Automatically installed: no
Priority: optional
Section: devel
Maintainer: Distro Developers <distro-devel-discuss@lists.distro.com>
Architecture: amd64
Uncompressed Size: 51,2 k
Depends: cpp (= 4:9.3.0-1distro2), gcc-9 (>= 9.3.0-3~)
Recommends: libc6-dev | libc-dev
Suggests: gcc-multilib, make, manpages-dev, autoconf, automake, libtool, flex, bison, gdb, gcc-doc
Conflicts: gcc-doc (< 1:2.95.3), gcc-doc:i386 (< 1:2.95.3), gcc:i386
Provides: c-compiler, gcc-x86-64-linux-gnu (= 4:9.3.0-1distro2), gcc:amd64 (= 4:9.3.0-1distro2)
Description: GNU C compiler
This is the GNU C compiler, a fairly portable optimizing compiler for C.
This is a dependency package providing the default GNU C compiler.
"""))
def test_show__all_attributes(self, execute: Mock):
info = self.aptitude.show(('firefox', 'gcc'))
execute.assert_called_once_with('aptitude show -q firefox gcc', shell=True,
custom_env=system.gen_env(global_interpreter=system.USE_GLOBAL_INTERPRETER,
lang=''))
expected = {
'firefox': {
'version': '97.0+distro1+una',
'state': ('installed (95.0.1+distro1.1+una)', 'upgrade available (97.0+distro1+una)'),
'automatically installed': 'no',
'priority': 'optional',
'section': 'web',
'maintainer': 'Distro Dev <root@distro.com>',
'architecture': 'amd64',
'uncompressed size': 236000000,
'predepends': ('distro-system-adjustments (>= 2021.12.16)', ),
'breaks': ('firefox-dbg (< 95.0.1+distro1+una)', 'firefox-dev (< 95.0.1+distro1+una)',
'firefox-geckodriver (< 95.0.1+distro1+una)', 'firefox-mozsymbols (< 95.0.1+distro1+una)'),
'replaces': ('firefox-dbg (< 95.0.1+distro1+una)', 'firefox-dev (< 95.0.1+distro1+una)',
'firefox-geckodriver (< 95.0.1+distro1+una)', 'firefox-mozsymbols (< 95.0.1+distro1+una)'),
'provides': ('gnome-www-browser', 'www-browser'),
'description': 'The Firefox web browser'
},
'gcc': {
'version': '4:9.3.0-1distro2',
'state': ('installed', ),
'automatically installed': 'no',
'priority': 'optional',
'section': 'devel',
'maintainer': 'Distro Developers <distro-devel-discuss@lists.distro.com>',
'architecture': 'amd64',
'uncompressed size': 51200,
'depends': ('cpp (= 4:9.3.0-1distro2)', 'gcc-9 (>= 9.3.0-3~)'),
'recommends': ('libc6-dev | libc-dev', ),
'suggests': ('gcc-multilib', 'make', 'manpages-dev', 'autoconf', 'automake',
'libtool', 'flex', 'bison', 'gdb', 'gcc-doc'),
'conflicts': ('gcc-doc (< 1:2.95.3)', 'gcc-doc:i386 (< 1:2.95.3)', 'gcc:i386'),
'provides': ('c-compiler', 'gcc-x86-64-linux-gnu (= 4:9.3.0-1distro2)', 'gcc:amd64 (= 4:9.3.0-1distro2)'),
'description': 'GNU C compiler'
}
}
self.assertEqual(expected, info)
@patch(f'{__app_name__}.gems.debian.aptitude.system.execute', return_value=(0, """
gir1.2-javascriptcoregtk-4.0^2.34.1-0distro0.20.04.1^2.34.4-0distro0.20.04.1^Distro Developers <distro-devel-discuss@lists.distro.com>^library^JavaScript engine library from WebKitGTK - GObject introspection data
gir1.2-nm-1.0^1.22.10-1distro2.2^1.22.10-1distro2.3^Distro Developers <distro-devel-discuss@lists.distro.com>^library^GObject introspection data for the libnm library
xwayland^2:1.20.13-1distro1~20.04.2^2:1.20.13-1distro1~20.04.2^Distro X-SWAT <distro-x@lists.distro.com>^X11^Xwayland X server
"""))
def test_read_installed__with_updates_available(self, execute: Mock):
returned = [p for p in self.aptitude.read_installed()]
execute.assert_called_once()
expected = [DebianPackage(name='gir1.2-javascriptcoregtk-4.0', version='2.34.1-0distro0.20.04.1',
latest_version='2.34.4-0distro0.20.04.1',
maintainer='Distro Developers', update=True, installed=True,
categories=('library',),
description='JavaScript engine library from WebKitGTK - GObject introspection data'),
DebianPackage(name='gir1.2-nm-1.0', version='1.22.10-1distro2.2',
latest_version='1.22.10-1distro2.3',
maintainer='Distro Developers', update=True, installed=True,
categories=('library',),
description='GObject introspection data for the libnm library'),
DebianPackage(name='xwayland', version='2:1.20.13-1distro1~20.04.2',
latest_version='2:1.20.13-1distro1~20.04.2',
maintainer='Distro X-SWAT', update=False, installed=True,
categories=('X11',),
description='Xwayland X server')
]
self.assertEqual([p.__dict__ for p in expected], [p.__dict__ for p in returned])

View File

@@ -0,0 +1,99 @@
from unittest import TestCase
from unittest.mock import MagicMock, patch, Mock
from bauh import __app_name__
from bauh.api.abstract.controller import SearchResult
from bauh.gems.debian.controller import DebianPackageManager
from bauh.gems.debian.model import DebianPackage, DebianApplication
class DebianPackageManagerTest(TestCase):
def setUp(self):
self.controller = DebianPackageManager(MagicMock())
self.controller._apps_index = {}
@patch(f'{__app_name__}.gems.debian.controller.Aptitude.read_installed', return_value=iter((
DebianPackage(name='gir1.2-javascriptcoregtk-4.0', version='2.34.1-0distro0.20.04.1',
latest_version='2.34.1-0distro0.20.04.1',
maintainer='Distro Developers', update=False, installed=True,
description='JavaScript engine library from WebKitGTK - GObject introspection data'),
DebianPackage(name='xwayland', version='2:1.20.13-1distro1~20.04.2',
latest_version='2:1.20.13-1distro1~20.04.2',
maintainer='Distro X-SWAT', update=False, installed=True,
description='Xwayland X server')
)))
def test_read_installed__must_associated_packages_found_to_applications_if_appliable(self, read_installed: Mock):
app = DebianApplication(name='xwayland', exe_path='xwayland', icon_path='xwayland', categories=('app',))
self.controller.__apps_index = {'xwayland': app}
result = self.controller.read_installed(disk_loader=None, pkg_types=None, internet_available=False)
read_installed.assert_called_once()
self.assertIsNone(result.new)
expected = [DebianPackage(name='gir1.2-javascriptcoregtk-4.0', version='2.34.1-0distro0.20.04.1', latest_version='2.34.1-0distro0.20.04.1',
maintainer='Distro Developers', update=False, installed=True,
description='JavaScript engine library from WebKitGTK - GObject introspection data'),
DebianPackage(name='xwayland', version='2:1.20.13-1distro1~20.04.2', latest_version='2:1.20.13-1distro1~20.04.2',
maintainer='Distro X-SWAT', update=False, installed=True,
description='Xwayland X server', app=app)
]
self.assertEqual(expected, result.installed)
@patch(f'{__app_name__}.gems.debian.controller.Aptitude.read_installed', return_value=iter((
DebianPackage(name='gir1.2-javascriptcoregtk-4.0', version='2.34.1-0distro0.20.04.1',
latest_version='2.34.1-0distro0.20.04.1',
maintainer='Distro Developers', update=False, installed=True,
description='JavaScript engine library from WebKitGTK - GObject introspection data'),
DebianPackage(name='xwayland', version='2:1.20.13-1distro1~20.04.2',
latest_version='2:1.20.13-1distro1~20.04.2',
maintainer='Distro X-SWAT', update=False, installed=True,
description='Xwayland X server')
)))
def test_read_installed__internet_not_available(self, read_installed: Mock):
result = self.controller.read_installed(disk_loader=None, pkg_types=None, internet_available=False)
read_installed.assert_called_once()
self.assertIsNone(result.new)
expected = [DebianPackage(name='gir1.2-javascriptcoregtk-4.0', version='2.34.1-0distro0.20.04.1', latest_version='2.34.1-0distro0.20.04.1',
maintainer='Distro Developers', update=False, installed=True,
description='JavaScript engine library from WebKitGTK - GObject introspection data'),
DebianPackage(name='xwayland', version='2:1.20.13-1distro1~20.04.2', latest_version='2:1.20.13-1distro1~20.04.2',
maintainer='Distro X-SWAT', update=False, installed=True,
description='Xwayland X server')
]
self.assertEqual(expected, result.installed)
@patch(f'{__app_name__}.gems.debian.controller.Aptitude.read_installed')
def test_search__must_return_empty_result_when_url(self, read_installed: Mock):
words = 'i'
res = self.controller.search(words=words, disk_loader=None, limit=-1, is_url=True)
read_installed.assert_not_called()
self.assertEqual(SearchResult.empty(), res)
@patch(f'{__app_name__}.gems.debian.controller.Aptitude.search', return_value=iter((
DebianPackage(name='xpto', version='1.0', latest_version='1.0', installed=True, update=False, description=''),
DebianPackage(name='test', version='1.0', latest_version='1.0', installed=False, update=False, description=''),
DebianPackage(name='myapp', version='1.0', latest_version='1.0', installed=True, update=False, description=''),
)))
def test_search__returned_packages_should_be_associated_with_apps_if_appliable(self, search: Mock):
app = DebianApplication(name='myapp', exe_path='myapp', icon_path='myapp',
categories=('app',))
self.controller._apps_index = {'myapp': app}
words = 'test'
res = self.controller.search(words=words, disk_loader=None, limit=-1, is_url=False)
search.assert_called_once_with(words)
self.assertEqual([DebianPackage(name='xpto', version='1.0', latest_version='1.0', installed=True, update=False, description=''),
DebianPackage(name='myapp', version='1.0', latest_version='1.0', installed=True, update=False,
description='', app=app)
], res.installed)
self.assertEqual([DebianPackage(name='test', version='1.0', latest_version='1.0', installed=False, update=False, description='')],
res.new)

View File

@@ -0,0 +1,171 @@
import json
import os.path
from unittest import TestCase
from unittest.mock import Mock, patch, call
from bauh import __app_name__
from bauh.gems.debian.index import ApplicationsMapper, ApplicationIndexer
from bauh.gems.debian.model import DebianApplication
from tests.gems.debian import DEBIAN_TESTS_DIR
def mock_read_file(fpath: str):
if fpath.endswith('firefox.desktop'):
return """
[Desktop Entry]
Version=1.0
Name=Firefox Web Browser
Comment=Browse the World Wide Web
GenericName=Web Browser
Keywords=Internet;WWW;Browser;Web;Explorer
Exec=firefox %u
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=firefox
Categories=GNOME;GTK;Network;WebBrowser;
"""
elif fpath.endswith('synaptic.desktop'):
return """
[Desktop Entry]
Name=Synaptic Package Manager
GenericName=Package Manager
Comment=Install, remove and upgrade software packages
Exec=synaptic-pkexec
Icon=synaptic
Terminal=false
Type=Application
"""
elif fpath.endswith('no-icon.desktop'):
return """
[Desktop Entry]
Name=No-icon
Exec=no-icon
Terminal=false
Type=Application
Categories=PackageManager;GTK;System;Settings;
"""
elif fpath.endswith('no-exe.desktop'):
return """
[Desktop Entry]
Name=No-exe
Terminal=false
Type=Application
Icon=no-exe
Categories=PackageManager;GTK;System;Settings;
"""
elif fpath.endswith('no-display.desktop'):
return """
[Desktop Entry]
Name=No-display
Exec=no-display
Terminal=false
Type=Application
Icon=no-display
NoDisplay=true
Categories=PackageManager;GTK;System;Settings;
"""
elif fpath.endswith('terminal-app.desktop'):
return """
[Desktop Entry]
Name=Terminal-App
Exec=terminal-app
Terminal=true
Type=Application
Icon=terminal-app
Categories=PackageManager;GTK;System;Settings;
"""
class ApplicationsMapperTest(TestCase):
def setUp(self):
self.mapper = ApplicationsMapper(logger=Mock(), workers=1)
@patch(f'{__app_name__}.gems.debian.index.ApplicationsMapper._read_file', side_effect=mock_read_file)
@patch(f'{__app_name__}.gems.debian.index.system.execute', return_value=(0, """
firefox: /usr/share/applications/firefox.desktop
app-install-data: /usr/share/app-install/desktop/firefox-launchpad-plugin.desktop
app-install-data: /usr/share/app-install/desktop/firefox:firefox.desktop
xfce4-helpers: /usr/share/xfce4/helpers/firefox.desktop
synaptic: /usr/share/applications/synaptic.desktop
no-icon: /usr/share/applications/no-icon.desktop
no-exe: /usr/share/applications/no-exe.desktop
no-display: /usr/share/applications/no-display.desktop
terminal-app: /usr/share/applications/terminal-app.desktop
"""))
def test_map_executable_applications__return_applications_with_exec_and_icon(self, execute: Mock, read_file: Mock):
apps = self.mapper.map_executable_applications()
execute.assert_called_once_with('dpkg-query -S .desktop', shell=True)
read_file.assert_has_calls([call('/usr/share/applications/firefox.desktop'),
call('/usr/share/applications/synaptic.desktop'),
call('/usr/share/applications/no-icon.desktop'),
call('/usr/share/applications/no-exe.desktop')], any_order=True)
self.assertEqual({
DebianApplication(name='firefox', exe_path='firefox %u', icon_path='firefox',
categories=('GNOME', 'GTK', 'Network', 'WebBrowser')),
DebianApplication(name='synaptic', exe_path='synaptic-pkexec', icon_path='synaptic',
categories=None)
}, apps)
class ApplicationIndexerTest(TestCase):
def setUp(self):
self.update_idx_file_path = f'{DEBIAN_TESTS_DIR}/resources/apps_idx.json'
self.update_idx_ts_file_path = f'{self.update_idx_file_path}.ts'
self.app_indexer = ApplicationIndexer(logger=Mock(),
index_file_path=self.update_idx_file_path)
if os.path.exists(self.update_idx_file_path):
os.remove(self.update_idx_file_path)
def tearDown(self) -> None:
if os.path.exists(self.update_idx_file_path):
os.remove(self.update_idx_file_path)
if os.path.exists(self.update_idx_ts_file_path):
os.remove(self.update_idx_ts_file_path)
def test_update_index(self):
apps = {
DebianApplication(name='firefox', exe_path='firefox %u', icon_path='firefox',
categories=('GNOME', 'GTK', 'Network', 'WebBrowser')),
DebianApplication(name='synaptic', exe_path='synaptic-pkexec', icon_path='synaptic',
categories=None)
}
self.app_indexer.update_index(apps)
self.assertTrue(os.path.exists(self.update_idx_file_path))
with open(self.update_idx_file_path) as f:
index_content = f.read()
self.assertEqual({'firefox': {'exe_path': 'firefox %u', 'icon_path': 'firefox',
'categories': ['GNOME', 'GTK', 'Network', 'WebBrowser']},
'synaptic': {'exe_path': 'synaptic-pkexec', 'icon_path': 'synaptic',
'categories': None}
}, json.loads(index_content))
self.assertTrue(os.path.isfile(self.update_idx_ts_file_path))
with open(self.update_idx_ts_file_path) as f:
ts_str = f.read()
try:
float(ts_str)
except ValueError:
self.assertFalse(False, "index timestamp must be a float number")
def test_read_index(self):
self.app_indexer._file_path = f'{DEBIAN_TESTS_DIR}/resources/app_idx_full.json'
expected = {
DebianApplication(name='firefox', exe_path='firefox %u', icon_path='firefox',
categories=('GNOME', 'GTK', 'Network', 'WebBrowser')),
DebianApplication(name='synaptic', exe_path='synaptic-pkexec', icon_path='synaptic',
categories=None)
}
self.assertEqual(expected, {app for app in self.app_indexer.read_index()})