1
0
mirror of https://github.com/ARM-software/devlib.git synced 2024-10-05 18:30:50 +01:00

target: Introduce make_temp() for creating temp file/folder on target

``Target.make_temp()`` employs ``mktemp`` command to create a temporary
file or folder.

This method will be used in unit tests.

Signed-off-by: Metin Kaya <metin.kaya@arm.com>
This commit is contained in:
Metin Kaya 2024-01-31 12:48:31 +00:00 committed by Marc Bonnici
parent 84c0935fb2
commit 295f1269ed

View File

@ -14,6 +14,7 @@
#
import asyncio
from contextlib import contextmanager
import io
import base64
import functools
@ -1071,6 +1072,38 @@ fi
else:
raise
@contextmanager
def make_temp(self, is_directory=True, directory='', prefix='devlib-test'):
"""
Creates temporary file/folder on target and deletes it once it's done.
:param is_directory: Specifies if temporary object is a directory, defaults to True.
:type is_directory: bool, optional
:param directory: Temp object will be created under this directory,
defaults to :attr:`Target.working_directory`.
:type directory: str, optional
:param prefix: Prefix of temp object's name, defaults to 'devlib-test'.
:type prefix: str, optional
:yield: Full path of temp object.
:rtype: str
"""
directory = directory or self.working_directory
temp_obj = None
try:
cmd = f'mktemp -p {directory} {prefix}-XXXXXX'
if is_directory:
cmd += ' -d'
temp_obj = self.execute(cmd).strip()
yield temp_obj
finally:
if temp_obj is not None:
self.remove(temp_obj)
def reset(self):
try:
self.execute('reboot', as_root=self.needs_su, timeout=2)