Created
February 2, 2016 17:14
-
-
Save AlexTalker/2f75155eb2d12f9a88fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import tarfile | |
from sys import argv | |
from os.path import dirname, basename | |
class TarFileWithDirs(tarfile.TarFile): | |
def __init__(self, *args, **kwargs): | |
super(TarFileWithDirs, self).__init__(*args, **kwargs) | |
self._dirs = {} | |
for o in self: | |
self._append_to_dir(o) | |
def _append_to_dir(self, o): | |
basedir = dirname(o.name) | |
if basedir not in self._dirs: | |
self._dirs[basedir] = [] | |
if not o.name in self._dirs and o.isdir(): | |
self._dirs[o.name] = [] | |
self._dirs[basedir].append(o) | |
def getmembers(self, name=None): | |
if name is None: | |
return super(TarFileWithDirs, self).getmembers() | |
return self._dirs[name] | |
def addfile(self, tarinfo, fileobj=None): | |
super(TarFileWithDirs, self).addfile(tarinfo, fileobj) | |
self._append_to_dir(tarinfo) | |
class DBReader(dict): | |
def __init__(self, db_path): | |
super(DBReader, self).__init__() | |
tar = TarFileWithDirs.open(db_path) | |
for o in tar: | |
if o.isdir(): | |
self.append_pkg_to_tree(o, tar) | |
def append_pkg_to_tree(self, o, tar): | |
for m in tar.getmembers(o.name): | |
if basename(m.name) == 'desc': | |
pkg = DBReader.config_parser(tar.extractfile(m.name).read().decode()) | |
self[pkg['name']] = pkg | |
@staticmethod | |
def config_parser(string): | |
lines = string.split('\n') | |
o = {} | |
i = 0 | |
param = '' | |
while i < len(lines): | |
line = lines[i] | |
if line.startswith('%'): | |
param = line[1:-1].lower() # Really we need cut-out value between %'s, so this is lazy implementation | |
o[param] = [] | |
elif param and line == '': | |
if len(o[param]) == 1: | |
o[param] = o[param][0] | |
param = '' | |
elif param: | |
o[param].append(line) | |
i += 1 | |
return o | |
# Test code | |
if __name__ == '__main__': | |
dbreader = DBReader(argv[1]) | |
required_pkgs = argv[2:] | |
i = 0 | |
for name, data in dbreader.items(): | |
if name in required_pkgs: | |
if i > 0: | |
print(' %s' % data['filename'], end='') | |
else: | |
print(data['filename'], end='') | |
i += 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment