mirror of
https://github.com/ARM-software/devlib.git
synced 2025-01-31 02:00:45 +00:00
target: Use Target._xfer_cache_file() context manager
Use the context manager to simplify the file transfer cache management. Also introduce devlib.utils.misc.nullcontext() mirroring the behavior of contextlib.nullcontext().
This commit is contained in:
parent
24e6de67ae
commit
f23fbd22b6
@ -56,7 +56,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, tls_property
|
from devlib.utils.misc import batch_contextmanager, tls_property, nullcontext
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
@ -510,11 +510,12 @@ class Target(object):
|
|||||||
tmpfile = os.path.join(dest, tar_file_name)
|
tmpfile = os.path.join(dest, tar_file_name)
|
||||||
|
|
||||||
# If root is required, use tmp location for tar creation.
|
# If root is required, use tmp location for tar creation.
|
||||||
if as_root:
|
tar_file_cm = self._xfer_cache_path if as_root else nullcontext
|
||||||
tar_file_name = self.path.join(self._file_transfer_cache, tar_file_name)
|
|
||||||
|
|
||||||
# Does the folder exist?
|
# Does the folder exist?
|
||||||
self.execute('ls -la {}'.format(quote(source_dir)), as_root=as_root)
|
self.execute('ls -la {}'.format(quote(source_dir)), as_root=as_root)
|
||||||
|
|
||||||
|
with tar_file_cm(tar_file_name) as tar_file_name:
|
||||||
# Try compressing the folder
|
# Try compressing the folder
|
||||||
try:
|
try:
|
||||||
self.execute('{} tar -cvf {} {}'.format(
|
self.execute('{} tar -cvf {} {}'.format(
|
||||||
@ -528,7 +529,7 @@ class Target(object):
|
|||||||
os.mkdir(dest)
|
os.mkdir(dest)
|
||||||
self.pull(tar_file_name, tmpfile)
|
self.pull(tar_file_name, tmpfile)
|
||||||
# Decompress
|
# Decompress
|
||||||
f = tarfile.open(tmpfile, 'r')
|
with tarfile.open(tmpfile, 'r') as f:
|
||||||
f.extractall(outdir)
|
f.extractall(outdir)
|
||||||
os.remove(tmpfile)
|
os.remove(tmpfile)
|
||||||
|
|
||||||
|
@ -737,6 +737,23 @@ def batch_contextmanager(f, kwargs_list):
|
|||||||
stack.enter_context(f(**kwargs))
|
stack.enter_context(f(**kwargs))
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def nullcontext(enter_result=None):
|
||||||
|
"""
|
||||||
|
Backport of Python 3.7 ``contextlib.nullcontext``
|
||||||
|
|
||||||
|
This context manager does nothing, so it can be used as a default
|
||||||
|
placeholder for code that needs to select at runtime what context manager
|
||||||
|
to use.
|
||||||
|
|
||||||
|
:param enter_result: Object that will be bound to the target of the with
|
||||||
|
statement, or `None` if nothing is specified.
|
||||||
|
:type enter_result: object
|
||||||
|
"""
|
||||||
|
yield enter_result
|
||||||
|
|
||||||
|
|
||||||
class tls_property:
|
class tls_property:
|
||||||
"""
|
"""
|
||||||
Use it like `property` decorator, but the result will be memoized per
|
Use it like `property` decorator, but the result will be memoized per
|
||||||
|
Loading…
x
Reference in New Issue
Block a user