File: coverage_targets.py

package info (click to toggle)
android-platform-development 8.1.0%2Br23-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 37,780 kB
  • sloc: ansic: 166,133; xml: 75,737; java: 19,329; python: 11,511; cpp: 8,221; sh: 2,075; lisp: 261; ruby: 183; asm: 132; perl: 129; makefile: 18
file content (128 lines) | stat: -rw-r--r-- 3,890 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python2.4
#
#
# Copyright 2008, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import xml.dom.minidom
import xml.parsers
import os


import coverage_target
import logger
import errors

class CoverageTargets:
  """Accessor for the code coverage target xml file
  Expects the following format:
  <targets>
    <target
      name=""
      type="JAVA_LIBRARIES|APPS"
      build_path=""

      [<src path=""/>] (0..*)  - These are relative to build_path. If missing,
                                 assumes 'src'
    >/target>

    TODO: add more format checking
  """

  _TARGET_TAG_NAME = 'coverage_target'
  _NAME_ATTR = 'name'
  _TYPE_ATTR = 'type'
  _BUILD_ATTR = 'build_path'
  _SRC_TAG = 'src'
  _PATH_ATTR = 'path'

  def __init__(self, ):
    self._target_map= {}

  def __iter__(self):
    return iter(self._target_map.values())

  def Parse(self, file_path):
    """Parse the coverage target data from from given file path, and add it to
       the current object
       Args:
         file_path: absolute file path to parse
       Raises:
         errors.ParseError if file_path cannot be parsed
    """
    try:
      doc = xml.dom.minidom.parse(file_path)
    except IOError:
      # Error: The results file does not exist
      logger.Log('Results file %s does not exist' % file_path)
      raise errors.ParseError
    except xml.parsers.expat.ExpatError:
      logger.Log('Error Parsing xml file: %s ' %  file_path)
      raise errors.ParseError

    target_elements = doc.getElementsByTagName(self._TARGET_TAG_NAME)

    for target_element in target_elements:
      target = coverage_target.CoverageTarget()
      self._ParseCoverageTarget(target, target_element)
      self._AddTarget(target)

  def _AddTarget(self, target):
    self._target_map[target.GetName()] = target

  def GetBuildTargets(self):
    """ returns list of target names """
    build_targets = []
    for target in self:
      build_targets.append(target.GetName())
    return build_targets

  def GetTargets(self):
    """ returns list of CoverageTarget"""
    return self._target_map.values()

  def GetTarget(self, name):
    """ returns CoverageTarget for given name. None if not found """
    try:
      return self._target_map[name]
    except KeyError:
      return None

  def _ParseCoverageTarget(self, target, target_element):
    """Parse coverage data from XML.

    Args:
      target: the Coverage object to populate
      target_element: the XML element to get data from
    """
    target.SetName(target_element.getAttribute(self._NAME_ATTR))
    target.SetType(target_element.getAttribute(self._TYPE_ATTR))
    target.SetBuildPath(target_element.getAttribute(self._BUILD_ATTR))
    self._paths = []
    self._ParsePaths(target, target_element)

  def _ParsePaths(self, target, target_element):
    src_elements = target_element.getElementsByTagName(self._SRC_TAG)
    if len(src_elements) <= 0:
      # no src tags specified. Assume build_path + src
      target.AddPath(os.path.join(target.GetBuildPath(), "src"))
    for src_element in src_elements:
      rel_path = src_element.getAttribute(self._PATH_ATTR)
      target.AddPath(os.path.join(target.GetBuildPath(), rel_path))


def Parse(xml_file_path):
  """parses out a file_path class from given path to xml"""
  targets = CoverageTargets()
  targets.Parse(xml_file_path)
  return targets