1# Copyright 2017 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Implementation of gsi_util command 'pull'."""
15
16import argparse
17import logging
18import shutil
19import sys
20
21from gsi_util.commands.common import image_sources
22
23
24def do_pull(args):
25  logging.info('==== PULL ====')
26
27  source, dest = args.SOURCE, args.DEST
28
29  mounter = image_sources.create_composite_mounter_by_args(args)
30  with mounter as file_accessor:
31    with file_accessor.prepare_file(source) as filename:
32      if not filename:
33        print >> sys.stderr, 'Can not dump file: {}'.format(source)
34      else:
35        logging.debug('Copy %s -> %s', filename, dest)
36        shutil.copy(filename, dest)
37
38  logging.info('==== DONE ====')
39
40
41_PULL_DESCRIPTION = ("""'pull' command pulls a file from the give image.
42
43You must assign at least one image source.
44
45SOURCE is the full path file name to pull, which must start with '/' and
46includes the mount point. ex.
47
48    /system/build.prop
49    /vendor/compatibility_matrix.xml
50
51Some usage examples:
52
53    $ ./gsi_util.py pull --system adb:AB0123456789 /system/manifest.xml
54    $ ./gsi_util.py pull --vendor adb /vendor/compatibility_matrix.xml
55    $ ./gsi_util.py pull --system system.img /system/build.prop
56    $ ./gsi_util.py pull --system my/out/folder/system /system/build.prop""")
57
58
59def setup_command_args(parser):
60  # command 'pull'
61  pull_parser = parser.add_parser(
62      'pull',
63      help='pull a file from the given image',
64      description=_PULL_DESCRIPTION,
65      formatter_class=argparse.RawTextHelpFormatter)
66  image_sources.add_argument_group(pull_parser)
67  pull_parser.add_argument(
68      'SOURCE',
69      type=str,
70      help='the full path file name in given image to be pull')
71  pull_parser.add_argument(
72      'DEST',
73      nargs='?',
74      default='.',
75      type=str,
76      help='the file name or directory to save the pulled file (default: .)')
77  pull_parser.set_defaults(func=do_pull)
78