1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-03-04 01:07: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

@ -335,35 +335,28 @@ def _mount_v2_controllers(target: LinuxTarget):
:yield: The path to the root of the mounted V2 controller hierarchy.
:rtype: str
:raises TargetStableError: Occurs in the case where the root directory of the requested CGroup V2 Controller hierarchy
:raises TargetStableError: Occurs in the case where the root directory of the requested CGroup V2 Controller hierarchy
is unable to be created up on the target system.
"""
path = target.tempfile()
try:
target.makedirs(path, as_root=True)
except TargetStableCalledProcessError:
raise TargetStableError("Un-able to create the root directory of the requested CGroup V2 hierarchy")
try:
target.execute(
"{busybox} mount -t cgroup2 none {path}".format(
busybox=quote(target.busybox), path=quote(path)
),
as_root=True,
)
yield path
finally:
target.execute(
"{busybox} umount {path} && {busybox} rmdir -- {path}".format(
busybox=quote(target.busybox),
path=quote(path),
),
as_root=True,
)
with target.make_temp() as path:
try:
target.execute(
"{busybox} mount -t cgroup2 none {path}".format(
busybox=quote(target.busybox), path=quote(path)
),
as_root=True,
)
yield path
finally:
target.execute(
"{busybox} umount {path}".format(
busybox=quote(target.busybox),
path=quote(path),
),
as_root=True,
)
@contextmanager
@ -379,8 +372,8 @@ def _mount_v1_controllers(target: LinuxTarget, controllers: Set[str]):
:yield: A dictionary mapping CGroup controller names to the paths that they're currently mounted at.
:rtype: Dict[str,str]
:raises TargetStableError: Occurs in the case where the root directory of a requested CGroup V1 Controller hierarchy
:raises TargetStableError: Occurs in the case where the root directory of a requested CGroup V1 Controller hierarchy
is unable to be created up on the target system.
"""
@ -388,33 +381,25 @@ def _mount_v1_controllers(target: LinuxTarget, controllers: Set[str]):
# its mount path.
@contextmanager
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()
try:
target.makedirs(path, as_root=True)
except TargetStableCalledProcessError as err:
raise TargetStableError("Un-able to create the root directory of the {controller} CGroup V1 hierarchy".format(controller = controller))
try:
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,
)
yield path
finally:
target.execute(
"{busybox} umount {path}".format(
busybox=quote(target.busybox),
path=quote(path),
),
as_root=True,
)
with ExitStack() as stack:
yield {
@ -569,7 +554,7 @@ class _CGroupBase(ABC):
)
except TargetStableError:
self._set_controller_attribute("cgroup", "procs", pid)
else:
if str(pid) not in member_processes:
self._set_controller_attribute("cgroup", "procs", pid)

View File

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