Files
android_development/gsi/gsi_util/gsi_util/mounters/adb_mounter.py
SzuWei Lin e3658bd0c1 gsi_util: adding pull subcommand
'pull' command can pull a file from an image file, folder or adb.

The patch includes a "mounter" framework to implement different
source of the system/vendor image. And also includes several
"mounter" implementations.

CompositeMounter integrates all possible mounter implementations.
Usually just using CompositeMounter is enough. With
CompositeMounter, you could access files in different target
with an unique interface, such files in an image file, a folder or
a device with an unique interface. pull.py is an basic example to
use CompositeMounter.

Here are some example to use 'pull' command:

$ ./gsi_util.py pull --system adb:AB0123456789 /system/manifest.xml
$ ./gsi_util.py pull --vendor adb /vendor/compatibility_matrix.xml
$ ./gsi_util.py pull --system system.img /system/build.prop
$ ./gsi_util.py pull --system my/out/folder/system /system/build.prop

As current implementation, accessing files in a the image file requires
root permission. gsi_util will need user to input the password for sudo.

For the detail usage, reference:

$ ./gsi_util.py pull --help

Bug: 71029338
Test: pull /system/build.prop from different targets
Change-Id: Iaeb6352c14ebc24860ed79fc30edd314e225aef9
2018-01-12 11:48:50 +08:00

83 lines
2.5 KiB
Python

# Copyright 2017 - The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Provides class AdbMounter.
The AdbMounter implements the abstract class BaseMounter. It can get files from
a device which is connected by adb.
"""
import errno
import logging
import os
import shutil
import tempfile
import gsi_util.mounters.base_mounter as base_mounter
import gsi_util.utils.adb_utils as adb_utils
class _AdbFileAccessor(base_mounter.BaseFileAccessor):
def __init__(self, temp_dir, serial_num):
super(_AdbFileAccessor, self).__init__()
self._temp_dir = temp_dir
self._serial_num = serial_num
@staticmethod
def _make_parent_dirs(filename):
"""Make parent directories as needed, no error if it exists."""
dir_path = os.path.dirname(filename)
try:
os.makedirs(dir_path)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
# override
def _handle_prepare_file(self, filename_in_storage):
filename = os.path.join(self._temp_dir, filename_in_storage)
logging.info('Prepare file %s -> %s', filename_in_storage, filename)
self._make_parent_dirs(filename)
if not adb_utils.pull(filename, filename_in_storage, self._serial_num):
logging.error('Fail to prepare file: %s', filename_in_storage)
return None
return base_mounter.MounterFile(filename)
class AdbMounter(base_mounter.BaseMounter):
"""Provides a file accessor which can access files by adb."""
def __init__(self, serial_num=None):
super(AdbMounter, self).__init__()
self._serial_num = serial_num
# override
def _handle_mount(self):
adb_utils.root(self._serial_num)
self._temp_dir = tempfile.mkdtemp()
logging.debug('Created temp dir: %s', self._temp_dir)
return _AdbFileAccessor(self._temp_dir, self._serial_num)
# override
def _handle_unmount(self):
if hasattr(self, '_temp_dir'):
logging.debug('Remove temp dir: %s', self._temp_dir)
shutil.rmtree(self._temp_dir)
del self._temp_dir