From 330646de52b7b0d9e4010d2c90cb492a9e5cbd70 Mon Sep 17 00:00:00 2001 From: Paul Sherwood Date: Thu, 10 Sep 2015 05:59:16 +0000 Subject: Clean tmp with locks --- app.py | 17 +++++++++++++++++ ybd.py | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/app.py b/app.py index 88d8327..0c6baa8 100644 --- a/app.py +++ b/app.py @@ -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() diff --git a/ybd.py b/ybd.py index a3e632e..c7d5b8c 100755 --- a/ybd.py +++ b/ybd.py @@ -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']): -- cgit v1.2.1