From dfcb9d3c2da1ff397e658e0fac64227ff2941c60 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Tue, 4 Sep 2012 14:16:30 +0200 Subject: [PATCH] Move backup to module_common --- lib/ansible/module_common.py | 14 ++++++++++++++ library/copy | 19 +------------------ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/ansible/module_common.py b/lib/ansible/module_common.py index c2376564415..4106c44d84d 100644 --- a/lib/ansible/module_common.py +++ b/lib/ansible/module_common.py @@ -47,6 +47,8 @@ import subprocess import sys import syslog import types +import time +import shutil try: from hashlib import md5 as _md5 @@ -274,6 +276,18 @@ class AnsibleModule(object): infile.close() return digest.hexdigest() + def backuplocal(self, fn): + '''make a date-marked backup of the specified file, return True or False on success or failure''' + # backups named basename-YYYY-MM-DD@HH:MM~ + ext = time.strftime("%Y-%m-%d@%H:%M~", time.localtime(time.time())) + backupdest = '%s.%s' % (fn, ext) + + try: + shutil.copy2(fn, backupdest) + except shutil.Error, e: + self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e)) + return backupdest + # == END DYNAMICALLY INSERTED CODE === diff --git a/library/copy b/library/copy index be7e685a8ab..9d9a00241bc 100755 --- a/library/copy +++ b/library/copy @@ -20,19 +20,6 @@ import os import shutil -import time - -def backuplocal(fn): - """make a date-marked backup of the specified file, return True or False on success or failure""" - # backups named basename-YYYY-MM-DD@HH:MM~ - ext = time.strftime("%Y-%m-%d@%H:%M~", time.localtime(time.time())) - backupdest = '%s.%s' % (fn, ext) - - try: - shutil.copy2(fn, backupdest) - except shutil.Error, e: - return False, 'Could not make backup of %s to %s: %s' % (fn, backupdest, e) - return True, backupdest def main(): @@ -76,11 +63,7 @@ def main(): try: if backup: if os.path.exists(dest): - success, msg = backuplocal(dest) - if not success: - module.fail_jason(msg=msg) - else: - backup_file = msg + backup_file = module.backuplocal(dest) shutil.copyfile(src, dest) except shutil.Error: module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest))