config.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Config file for coverage.py"""
  4. import collections
  5. import os
  6. import re
  7. import sys
  8. from coverage.backward import configparser, iitems, string_class
  9. from coverage.misc import contract, CoverageException, isolate_module
  10. os = isolate_module(os)
  11. class HandyConfigParser(configparser.RawConfigParser):
  12. """Our specialization of ConfigParser."""
  13. def __init__(self, section_prefix):
  14. configparser.RawConfigParser.__init__(self)
  15. self.section_prefix = section_prefix
  16. def read(self, filename):
  17. """Read a file name as UTF-8 configuration data."""
  18. kwargs = {}
  19. if sys.version_info >= (3, 2):
  20. kwargs['encoding'] = "utf-8"
  21. return configparser.RawConfigParser.read(self, filename, **kwargs)
  22. def has_option(self, section, option):
  23. section = self.section_prefix + section
  24. return configparser.RawConfigParser.has_option(self, section, option)
  25. def has_section(self, section):
  26. section = self.section_prefix + section
  27. return configparser.RawConfigParser.has_section(self, section)
  28. def options(self, section):
  29. section = self.section_prefix + section
  30. return configparser.RawConfigParser.options(self, section)
  31. def get_section(self, section):
  32. """Get the contents of a section, as a dictionary."""
  33. d = {}
  34. for opt in self.options(section):
  35. d[opt] = self.get(section, opt)
  36. return d
  37. def get(self, section, *args, **kwargs):
  38. """Get a value, replacing environment variables also.
  39. The arguments are the same as `RawConfigParser.get`, but in the found
  40. value, ``$WORD`` or ``${WORD}`` are replaced by the value of the
  41. environment variable ``WORD``.
  42. Returns the finished value.
  43. """
  44. section = self.section_prefix + section
  45. v = configparser.RawConfigParser.get(self, section, *args, **kwargs)
  46. def dollar_replace(m):
  47. """Called for each $replacement."""
  48. # Only one of the groups will have matched, just get its text.
  49. word = next(w for w in m.groups() if w is not None) # pragma: part covered
  50. if word == "$":
  51. return "$"
  52. else:
  53. return os.environ.get(word, '')
  54. dollar_pattern = r"""(?x) # Use extended regex syntax
  55. \$(?: # A dollar sign, then
  56. (?P<v1>\w+) | # a plain word,
  57. {(?P<v2>\w+)} | # or a {-wrapped word,
  58. (?P<char>[$]) # or a dollar sign.
  59. )
  60. """
  61. v = re.sub(dollar_pattern, dollar_replace, v)
  62. return v
  63. def getlist(self, section, option):
  64. """Read a list of strings.
  65. The value of `section` and `option` is treated as a comma- and newline-
  66. separated list of strings. Each value is stripped of whitespace.
  67. Returns the list of strings.
  68. """
  69. value_list = self.get(section, option)
  70. values = []
  71. for value_line in value_list.split('\n'):
  72. for value in value_line.split(','):
  73. value = value.strip()
  74. if value:
  75. values.append(value)
  76. return values
  77. def getregexlist(self, section, option):
  78. """Read a list of full-line regexes.
  79. The value of `section` and `option` is treated as a newline-separated
  80. list of regexes. Each value is stripped of whitespace.
  81. Returns the list of strings.
  82. """
  83. line_list = self.get(section, option)
  84. value_list = []
  85. for value in line_list.splitlines():
  86. value = value.strip()
  87. try:
  88. re.compile(value)
  89. except re.error as e:
  90. raise CoverageException(
  91. "Invalid [%s].%s value %r: %s" % (section, option, value, e)
  92. )
  93. if value:
  94. value_list.append(value)
  95. return value_list
  96. # The default line exclusion regexes.
  97. DEFAULT_EXCLUDE = [
  98. r'(?i)#\s*pragma[:\s]?\s*no\s*cover',
  99. ]
  100. # The default partial branch regexes, to be modified by the user.
  101. DEFAULT_PARTIAL = [
  102. r'(?i)#\s*pragma[:\s]?\s*no\s*branch',
  103. ]
  104. # The default partial branch regexes, based on Python semantics.
  105. # These are any Python branching constructs that can't actually execute all
  106. # their branches.
  107. DEFAULT_PARTIAL_ALWAYS = [
  108. 'while (True|1|False|0):',
  109. 'if (True|1|False|0):',
  110. ]
  111. class CoverageConfig(object):
  112. """Coverage.py configuration.
  113. The attributes of this class are the various settings that control the
  114. operation of coverage.py.
  115. """
  116. def __init__(self):
  117. """Initialize the configuration attributes to their defaults."""
  118. # Metadata about the config.
  119. self.attempted_config_files = []
  120. self.config_files = []
  121. # Defaults for [run]
  122. self.branch = False
  123. self.concurrency = None
  124. self.cover_pylib = False
  125. self.data_file = ".coverage"
  126. self.debug = []
  127. self.note = None
  128. self.parallel = False
  129. self.plugins = []
  130. self.source = None
  131. self.timid = False
  132. # Defaults for [report]
  133. self.exclude_list = DEFAULT_EXCLUDE[:]
  134. self.fail_under = 0
  135. self.ignore_errors = False
  136. self.include = None
  137. self.omit = None
  138. self.partial_always_list = DEFAULT_PARTIAL_ALWAYS[:]
  139. self.partial_list = DEFAULT_PARTIAL[:]
  140. self.precision = 0
  141. self.show_missing = False
  142. self.skip_covered = False
  143. # Defaults for [html]
  144. self.extra_css = None
  145. self.html_dir = "htmlcov"
  146. self.html_title = "Coverage report"
  147. # Defaults for [xml]
  148. self.xml_output = "coverage.xml"
  149. self.xml_package_depth = 99
  150. # Defaults for [paths]
  151. self.paths = {}
  152. # Options for plugins
  153. self.plugin_options = {}
  154. MUST_BE_LIST = ["omit", "include", "debug", "plugins", "concurrency"]
  155. def from_args(self, **kwargs):
  156. """Read config values from `kwargs`."""
  157. for k, v in iitems(kwargs):
  158. if v is not None:
  159. if k in self.MUST_BE_LIST and isinstance(v, string_class):
  160. v = [v]
  161. setattr(self, k, v)
  162. @contract(filename=str)
  163. def from_file(self, filename, section_prefix=""):
  164. """Read configuration from a .rc file.
  165. `filename` is a file name to read.
  166. Returns True or False, whether the file could be read.
  167. """
  168. self.attempted_config_files.append(filename)
  169. cp = HandyConfigParser(section_prefix)
  170. try:
  171. files_read = cp.read(filename)
  172. except configparser.Error as err:
  173. raise CoverageException("Couldn't read config file %s: %s" % (filename, err))
  174. if not files_read:
  175. return False
  176. self.config_files.extend(files_read)
  177. try:
  178. for option_spec in self.CONFIG_FILE_OPTIONS:
  179. self._set_attr_from_config_option(cp, *option_spec)
  180. except ValueError as err:
  181. raise CoverageException("Couldn't read config file %s: %s" % (filename, err))
  182. # Check that there are no unrecognized options.
  183. all_options = collections.defaultdict(set)
  184. for option_spec in self.CONFIG_FILE_OPTIONS:
  185. section, option = option_spec[1].split(":")
  186. all_options[section].add(option)
  187. for section, options in iitems(all_options):
  188. if cp.has_section(section):
  189. for unknown in set(cp.options(section)) - options:
  190. if section_prefix:
  191. section = section_prefix + section
  192. raise CoverageException(
  193. "Unrecognized option '[%s] %s=' in config file %s" % (
  194. section, unknown, filename
  195. )
  196. )
  197. # [paths] is special
  198. if cp.has_section('paths'):
  199. for option in cp.options('paths'):
  200. self.paths[option] = cp.getlist('paths', option)
  201. # plugins can have options
  202. for plugin in self.plugins:
  203. if cp.has_section(plugin):
  204. self.plugin_options[plugin] = cp.get_section(plugin)
  205. return True
  206. CONFIG_FILE_OPTIONS = [
  207. # These are *args for _set_attr_from_config_option:
  208. # (attr, where, type_="")
  209. #
  210. # attr is the attribute to set on the CoverageConfig object.
  211. # where is the section:name to read from the configuration file.
  212. # type_ is the optional type to apply, by using .getTYPE to read the
  213. # configuration value from the file.
  214. # [run]
  215. ('branch', 'run:branch', 'boolean'),
  216. ('concurrency', 'run:concurrency', 'list'),
  217. ('cover_pylib', 'run:cover_pylib', 'boolean'),
  218. ('data_file', 'run:data_file'),
  219. ('debug', 'run:debug', 'list'),
  220. ('include', 'run:include', 'list'),
  221. ('note', 'run:note'),
  222. ('omit', 'run:omit', 'list'),
  223. ('parallel', 'run:parallel', 'boolean'),
  224. ('plugins', 'run:plugins', 'list'),
  225. ('source', 'run:source', 'list'),
  226. ('timid', 'run:timid', 'boolean'),
  227. # [report]
  228. ('exclude_list', 'report:exclude_lines', 'regexlist'),
  229. ('fail_under', 'report:fail_under', 'int'),
  230. ('ignore_errors', 'report:ignore_errors', 'boolean'),
  231. ('include', 'report:include', 'list'),
  232. ('omit', 'report:omit', 'list'),
  233. ('partial_always_list', 'report:partial_branches_always', 'regexlist'),
  234. ('partial_list', 'report:partial_branches', 'regexlist'),
  235. ('precision', 'report:precision', 'int'),
  236. ('show_missing', 'report:show_missing', 'boolean'),
  237. ('skip_covered', 'report:skip_covered', 'boolean'),
  238. ('sort', 'report:sort'),
  239. # [html]
  240. ('extra_css', 'html:extra_css'),
  241. ('html_dir', 'html:directory'),
  242. ('html_title', 'html:title'),
  243. # [xml]
  244. ('xml_output', 'xml:output'),
  245. ('xml_package_depth', 'xml:package_depth', 'int'),
  246. ]
  247. def _set_attr_from_config_option(self, cp, attr, where, type_=''):
  248. """Set an attribute on self if it exists in the ConfigParser."""
  249. section, option = where.split(":")
  250. if cp.has_option(section, option):
  251. method = getattr(cp, 'get' + type_)
  252. setattr(self, attr, method(section, option))
  253. def get_plugin_options(self, plugin):
  254. """Get a dictionary of options for the plugin named `plugin`."""
  255. return self.plugin_options.get(plugin, {})
  256. def set_option(self, option_name, value):
  257. """Set an option in the configuration.
  258. `option_name` is a colon-separated string indicating the section and
  259. option name. For example, the ``branch`` option in the ``[run]``
  260. section of the config file would be indicated with `"run:branch"`.
  261. `value` is the new value for the option.
  262. """
  263. # Check all the hard-coded options.
  264. for option_spec in self.CONFIG_FILE_OPTIONS:
  265. attr, where = option_spec[:2]
  266. if where == option_name:
  267. setattr(self, attr, value)
  268. return
  269. # See if it's a plugin option.
  270. plugin_name, _, key = option_name.partition(":")
  271. if key and plugin_name in self.plugins:
  272. self.plugin_options.setdefault(plugin_name, {})[key] = value
  273. return
  274. # If we get here, we didn't find the option.
  275. raise CoverageException("No such option: %r" % option_name)
  276. def get_option(self, option_name):
  277. """Get an option from the configuration.
  278. `option_name` is a colon-separated string indicating the section and
  279. option name. For example, the ``branch`` option in the ``[run]``
  280. section of the config file would be indicated with `"run:branch"`.
  281. Returns the value of the option.
  282. """
  283. # Check all the hard-coded options.
  284. for option_spec in self.CONFIG_FILE_OPTIONS:
  285. attr, where = option_spec[:2]
  286. if where == option_name:
  287. return getattr(self, attr)
  288. # See if it's a plugin option.
  289. plugin_name, _, key = option_name.partition(":")
  290. if key and plugin_name in self.plugins:
  291. return self.plugin_options.get(plugin_name, {}).get(key)
  292. # If we get here, we didn't find the option.
  293. raise CoverageException("No such option: %r" % option_name)