Home | History | Annotate | Download | only in ota_tools
      1 #!/usr/bin/env python3.4
      2 #
      3 #   Copyright 2017 - The Android Open Source Project
      4 #
      5 #   Licensed under the Apache License, Version 2.0 (the "License");
      6 #   you may not use this file except in compliance with the License.
      7 #   You may obtain a copy of the License at
      8 #
      9 #       http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 #   Unless required by applicable law or agreed to in writing, software
     12 #   distributed under the License is distributed on an "AS IS" BASIS,
     13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 #   See the License for the specific language governing permissions and
     15 #   limitations under the License.
     16 
     17 from acts.libs.ota.ota_tools.adb_sideload_ota_tool import AdbSideloadOtaTool
     18 from acts.libs.ota.ota_tools.update_device_ota_tool import UpdateDeviceOtaTool
     19 
     20 _CONSTRUCTORS = {
     21     AdbSideloadOtaTool.__name__: lambda command: AdbSideloadOtaTool(command),
     22     UpdateDeviceOtaTool.__name__: lambda command: UpdateDeviceOtaTool(command),
     23 }
     24 _constructed_tools = {}
     25 
     26 
     27 def create(ota_tool_class, command):
     28     """Returns an OtaTool with the given class name.
     29 
     30     If the tool has already been created, the existing instance will be
     31     returned.
     32 
     33     Args:
     34         ota_tool_class: the class/type of the tool you wish to use.
     35         command: the command line tool being used.
     36 
     37     Returns:
     38         An OtaTool.
     39     """
     40     if ota_tool_class in _constructed_tools:
     41         return _constructed_tools[ota_tool_class]
     42 
     43     if ota_tool_class not in _CONSTRUCTORS:
     44         raise KeyError('Given Ota Tool class name does not match a known '
     45                        'name. Found "%s". Expected any of %s. If this tool '
     46                        'does exist, add it to the _CONSTRUCTORS dict in this '
     47                        'module.' % (ota_tool_class, _CONSTRUCTORS.keys()))
     48 
     49     new_update_tool = _CONSTRUCTORS[ota_tool_class](command)
     50     _constructed_tools[ota_tool_class] = new_update_tool
     51 
     52     return new_update_tool
     53