def apt_cache(*_, **__): """Shim returning an object simulating the apt_pkg Cache. :param _: Accept arguments for compatibility, not used. :type _: any :param __: Accept keyword arguments for compatibility, not used. :type __: any :returns:Object used to interrogate the system apt and dpkg databases. :rtype:ubuntu_apt_pkg.Cache """ if 'apt_pkg' in sys.modules: # NOTE(fnordahl): When our consumer use the upstream ``apt_pkg`` module # in conjunction with the apt_cache helper function, they may expect us # to call ``apt_pkg.init()`` for them. # # Detect this situation, log a warning and make the call to # ``apt_pkg.init()`` to avoid the consumer Python interpreter from # crashing with a segmentation fault. @deprecate( 'Support for use of upstream ``apt_pkg`` module in conjunction' 'with charm-helpers is deprecated since 2019-06-25', date=None, log=lambda x: log(x, level=WARNING)) def one_shot_log(): pass one_shot_log() sys.modules['apt_pkg'].init() return ubuntu_apt_pkg.Cache()
def fetch_gitlab_apt_package(self): """Return reference to GitLab package information in the APT cache.""" self.add_sources() apt_update() apt_cache = ubuntu_apt_pkg.Cache() hookenv.log("Fetching package information for {}".format( self.package_name)) package = False try: package = apt_cache[self.package_name] except KeyError as ke: hookenv.log( "Fetching package information failed for {} with {}".format( self.package_name, ke)) package = False return package
def test_apt_cache_show(self): self.patch_object(apt_pkg.subprocess, 'check_output') apt_cache = apt_pkg.Cache() self.check_output.return_value = ( 'Package: dpkg\n' 'Version: 1.19.0.5ubuntu2.1\n' 'Bugs: https://bugs.launchpad.net/ubuntu/+filebug\n' 'Description-en: Debian package management system\n' ' Multiline description\n' '\n' 'Package: lsof\n' 'Architecture: amd64\n' 'Version: 4.91+dfsg-1ubuntu1\n' '\n' 'N: There is 1 additional record.\n') self.assertEquals( apt_cache._apt_cache_show(['package']), { 'dpkg': { 'package': 'dpkg', 'version': '1.19.0.5ubuntu2.1', 'bugs': 'https://bugs.launchpad.net/ubuntu/+filebug', 'description-en': 'Debian package management system\n' 'Multiline description' }, 'lsof': { 'package': 'lsof', 'architecture': 'amd64', 'version': '4.91+dfsg-1ubuntu1' }, }) self.check_output.assert_called_once_with( ['apt-cache', 'show', '--no-all-versions', 'package'], stderr=subprocess.STDOUT, universal_newlines=True)
def test_dpkg_list(self): self.patch_object(apt_pkg.subprocess, 'check_output') apt_cache = apt_pkg.Cache() self.check_output.return_value = ( 'Desired=Unknown/Install/Remove/Purge/Hold\n' '| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/' 'trig-aWait/Trig-pend\n' '|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)\n' '||/ Name Version Architecture Description\n' '+++-=============================-==================-===========-' '=================================\n' 'ii dpkg 1.19.0.5ubuntu2.1 amd64 ' 'Debian package management system\n' 'rc linux-image-4.15.0-42-generic 4.15.0-42.45 amd64 ' 'Signed kernel image generic\n' 'ii lsof 4.91+dfsg-1ubuntu1 amd64 ' 'utility to list open files\n') expect = { 'dpkg': { 'name': 'dpkg', 'version': '1.19.0.5ubuntu2.1', 'architecture': 'amd64', 'description': 'Debian package management system' }, 'lsof': { 'name': 'lsof', 'version': '4.91+dfsg-1ubuntu1', 'architecture': 'amd64', 'description': 'utility to list open files' }, } self.assertEquals(apt_cache._dpkg_list(['package']), expect) self.check_output.side_effect = subprocess.CalledProcessError( 1, '', output=self.check_output.return_value) self.assertEquals(apt_cache._dpkg_list(['package']), expect) self.check_output.side_effect = subprocess.CalledProcessError(2, '') with self.assertRaises(subprocess.CalledProcessError): _ = apt_cache._dpkg_list(['package'])
def test_apt_cache(self): self.patch_object(apt_pkg.subprocess, 'check_output') apt_cache = apt_pkg.Cache() self.check_output.side_effect = [ ('Package: dpkg\n' 'Version: 1.19.0.6ubuntu0\n' 'Bugs: https://bugs.launchpad.net/ubuntu/+filebug\n' 'Description-en: Debian package management system\n' ' Multiline description\n' '\n' 'Package: lsof\n' 'Architecture: amd64\n' 'Version: 4.91+dfsg-1ubuntu1\n' '\n' 'N: There is 1 additional record.\n'), ('Desired=Unknown/Install/Remove/Purge/Hold\n' '| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/' 'trig-aWait/Trig-pend\n' '|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)\n' '||/ Name Version Architecture Description\n' '+++-=============================-==================-===========-' '=================================\n' 'ii dpkg 1.19.0.5ubuntu2.1 amd64 ' 'Debian package management system\n' 'rc linux-image-4.15.0-42-generic 4.15.0-42.45 amd64 ' 'Signed kernel image generic\n' 'ii lsof 4.91+dfsg-1ubuntu1 amd64 ' 'utility to list open files\n'), ] pkg = apt_cache['dpkg'] self.assertEquals(pkg.name, 'dpkg') self.assertEquals(pkg.current_ver.ver_str, '1.19.0.5ubuntu2.1') self.assertEquals(pkg.architecture, 'amd64') self.check_output.side_effect = [ subprocess.CalledProcessError(100, ''), subprocess.CalledProcessError(1, ''), ] with self.assertRaises(KeyError): pkg = apt_cache['nonexistent'] self.check_output.side_effect = [ ('Package: dpkg\n' 'Version: 1.19.0.6ubuntu0\n' 'Bugs: https://bugs.launchpad.net/ubuntu/+filebug\n' 'Description-en: Debian package management system\n' ' Multiline description\n' '\n' 'Package: lsof\n' 'Architecture: amd64\n' 'Version: 4.91+dfsg-1ubuntu1\n' '\n' 'N: There is 1 additional record.\n'), subprocess.CalledProcessError(42, ''), ] with self.assertRaises(subprocess.CalledProcessError): # System error occurs while making dpkg inquiry pkg = apt_cache['dpkg'] self.check_output.side_effect = [ subprocess.CalledProcessError(42, ''), subprocess.CalledProcessError(1, ''), ] with self.assertRaises(subprocess.CalledProcessError): pkg = apt_cache['system-error-occurs-while-making-apt-inquiry']