
From: Simon Glass <sjg@chromium.org> At present a temporary directory is used to hold the files before placing them in the filesystem. Use a suitably named directory inside the persistent-data directory, so that it is easy to take a look at what was produced. Signed-off-by: Simon Glass <sjg@chromium.org> --- test/py/tests/fs_helper.py | 40 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/test/py/tests/fs_helper.py b/test/py/tests/fs_helper.py index 3647af8fd89..c9df7a028a9 100644 --- a/test/py/tests/fs_helper.py +++ b/test/py/tests/fs_helper.py @@ -7,6 +7,7 @@ import re import os +import shutil from subprocess import call, check_call, check_output, CalledProcessError from subprocess import DEVNULL import tempfile @@ -63,16 +64,21 @@ class FsHelper: # Use a default filename; the caller can adjust it leaf = f'{prefix}.{fs_type}.img' - self.fs_img = os.path.join(config.persistent_data_dir, leaf) + if config: + self.fs_img = os.path.join(config.persistent_data_dir, leaf) + if os.path.exists(self.fs_img): + os.remove(self.fs_img) + else: + self.fs_img = leaf # Some distributions do not add /sbin to the default PATH, where mkfs # lives if '/sbin' not in os.environ["PATH"].split(os.pathsep): os.environ["PATH"] += os.pathsep + '/sbin' - self.srcdir = None self.tmpdir = None - self._do_cleanup = True + self.srcdir = None + self._do_cleanup = False def _get_fs_args(self): """Get the mkfs options and program to use @@ -108,6 +114,7 @@ class FsHelper: fs_img = self.fs_img with open(fs_img, 'wb') as fsi: fsi.truncate(self.size_mb << 20) + self._do_cleanup = True try: mkfs_opt, fs_lnxtype = self._get_fs_args() @@ -132,14 +139,22 @@ class FsHelper: def setup(self): """Set up the srcdir ready to receive files""" if not self.srcdir: - self.tmpdir = tempfile.TemporaryDirectory('fs_helper') - self.srcdir = self.tmpdir.name + if self.config: + self.srcdir = os.path.join(self.config.persistent_data_dir, + f'{self.prefix}.{self.fs_type}.tmp') + if os.path.exists(self.srcdir): + shutil.rmtree(self.srcdir) + os.mkdir(self.srcdir) + else: + self.tmpdir = tempfile.TemporaryDirectory('fs_helper') + self.srcdir = self.tmpdir.name def cleanup(self): - """Remove created files""" + """Remove created image""" if self.tmpdir: self.tmpdir.cleanup() - os.remove(self.fs_img) + if self._do_cleanup: + os.remove(self.fs_img) def __enter__(self): self.setup() @@ -188,6 +203,8 @@ class DiskHelper: self.fs_list = [] self.fname = os.path.join('' if cur_dir else config.persistent_data_dir, f'{prefix}{devnum}.img') + self.remove_img = True + self._do_cleanup = False def add_fs(self, fs_img, part_type, bootable=False): """Add a new filesystem @@ -217,6 +234,7 @@ class DiskHelper: img_size = pos try: check_call(f'qemu-img create {self.fname} {img_size}M', shell=True) + self._do_cleanup = True check_call(f'printf "{spec}" | sfdisk {self.fname}', shell=True) except CalledProcessError: os.remove(self.fname) @@ -230,15 +248,17 @@ class DiskHelper: pos += fsi.size_mb return self.fname - def cleanup(self, remove_full_img=False): + def cleanup(self): """Remove created file""" - os.remove(self.fname) + if self._do_cleanup: + os.remove(self.fname) def __enter__(self): return self def __exit__(self, extype, value, traceback): - self.cleanup() + if self.remove_img: + self.cleanup() def mk_fs(config, fs_type, size, prefix, src_dir=None, size_gran = 0x100000, -- 2.43.0