diff options
author | Guido Günther <agx@sigxcpu.org> | 2010-09-06 09:54:09 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2010-09-06 09:56:18 +0200 |
commit | 49357a33440b8b26c8f4e7c7945c58e68d39bd06 (patch) | |
tree | e9ca53d5942c822e30e70be8c7be7124e1d2daa5 | |
parent | fb3eb29429c278df683d90a06f65d752c4f59ab4 (diff) |
Simplify code
-rwxr-xr-x | whatmaps | 55 |
1 files changed, 30 insertions, 25 deletions
@@ -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>/.*\.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>/.*\.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 "<Debian Pkg object name:'%s'>" % self.name - - services = property(_get_services, None) - shared_objects = property(_get_shared_objects, None) - def check_maps(procs, shared_objects): restart_procs = {} |