From 49357a33440b8b26c8f4e7c7945c58e68d39bd06 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 6 Sep 2010 09:54:09 +0200 Subject: Simplify code --- whatmaps | 55 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/whatmaps b/whatmaps index b1fdb46..70ecdae 100755 --- a/whatmaps +++ b/whatmaps @@ -83,6 +83,9 @@ class Pkg(object): def __init__(self): raise NotImplementedError + def __repr__(self): + return "<%s Pkg object name:'%s'>" % (self.type, self.name) + class DebianDistro(Distro): "Debian (dpkg) based distribution""" @@ -106,53 +109,55 @@ class DebianDistro(Distro): class DebianPkg(Pkg): + type = 'Debian' + _so_regex = re.compile(r'(?P/.*\.so(\.[^/])*$)') + _init_script_re = re.compile('/etc/init.d/[a-zA-Z0-9]') + def __init__(self, name): self.name = name self._services = None self._shared_objects = None + self._contents = None - def _get_shared_objects(self): + def _get_contents(self): + if self._contents: + return self._contents + else: + list_contents = subprocess.Popen(["dpkg-query -L %s 2>/dev/null" % self.name], + stdout=subprocess.PIPE, shell=True) + output = list_contents.communicate()[0] + if list_contents.returncode: + raise PkgError + self.contents = output.split('\n') + return self.contents + + @property + def shared_objects(self): if self._shared_objects != None: return self._shared_objects self._shared_objects = [] - regex = re.compile(r'(?P/.*\.so(\.[^/])*$)') - list_contents = subprocess.Popen(["dpkg-query -L %s 2>/dev/null" % self.name], - stdout=subprocess.PIPE, shell=True) - output = list_contents.communicate()[0] - if list_contents.returncode: - raise PkgError + contents = self._get_contents() - for line in output.split('\n'): - m = regex.match(line) + for line in contents: + m = self._so_regex.match(line) if m: self._shared_objects.append(m.group('so')) return self._shared_objects - def _get_services(self): + @property + def services(self): if self._services != None: return self._services self._services = [] + contents = self._get_contents() # Only supports sysvinit so far: - script_re = re.compile('/etc/init.d/[a-zA-Z0-9]') - list_contents = subprocess.Popen(["dpkg-query -L %s 2>/dev/null" % self.name], - stdout=subprocess.PIPE, shell=True) - output = list_contents.communicate()[0] - if list_contents.returncode: - raise PkgError - - for line in output.split('\n'): - if script_re.match(line): + for line in contents: + if self._init_script_re.match(line): self._services.append(os.path.basename(line.strip())) return self._services - def __repr__(self): - return "" % self.name - - services = property(_get_services, None) - shared_objects = property(_get_shared_objects, None) - def check_maps(procs, shared_objects): restart_procs = {} -- cgit v1.2.3