mirror of
https://source.denx.de/u-boot/u-boot.git
synced 2026-06-02 09:46:37 +03:00
test: Convert fs_helper to use a class
Create a class around mk_fs() (and later setup_image()) to handle the common tasks of image creation. Many callers of fs_helper.mk_fs() create their own scratch directories while users of fs_helper.setup_image() rely on one being returned. Unify this by adding 'srcdir' as a field while converting to a class. The class delegates to the existing mk_fs() function for the actual filesystem creation, adding lifecycle management for scratch directories and the image file. Signed-off-by: Simon Glass <simon.glass@canonical.com> Reviewed-by: Tom Rini <trini@konsulko.com>
This commit is contained in:
@@ -7,13 +7,96 @@
|
||||
|
||||
import re
|
||||
import os
|
||||
import shutil
|
||||
from subprocess import call, check_call, check_output, CalledProcessError
|
||||
from subprocess import DEVNULL
|
||||
import tempfile
|
||||
|
||||
# size_gran (int): Size granularity of file system image in bytes
|
||||
SIZE_GRAN = 1 << 20
|
||||
|
||||
|
||||
class FsHelper:
|
||||
"""Creating a filesystem containing test files
|
||||
|
||||
Usage:
|
||||
with FsHelper(ubman.config, 'ext4', 10, 'mmc1') as fsh:
|
||||
# create files in the self.srcdir directory
|
||||
fsh.mk_fs()
|
||||
# Now use the filesystem
|
||||
|
||||
# The filesystem and srcdir are erased after the 'with' statement.
|
||||
|
||||
It is also possible to use an existing srcdir:
|
||||
|
||||
with FsHelper(ubman.config, 'fat32', 10, 'usb2') as fsh:
|
||||
fsh.srcdir = src_dir
|
||||
fsh.mk_fs()
|
||||
...
|
||||
|
||||
Properties:
|
||||
fs_img (str): Filename for the filesystem image
|
||||
"""
|
||||
def __init__(self, config, fs_type, size_mb, prefix):
|
||||
"""Set up a new object
|
||||
|
||||
Args:
|
||||
config (u_boot_config): U-Boot configuration
|
||||
fs_type (str): File system type: one of ext2, ext3, ext4, vfat,
|
||||
fat12, fat16, fat32, exfat, fs_generic (which means vfat)
|
||||
size_mb (int): Size of file system in MB
|
||||
prefix (str): Prefix string of volume's file name
|
||||
"""
|
||||
if fs_type not in ['fat12', 'fat16', 'fat32', 'vfat',
|
||||
'ext2', 'ext3', 'ext4',
|
||||
'exfat', 'fs_generic']:
|
||||
raise ValueError(f"Unsupported filesystem type '{fs_type}'")
|
||||
|
||||
self.config = config
|
||||
self.fs_type = fs_type
|
||||
self.size_mb = size_mb
|
||||
self.prefix = prefix
|
||||
self.quiet = True
|
||||
self.fs_img = None
|
||||
self.tmpdir = None
|
||||
self.srcdir = None
|
||||
self._do_cleanup = False
|
||||
|
||||
def mk_fs(self):
|
||||
"""Make a new filesystem and copy in the files"""
|
||||
self.setup()
|
||||
self._do_cleanup = True
|
||||
self.fs_img = mk_fs(self.config, self.fs_type, self.size_mb << 20,
|
||||
self.prefix, self.srcdir, quiet=self.quiet)
|
||||
|
||||
def setup(self):
|
||||
"""Set up the srcdir ready to receive files"""
|
||||
if not self.srcdir:
|
||||
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 image"""
|
||||
if self.tmpdir:
|
||||
self.tmpdir.cleanup()
|
||||
if self._do_cleanup:
|
||||
os.remove(self.fs_img)
|
||||
|
||||
def __enter__(self):
|
||||
self.setup()
|
||||
return self
|
||||
|
||||
def __exit__(self, extype, value, traceback):
|
||||
self.cleanup()
|
||||
|
||||
|
||||
def mk_fs(config, fs_type, size, prefix, src_dir=None, fs_img=None, quiet=False):
|
||||
"""Create a file system volume
|
||||
|
||||
|
||||
Reference in New Issue
Block a user