The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # |
| 4 | # Copyright 2008, The Android Open Source Project |
| 5 | # |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 6 | # 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 Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 9 | # |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 11 | # |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 12 | # 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 Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 16 | # limitations under the License. |
| 17 | import xml.dom.minidom |
| 18 | import xml.parsers |
| 19 | import os |
| 20 | |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 21 | |
| 22 | import coverage_target |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 23 | import logger |
| 24 | import errors |
| 25 | |
| 26 | class CoverageTargets: |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 27 | """Accessor for the code coverage target xml file |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 28 | Expects the following format: |
| 29 | <targets> |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 30 | <target |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 31 | name="" |
| 32 | type="JAVA_LIBRARIES|APPS" |
| 33 | build_path="" |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 34 | |
| 35 | [<src path=""/>] (0..*) - These are relative to build_path. If missing, |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 36 | assumes 'src' |
| 37 | >/target> |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 38 | |
| 39 | TODO: add more format checking |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 40 | """ |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 41 | |
| 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 Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 49 | def __init__(self, ): |
| 50 | self._target_map= {} |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 51 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 52 | def __iter__(self): |
Brett Chabot | 99f717d | 2012-09-21 13:01:03 -0700 | [diff] [blame] | 53 | return iter(self._target_map.values()) |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 54 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 55 | def Parse(self, file_path): |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 56 | """Parse the coverage target data from from given file path, and add it to |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 57 | the current object |
| 58 | Args: |
| 59 | file_path: absolute file path to parse |
| 60 | Raises: |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 61 | errors.ParseError if file_path cannot be parsed |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 62 | """ |
| 63 | try: |
| 64 | doc = xml.dom.minidom.parse(file_path) |
| 65 | except IOError: |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 66 | # Error: The results file does not exist |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 67 | 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 Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 73 | target_elements = doc.getElementsByTagName(self._TARGET_TAG_NAME) |
| 74 | |
| 75 | for target_element in target_elements: |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 76 | target = coverage_target.CoverageTarget() |
| 77 | self._ParseCoverageTarget(target, target_element) |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 78 | self._AddTarget(target) |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 79 | |
| 80 | def _AddTarget(self, target): |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 81 | self._target_map[target.GetName()] = target |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 83 | def GetBuildTargets(self): |
| 84 | """ returns list of target names """ |
| 85 | build_targets = [] |
| 86 | for target in self: |
| 87 | build_targets.append(target.GetName()) |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 88 | return build_targets |
| 89 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 90 | def GetTargets(self): |
| 91 | """ returns list of CoverageTarget""" |
| 92 | return self._target_map.values() |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 93 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 94 | 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 Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 100 | |
| 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 Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 111 | self._paths = [] |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 112 | self._ParsePaths(target, target_element) |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 113 | |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 114 | def _ParsePaths(self, target, target_element): |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 115 | src_elements = target_element.getElementsByTagName(self._SRC_TAG) |
Brett Chabot | eb42174 | 2009-04-08 18:55:01 -0700 | [diff] [blame] | 116 | if len(src_elements) <= 0: |
| 117 | # no src tags specified. Assume build_path + src |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 118 | target.AddPath(os.path.join(target.GetBuildPath(), "src")) |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 119 | for src_element in src_elements: |
| 120 | rel_path = src_element.getAttribute(self._PATH_ATTR) |
Brett Chabot | 8ac5118 | 2012-09-19 07:35:35 -0700 | [diff] [blame] | 121 | target.AddPath(os.path.join(target.GetBuildPath(), rel_path)) |
| 122 | |
| 123 | |
The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 124 | def 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 |