def test_iter(self): path = os.path.join(BASEDIR, "selftests", ".data", "hello.deb") expected = [("debian-binary", 4, 68), ("control.tar.xz", 1868, 132), ("data.tar.xz", 54072, 2060)] for count, member in enumerate(ar.Ar(path)): self.assertEqual(expected[count][0], member.identifier) self.assertEqual(expected[count][1], member.size) self.assertEqual(expected[count][2], member.offset)
def is_valid(package_path): """Verifies if a package is a valid deb file. :param str package_path: .deb package path. :returns: True if valid, otherwise false. :rtype: bool """ abs_path = os.path.abspath(os.path.expanduser((package_path))) member_regexes = [ r'debian-binary', r'control\.tar\..*', r'data\.tar\..*' ] members = ar.Ar(abs_path).list() if len(members) != len(member_regexes): return False for regex, member in zip(member_regexes, members): if not re.match(regex, member): return False return True
def extract_from_package(package_path, dest_path=None): """Extracts the package content to a specific destination path. :param str package_path: path to the deb package. :param dest_path: destination path to extract the files. Default is the current directory. :returns: path of the extracted file :returns: the path of the extracted files. :rtype: str """ abs_path = os.path.abspath(os.path.expanduser(package_path)) dest = dest_path or os.path.curdir os.makedirs(dest, exist_ok=True) archive = ar.Ar(abs_path) data_tarball_name = archive.list()[2] member_data = archive.read_member(data_tarball_name) tarball = tarfile.open(fileobj=io.BytesIO(member_data)) tarball.extractall(dest) return dest
def test_is_ar(self): path = os.path.join(BASEDIR, "selftests", ".data", "hello.deb") self.assertTrue(ar.Ar(path).is_valid())
def test_read_member(self): path = os.path.join(BASEDIR, "selftests", ".data", "guaca.a") self.assertEqual( ar.Ar(path).read_member("shopping"), b"avocados, salt") self.assertEqual(ar.Ar(path).read_member("recipe"), b"cut, mix")
def test_list(self): path = os.path.join(BASEDIR, "selftests", ".data", "hello.deb") self.assertEqual( ar.Ar(path).list(), ["debian-binary", "control.tar.xz", "data.tar.xz"])
def test_is_not_ar(self): path = os.path.join(BASEDIR, "selftests", ".data", "hello.rpm") self.assertFalse(ar.Ar(path).is_valid())
def test_read_member(self): path = os.path.join(BASEDIR, "selftests", ".data", "guaca.a") self.assertEqual( ar.Ar(path).read_member('shopping'), b'avocados, salt') self.assertEqual(ar.Ar(path).read_member('recipe'), b'cut, mix')