From cdb6c7ca1b8a032f28838cbb1396d641c60e1635 Mon Sep 17 00:00:00 2001 From: "David Adam (zanchey)" <zanchey@ucc.gu.uwa.edu.au> Date: Wed, 21 Dec 2011 23:54:35 +0800 Subject: [PATCH] Return a dictionary rather than a string from each run (allows us to return metadata like the success or failure of the job) --- rdiff-manager.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/rdiff-manager.py b/rdiff-manager.py index faf8cec..fed0b13 100755 --- a/rdiff-manager.py +++ b/rdiff-manager.py @@ -43,20 +43,22 @@ class Host(object): self.include_file, 'root@%s::/' % (self.hostname), self.destination ] ) ) def run_all(self): - result = '' + output = '' + success = False try: # only add output if error occurs self.test_server() self.test_directory() # always print output - result += self.remove_old() - result += self.run_backup() + output += self.remove_old() + output += self.run_backup() + success = True except subprocess.CalledProcessError as e: - result += e.output.decode('utf-8') - result += 'Backup failed with error %d' % e.returncode + output += e.output.decode('utf-8') + output += 'Backup failed with error %d' % e.returncode except OSError as e: - result += 'Backup failed: %s (%d)' % (e.strerror, e.errno) - return result + output += 'Backup failed: %s (%d)' % (e.strerror, e.errno) + return {'success': success, 'output': output} def rdiff_backup(config): target = Host(config) @@ -82,12 +84,12 @@ if __name__ == '__main__': pool = Pool(concurrent_num) # Start the work and wait for it to finish - results = [ (host, pool.apply_async(rdiff_backup, (host, )) ) for host in hosts ] + results = { host['hostname']: pool.apply_async(rdiff_backup, (host, )) for host in hosts } pool.close() pool.join() - for host, r in results: - print("Backup results for", host['hostname']) - print(r.get()) + for host, r in results.items(): + print("Backup results for", host) + print(r.get()['output']) print('-' * 40) print() -- GitLab