diff mbox series

[U-Boot,27/53] binman: Tidy up _SetupDtb() to use its own temporary file

Message ID 20190720182416.183626-28-sjg@chromium.org
State Accepted
Commit a004f29464d14f3535ed8db22e5dfed02c8fc9d8
Delegated to: Simon Glass
Headers show
Series binman: Support replacing entries in an existing image | expand

Commit Message

Simon Glass July 20, 2019, 6:23 p.m. UTC
At present EnsureCompiled() uses an file from the 'output' directory (in
the tools module) when compiling the device tree. This is fine in most
cases, allowing useful inspection of the output files from binman.

However in functional tests, _SetupDtb() creates an output directory and
immediately removes it afterwards. This serves no benefit and just
confuses things, since the 'official' output directory is supposed to be
created and destroyed in control.Binman().

Add a new parameter for the optional temporary directory to use, and use a
separate temporary directory in _SetupDtb().

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/binman/ftest.py  |  6 +++---
 tools/dtoc/fdt_util.py | 12 +++++++++---
 tools/dtoc/test_fdt.py | 17 ++++++++++++++++-
 3 files changed, 28 insertions(+), 7 deletions(-)

Comments

Simon Glass July 29, 2019, 9:22 p.m. UTC | #1
At present EnsureCompiled() uses an file from the 'output' directory (in
the tools module) when compiling the device tree. This is fine in most
cases, allowing useful inspection of the output files from binman.

However in functional tests, _SetupDtb() creates an output directory and
immediately removes it afterwards. This serves no benefit and just
confuses things, since the 'official' output directory is supposed to be
created and destroyed in control.Binman().

Add a new parameter for the optional temporary directory to use, and use a
separate temporary directory in _SetupDtb().

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/binman/ftest.py  |  6 +++---
 tools/dtoc/fdt_util.py | 12 +++++++++---
 tools/dtoc/test_fdt.py | 17 ++++++++++++++++-
 3 files changed, 28 insertions(+), 7 deletions(-)

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index bb886266277..47153285922 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -287,12 +287,12 @@  class TestFunctional(unittest.TestCase):
         Returns:
             Contents of device-tree binary
         """
-        tools.PrepareOutputDir(None)
-        dtb = fdt_util.EnsureCompiled(self.TestFile(fname))
+        tmpdir = tempfile.mkdtemp(prefix='binmant.')
+        dtb = fdt_util.EnsureCompiled(self.TestFile(fname), tmpdir)
         with open(dtb, 'rb') as fd:
             data = fd.read()
             TestFunctional._MakeInputFile(outfile, data)
-        tools.FinaliseOutputDir()
+        shutil.rmtree(tmpdir)
         return data
 
     def _GetDtbContentsForSplTpl(self, dtb_data, name):
diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py
index f47879ac006..b105faec749 100644
--- a/tools/dtoc/fdt_util.py
+++ b/tools/dtoc/fdt_util.py
@@ -43,12 +43,14 @@  def fdt_cells_to_cpu(val, cells):
         out = out << 32 | fdt32_to_cpu(val[1])
     return out
 
-def EnsureCompiled(fname, capture_stderr=False):
+def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
     """Compile an fdt .dts source file into a .dtb binary blob if needed.
 
     Args:
         fname: Filename (if .dts it will be compiled). It not it will be
             left alone
+        tmpdir: Temporary directory for output files, or None to use the
+            tools-module output directory
 
     Returns:
         Filename of resulting .dtb file
@@ -57,8 +59,12 @@  def EnsureCompiled(fname, capture_stderr=False):
     if ext != '.dts':
         return fname
 
-    dts_input = tools.GetOutputFilename('source.dts')
-    dtb_output = tools.GetOutputFilename('source.dtb')
+    if tmpdir:
+        dts_input = os.path.join(tmpdir, 'source.dts')
+        dtb_output = os.path.join(tmpdir, 'source.dtb')
+    else:
+        dts_input = tools.GetOutputFilename('source.dts')
+        dtb_output = tools.GetOutputFilename('source.dtb')
 
     search_paths = [os.path.join(os.getcwd(), 'include')]
     root, _ = os.path.splitext(fname)
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index ed2d982a8fc..16a4430892e 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -9,7 +9,9 @@  from __future__ import print_function
 from optparse import OptionParser
 import glob
 import os
+import shutil
 import sys
+import tempfile
 import unittest
 
 # Bring in the patman libraries
@@ -540,10 +542,23 @@  class TestFdtUtil(unittest.TestCase):
         self.assertEqual(0x12345678, fdt_util.fdt_cells_to_cpu(val, 1))
 
     def testEnsureCompiled(self):
-        """Test a degenerate case of this function"""
+        """Test a degenerate case of this function (file already compiled)"""
         dtb = fdt_util.EnsureCompiled('tools/dtoc/dtoc_test_simple.dts')
         self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb))
 
+    def testEnsureCompiledTmpdir(self):
+        """Test providing a temporary directory"""
+        try:
+            old_outdir = tools.outdir
+            tools.outdir= None
+            tmpdir = tempfile.mkdtemp(prefix='test_fdt.')
+            dtb = fdt_util.EnsureCompiled('tools/dtoc/dtoc_test_simple.dts',
+                                          tmpdir)
+            self.assertEqual(tmpdir, os.path.dirname(dtb))
+            shutil.rmtree(tmpdir)
+        finally:
+            tools.outdir= old_outdir
+
 
 def RunTestCoverage():
     """Run the tests and check that we get 100% coverage"""