summaryrefslogtreecommitdiff
path: root/files
diff options
context:
space:
mode:
authorBen Doherty <ben@thinkoomph.com>2016-05-31 23:42:37 -0400
committerBen Doherty <ben@thinkoomph.com>2016-05-31 23:42:37 -0400
commit0a056eccbf84ac2886432e86d04833a56ad62d7b (patch)
treebb785b30b0af7a48be955346c62500a9b6d19f50 /files
parentec856c85ad07d1643e749e357b91577ade28fb68 (diff)
downloadansible-modules-extras-0a056eccbf84ac2886432e86d04833a56ad62d7b.tar.gz
Refactor zip and tarfile loops together, branch where calls are different
This fixed a few bugs and simplified the code
Diffstat (limited to 'files')
-rw-r--r--files/archive.py76
1 files changed, 36 insertions, 40 deletions
diff --git a/files/archive.py b/files/archive.py
index 04f9e1f5..21b3022f 100644
--- a/files/archive.py
+++ b/files/archive.py
@@ -220,58 +220,54 @@ def main():
if state != 'archive':
try:
- # Easier compression using tarfile module
- if compression == 'gz' or compression == 'bz2':
- arcfile = tarfile.open(creates, 'w|' + compression)
-
- arcfile.add(arcroot, os.path.basename(arcroot), recursive=False)
- for path in archive_paths:
- basename = ''
-
- # Prefix trees in the archive with their basename, unless specifically prevented with '.'
- if os.path.isdir(path) and not path.endswith(os.sep + '.'):
- basename = os.path.basename(path) + os.sep
-
- try:
- def exclude_creates(f):
- if os.path.exists(f.name) and not filecmp.cmp(f.name, creates):
- return f
+ # Slightly more difficult (and less efficient!) compression using zipfile module
+ if compression == 'zip':
+ arcfile = zipfile.ZipFile(creates, 'w', zipfile.ZIP_DEFLATED)
- return None
+ # Easier compression using tarfile module
+ elif compression == 'gz' or compression == 'bz2':
+ arcfile = tarfile.open(creates, 'w|' + compression)
- arcfile.add(path, basename + path[len(arcroot):], filter=exclude_creates)
- successes.append(path)
+ for path in archive_paths:
+ basename = ''
- except:
- e = get_exception()
- errors.append('error adding %s: %s' % (path, str(e)))
+ # Prefix trees in the archive with their basename, unless specifically prevented with '.'
+ if os.path.isdir(path) and not path.endswith(os.sep + '.'):
+ basename = os.path.basename(path) + os.sep
- # Slightly more difficult (and less efficient!) compression using zipfile module
- elif compression == 'zip':
- arcfile = zipfile.ZipFile(creates, 'w', zipfile.ZIP_DEFLATED)
+ for dirpath, dirnames, filenames in os.walk(path, topdown=True):
+ for dirname in dirnames:
+ fullpath = dirpath + os.sep + dirname
- for path in archive_paths:
- basename = ''
+ try:
+ if compression == 'zip':
+ arcfile.write(fullpath, basename + dirname)
+ else:
+ arcfile.add(fullpath, basename + dirname, recursive=False)
- # Prefix trees in the archive with their basename, unless specifically prevented with '.'
- if os.path.isdir(path) and not path.endswith(os.sep + '.'):
- basename = os.path.basename(path) + os.sep
+ except Exception:
+ e = get_exception()
+ errors.append('%s: %s' % (fullpath, str(e)))
- for dirpath, dirnames, filenames in os.walk(path, topdown=True):
- for dirname in dirnames:
- arcfile.write(dirpath + os.sep + dirname, basename + dirname)
- for filename in filenames:
- fullpath = dirpath + os.sep + filename
+ for filename in filenames:
+ fullpath = dirpath + os.sep + filename
- if not filecmp.cmp(fullpath, creates):
- arcfile.write(fullpath, basename + filename)
+ if not filecmp.cmp(fullpath, creates):
+ try:
+ if compression == 'zip':
+ arcfile.write(fullpath, basename + filename)
+ else:
+ arcfile.add(fullpath, basename + filename, recursive=False)
- successes.append(path)
+ successes.append(fullpath)
+ except Exception:
+ e = get_exception()
+ errors.append('Adding %s: %s' % (path, str(e)))
- except OSError:
+ except Exception:
e = get_exception()
- module.fail_json(msg='Error when writing %s archive at %s: %s' % (compression == 'zip' and 'zip' or ('tar.' + compression), creates, str(e)))
+ return module.fail_json(msg='Error when writing %s archive at %s: %s' % (compression == 'zip' and 'zip' or ('tar.' + compression), creates, str(e)))
if arcfile:
arcfile.close()