mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Refactor zip and tarfile loops together, branch where calls are different
This fixed a few bugs and simplified the code
This commit is contained in:
parent
8fc2a22b4c
commit
fb03fc8eb1
1 changed files with 38 additions and 42 deletions
|
@ -220,37 +220,15 @@ 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
|
||||
|
||||
return None
|
||||
|
||||
arcfile.add(path, basename + path[len(arcroot):], filter=exclude_creates)
|
||||
successes.append(path)
|
||||
|
||||
except:
|
||||
e = get_exception()
|
||||
errors.append('error adding %s: %s' % (path, str(e)))
|
||||
|
||||
# Slightly more difficult (and less efficient!) compression using zipfile module
|
||||
elif compression == 'zip':
|
||||
if compression == 'zip':
|
||||
arcfile = zipfile.ZipFile(creates, 'w', zipfile.ZIP_DEFLATED)
|
||||
|
||||
# Easier compression using tarfile module
|
||||
elif compression == 'gz' or compression == 'bz2':
|
||||
arcfile = tarfile.open(creates, 'w|' + compression)
|
||||
|
||||
for path in archive_paths:
|
||||
basename = ''
|
||||
|
||||
|
@ -260,18 +238,36 @@ def main():
|
|||
|
||||
for dirpath, dirnames, filenames in os.walk(path, topdown=True):
|
||||
for dirname in dirnames:
|
||||
arcfile.write(dirpath + os.sep + dirname, basename + dirname)
|
||||
fullpath = dirpath + os.sep + dirname
|
||||
|
||||
try:
|
||||
if compression == 'zip':
|
||||
arcfile.write(fullpath, basename + dirname)
|
||||
else:
|
||||
arcfile.add(fullpath, basename + dirname, recursive=False)
|
||||
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
errors.append('%s: %s' % (fullpath, str(e)))
|
||||
|
||||
for filename in filenames:
|
||||
fullpath = dirpath + os.sep + 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)
|
||||
|
||||
except OSError:
|
||||
successes.append(fullpath)
|
||||
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)))
|
||||
errors.append('Adding %s: %s' % (path, str(e)))
|
||||
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue