# HG changeset patch # User Doug Simon # Date 1364556147 -3600 # Node ID aaf3988bd1b47ed67d44877985666b9fa053e944 # Parent c324983e5d2885729435f72ff2308c72b109b7ae made 'mx archive' clean up temporary files upon failure during archive file creation diff -r c324983e5d28 -r aaf3988bd1b4 mxtool/mx.py --- a/mxtool/mx.py Fri Mar 29 03:11:53 2013 +0100 +++ b/mxtool/mx.py Fri Mar 29 12:22:27 2013 +0100 @@ -1659,46 +1659,56 @@ d = distribution(dname) fd, tmp = tempfile.mkstemp(suffix='', prefix=basename(d.path) + '.', dir=dirname(d.path)) services = tempfile.mkdtemp(suffix='', prefix=basename(d.path) + '.', dir=dirname(d.path)) - zf = zipfile.ZipFile(tmp, 'w') - for p in sorted_deps(d.deps): - outputDir = p.output_dir() - for root, _, files in os.walk(outputDir): - relpath = root[len(outputDir) + 1:] - if relpath == join('META-INF', 'services'): - for f in files: - with open(join(services, f), 'a') as outfile: - with open(join(root, f), 'r') as infile: - for line in infile: - outfile.write(line) - else: - for f in files: - arcname = join(relpath, f).replace(os.sep, '/') - zf.write(join(root, f), arcname) - for f in os.listdir(services): - arcname = join('META-INF', 'services', f).replace(os.sep, '/') - zf.write(join(services, f), arcname) - zf.close() - os.close(fd) - shutil.rmtree(services) - # Atomic on Unix - shutil.move(tmp, d.path) - #print time.time(), 'move:', tmp, '->', d.path - d.notify_updated() + try: + zf = zipfile.ZipFile(tmp, 'w') + for p in sorted_deps(d.deps): + outputDir = p.output_dir() + for root, _, files in os.walk(outputDir): + relpath = root[len(outputDir) + 1:] + if relpath == join('META-INF', 'services'): + for f in files: + with open(join(services, f), 'a') as outfile: + with open(join(root, f), 'r') as infile: + for line in infile: + outfile.write(line) + else: + for f in files: + arcname = join(relpath, f).replace(os.sep, '/') + zf.write(join(root, f), arcname) + for f in os.listdir(services): + arcname = join('META-INF', 'services', f).replace(os.sep, '/') + zf.write(join(services, f), arcname) + zf.close() + os.close(fd) + shutil.rmtree(services) + # Atomic on Unix + shutil.move(tmp, d.path) + #print time.time(), 'move:', tmp, '->', d.path + d.notify_updated() + finally: + if exists(tmp): + os.remove(tmp) + if exists(services): + shutil.rmtree(services) else: p = project(name) outputDir = p.output_dir() fd, tmp = tempfile.mkstemp(suffix='', prefix=p.name, dir=p.dir) - zf = zipfile.ZipFile(tmp, 'w') - for root, _, files in os.walk(outputDir): - for f in files: - relpath = root[len(outputDir) + 1:] - arcname = join(relpath, f).replace(os.sep, '/') - zf.write(join(root, f), arcname) - zf.close() - os.close(fd) - # Atomic on Unix - shutil.move(tmp, join(p.dir, p.name + '.jar')) + try: + zf = zipfile.ZipFile(tmp, 'w') + for root, _, files in os.walk(outputDir): + for f in files: + relpath = root[len(outputDir) + 1:] + arcname = join(relpath, f).replace(os.sep, '/') + zf.write(join(root, f), arcname) + zf.close() + os.close(fd) + # Atomic on Unix + shutil.move(tmp, join(p.dir, p.name + '.jar')) + finally: + if exists(tmp): + os.remove(tmp) def canonicalizeprojects(args): """process all project files to canonicalize the dependencies