1# 2# Copyright (C) 2017 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 17import logging 18import os 19 20from google.protobuf import text_format 21from vts.proto import ComponentSpecificationMessage_pb2 as CompSpecMsg 22 23 24def HalPackageToNameAndVersion(hal_package): 25 """Returns hal name and version given hal package name. 26 27 Args: 28 hal_package: string, e.g. 'android.hardware.vibrator@1.0' 29 30 Return: 31 tuple, hal name and version e.g. ('vibrator', '1.0') 32 """ 33 # TODO(trong): check if proper package name. 34 prefix = 'android.hardware.' 35 if not hal_package.startswith(prefix): 36 logging.error("Invalid hal package name: %s" % hal_package) 37 [hal_name, hal_version] = hal_package[len(prefix):].split('@') 38 return (hal_name, hal_version) 39 40 41class VtsSpecParser(object): 42 """Provides an API to parse .vts spec files. 43 44 Attributes: 45 data_file_path: string, path to vts data directory on target. 46 """ 47 48 def __init__(self, data_file_path): 49 """VtsSpecParser constructor. 50 51 Args: 52 data_file_path: string, path to vts data directory on target. 53 """ 54 self._data_file_path = data_file_path 55 56 def _VtsSpecDir(self, hal_name, hal_version): 57 """Returns directory path to .vts spec files. 58 59 Args: 60 hal_name: string, name of the hal, e.g. 'vibrator'. 61 hal_version: string, version of the hal, e.g '7.4' 62 63 Returns: 64 string, directory path to .vts spec files. 65 """ 66 return os.path.join( 67 self._data_file_path, 'spec', 'hardware', 'interfaces', 68 hal_name.replace('.', '/'), hal_version, 'vts') 69 70 def VtsSpecNames(self, hal_name, hal_version): 71 """Returns list of .vts file names for given hal name and version. 72 73 Args: 74 hal_name: string, name of the hal, e.g. 'vibrator'. 75 hal_version: string, version of the hal, e.g '7.4' 76 77 Returns: 78 list of string, .vts files for given hal name and version, 79 e.g. ['Vibrator.vts', 'types.vts'] 80 """ 81 vts_spec_names = filter( 82 lambda x: x.endswith('.vts'), 83 os.listdir(self._VtsSpecDir(hal_name, hal_version))) 84 return sorted(vts_spec_names) 85 86 def VtsSpecProto(self, hal_name, hal_version, vts_spec_name=''): 87 """Returns list of .vts protos for given hal name and version. 88 89 Args: 90 hal_name: string, name of the hal, e.g. 'vibrator'. 91 hal_version: string, version of the hal, e.g '7.4' 92 vts_spec: 93 94 Returns: 95 list with all vts spec protos for a given hal and version if 96 vts_spec_name is not given. If vts_spec_name is not empty, then 97 returns ComponentSpecificationMessage matching vts_spec_name. 98 If no such vts_spec_name, return None. 99 """ 100 if not vts_spec_name: 101 vts_spec_protos = [] 102 for vts_spec in self.VtsSpecNames(hal_name, hal_version): 103 vts_spec_proto = self.VtsSpecProto(hal_name, hal_version, 104 vts_spec) 105 vts_spec_protos.append(vts_spec_proto) 106 return vts_spec_protos 107 else: 108 if vts_spec_name in self.VtsSpecNames(hal_name, hal_version): 109 vts_spec_proto = CompSpecMsg.ComponentSpecificationMessage() 110 vts_spec_path = os.path.join( 111 self._VtsSpecDir(hal_name, hal_version), vts_spec_name) 112 with open(vts_spec_path, 'r') as vts_spec_file: 113 vts_spec_string = vts_spec_file.read() 114 text_format.Merge(vts_spec_string, vts_spec_proto) 115 return vts_spec_proto 116