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:
parent
1da260b897
commit
c88a5dbb8b
@ -340,14 +340,7 @@ 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:
|
|
||||||
target.makedirs(path, as_root=True)
|
|
||||||
except TargetStableCalledProcessError:
|
|
||||||
raise TargetStableError("Un-able to create the root directory of the requested CGroup V2 hierarchy")
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
target.execute(
|
target.execute(
|
||||||
"{busybox} mount -t cgroup2 none {path}".format(
|
"{busybox} mount -t cgroup2 none {path}".format(
|
||||||
@ -358,7 +351,7 @@ def _mount_v2_controllers(target: LinuxTarget):
|
|||||||
yield path
|
yield path
|
||||||
finally:
|
finally:
|
||||||
target.execute(
|
target.execute(
|
||||||
"{busybox} umount {path} && {busybox} rmdir -- {path}".format(
|
"{busybox} umount {path}".format(
|
||||||
busybox=quote(target.busybox),
|
busybox=quote(target.busybox),
|
||||||
path=quote(path),
|
path=quote(path),
|
||||||
),
|
),
|
||||||
@ -388,14 +381,7 @@ 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:
|
||||||
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:
|
try:
|
||||||
target.execute(
|
target.execute(
|
||||||
"{busybox} mount -t cgroup -o {controller} none {path}".format(
|
"{busybox} mount -t cgroup -o {controller} none {path}".format(
|
||||||
@ -406,10 +392,9 @@ def _mount_v1_controllers(target: LinuxTarget, controllers: Set[str]):
|
|||||||
)
|
)
|
||||||
|
|
||||||
yield path
|
yield path
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
target.execute(
|
target.execute(
|
||||||
"{busybox} umount {path} && {busybox} rmdir -- {path}".format(
|
"{busybox} umount {path}".format(
|
||||||
busybox=quote(target.busybox),
|
busybox=quote(target.busybox),
|
||||||
path=quote(path),
|
path=quote(path),
|
||||||
),
|
),
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user