Home | History | Annotate | Download | only in command_processor
      1 #
      2 # Copyright (C) 2018 The Android Open Source Project
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the 'License');
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #      http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an 'AS IS' BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 #
     16 
     17 import logging
     18 
     19 from host_controller import common
     20 from host_controller.command_processor import base_command_processor
     21 
     22 
     23 class CommandFetch(base_command_processor.BaseCommandProcessor):
     24     """Command processor for fetch command.
     25 
     26     Attributes:
     27         arg_parser: ConsoleArgumentParser object, argument parser.
     28         console: cmd.Cmd console object.
     29         command: string, command name which this processor will handle.
     30         command_detail: string, detailed explanation for the command.
     31     """
     32 
     33     command = "fetch"
     34     command_detail = "Fetch a build artifact."
     35 
     36     # @Override
     37     def SetUp(self):
     38         """Initializes the parser for fetch command."""
     39         self.arg_parser.add_argument(
     40             '--type',
     41             default='pab',
     42             choices=('local_fs', 'gcs', 'pab', 'ab'),
     43             help='Build provider type')
     44         self.arg_parser.add_argument(
     45             '--method',
     46             default='GET',
     47             choices=('GET', 'POST'),
     48             help='Method for fetching')
     49         self.arg_parser.add_argument(
     50             "--path",  # required for local_fs
     51             help="The path of a local directory which keeps the artifacts.")
     52         self.arg_parser.add_argument(
     53             "--branch",  # required for pab
     54             help="Branch to grab the artifact from.")
     55         self.arg_parser.add_argument(
     56             "--target",  # required for pab
     57             help="Target product to grab the artifact from.")
     58         # TODO(lejonathan): find a way to not specify this?
     59         self.arg_parser.add_argument(
     60             "--account_id",
     61             default=common._DEFAULT_ACCOUNT_ID,
     62             help="Partner Android Build account_id to use.")
     63         self.arg_parser.add_argument(
     64             '--build_id',
     65             default='latest',
     66             help='Build ID to use default latest.')
     67         self.arg_parser.add_argument(
     68             "--artifact_name",  # required for pab
     69             help=
     70             "Name of the artifact to be fetched. {id} replaced with build id.")
     71         self.arg_parser.add_argument(
     72             "--userinfo-file",
     73             help=
     74             "Location of file containing email and password, if using POST.")
     75         self.arg_parser.add_argument(
     76             "--noauth_local_webserver",
     77             default=False,
     78             type=bool,
     79             help="True to not use a local webserver for authentication.")
     80 
     81     # @Override
     82     def Run(self, arg_line):
     83         """Makes the host download a build artifact from PAB."""
     84         args = self.arg_parser.ParseLine(arg_line)
     85 
     86         if args.type not in self.console._build_provider:
     87             print("ERROR: uninitialized fetch type %s" % args.type)
     88             return False
     89 
     90         provider = self.console._build_provider[args.type]
     91         if args.type == "pab":
     92             # do we want this somewhere else? No harm in doing multiple times
     93             provider.Authenticate(args.userinfo_file,
     94                                   args.noauth_local_webserver)
     95             (device_images, test_suites, fetch_environment,
     96              _) = provider.GetArtifact(
     97                  account_id=args.account_id,
     98                  branch=args.branch,
     99                  target=args.target,
    100                  artifact_name=args.artifact_name,
    101                  build_id=args.build_id,
    102                  method=args.method)
    103             self.console.fetch_info["build_id"] = fetch_environment["build_id"]
    104         elif args.type == "local_fs":
    105             device_images, test_suites = provider.Fetch(args.path)
    106             self.console.fetch_info["build_id"] = None
    107         elif args.type == "gcs":
    108             device_images, test_suites, tools = provider.Fetch(args.path)
    109             self.console.fetch_info["build_id"] = None
    110         elif args.type == "ab":
    111             device_images, test_suites, fetch_environment = provider.Fetch(
    112                 branch=args.branch,
    113                 target=args.target,
    114                 artifact_name=args.artifact_name,
    115                 build_id=args.build_id)
    116             self.console.fetch_info["build_id"] = fetch_environment["build_id"]
    117         else:
    118             print("ERROR: unknown fetch type %s" % args.type)
    119             return False
    120 
    121         self.console.fetch_info["branch"] = args.branch
    122         self.console.fetch_info["target"] = args.target
    123 
    124         self.console.device_image_info.update(device_images)
    125         self.console.test_suite_info.update(test_suites)
    126         self.console.tools_info.update(provider.GetAdditionalFile())
    127 
    128         if self.console.device_image_info:
    129             logging.info("device images:\n%s", "\n".join(
    130                 image + ": " + path
    131                 for image, path in self.console.device_image_info.iteritems()))
    132         if self.console.test_suite_info:
    133             logging.info("test suites:\n%s", "\n".join(
    134                 suite + ": " + path
    135                 for suite, path in self.console.test_suite_info.iteritems()))
    136         if self.console.tools_info:
    137             logging.info("additional files:\n%s", "\n".join(
    138                 rel_path + ": " + full_path
    139                 for rel_path, full_path in
    140                 self.console.tools_info.iteritems()))
    141