From c5be97ca5c53185bce591700c75f50e7fe3835a0 Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Fri, 4 Mar 2016 12:03:35 +0100 Subject: [PATCH] Add diff support to apt_repository module The returned list of diffs aims to simulate how a file system diff would look before and after writing the sources list files. ![screenshot](http://i.imgur.com/dH6QXtY.png) n.b. Ternary conditional is due to failing integration test for python 2.4 --- packaging/os/apt_repository.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/packaging/os/apt_repository.py b/packaging/os/apt_repository.py index 6cf5014c2fa..55b60987824 100644 --- a/packaging/os/apt_repository.py +++ b/packaging/os/apt_repository.py @@ -287,7 +287,22 @@ class SourcesList(object): os.remove(filename) def dump(self): - return '\n'.join([str(i) for i in self]) + dumpstruct = {} + for filename, sources in self.files.items(): + if sources: + lines = [] + for n, valid, enabled, source, comment in sources: + chunks = [] + if not enabled: + chunks.append('# ') + chunks.append(source) + if comment: + chunks.append(' # ') + chunks.append(comment) + chunks.append('\n') + lines.append(''.join(chunks)) + dumpstruct[filename] = ''.join(lines) + return dumpstruct def _choice(self, new, old): if new is None: @@ -479,7 +494,17 @@ def main(): sources_after = sourceslist.dump() changed = sources_before != sources_after - if not module.check_mode and changed: + if changed and module._diff: + diff = [] + for filename in set(sources_before.keys()).union(sources_after.keys()): + diff.append({'before': sources_before.get(filename, ''), + 'after': sources_after.get(filename, ''), + 'before_header': (filename, '/dev/null')[filename not in sources_before], + 'after_header': (filename, '/dev/null')[filename not in sources_after]}) + else: + diff = {} + + if changed and not module.check_mode: try: sourceslist.save() if update_cache: @@ -488,7 +513,7 @@ def main(): except OSError, err: module.fail_json(msg=unicode(err)) - module.exit_json(changed=changed, repo=repo, state=state) + module.exit_json(changed=changed, repo=repo, state=state, diff=diff) # import module snippets from ansible.module_utils.basic import *