[llvm] r280501 - [lit] Clean up temporary files created by tests

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 2 09:29:24 PDT 2016


Author: rnk
Date: Fri Sep  2 11:29:24 2016
New Revision: 280501

URL: http://llvm.org/viewvc/llvm-project?rev=280501&view=rev
Log:
[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

Modified:
    llvm/trunk/utils/lit/lit/TestingConfig.py
    llvm/trunk/utils/lit/lit/main.py

Modified: llvm/trunk/utils/lit/lit/TestingConfig.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lit/lit/TestingConfig.py?rev=280501&r1=280500&r2=280501&view=diff
==============================================================================
--- llvm/trunk/utils/lit/lit/TestingConfig.py (original)
+++ llvm/trunk/utils/lit/lit/TestingConfig.py Fri Sep  2 11:29:24 2016
@@ -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:

Modified: llvm/trunk/utils/lit/lit/main.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lit/lit/main.py?rev=280501&r1=280500&r2=280501&view=diff
==============================================================================
--- llvm/trunk/utils/lit/lit/main.py (original)
+++ llvm/trunk/utils/lit/lit/main.py Fri Sep  2 11:29:24 2016
@@ -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}")




More information about the llvm-commits mailing list