clang-tidy: use TemporaryDirectory

This patch changes clang-tidy build of cmake and meson to use
TemporaryDirectory. So that no need to explicitly delete the build
directory. There is no need to hardcode the name as well. I kept the
prefix of the temp folder to be "build". This is to make gitignore of
most repos still work.

Tested:
1. used phosphor-user-manager as a typical meson repo. Tested
   with/without clang-tidy. Both worked
2. used peci-pcie as a typical CMakeList repo. Tested with/without
   clang-tidy. Both worked (but the repo itself doesn't pass clang build
   bacause of its own reason)

Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: Ic355e390db7349ded73ad3d17a03a40b83460249
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index 02b5846..860d2f2 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -745,26 +745,16 @@
 
     def analyze(self):
         if os.path.isfile('.clang-tidy'):
-            shutil.rmtree("tidy-build", ignore_errors=True)
-            os.mkdir("tidy-build")
+            with TemporaryDirectory(prefix='build', dir='.') as build_dir:
+                # clang-tidy needs to run on a clang-specific build
+                check_call_cmd('cmake', '-DCMAKE_C_COMPILER=clang',
+                               '-DCMAKE_CXX_COMPILER=clang++',
+                               '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON',
+                               '-H.',
+                               '-B' + build_dir)
 
-            # clang-tidy needs to run on a clang-specific build
-            check_call_cmd('cmake', '-DCMAKE_C_COMPILER=clang',
-                           '-DCMAKE_CXX_COMPILER=clang++',
-                           '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON',
-                           '-H.',
-                           '-Btidy-build')
-            # we need to cd here because otherwise clang-tidy doesn't find the
-            # .clang-tidy file in the roots of repos.  It's arguably a "bug"
-            # with run-clang-tidy at a minimum it's "weird" that it requires
-            # the .clang-tidy to be up a dir
-            os.chdir("tidy-build")
-            try:
                 check_call_cmd('run-clang-tidy', "-header-filter=.*", '-p',
-                               '.')
-            finally:
-                os.chdir("..")
-                shutil.rmtree("tidy-build", ignore_errors=True)
+                               build_dir)
 
         maybe_make_valgrind()
         maybe_make_coverage()
@@ -960,19 +950,15 @@
         # Run clang-tidy only if the project has a configuration
         if os.path.isfile('.clang-tidy'):
             os.environ["CXX"] = "clang++"
-            shutil.rmtree("build-clang", ignore_errors=True)
-            check_call_cmd('meson', 'setup', 'build-clang')
-            os.chdir("build-clang")
-            try:
-                check_call_cmd('run-clang-tidy', '-fix', '-format', '-p', '.')
-            except subprocess.CalledProcessError:
-                check_call_cmd("git", "-C", CODE_SCAN_DIR,
-                               "--no-pager", "diff")
-                raise
-            finally:
-                os.chdir("..")
-                shutil.rmtree("build-clang", ignore_errors=True)
-
+            with TemporaryDirectory(prefix='build', dir='.') as build_dir:
+                check_call_cmd('meson', 'setup', build_dir)
+                try:
+                    check_call_cmd('run-clang-tidy', '-fix',
+                                   '-format', '-p', build_dir)
+                except subprocess.CalledProcessError:
+                    check_call_cmd("git", "-C", CODE_SCAN_DIR,
+                                   "--no-pager", "diff")
+                    raise
         # Run the basic clang static analyzer otherwise
         else:
             check_call_cmd('ninja', '-C', 'build',