blob: 11988053d1a71b2df38d271b9ce1d587bf78e391 [file] [log] [blame]
The Android Open Source Project52d4c302009-03-03 19:29:09 -08001#!/usr/bin/python2.4
2#
3#
4# Copyright 2008, The Android Open Source Project
5#
Brett Chabot8ac51182012-09-19 07:35:35 -07006# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
The Android Open Source Project52d4c302009-03-03 19:29:09 -08009#
Brett Chabot8ac51182012-09-19 07:35:35 -070010# http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project52d4c302009-03-03 19:29:09 -080011#
Brett Chabot8ac51182012-09-19 07:35:35 -070012# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
The Android Open Source Project52d4c302009-03-03 19:29:09 -080016# limitations under the License.
17import xml.dom.minidom
18import xml.parsers
19import os
20
Brett Chabot8ac51182012-09-19 07:35:35 -070021
22import coverage_target
The Android Open Source Project52d4c302009-03-03 19:29:09 -080023import logger
24import errors
25
26class CoverageTargets:
Brett Chabot8ac51182012-09-19 07:35:35 -070027 """Accessor for the code coverage target xml file
The Android Open Source Project52d4c302009-03-03 19:29:09 -080028 Expects the following format:
29 <targets>
Brett Chabot8ac51182012-09-19 07:35:35 -070030 <target
The Android Open Source Project52d4c302009-03-03 19:29:09 -080031 name=""
32 type="JAVA_LIBRARIES|APPS"
33 build_path=""
Brett Chabot8ac51182012-09-19 07:35:35 -070034
35 [<src path=""/>] (0..*) - These are relative to build_path. If missing,
The Android Open Source Project52d4c302009-03-03 19:29:09 -080036 assumes 'src'
37 >/target>
Brett Chabot8ac51182012-09-19 07:35:35 -070038
39 TODO: add more format checking
The Android Open Source Project52d4c302009-03-03 19:29:09 -080040 """
Brett Chabot8ac51182012-09-19 07:35:35 -070041
42 _TARGET_TAG_NAME = 'coverage_target'
43 _NAME_ATTR = 'name'
44 _TYPE_ATTR = 'type'
45 _BUILD_ATTR = 'build_path'
46 _SRC_TAG = 'src'
47 _PATH_ATTR = 'path'
48
The Android Open Source Project52d4c302009-03-03 19:29:09 -080049 def __init__(self, ):
50 self._target_map= {}
Brett Chabot8ac51182012-09-19 07:35:35 -070051
The Android Open Source Project52d4c302009-03-03 19:29:09 -080052 def __iter__(self):
Brett Chabot99f717d2012-09-21 13:01:03 -070053 return iter(self._target_map.values())
Brett Chabot8ac51182012-09-19 07:35:35 -070054
The Android Open Source Project52d4c302009-03-03 19:29:09 -080055 def Parse(self, file_path):
Brett Chabot8ac51182012-09-19 07:35:35 -070056 """Parse the coverage target data from from given file path, and add it to
The Android Open Source Project52d4c302009-03-03 19:29:09 -080057 the current object
58 Args:
59 file_path: absolute file path to parse
60 Raises:
Brett Chabot8ac51182012-09-19 07:35:35 -070061 errors.ParseError if file_path cannot be parsed
The Android Open Source Project52d4c302009-03-03 19:29:09 -080062 """
63 try:
64 doc = xml.dom.minidom.parse(file_path)
65 except IOError:
Brett Chabot8ac51182012-09-19 07:35:35 -070066 # Error: The results file does not exist
The Android Open Source Project52d4c302009-03-03 19:29:09 -080067 logger.Log('Results file %s does not exist' % file_path)
68 raise errors.ParseError
69 except xml.parsers.expat.ExpatError:
70 logger.Log('Error Parsing xml file: %s ' % file_path)
71 raise errors.ParseError
Brett Chabot8ac51182012-09-19 07:35:35 -070072
The Android Open Source Project52d4c302009-03-03 19:29:09 -080073 target_elements = doc.getElementsByTagName(self._TARGET_TAG_NAME)
74
75 for target_element in target_elements:
Brett Chabot8ac51182012-09-19 07:35:35 -070076 target = coverage_target.CoverageTarget()
77 self._ParseCoverageTarget(target, target_element)
The Android Open Source Project52d4c302009-03-03 19:29:09 -080078 self._AddTarget(target)
Brett Chabot8ac51182012-09-19 07:35:35 -070079
80 def _AddTarget(self, target):
The Android Open Source Project52d4c302009-03-03 19:29:09 -080081 self._target_map[target.GetName()] = target
Brett Chabot8ac51182012-09-19 07:35:35 -070082
The Android Open Source Project52d4c302009-03-03 19:29:09 -080083 def GetBuildTargets(self):
84 """ returns list of target names """
85 build_targets = []
86 for target in self:
87 build_targets.append(target.GetName())
Brett Chabot8ac51182012-09-19 07:35:35 -070088 return build_targets
89
The Android Open Source Project52d4c302009-03-03 19:29:09 -080090 def GetTargets(self):
91 """ returns list of CoverageTarget"""
92 return self._target_map.values()
Brett Chabot8ac51182012-09-19 07:35:35 -070093
The Android Open Source Project52d4c302009-03-03 19:29:09 -080094 def GetTarget(self, name):
95 """ returns CoverageTarget for given name. None if not found """
96 try:
97 return self._target_map[name]
98 except KeyError:
99 return None
Brett Chabot8ac51182012-09-19 07:35:35 -0700100
101 def _ParseCoverageTarget(self, target, target_element):
102 """Parse coverage data from XML.
103
104 Args:
105 target: the Coverage object to populate
106 target_element: the XML element to get data from
107 """
108 target.SetName(target_element.getAttribute(self._NAME_ATTR))
109 target.SetType(target_element.getAttribute(self._TYPE_ATTR))
110 target.SetBuildPath(target_element.getAttribute(self._BUILD_ATTR))
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800111 self._paths = []
Brett Chabot8ac51182012-09-19 07:35:35 -0700112 self._ParsePaths(target, target_element)
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800113
Brett Chabot8ac51182012-09-19 07:35:35 -0700114 def _ParsePaths(self, target, target_element):
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800115 src_elements = target_element.getElementsByTagName(self._SRC_TAG)
Brett Chaboteb421742009-04-08 18:55:01 -0700116 if len(src_elements) <= 0:
117 # no src tags specified. Assume build_path + src
Brett Chabot8ac51182012-09-19 07:35:35 -0700118 target.AddPath(os.path.join(target.GetBuildPath(), "src"))
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800119 for src_element in src_elements:
120 rel_path = src_element.getAttribute(self._PATH_ATTR)
Brett Chabot8ac51182012-09-19 07:35:35 -0700121 target.AddPath(os.path.join(target.GetBuildPath(), rel_path))
122
123
The Android Open Source Project52d4c302009-03-03 19:29:09 -0800124def Parse(xml_file_path):
125 """parses out a file_path class from given path to xml"""
126 targets = CoverageTargets()
127 targets.Parse(xml_file_path)
128 return targets