def _find_dependencies(root, part_files): ms = magic.open(magic.NONE) if ms.load() != 0: raise RuntimeError('Cannot load magic header detection') elf_files = set() fs_encoding = sys.getfilesystemencoding() for part_file in part_files: # Filter out object (*.o) files-- we only care about binaries. if part_file.endswith('.o'): continue # No need to crawl links-- the original should be here, too. path = os.path.join(root, part_file) if os.path.islink(path): logger.debug( 'Skipped link {!r} while finding dependencies'.format(path)) continue path = path.encode(fs_encoding, errors='surrogateescape') # Finally, make sure this is actually an ELF before queueing it up # for an ldd call. file_m = ms.file(path) if file_m.startswith('ELF') and 'dynamically linked' in file_m: elf_files.add(path) dependencies = [] for elf_file in elf_files: dependencies += libraries.get_dependencies(elf_file) return set(dependencies)
def _find_dependencies(root, part_files): ms = magic.open(magic.NONE) if ms.load() != 0: raise RuntimeError('Cannot load magic header detection') elf_files = set() fs_encoding = sys.getfilesystemencoding() for part_file in part_files: # Filter out object (*.o) files-- we only care about binaries. if part_file.endswith('.o'): continue # No need to crawl links-- the original should be here, too. path = os.path.join(root, part_file) if os.path.islink(path): logger.debug('Skipped link {!r} while finding dependencies'.format( path)) continue path = path.encode(fs_encoding, errors='surrogateescape') # Finally, make sure this is actually an ELF before queueing it up # for an ldd call. file_m = ms.file(path) if file_m.startswith('ELF') and 'dynamically linked' in file_m: elf_files.add(path) dependencies = [] for elf_file in elf_files: dependencies += libraries.get_dependencies(elf_file) return set(dependencies)
def _find_dependencies(workdir): ms = magic.open(magic.NONE) if ms.load() != 0: raise RuntimeError('Cannot load magic header detection') elf_files = set() fs_encoding = sys.getfilesystemencoding() for root, dirs, files in os.walk(workdir.encode(fs_encoding)): # Filter out object (*.o) files-- we only care about binaries. entries = (entry for entry in itertools.chain(files, dirs) if not entry.endswith(b'.o')) for entry in entries: path = os.path.join(root, entry) if os.path.islink(path): logger.debug('Skipped link {!r} when parsing {!r}'.format( path, workdir)) continue file_m = ms.file(path) if file_m.startswith('ELF') and 'dynamically linked' in file_m: elf_files.add(path) dependencies = [] for elf_file in elf_files: dependencies += libraries.get_dependencies(elf_file) return set(dependencies)
def test_get_libraries_ldd_failure_logs_warning(self): self.run_output_mock.side_effect = subprocess.CalledProcessError( 1, 'foo', b'bar') self.assertThat(libraries.get_dependencies('foo'), Equals([])) self.assertThat( self.fake_logger.output, Equals("Unable to determine library dependencies for 'foo'\n"))
def test_get_libraries_ldd_failure_logs_warning(self): self.run_output_mock.side_effect = subprocess.CalledProcessError( 1, 'foo', b'bar') self.assertEqual(libraries.get_dependencies('foo'), []) self.assertEqual( "Unable to determine library dependencies for 'foo'\n", self.fake_logger.output)
def test_get_libraries_excludes_slash_snap(self): lines = [ 'foo.so.1 => /lib/foo.so.1 (0xdead)', 'bar.so.2 => /usr/lib/bar.so.2 (0xbeef)', 'barsnap.so.2 => /snap/snapcraft/current/bar.so.2 (0xbeef)', '/lib/baz.so.2 (0x1234)', ] self.run_output_mock.return_value = '\t' + '\n\t'.join(lines) + '\n' libs = libraries.get_dependencies('foo') self.assertThat(libs, Equals(['/lib/foo.so.1', '/usr/lib/bar.so.2']))
def test_get_libraries_filtered_by_system_libraries(self): self.get_system_libs_mock.return_value = frozenset(['foo.so.1']) libs = libraries.get_dependencies('foo') self.assertThat(libs, Equals(['/usr/lib/bar.so.2']))
def test_get_libraries(self): libs = libraries.get_dependencies('foo') self.assertThat(libs, Equals(['/lib/foo.so.1', '/usr/lib/bar.so.2']))
def test_fail_gracefully_if_system_libs_not_found(self): self.assertThat(libraries.get_dependencies('foo'), Equals([]))
def test_get_libraries_filtered_by_system_libraries(self): self.get_system_libs_mock.return_value = frozenset(['foo.so.1']) libs = libraries.get_dependencies('foo') self.assertEqual(libs, ['/usr/lib/bar.so.2'])
def test_get_libraries(self): libs = libraries.get_dependencies('foo') self.assertEqual(libs, ['/lib/foo.so.1', '/usr/lib/bar.so.2'])
def test_get_libraries(self): libs = libraries.get_dependencies("foo") self.assertEqual(libs, ["/lib/foo.so.1", "/usr/lib/bar.so.2"])
def test_fail_gracefully_if_system_libs_not_found(self): self.assertEqual(libraries.get_dependencies("foo"), [])