1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

target: Customize as_root parameter of *write_value()

Not all command executions (or write operations in this specific case)
requires being root. So, allow write_value() and dependent
revertable_write_value() to support non-root executions by introducing
'as_root' optional parameter whose default is True to preserve current
behavior of the aforementioned methods.

Meanwhile, update the copyright year of the touched file, too.

Signed-off-by: Metin Kaya <metin.kaya@arm.com>
This commit is contained in:
Metin Kaya 2024-01-11 14:29:54 +00:00 committed by Marc Bonnici
parent 38d8053f2f
commit e334f8816c
2 changed files with 9 additions and 7 deletions

View File

@ -1015,19 +1015,19 @@ class Target(object):
return await self.read_value.asyn(path, kind=boolean) return await self.read_value.asyn(path, kind=boolean)
@asyn.asynccontextmanager @asyn.asynccontextmanager
async def revertable_write_value(self, path, value, verify=True): async def revertable_write_value(self, path, value, verify=True, as_root=True):
orig_value = self.read_value(path) orig_value = self.read_value(path)
try: try:
await self.write_value.asyn(path, value, verify) await self.write_value.asyn(path, value, verify=verify, as_root=as_root)
yield yield
finally: finally:
await self.write_value.asyn(path, orig_value, verify) await self.write_value.asyn(path, orig_value, verify=verify, as_root=as_root)
def batch_revertable_write_value(self, kwargs_list): def batch_revertable_write_value(self, kwargs_list):
return batch_contextmanager(self.revertable_write_value, kwargs_list) return batch_contextmanager(self.revertable_write_value, kwargs_list)
@asyn.asyncf @asyn.asyncf
async def write_value(self, path, value, verify=True): async def write_value(self, path, value, verify=True, as_root=True):
self.async_manager.track_access( self.async_manager.track_access(
asyn.PathAccess(namespace='target', path=path, mode='w') asyn.PathAccess(namespace='target', path=path, mode='w')
) )
@ -1059,7 +1059,7 @@ fi
cmd = cmd.format(busybox=quote(self.busybox), path=quote(path), value=quote(value)) cmd = cmd.format(busybox=quote(self.busybox), path=quote(path), value=quote(value))
try: try:
await self.execute.asyn(cmd, check_exit_code=True, as_root=True) await self.execute.asyn(cmd, check_exit_code=True, as_root=as_root)
except TargetCalledProcessError as e: except TargetCalledProcessError as e:
if e.returncode == 10: if e.returncode == 10:
raise TargetStableError('Could not write "{value}" to {path}: {e.output}'.format( raise TargetStableError('Could not write "{value}" to {path}: {e.output}'.format(

View File

@ -386,7 +386,7 @@ Target
Equivalent to ``Target.read_value(path, kind=devlib.utils.types.boolean)`` Equivalent to ``Target.read_value(path, kind=devlib.utils.types.boolean)``
.. method:: Target.write_value(path, value [, verify]) .. method:: Target.write_value(path, value [, verify, as_root])
Write the value to the specified path on the target. This is primarily Write the value to the specified path on the target. This is primarily
intended for sysfs/procfs/debugfs etc. intended for sysfs/procfs/debugfs etc.
@ -397,8 +397,10 @@ Target
it is written to make sure it has been written successfully. This due to it is written to make sure it has been written successfully. This due to
some sysfs entries silently failing to set the written value without some sysfs entries silently failing to set the written value without
returning an error code. returning an error code.
:param as_root: specifies if writing requires being root. Its default value
is ``True``.
.. method:: Target.revertable_write_value(path, value [, verify]) .. method:: Target.revertable_write_value(path, value [, verify, as_root])
Same as :meth:`Target.write_value`, but as a context manager that will write Same as :meth:`Target.write_value`, but as a context manager that will write
back the previous value on exit. back the previous value on exit.