From 44d16240a8134fbeb25f5598cdb6c6017bc27c37 Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Thu, 30 Jul 2015 13:06:50 +0530 Subject: [PATCH] Make SourcesList __init__ method also set self.module This was originally required to allow other methods in SourcesList to fail, but subsequent changes rendered that unnecessary, and it's just a cleanup now, and avoids passing in module separately to save(). --- packaging/os/apt_repository.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packaging/os/apt_repository.py b/packaging/os/apt_repository.py index aa7e8d07a60..0e6140651e1 100644 --- a/packaging/os/apt_repository.py +++ b/packaging/os/apt_repository.py @@ -124,7 +124,8 @@ class InvalidSource(Exception): # Simple version of aptsources.sourceslist.SourcesList. # No advanced logic and no backups inside. class SourcesList(object): - def __init__(self): + def __init__(self, module): + self.module = module self.files = {} # group sources by file # Repositories that we're adding -- used to implement mode param self.new_repos = set() @@ -234,7 +235,7 @@ class SourcesList(object): group.append((n, valid, enabled, source, comment)) self.files[file] = group - def save(self, module): + def save(self): for filename, sources in self.files.items(): if sources: d, fn = os.path.split(filename) @@ -255,13 +256,13 @@ class SourcesList(object): try: f.write(line) except IOError, err: - module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, unicode(err))) - module.atomic_move(tmp_path, filename) + self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, unicode(err))) + self.module.atomic_move(tmp_path, filename) # allow the user to override the default mode if filename in self.new_repos: - this_mode = module.params['mode'] - module.set_mode_if_different(filename, this_mode, False) + this_mode = self.module.params['mode'] + self.module.set_mode_if_different(filename, this_mode, False) else: del self.files[filename] if os.path.exists(filename): @@ -329,7 +330,7 @@ class UbuntuSourcesList(SourcesList): def __init__(self, module, add_ppa_signing_keys_callback=None): self.module = module self.add_ppa_signing_keys_callback = add_ppa_signing_keys_callback - super(UbuntuSourcesList, self).__init__() + super(UbuntuSourcesList, self).__init__(module) def _get_ppa_info(self, owner_name, ppa_name): lp_api = self.LP_API % (owner_name, ppa_name) @@ -438,7 +439,7 @@ def main(): sourceslist = UbuntuSourcesList(module, add_ppa_signing_keys_callback=get_add_ppa_signing_key_callback(module)) elif isinstance(distro, aptsources_distro.DebianDistribution) or isinstance(distro, aptsources_distro.Distribution): - sourceslist = SourcesList() + sourceslist = SourcesList(module) else: module.fail_json(msg='Module apt_repository supports only Debian and Ubuntu.') @@ -462,7 +463,7 @@ def main(): if not module.check_mode and changed: try: - sourceslist.save(module) + sourceslist.save() if update_cache: cache = apt.Cache() cache.update()