def test_get_file_package(self): '''get_file_package() on installed files.''' self.assertEqual(impl.get_file_package('/bin/bash'), 'bash') self.assertEqual(impl.get_file_package('/bin/cat'), 'coreutils') self.assertEqual(impl.get_file_package('/etc/blkid.tab'), 'libblkid1') self.assertEqual(impl.get_file_package('/nonexisting'), None)
def test_get_file_package(self): '''get_file_package() on installed files.''' self.assertEqual(impl.get_file_package('/bin/bash'), 'bash') self.assertEqual(impl.get_file_package('/bin/cat'), 'coreutils') self.assertEqual(impl.get_file_package('/etc/blkid.tab'), 'libblkid1') self.assertEqual(impl.get_file_package('/nonexisting'), None)
def test_get_file_package(self): '''get_file_package() on installed files.''' self.assertEqual(impl.get_file_package('/bin/bash'), 'bash') self.assertEqual(impl.get_file_package('/bin/cat'), 'coreutils') self.assertEqual(impl.get_file_package('/etc/pam.conf'), 'libpam-runtime') self.assertEqual(impl.get_file_package('/nonexisting'), None)
def test_get_file_package_uninstalled(self): '''get_file_package() on uninstalled packages.''' # determine distro release code name lsb_release = subprocess.Popen(['lsb_release', '-sc'], stdout=subprocess.PIPE) release_name = lsb_release.communicate()[0].decode('UTF-8').strip() assert lsb_release.returncode == 0 # generate a test Contents.gz basedir = tempfile.mkdtemp() try: mapdir = os.path.join(basedir, 'dists', release_name) os.makedirs(mapdir) with gzip.open(os.path.join(mapdir, 'Contents-%s.gz' % impl.get_system_architecture()), 'w') as f: f.write(b''' foo header FILE LOCATION usr/bin/frobnicate foo/frob usr/bin/frob foo/frob-utils bo/gu/s na/mypackage ''') self.assertEqual(impl.get_file_package('usr/bin/frob', False, mapdir), None) # must not match frob (same file name prefix) self.assertEqual(impl.get_file_package('usr/bin/frob', True, mapdir), 'frob-utils') self.assertEqual(impl.get_file_package('/usr/bin/frob', True, mapdir), 'frob-utils') # invalid mirror impl.set_mirror('file:///foo/nonexisting') self.assertRaises(IOError, impl.get_file_package, 'usr/bin/frob', True) # valid mirror, no cache directory impl.set_mirror('file://' + basedir) self.assertEqual(impl.get_file_package('usr/bin/frob', True), 'frob-utils') self.assertEqual(impl.get_file_package('/usr/bin/frob', True), 'frob-utils') # valid mirror, test caching cache_dir = os.path.join(basedir, 'cache') os.mkdir(cache_dir) self.assertEqual(impl.get_file_package('usr/bin/frob', True, cache_dir), 'frob-utils') self.assertEqual(len(os.listdir(cache_dir)), 1) cache_file = os.listdir(cache_dir)[0] self.assertTrue(cache_file.startswith('Contents-')) self.assertEqual(impl.get_file_package('/bo/gu/s', True, cache_dir), 'mypackage') # valid cache, should not need to access the mirror impl.set_mirror('file:///foo/nonexisting') self.assertEqual(impl.get_file_package('/bo/gu/s', True, cache_dir), 'mypackage') # outdated cache, must refresh the cache and hit the invalid # mirror now = int(time.time()) os.utime(os.path.join(cache_dir, cache_file), (now, now-90000)) self.assertRaises(IOError, impl.get_file_package, '/bo/gu/s', True, cache_dir) finally: shutil.rmtree(basedir)
def test_find_package_desktopfile(self): '''find_package_desktopfile().''' # package without any .desktop file nodesktop = 'bash' assert len([f for f in packaging.get_files(nodesktop) if f.endswith('.desktop')]) == 0 # find a package with one and a package with multiple .desktop files onedesktop = None multidesktop = None for d in os.listdir('/usr/share/applications/'): if not d.endswith('.desktop'): continue pkg = packaging.get_file_package( os.path.join('/usr/share/applications/', d)) num = len([f for f in packaging.get_files(pkg) if f.endswith('.desktop')]) if not onedesktop and num == 1: onedesktop = pkg elif not multidesktop and num > 1: multidesktop = pkg if onedesktop and multidesktop: break if nodesktop: self.assertEqual(find_package_desktopfile(nodesktop), None, 'no-desktop package %s' % nodesktop) if multidesktop: self.assertEqual(find_package_desktopfile(multidesktop), None, 'multi-desktop package %s' % multidesktop) if onedesktop: d = find_package_desktopfile(onedesktop) self.assertNotEqual(d, None, 'one-desktop package %s' % onedesktop) self.assertTrue(os.path.exists(d)) self.assertTrue(d.endswith('.desktop'))
def test_get_file_package_diversion(self): '''get_file_package() for a diverted file.''' # pick first diversion we have p = subprocess.Popen('LC_ALL=C dpkg-divert --list | head -n 1', shell=True, stdout=subprocess.PIPE) out = p.communicate()[0].decode('UTF-8') assert p.returncode == 0 assert out fields = out.split() file = fields[2] pkg = fields[-1] self.assertEqual(impl.get_file_package(file), pkg)
def test_get_file_package_diversion(self): '''get_file_package() for a diverted file.''' # pick first diversion we have p = subprocess.Popen('LC_ALL=C dpkg-divert --list | head -n 1', shell=True, stdout=subprocess.PIPE) out = p.communicate()[0].decode('UTF-8') assert p.returncode == 0 assert out fields = out.split() file = fields[2] pkg = fields[-1] self.assertEqual(impl.get_file_package(file), pkg)
def find_file_package(file): '''Return the package that ships the given file. Return None if no package ships it. ''' # resolve symlinks in directories (dir, name) = os.path.split(file) resolved_dir = os.path.realpath(dir) if os.path.isdir(resolved_dir): file = os.path.join(resolved_dir, name) if not likely_packaged(file): return None return packaging.get_file_package(file)
def find_file_package(file): '''Return the package that ships the given file. Return None if no package ships it. ''' # resolve symlinks in directories (dir, name) = os.path.split(file) resolved_dir = os.path.realpath(dir) if os.path.isdir(resolved_dir): file = os.path.join(resolved_dir, name) if not likely_packaged(file): return None return packaging.get_file_package(file)
def test_get_file_package_uninstalled_multiarch(self): '''get_file_package() on foreign arches and releases''' # map "Foonux 3.14" to "mocky" orig_distro_release_to_codename = impl._distro_release_to_codename impl._distro_release_to_codename = lambda r: (r == 'Foonux 3.14' ) and 'mocky' or None # generate test Contents.gz for two fantasy architectures basedir = tempfile.mkdtemp() try: mapdir = os.path.join(basedir, 'dists', impl.get_distro_codename()) os.makedirs(mapdir) with gzip.open(os.path.join(mapdir, 'Contents-even.gz'), 'w') as f: f.write(b''' foo header FILE LOCATION usr/lib/even/libfrob.so.1 foo/libfrob1 usr/bin/frob foo/frob-utils ''') with gzip.open(os.path.join(mapdir, 'Contents-odd.gz'), 'w') as f: f.write(b''' foo header FILE LOCATION usr/lib/odd/libfrob.so.1 foo/libfrob1 usr/bin/frob foo/frob-utils ''') # and another one for fantasy release os.mkdir(os.path.join(basedir, 'dists', 'mocky')) with gzip.open( os.path.join(basedir, 'dists', 'mocky', 'Contents-even.gz'), 'w') as f: f.write(b''' foo header FILE LOCATION usr/lib/even/libfrob.so.0 foo/libfrob0 usr/bin/frob foo/frob ''') # use this as a mirror impl.set_mirror('file://' + basedir) # must not match system architecture self.assertEqual(impl.get_file_package('usr/bin/frob', False), None) # must match correct architecture self.assertEqual( impl.get_file_package('usr/bin/frob', True, arch='even'), 'frob-utils') self.assertEqual( impl.get_file_package('usr/bin/frob', True, arch='odd'), 'frob-utils') self.assertEqual( impl.get_file_package('/usr/lib/even/libfrob.so.1', True, arch='even'), 'libfrob1') self.assertEqual( impl.get_file_package('/usr/lib/even/libfrob.so.1', True, arch='odd'), None) self.assertEqual( impl.get_file_package('/usr/lib/odd/libfrob.so.1', True, arch='odd'), 'libfrob1') # for mocky release ("Foonux 3.14") self.assertEqual( impl.get_file_package('/usr/lib/even/libfrob.so.1', True, release='Foonux 3.14', arch='even'), None) self.assertEqual( impl.get_file_package('/usr/lib/even/libfrob.so.0', True, release='Foonux 3.14', arch='even'), 'libfrob0') self.assertEqual( impl.get_file_package('/usr/bin/frob', True, release='Foonux 3.14', arch='even'), 'frob') # invalid mirror impl.set_mirror('file:///foo/nonexisting') self.assertRaises(IOError, impl.get_file_package, '/usr/lib/even/libfrob.so.1', True, arch='even') self.assertRaises(IOError, impl.get_file_package, '/usr/lib/even/libfrob.so.0', True, release='Foonux 3.14', arch='even') # valid mirror, test caching impl.set_mirror('file://' + basedir) cache_dir = os.path.join(basedir, 'cache') os.mkdir(cache_dir) self.assertEqual( impl.get_file_package('/usr/lib/even/libfrob.so.1', True, cache_dir, arch='even'), 'libfrob1') self.assertEqual(len(os.listdir(cache_dir)), 1) cache_file = os.listdir(cache_dir)[0] self.assertEqual( impl.get_file_package('/usr/lib/even/libfrob.so.0', True, cache_dir, release='Foonux 3.14', arch='even'), 'libfrob0') self.assertEqual(len(os.listdir(cache_dir)), 2) # valid cache, should not need to access the mirror impl.set_mirror('file:///foo/nonexisting') self.assertEqual( impl.get_file_package('usr/bin/frob', True, cache_dir, arch='even'), 'frob-utils') self.assertEqual( impl.get_file_package('usr/bin/frob', True, cache_dir, release='Foonux 3.14', arch='even'), 'frob') # but no cached file for the other arch self.assertRaises(IOError, impl.get_file_package, 'usr/bin/frob', True, cache_dir, arch='odd') # outdated cache, must refresh the cache and hit the invalid # mirror now = int(time.time()) os.utime(os.path.join(cache_dir, cache_file), (now, now - 90000)) self.assertRaises(IOError, impl.get_file_package, 'usr/bin/frob', True, cache_dir, arch='even') finally: shutil.rmtree(basedir) impl._distro_release_to_codename = orig_distro_release_to_codename
def test_get_file_package_uninstalled(self): '''get_file_package() on uninstalled packages.''' # generate a test Contents.gz basedir = tempfile.mkdtemp() try: # test Contents.gz for release pocket mapdir = os.path.join(basedir, 'dists', impl.get_distro_codename()) os.makedirs(mapdir) with gzip.open( os.path.join( mapdir, 'Contents-%s.gz' % impl.get_system_architecture()), 'w') as f: f.write(b''' foo header FILE LOCATION usr/bin/frobnicate foo/frob usr/bin/frob foo/frob-utils bo/gu/s na/mypackage bin/true admin/superutils ''') # test Contents.gz for -updates pocket mapdir = os.path.join(basedir, 'dists', impl.get_distro_codename() + '-updates') os.makedirs(mapdir) with gzip.open( os.path.join( mapdir, 'Contents-%s.gz' % impl.get_system_architecture()), 'w') as f: f.write(b''' foo header FILE LOCATION lib/libnew.so.5 universe/libs/libnew5 ''') # use this as a mirror impl.set_mirror('file://' + basedir) self.assertEqual(impl.get_file_package('usr/bin/frob', False), None) # must not match frob (same file name prefix) self.assertEqual(impl.get_file_package('usr/bin/frob', True), 'frob-utils') self.assertEqual(impl.get_file_package('/usr/bin/frob', True), 'frob-utils') # find files from -updates pocket self.assertEqual(impl.get_file_package('/lib/libnew.so.5', False), None) self.assertEqual(impl.get_file_package('/lib/libnew.so.5', True), 'libnew5') # invalid mirror impl.set_mirror('file:///foo/nonexisting') self.assertRaises(IOError, impl.get_file_package, 'usr/bin/frob', True) # valid mirror, test cache directory impl.set_mirror('file://' + basedir) cache_dir = os.path.join(basedir, 'cache') os.mkdir(cache_dir) self.assertEqual( impl.get_file_package('usr/bin/frob', True, cache_dir), 'frob-utils') cache_dir_files = os.listdir(cache_dir) self.assertEqual(len(cache_dir_files), 2) self.assertEqual( impl.get_file_package('/bo/gu/s', True, cache_dir), 'mypackage') # valid cache, should not need to access the mirror impl.set_mirror('file:///foo/nonexisting') self.assertEqual( impl.get_file_package('/bin/true', True, cache_dir), 'superutils') self.assertEqual( impl.get_file_package('/bo/gu/s', True, cache_dir), 'mypackage') self.assertEqual( impl.get_file_package('/lib/libnew.so.5', True, cache_dir), 'libnew5') # outdated cache, must refresh the cache and hit the invalid # mirror if 'updates' in cache_dir_files[0]: cache_file = cache_dir_files[1] else: cache_file = cache_dir_files[0] now = int(time.time()) os.utime(os.path.join(cache_dir, cache_file), (now, now - 90000)) self.assertRaises(IOError, impl.get_file_package, '/bo/gu/s', True, cache_dir) finally: shutil.rmtree(basedir)
def test_get_file_package_uninstalled_multiarch(self): '''get_file_package() on foreign arches and releases''' # map "Foonux 3.14" to "mocky" orig_distro_release_to_codename = impl._distro_release_to_codename impl._distro_release_to_codename = lambda r: (r == 'Foonux 3.14') and 'mocky' or None # generate test Contents.gz for two fantasy architectures basedir = tempfile.mkdtemp() try: mapdir = os.path.join(basedir, 'dists', impl.get_distro_codename()) os.makedirs(mapdir) with gzip.open(os.path.join(mapdir, 'Contents-even.gz'), 'w') as f: f.write(b''' foo header FILE LOCATION usr/lib/even/libfrob.so.1 foo/libfrob1 usr/bin/frob foo/frob-utils ''') with gzip.open(os.path.join(mapdir, 'Contents-odd.gz'), 'w') as f: f.write(b''' foo header FILE LOCATION usr/lib/odd/libfrob.so.1 foo/libfrob1 usr/bin/frob foo/frob-utils ''') # and another one for fantasy release os.mkdir(os.path.join(basedir, 'dists', 'mocky')) with gzip.open(os.path.join(basedir, 'dists', 'mocky', 'Contents-even.gz'), 'w') as f: f.write(b''' foo header FILE LOCATION usr/lib/even/libfrob.so.0 foo/libfrob0 usr/bin/frob foo/frob ''') # use this as a mirror impl.set_mirror('file://' + basedir) # must not match system architecture self.assertEqual(impl.get_file_package('usr/bin/frob', False), None) # must match correct architecture self.assertEqual(impl.get_file_package('usr/bin/frob', True, arch='even'), 'frob-utils') self.assertEqual(impl.get_file_package('usr/bin/frob', True, arch='odd'), 'frob-utils') self.assertEqual(impl.get_file_package('/usr/lib/even/libfrob.so.1', True, arch='even'), 'libfrob1') self.assertEqual(impl.get_file_package('/usr/lib/even/libfrob.so.1', True, arch='odd'), None) self.assertEqual(impl.get_file_package('/usr/lib/odd/libfrob.so.1', True, arch='odd'), 'libfrob1') # for mocky release ("Foonux 3.14") self.assertEqual(impl.get_file_package('/usr/lib/even/libfrob.so.1', True, release='Foonux 3.14', arch='even'), None) self.assertEqual(impl.get_file_package('/usr/lib/even/libfrob.so.0', True, release='Foonux 3.14', arch='even'), 'libfrob0') self.assertEqual(impl.get_file_package('/usr/bin/frob', True, release='Foonux 3.14', arch='even'), 'frob') # invalid mirror impl.set_mirror('file:///foo/nonexisting') self.assertRaises(IOError, impl.get_file_package, '/usr/lib/even/libfrob.so.1', True, arch='even') self.assertRaises(IOError, impl.get_file_package, '/usr/lib/even/libfrob.so.0', True, release='Foonux 3.14', arch='even') # valid mirror, test caching impl.set_mirror('file://' + basedir) cache_dir = os.path.join(basedir, 'cache') os.mkdir(cache_dir) self.assertEqual(impl.get_file_package('/usr/lib/even/libfrob.so.1', True, cache_dir, arch='even'), 'libfrob1') self.assertEqual(len(os.listdir(cache_dir)), 1) cache_file = os.listdir(cache_dir)[0] self.assertEqual(impl.get_file_package('/usr/lib/even/libfrob.so.0', True, cache_dir, release='Foonux 3.14', arch='even'), 'libfrob0') self.assertEqual(len(os.listdir(cache_dir)), 2) # valid cache, should not need to access the mirror impl.set_mirror('file:///foo/nonexisting') self.assertEqual(impl.get_file_package('usr/bin/frob', True, cache_dir, arch='even'), 'frob-utils') self.assertEqual(impl.get_file_package('usr/bin/frob', True, cache_dir, release='Foonux 3.14', arch='even'), 'frob') # but no cached file for the other arch self.assertRaises(IOError, impl.get_file_package, 'usr/bin/frob', True, cache_dir, arch='odd') # outdated cache, must refresh the cache and hit the invalid # mirror now = int(time.time()) os.utime(os.path.join(cache_dir, cache_file), (now, now - 90000)) self.assertRaises(IOError, impl.get_file_package, 'usr/bin/frob', True, cache_dir, arch='even') finally: shutil.rmtree(basedir) impl._distro_release_to_codename = orig_distro_release_to_codename
def test_get_file_package_uninstalled(self): '''get_file_package() on uninstalled packages.''' # generate a test Contents.gz basedir = tempfile.mkdtemp() try: # test Contents.gz for release pocket mapdir = os.path.join(basedir, 'dists', impl.get_distro_codename()) os.makedirs(mapdir) with gzip.open(os.path.join(mapdir, 'Contents-%s.gz' % impl.get_system_architecture()), 'w') as f: f.write(b''' foo header FILE LOCATION usr/bin/frobnicate foo/frob usr/bin/frob foo/frob-utils bo/gu/s na/mypackage ''') # test Contents.gz for -updates pocket mapdir = os.path.join(basedir, 'dists', impl.get_distro_codename() + '-updates') os.makedirs(mapdir) with gzip.open(os.path.join(mapdir, 'Contents-%s.gz' % impl.get_system_architecture()), 'w') as f: f.write(b''' foo header FILE LOCATION lib/libnew.so.5 universe/libs/libnew5 ''') # use this as a mirror impl.set_mirror('file://' + basedir) self.assertEqual(impl.get_file_package('usr/bin/frob', False), None) # must not match frob (same file name prefix) self.assertEqual(impl.get_file_package('usr/bin/frob', True), 'frob-utils') self.assertEqual(impl.get_file_package('/usr/bin/frob', True), 'frob-utils') # find files from -updates pocket self.assertEqual(impl.get_file_package('/lib/libnew.so.5', False), None) self.assertEqual(impl.get_file_package('/lib/libnew.so.5', True), 'libnew5') # invalid mirror impl.set_mirror('file:///foo/nonexisting') self.assertRaises(IOError, impl.get_file_package, 'usr/bin/frob', True) # valid mirror, test cache directory impl.set_mirror('file://' + basedir) cache_dir = os.path.join(basedir, 'cache') os.mkdir(cache_dir) self.assertEqual(impl.get_file_package('usr/bin/frob', True, cache_dir), 'frob-utils') cache_dir_files = os.listdir(cache_dir) self.assertEqual(len(cache_dir_files), 2) self.assertEqual(impl.get_file_package('/bo/gu/s', True, cache_dir), 'mypackage') # valid cache, should not need to access the mirror impl.set_mirror('file:///foo/nonexisting') self.assertEqual(impl.get_file_package('/bo/gu/s', True, cache_dir), 'mypackage') self.assertEqual(impl.get_file_package('/lib/libnew.so.5', True, cache_dir), 'libnew5') # outdated cache, must refresh the cache and hit the invalid # mirror if 'updates' in cache_dir_files[0]: cache_file = cache_dir_files[1] else: cache_file = cache_dir_files[0] now = int(time.time()) os.utime(os.path.join(cache_dir, cache_file), (now, now - 90000)) self.assertRaises(IOError, impl.get_file_package, '/bo/gu/s', True, cache_dir) finally: shutil.rmtree(basedir)