summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2014-05-07 16:23:48 +0000
committerSam Thursfield <sam.thursfield@codethink.co.uk>2014-05-08 09:28:39 +0000
commitbbd332414ccada1228d5e080a9094fbb8e270d3c (patch)
treee659abf19d49db7c131c0bbe6b40c07c01f2057e
parent15c82afd22557d8ecc48d5048c70012c4eef2044 (diff)
downloadmorph-bbd332414ccada1228d5e080a9094fbb8e270d3c.tar.gz
Make artifact creation more exception-safe
-rw-r--r--morphlib/bins.py30
1 files changed, 14 insertions, 16 deletions
diff --git a/morphlib/bins.py b/morphlib/bins.py
index 2373ae0f..eb783ae2 100644
--- a/morphlib/bins.py
+++ b/morphlib/bins.py
@@ -69,19 +69,18 @@ def create_chunk(rootdir, f, include, dump_memory_profile=None):
path_pairs = [(relname, os.path.join(rootdir, relname))
for relname in include]
- tar = tarfile.open(fileobj=f, mode='w')
- for relname, filename in path_pairs:
- # Normalize mtime for everything.
- tarinfo = tar.gettarinfo(filename,
- arcname=relname)
- tarinfo.ctime = normalized_timestamp
- tarinfo.mtime = normalized_timestamp
- if tarinfo.isreg():
- with open(filename, 'rb') as f:
- tar.addfile(tarinfo, fileobj=f)
- else:
- tar.addfile(tarinfo)
- tar.close()
+ with tarfile.open(fileobj=f, mode='w') as tar:
+ for relname, filename in path_pairs:
+ # Normalize mtime for everything.
+ tarinfo = tar.gettarinfo(filename,
+ arcname=relname)
+ tarinfo.ctime = normalized_timestamp
+ tarinfo.mtime = normalized_timestamp
+ if tarinfo.isreg():
+ with open(filename, 'rb') as f:
+ tar.addfile(tarinfo, fileobj=f)
+ else:
+ tar.addfile(tarinfo)
for relname, filename in reversed(path_pairs):
if os.path.isdir(filename) and not os.path.islink(filename):
@@ -104,9 +103,8 @@ def create_system(rootdir, f, name):
info.linkname = os.path.relpath(info.linkname, unslashy_root)
return info
- tar = tarfile.open(fileobj=f, mode="w", name=name)
- tar.add(rootdir, recursive=True, filter=uproot_info)
- tar.close()
+ with tarfile.open(fileobj=f, mode="w", name=name) as tar:
+ tar.add(rootdir, recursive=True, filter=uproot_info)
def unpack_binary_from_file(f, dirname): # pragma: no cover