def extract_from_file(self, name): package_file = arfile.open(self.deb_file) for info in package_file.infolist(): if info.name.decode().startswith(name): return package_file.open(info.name.decode()) package_file.close() return None
def extract(self): ar_file = unix_ar.open(self.download_path) tarball = ar_file.open("data.tar.gz/") tar_file = tarfile.open(fileobj=tarball) download_dir = os.path.split(self.download_path)[0] logger.debug("Extracting session-manager-plugin archive at %s", download_dir) tar_file.extractall(download_dir)
def test_empty(self): archive = unix_ar.open('empty.ar', 'w') with self.assertRaises(ValueError) as cm: archive.infolist() self.assertEqual(cm.exception.args[0], "Can't read from a write-only archive") archive.close() with open('empty.ar', 'rb') as fp: self.assertEqual(fp.read(), b'!<arch>\n')
def test_empty(self): archive = unix_ar.open('empty.ar', 'w') with self.assertRaises(ValueError) as cm: archive.infolist() self.assertEqual(cm.exception.args[0], "Can't read from a write-only archive") archive.close() with open('empty.ar', 'rb') as fp: self.assertEqual(fp.read(), b'!<arch>\n')
def test_extract(self): with open('archive.ar', 'wb') as fp: fp.write( b'!<arch>\n' b'h.txt 1464380987 501 ' b'20 100644 7 `\n' b'hello,\n' b'\n' b'w.txt 1464380990 501 ' b'20 100644 6 `\n' b'world\n') archive = unix_ar.open('archive.ar', 'r') with self.assertRaises(ValueError) as cm: archive.add('/etc/passwd') self.assertEqual(cm.exception.args[0], "Can't change a read-only archive") self.assertEqual( [(e.name, e.size, e.mtime, e.perms, e.uid, e.gid, e.offset) for e in archive.infolist()], [(b'h.txt', 7, 1464380987, 0o100644, 501, 20, 8), (b'w.txt', 6, 1464380990, 0o100644, 501, 20, 8 + 60 + 8)]) info = archive.getinfo(b'w.txt') self.assertEqual( (info.name, info.size, info.mtime, info.perms, info.uid, info.gid, info.offset), (b'w.txt', 6, 1464380990, 0o100644, 501, 20, 8 + 60 + 8)) archive.extract(info) with open('w.txt', 'rb') as fp: self.assertEqual(fp.read(), b'world\n') os.mkdir('sub') archive.extract('h.txt', b'sub') with open('sub/h.txt', 'rb') as fp: self.assertEqual(fp.read(), b'hello,\n') info.name = 'sub/other.txt' info.size = 3 archive.extract(info) with open('sub/other.txt', 'rb') as fp: self.assertEqual(fp.read(), b'wor') os.mkdir('all') archive.extractall('all') with open('all/h.txt', 'rb') as fp: self.assertEqual(fp.read(), b'hello,\n') with open('all/w.txt', 'rb') as fp: self.assertEqual(fp.read(), b'world\n') archive.close()
def test_extract(self): with open('archive.ar', 'wb') as fp: fp.write(b'!<arch>\n' b'h.txt 1464380987 501 ' b'20 100644 7 `\n' b'hello,\n' b'\n' b'w.txt 1464380990 501 ' b'20 100644 6 `\n' b'world\n') archive = unix_ar.open('archive.ar', 'r') with self.assertRaises(ValueError) as cm: archive.add('/etc/passwd') self.assertEqual(cm.exception.args[0], "Can't change a read-only archive") self.assertEqual( [(e.name, e.size, e.mtime, e.perms, e.uid, e.gid, e.offset) for e in archive.infolist()], [(b'h.txt', 7, 1464380987, 0o100644, 501, 20, 8), (b'w.txt', 6, 1464380990, 0o100644, 501, 20, 8 + 60 + 8)]) info = archive.getinfo(b'w.txt') self.assertEqual( (info.name, info.size, info.mtime, info.perms, info.uid, info.gid, info.offset), (b'w.txt', 6, 1464380990, 0o100644, 501, 20, 8 + 60 + 8)) archive.extract(info) with open('w.txt', 'rb') as fp: self.assertEqual(fp.read(), b'world\n') os.mkdir('sub') archive.extract('h.txt', b'sub') with open('sub/h.txt', 'rb') as fp: self.assertEqual(fp.read(), b'hello,\n') info.name = 'sub/other.txt' info.size = 3 archive.extract(info) with open('sub/other.txt', 'rb') as fp: self.assertEqual(fp.read(), b'wor') os.mkdir('all') archive.extractall('all') with open('all/h.txt', 'rb') as fp: self.assertEqual(fp.read(), b'hello,\n') with open('all/w.txt', 'rb') as fp: self.assertEqual(fp.read(), b'world\n') archive.close()
def extract_tar_file(self, package_path): """ Extract a single debian package into the specified root directory. """ try: package_file = arfile.open(package_path) for info in package_file.infolist(): if info.name.decode().startswith("data.tar"): with package_file.open(info.name.decode()) as tar_ball: with tarfile.open(fileobj=tar_ball) as tar_file: tar_file.extractall(self.root) return self.extract_binary_paths(tar_file) finally: package_file.close()
def unpack_into_tar(self, tar, status_file): debian_file_archive = arfile.open(self.deb_file) for info in debian_file_archive.infolist(): with debian_file_archive.open( info.name.decode()) as content_archive: if info.name.decode().startswith("control"): with tarfile.open( fileobj=content_archive) as control_archive: self._unpack_control_data(tar, control_archive) if info.name.decode().startswith("data"): with tarfile.open(fileobj=content_archive) as data_archive: self._unpack_data(tar, data_archive) self._append_to_status(status_file) debian_file_archive.close()
def test_addfile(self): archive = unix_ar.open('addfile.ar', 'w') archive.addfile(unix_ar.ArInfo('w.txt', size=4)) with open('h.txt', 'rb') as fp: archive.addfile('w.txt', fp) archive.close() with open('addfile.ar', 'rb') as fp: self.assertEqualExceptTwos( fp.read(), b'!<arch>\n' b'w.txt 2222222222 2222 ' b'2222 100622 4 `\n' b'worl' b'w.txt 2222222222 2222 ' b'2222 100622 6 `\n' b'hello,')
def update_webui_archive(upload): """ Install a new webui archive from f, a flask upload """ FILES = ("page.html", "page.css", "page.js") archive = unix_ar.open(upload, "r") with tempfile.TemporaryDirectory() as tmpdir: for name in FILES: archive.extract( name.encode("ascii") + b"/", os.path.join(tmpdir, name).encode("utf-8")) for name in FILES: shutil.copy(os.path.join(tmpdir, name), os.path.join(DATAVOL_WEBUI, name))
def test_addfile(self): archive = unix_ar.open('addfile.ar', 'w') archive.addfile(unix_ar.ArInfo('w.txt', size=4)) with open('h.txt', 'rb') as fp: archive.addfile('w.txt', fp) archive.close() with open('addfile.ar', 'rb') as fp: self.assertEqualExceptTwos( fp.read(), b'!<arch>\n' b'w.txt 2222222222 2222 ' b'2222 100622 4 `\n' b'worl' b'w.txt 2222222222 2222 ' b'2222 100622 6 `\n' b'hello,')
def test_add(self): archive = unix_ar.open('add.ar', 'w') archive.add('h.txt', arcname='he') archive.add('w.txt') archive.close() with open('add.ar', 'rb') as fp: self.assertEqualExceptTwos( fp.read(), b'!<arch>\n' b'he 2222222222 2222 ' b'2222 100622 7 `\n' b'hello,\n' b'\n' b'w.txt 2222222222 2222 ' b'2222 100622 6 `\n' b'world\n') with self.assertRaises(ValueError) as cm: archive.add('h.txt') self.assertEqual(cm.exception.args[0], "Attempted to use a closed ArFile")
def test_add(self): archive = unix_ar.open('add.ar', 'w') archive.add('h.txt', arcname='he') archive.add('w.txt') archive.close() with open('add.ar', 'rb') as fp: self.assertEqualExceptTwos( fp.read(), b'!<arch>\n' b'he 2222222222 2222 ' b'2222 100622 7 `\n' b'hello,\n' b'\n' b'w.txt 2222222222 2222 ' b'2222 100622 6 `\n' b'world\n') with self.assertRaises(ValueError) as cm: archive.add('h.txt') self.assertEqual(cm.exception.args[0], "Attempted to use a closed ArFile")
def extract(self): if self.is_downloaded(): print_bold(f'\nFile {self.filename} already downloaded in cache.') self.location = self.cache_dir + self.filename print(f'{self.location}\n') else: self.download() ar_file = unix_ar.open(self.location) for info in ar_file.infolist(): archive = info.name.decode("utf-8") if '.tar.gz' in archive or '.tar.xz' in archive: print(f'Extracting {archive}', end=' ') stdout.flush() folder = archive.split(".")[0] if path.isdir(f'{self.extract_location}/{folder}'): print('[already extracted]') else: tarball = ar_file.open(archive) tar_file = tarfile.open(fileobj=tarball) tar_file.extractall(f'{self.extract_location}/{folder}') print('[done]')
def getSha512(file): return hashlib.sha512(open(file, 'rb').read()).hexdigest() if not PACKAGES_DIR or not REPO_DIR: print( "One of your directory statics are empty. Please fill said static with the correct path, then run this script." ) exit() for filename in os.listdir(f'{PACKAGES_DIR}'): if filename.endswith(".deb"): finalControlData += tarfile.open(fileobj=ar.open( f'{PACKAGES_DIR}/' + filename).open('control.tar.xz')).extractfile('./control').read() finalControlData += bytes("\n", "utf-8") revisions += 1 with open("Packages", "w+") as packagesFile: packagesFile.write(finalControlData.decode('utf-8')) with open("Packages.xz", "wb+") as packagesXzFile: packagesXzFile.write(xz.compress(finalControlData)) with open("Packages.bz2", "wb+") as packagesBz2File: packagesBz2File.write(bz2.compress(finalControlData)) with open("Packages.gz", "wb+") as packagesGzFile: packagesGzFile.write(gzip.compress(finalControlData))
# # Extract (in this example) a control file's contents from a .deb # import unix_ar as ar import tarfile as tar filename = 'example.deb' control = tar.open(fileobj=ar.open(filename).open( 'control.tar.gz')).extractfile('./control').read()