mirror of
https://github.com/ARM-software/devlib.git
synced 2025-09-09 13:31:53 +01: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:
committed by
Marc Bonnici
parent
ded30eef00
commit
988de69b61
@@ -29,6 +29,7 @@ import threading
|
||||
import xml.dom.minidom
|
||||
import copy
|
||||
from collections import namedtuple, defaultdict
|
||||
from contextlib import contextmanager
|
||||
from pipes import quote
|
||||
from past.builtins import long
|
||||
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 commonprefix, merge_lists
|
||||
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
|
||||
|
||||
|
||||
@@ -70,7 +72,6 @@ GOOGLE_DNS_SERVER_ADDRESS = '8.8.8.8'
|
||||
|
||||
installed_package_info = namedtuple('installed_package_info', 'apk_path package')
|
||||
|
||||
|
||||
class Target(object):
|
||||
|
||||
path = None
|
||||
@@ -481,6 +482,18 @@ class Target(object):
|
||||
def read_bool(self, path):
|
||||
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):
|
||||
value = str(value)
|
||||
self.execute('echo {} > {}'.format(quote(value), quote(path)), check_exit_code=False, as_root=True)
|
||||
|
Reference in New Issue
Block a user