diff options
author | Paul Sherwood <paul.sherwood@codethink.co.uk> | 2015-09-10 05:59:16 +0000 |
---|---|---|
committer | Paul Sherwood <paul.sherwood@codethink.co.uk> | 2015-09-10 05:59:18 +0000 |
commit | 330646de52b7b0d9e4010d2c90cb492a9e5cbd70 (patch) | |
tree | 5bb4bd56641c51e75d1106c1ed2f31d6ecbbc7bf | |
parent | 26183b2cb355c93d9ca5169e42919419b8f415e0 (diff) | |
download | ybd-clean-tmp-with-locks.tar.gz |
Clean tmp with locksclean-tmp-with-locks
-rw-r--r-- | app.py | 17 | ||||
-rwxr-xr-x | ybd.py | 7 |
2 files changed, 24 insertions, 0 deletions
@@ -17,6 +17,7 @@ import contextlib import datetime import os +import fcntl import shutil import sys import warnings @@ -141,6 +142,22 @@ def load_configs(config_files): log('SETUP', 'Configuration from %s:\n\n' % config_file, text) +def cleanup(tmpdir): + try: + with open(os.path.join(config['base'], 'lock'), 'w') as lockfile: + to_delete = os.listdir(tmpdir) + fcntl.flock(lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) + fcntl.flock(lockfile, fcntl.LOCK_UN) + if os.fork() == 0: + for dirname in to_delete: + if os.path.isdir(dirname): + shutil.rmtree(dirname) + log('SETUP', 'Cleanup successful for', tmpdir) + sys.exit(0) + except IOError: + log('SETUP', 'No cleanup for', tmpdir) + + @contextlib.contextmanager def chdir(dirname=None): currentdir = os.getcwd() @@ -19,6 +19,7 @@ import os import sys +import fcntl import app from assembly import assemble from deployment import deploy @@ -31,6 +32,12 @@ import sandboxlib print('') with app.timer('TOTAL'): app.setup(sys.argv) + + app.cleanup(app.config['tmp']) + + lockfile = open(os.path.join(app.config['base'], 'lock'), 'r') + fcntl.flock(lockfile, fcntl.LOCK_SH | fcntl.LOCK_NB) + target = os.path.join(app.config['defdir'], app.config['target']) app.log('TARGET', 'Target is %s' % target, app.config['arch']) with app.timer('DEFINITIONS', 'parsing %s' % app.config['def-version']): |