1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-03-04 09:17:51 +00:00

devlib: Replace Target.tempfile() by Target.make_temp()

Replace as many uses of tempfile() by make_temp() as possible, as the
latter provide more reliable resource control by way of a context
manager. This also paves the way to having a single point in devlib
where temporary files are created, simplifying maintenance.
This commit is contained in:
Douglas Raillard 2025-02-12 11:52:08 +00:00 committed by Marc Bonnici
parent 1da260b897
commit c88a5dbb8b
2 changed files with 45 additions and 61 deletions

View File

@ -340,30 +340,23 @@ def _mount_v2_controllers(target: LinuxTarget):
is unable to be created up on the target system. is unable to be created up on the target system.
""" """
path = target.tempfile() with target.make_temp() as path:
try:
try: target.execute(
target.makedirs(path, as_root=True) "{busybox} mount -t cgroup2 none {path}".format(
except TargetStableCalledProcessError: busybox=quote(target.busybox), path=quote(path)
raise TargetStableError("Un-able to create the root directory of the requested CGroup V2 hierarchy") ),
as_root=True,
)
try: yield path
target.execute( finally:
"{busybox} mount -t cgroup2 none {path}".format( target.execute(
busybox=quote(target.busybox), path=quote(path) "{busybox} umount {path}".format(
), busybox=quote(target.busybox),
as_root=True, path=quote(path),
) ),
yield path as_root=True,
finally: )
target.execute(
"{busybox} umount {path} && {busybox} rmdir -- {path}".format(
busybox=quote(target.busybox),
path=quote(path),
),
as_root=True,
)
@contextmanager @contextmanager
@ -388,33 +381,25 @@ def _mount_v1_controllers(target: LinuxTarget, controllers: Set[str]):
# its mount path. # its mount path.
@contextmanager @contextmanager
def _mount_controller(controller): def _mount_controller(controller):
with target.make_temp() as path:
try:
target.execute(
"{busybox} mount -t cgroup -o {controller} none {path}".format(
busybox=quote(target.busybox),
controller=quote(controller),
path=quote(path),
),
)
path = target.tempfile() yield path
finally:
try: target.execute(
target.makedirs(path, as_root=True) "{busybox} umount {path}".format(
except TargetStableCalledProcessError as err: busybox=quote(target.busybox),
raise TargetStableError("Un-able to create the root directory of the {controller} CGroup V1 hierarchy".format(controller = controller)) path=quote(path),
),
try: as_root=True,
target.execute( )
"{busybox} mount -t cgroup -o {controller} none {path}".format(
busybox=quote(target.busybox),
controller=quote(controller),
path=quote(path),
),
)
yield path
finally:
target.execute(
"{busybox} umount {path} && {busybox} rmdir -- {path}".format(
busybox=quote(target.busybox),
path=quote(path),
),
as_root=True,
)
with ExitStack() as stack: with ExitStack() as stack:
yield { yield {

View File

@ -1900,12 +1900,11 @@ class LinuxTarget(Target):
return return
try: try:
tmpfile = await self.tempfile.asyn() async with self.make_temp(is_directory=False) as tmpfile:
cmd = 'DISPLAY=:0.0 scrot {} && {} date -u -Iseconds' cmd = 'DISPLAY=:0.0 scrot {} && {} date -u -Iseconds'
ts = (await self.execute.asyn(cmd.format(quote(tmpfile), quote(self.busybox)))).strip() ts = (await self.execute.asyn(cmd.format(quote(tmpfile), quote(self.busybox)))).strip()
filepath = filepath.format(ts=ts) filepath = filepath.format(ts=ts)
await self.pull.asyn(tmpfile, filepath) await self.pull.asyn(tmpfile, filepath)
await self.remove.asyn(tmpfile)
except TargetStableError as e: except TargetStableError as e:
if "Can't open X dispay." not in e.message: if "Can't open X dispay." not in e.message:
raise e raise e