From 8663b44318db4a18a5893382b1b5b9d3782f845b Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Fri, 2 Sep 2016 16:29:24 +0000 Subject: [PATCH] [lit] Clean up temporary files created by tests Do this by creating a temp directory in the normal system temp directory, and cleaning it up on exit. It is still possible for this temp directory to leak if Python exits abnormally, but this is probably good enough for now. Fixes PR18335 llvm-svn: 280501 --- llvm/utils/lit/lit/TestingConfig.py | 13 ++----------- llvm/utils/lit/lit/main.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/llvm/utils/lit/lit/TestingConfig.py b/llvm/utils/lit/lit/TestingConfig.py index 1e39a00..f3c0b11 100644 --- a/llvm/utils/lit/lit/TestingConfig.py +++ b/llvm/utils/lit/lit/TestingConfig.py @@ -25,7 +25,8 @@ class TestingConfig: pass_vars = ['LIBRARY_PATH', 'LD_LIBRARY_PATH', 'SYSTEMROOT', 'TERM', 'LD_PRELOAD', 'ASAN_OPTIONS', 'UBSAN_OPTIONS', 'LSAN_OPTIONS', 'ADB', 'ANDROID_SERIAL', - 'SANITIZER_IGNORE_CVE_2016_2143'] + 'SANITIZER_IGNORE_CVE_2016_2143', 'TMPDIR', 'TMP', 'TEMP', + 'TEMPDIR'] for var in pass_vars: val = os.environ.get(var, '') # Check for empty string as some variables such as LD_PRELOAD cannot be empty @@ -42,16 +43,6 @@ class TestingConfig: 'TMP' : os.environ.get('TMP',''), }) - # The option to preserve TEMP, TMP, and TMPDIR. - # This is intended to check how many temporary files would be generated - # (and be not cleaned up) in automated builders. - if 'LIT_PRESERVES_TMP' in os.environ: - environment.update({ - 'TEMP' : os.environ.get('TEMP',''), - 'TMP' : os.environ.get('TMP',''), - 'TMPDIR' : os.environ.get('TMPDIR',''), - }) - # Set the default available features based on the LitConfig. available_features = [] if litConfig.useValgrind: diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py index 30ae8b3..243a9dd 100755 --- a/llvm/utils/lit/lit/main.py +++ b/llvm/utils/lit/lit/main.py @@ -8,6 +8,8 @@ See lit.pod for more information. from __future__ import absolute_import import math, os, platform, random, re, sys, time +import tempfile +import shutil import lit.ProgressBar import lit.LitConfig @@ -132,6 +134,30 @@ def sort_by_incremental_cache(run): run.tests.sort(key = lambda t: sortIndex(t)) def main(builtinParameters = {}): + # Create a temp directory inside the normal temp directory so that we can + # try to avoid temporary test file leaks. The user can avoid this behavior + # by setting LIT_PRESERVES_TMP in the environment, so they can easily use + # their own temp directory to monitor temporary file leaks or handle them at + # the buildbot level. + lit_tmp = None + if 'LIT_PRESERVES_TMP' not in os.environ: + lit_tmp = tempfile.mkdtemp(prefix="lit_tmp_") + os.environ.update({ + 'TMPDIR': lit_tmp, + 'TMP': lit_tmp, + 'TEMP': lit_tmp, + 'TEMPDIR': lit_tmp, + }) + # FIXME: If Python does not exit cleanly, this directory will not be cleaned + # up. We should consider writing the lit pid into the temp directory, + # scanning for stale temp directories, and deleting temp directories whose + # lit process has died. + try: + main_with_tmp(builtinParameters) + finally: + shutil.rmtree(lit_tmp) + +def main_with_tmp(builtinParameters): global options from optparse import OptionParser, OptionGroup parser = OptionParser("usage: %prog [options] {file-or-path}") -- 2.7.4