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

target: Add Target.revertable_write_value()

Same as write_value(), but returns a context manager that will write
back the old value on exit.

Also add batch_revertable_write_value() that takes a list of kwargs
dict, and will call revertable_write_value() on each of them, returning
a single combined context manager.
This commit is contained in:
Douglas RAILLARD 2019-05-09 16:24:29 +01:00 committed by Marc Bonnici
parent ded30eef00
commit 988de69b61
2 changed files with 26 additions and 1 deletions

View File

@ -29,6 +29,7 @@ import threading
import xml.dom.minidom import xml.dom.minidom
import copy import copy
from collections import namedtuple, defaultdict from collections import namedtuple, defaultdict
from contextlib import contextmanager
from pipes import quote from pipes import quote
from past.builtins import long from past.builtins import long
from past.types import basestring from past.types import basestring
@ -51,6 +52,7 @@ from devlib.utils.android import AdbConnection, AndroidProperties, LogcatMonitor
from devlib.utils.misc import memoized, isiterable, convert_new_lines from devlib.utils.misc import memoized, isiterable, convert_new_lines
from devlib.utils.misc import commonprefix, merge_lists from devlib.utils.misc import commonprefix, merge_lists
from devlib.utils.misc import ABI_MAP, get_cpu_name, ranges_to_list from devlib.utils.misc import ABI_MAP, get_cpu_name, ranges_to_list
from devlib.utils.misc import batch_contextmanager
from devlib.utils.types import integer, boolean, bitmask, identifier, caseless_string, bytes_regex from devlib.utils.types import integer, boolean, bitmask, identifier, caseless_string, bytes_regex
@ -70,7 +72,6 @@ GOOGLE_DNS_SERVER_ADDRESS = '8.8.8.8'
installed_package_info = namedtuple('installed_package_info', 'apk_path package') installed_package_info = namedtuple('installed_package_info', 'apk_path package')
class Target(object): class Target(object):
path = None path = None
@ -481,6 +482,18 @@ class Target(object):
def read_bool(self, path): def read_bool(self, path):
return self.read_value(path, kind=boolean) return self.read_value(path, kind=boolean)
@contextmanager
def revertable_write_value(self, path, value, verify=True):
orig_value = self.read_value(path)
try:
self.write_value(path, value, verify)
yield
finally:
self.write_value(path, orig_value, verify)
def batch_revertable_write_value(self, kwargs_list):
return batch_contextmanager(self.revertable_write_value, kwargs_list)
def write_value(self, path, value, verify=True): def write_value(self, path, value, verify=True):
value = str(value) value = str(value)
self.execute('echo {} > {}'.format(quote(value), quote(path)), check_exit_code=False, as_root=True) self.execute('echo {} > {}'.format(quote(value), quote(path)), check_exit_code=False, as_root=True)

View File

@ -346,6 +346,18 @@ Target
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.
.. method:: Target.revertable_write_value(path, value [, verify])
Same as :meth:`Target.write_value`, but as a context manager that will write
back the previous value on exit.
.. method:: Target.batch_revertable_write_value(kwargs_list)
Calls :meth:`Target.revertable_write_value` with all the keyword arguments
dictionary given in the list. This is a convenience method to update
multiple files at once, leaving them in their original state on exit. If one
write fails, all the already-performed writes will be reverted as well.
.. method:: Target.read_tree_values(path, depth=1, dictcls=dict, [, tar [, decode_unicode [, strip_null_char ]]]): .. method:: Target.read_tree_values(path, depth=1, dictcls=dict, [, tar [, decode_unicode [, strip_null_char ]]]):
Read values of all sysfs (or similar) file nodes under ``path``, traversing Read values of all sysfs (or similar) file nodes under ``path``, traversing