config.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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, our_file):
  14. """Create the HandyConfigParser.
  15. `our_file` is True if this config file is specifically for coverage,
  16. False if we are examining another config file (tox.ini, setup.cfg)
  17. for possible settings.
  18. """
  19. configparser.RawConfigParser.__init__(self)
  20. self.section_prefixes = ["coverage:"]
  21. if our_file:
  22. self.section_prefixes.append("")
  23. def read(self, filenames):
  24. """Read a file name as UTF-8 configuration data."""
  25. kwargs = {}
  26. if sys.version_info >= (3, 2):
  27. kwargs['encoding'] = "utf-8"
  28. return configparser.RawConfigParser.read(self, filenames, **kwargs)
  29. def has_option(self, section, option):
  30. for section_prefix in self.section_prefixes:
  31. real_section = section_prefix + section
  32. has = configparser.RawConfigParser.has_option(self, real_section, option)
  33. if has:
  34. return has
  35. return False
  36. def has_section(self, section):
  37. for section_prefix in self.section_prefixes:
  38. real_section = section_prefix + section
  39. has = configparser.RawConfigParser.has_section(self, real_section)
  40. if has:
  41. return real_section
  42. return False
  43. def options(self, section):
  44. for section_prefix in self.section_prefixes:
  45. real_section = section_prefix + section
  46. if configparser.RawConfigParser.has_section(self, real_section):
  47. return configparser.RawConfigParser.options(self, real_section)
  48. raise configparser.NoSectionError
  49. def get_section(self, section):
  50. """Get the contents of a section, as a dictionary."""
  51. d = {}
  52. for opt in self.options(section):
  53. d[opt] = self.get(section, opt)
  54. return d
  55. def get(self, section, option, *args, **kwargs): # pylint: disable=arguments-differ
  56. """Get a value, replacing environment variables also.
  57. The arguments are the same as `RawConfigParser.get`, but in the found
  58. value, ``$WORD`` or ``${WORD}`` are replaced by the value of the
  59. environment variable ``WORD``.
  60. Returns the finished value.
  61. """
  62. for section_prefix in self.section_prefixes:
  63. real_section = section_prefix + section
  64. if configparser.RawConfigParser.has_option(self, real_section, option):
  65. break
  66. else:
  67. raise configparser.NoOptionError
  68. v = configparser.RawConfigParser.get(self, real_section, option, *args, **kwargs)
  69. def dollar_replace(m):
  70. """Called for each $replacement."""
  71. # Only one of the groups will have matched, just get its text.
  72. word = next(w for w in m.groups() if w is not None) # pragma: part covered
  73. if word == "$":
  74. return "$"
  75. else:
  76. return os.environ.get(word, '')
  77. dollar_pattern = r"""(?x) # Use extended regex syntax
  78. \$(?: # A dollar sign, then
  79. (?P<v1>\w+) | # a plain word,
  80. {(?P<v2>\w+)} | # or a {-wrapped word,
  81. (?P<char>[$]) # or a dollar sign.
  82. )
  83. """
  84. v = re.sub(dollar_pattern, dollar_replace, v)
  85. return v
  86. def getlist(self, section, option):
  87. """Read a list of strings.
  88. The value of `section` and `option` is treated as a comma- and newline-
  89. separated list of strings. Each value is stripped of whitespace.
  90. Returns the list of strings.
  91. """
  92. value_list = self.get(section, option)
  93. values = []
  94. for value_line in value_list.split('\n'):
  95. for value in value_line.split(','):
  96. value = value.strip()
  97. if value:
  98. values.append(value)
  99. return values
  100. def getregexlist(self, section, option):
  101. """Read a list of full-line regexes.
  102. The value of `section` and `option` is treated as a newline-separated
  103. list of regexes. Each value is stripped of whitespace.
  104. Returns the list of strings.
  105. """
  106. line_list = self.get(section, option)
  107. value_list = []
  108. for value in line_list.splitlines():
  109. value = value.strip()
  110. try:
  111. re.compile(value)
  112. except re.error as e:
  113. raise CoverageException(
  114. "Invalid [%s].%s value %r: %s" % (section, option, value, e)
  115. )
  116. if value:
  117. value_list.append(value)
  118. return value_list
  119. # The default line exclusion regexes.
  120. DEFAULT_EXCLUDE = [
  121. r'#\s*(pragma|PRAGMA)[:\s]?\s*(no|NO)\s*(cover|COVER)',
  122. ]
  123. # The default partial branch regexes, to be modified by the user.
  124. DEFAULT_PARTIAL = [
  125. r'#\s*(pragma|PRAGMA)[:\s]?\s*(no|NO)\s*(branch|BRANCH)',
  126. ]
  127. # The default partial branch regexes, based on Python semantics.
  128. # These are any Python branching constructs that can't actually execute all
  129. # their branches.
  130. DEFAULT_PARTIAL_ALWAYS = [
  131. 'while (True|1|False|0):',
  132. 'if (True|1|False|0):',
  133. ]
  134. class CoverageConfig(object):
  135. """Coverage.py configuration.
  136. The attributes of this class are the various settings that control the
  137. operation of coverage.py.
  138. """
  139. def __init__(self):
  140. """Initialize the configuration attributes to their defaults."""
  141. # Metadata about the config.
  142. self.attempted_config_files = []
  143. self.config_files = []
  144. # Defaults for [run] and [report]
  145. self._include = None
  146. self._omit = None
  147. # Defaults for [run]
  148. self.branch = False
  149. self.concurrency = None
  150. self.cover_pylib = False
  151. self.data_file = ".coverage"
  152. self.debug = []
  153. self.disable_warnings = []
  154. self.note = None
  155. self.parallel = False
  156. self.plugins = []
  157. self.source = None
  158. self.run_include = None
  159. self.run_omit = None
  160. self.timid = False
  161. # Defaults for [report]
  162. self.exclude_list = DEFAULT_EXCLUDE[:]
  163. self.fail_under = 0
  164. self.ignore_errors = False
  165. self.report_include = None
  166. self.report_omit = None
  167. self.partial_always_list = DEFAULT_PARTIAL_ALWAYS[:]
  168. self.partial_list = DEFAULT_PARTIAL[:]
  169. self.precision = 0
  170. self.show_missing = False
  171. self.skip_covered = False
  172. # Defaults for [html]
  173. self.extra_css = None
  174. self.html_dir = "htmlcov"
  175. self.html_title = "Coverage report"
  176. # Defaults for [xml]
  177. self.xml_output = "coverage.xml"
  178. self.xml_package_depth = 99
  179. # Defaults for [paths]
  180. self.paths = {}
  181. # Options for plugins
  182. self.plugin_options = {}
  183. MUST_BE_LIST = [
  184. "debug", "concurrency", "plugins",
  185. "report_omit", "report_include",
  186. "run_omit", "run_include",
  187. ]
  188. def from_args(self, **kwargs):
  189. """Read config values from `kwargs`."""
  190. for k, v in iitems(kwargs):
  191. if v is not None:
  192. if k in self.MUST_BE_LIST and isinstance(v, string_class):
  193. v = [v]
  194. setattr(self, k, v)
  195. @contract(filename=str)
  196. def from_file(self, filename, our_file):
  197. """Read configuration from a .rc file.
  198. `filename` is a file name to read.
  199. `our_file` is True if this config file is specifically for coverage,
  200. False if we are examining another config file (tox.ini, setup.cfg)
  201. for possible settings.
  202. Returns True or False, whether the file could be read, and it had some
  203. coverage.py settings in it.
  204. """
  205. self.attempted_config_files.append(filename)
  206. cp = HandyConfigParser(our_file)
  207. try:
  208. files_read = cp.read(filename)
  209. except configparser.Error as err:
  210. raise CoverageException("Couldn't read config file %s: %s" % (filename, err))
  211. if not files_read:
  212. return False
  213. self.config_files.extend(files_read)
  214. any_set = False
  215. try:
  216. for option_spec in self.CONFIG_FILE_OPTIONS:
  217. was_set = self._set_attr_from_config_option(cp, *option_spec)
  218. if was_set:
  219. any_set = True
  220. except ValueError as err:
  221. raise CoverageException("Couldn't read config file %s: %s" % (filename, err))
  222. # Check that there are no unrecognized options.
  223. all_options = collections.defaultdict(set)
  224. for option_spec in self.CONFIG_FILE_OPTIONS:
  225. section, option = option_spec[1].split(":")
  226. all_options[section].add(option)
  227. for section, options in iitems(all_options):
  228. real_section = cp.has_section(section)
  229. if real_section:
  230. for unknown in set(cp.options(section)) - options:
  231. raise CoverageException(
  232. "Unrecognized option '[%s] %s=' in config file %s" % (
  233. real_section, unknown, filename
  234. )
  235. )
  236. # [paths] is special
  237. if cp.has_section('paths'):
  238. for option in cp.options('paths'):
  239. self.paths[option] = cp.getlist('paths', option)
  240. any_set = True
  241. # plugins can have options
  242. for plugin in self.plugins:
  243. if cp.has_section(plugin):
  244. self.plugin_options[plugin] = cp.get_section(plugin)
  245. any_set = True
  246. # Was this file used as a config file? If it's specifically our file,
  247. # then it was used. If we're piggybacking on someone else's file,
  248. # then it was only used if we found some settings in it.
  249. if our_file:
  250. return True
  251. else:
  252. return any_set
  253. CONFIG_FILE_OPTIONS = [
  254. # These are *args for _set_attr_from_config_option:
  255. # (attr, where, type_="")
  256. #
  257. # attr is the attribute to set on the CoverageConfig object.
  258. # where is the section:name to read from the configuration file.
  259. # type_ is the optional type to apply, by using .getTYPE to read the
  260. # configuration value from the file.
  261. # [run]
  262. ('branch', 'run:branch', 'boolean'),
  263. ('concurrency', 'run:concurrency', 'list'),
  264. ('cover_pylib', 'run:cover_pylib', 'boolean'),
  265. ('data_file', 'run:data_file'),
  266. ('debug', 'run:debug', 'list'),
  267. ('disable_warnings', 'run:disable_warnings', 'list'),
  268. ('_include', 'run:include', 'list'),
  269. ('note', 'run:note'),
  270. ('_omit', 'run:omit', 'list'),
  271. ('parallel', 'run:parallel', 'boolean'),
  272. ('plugins', 'run:plugins', 'list'),
  273. ('source', 'run:source', 'list'),
  274. ('timid', 'run:timid', 'boolean'),
  275. # [report]
  276. ('exclude_list', 'report:exclude_lines', 'regexlist'),
  277. ('fail_under', 'report:fail_under', 'int'),
  278. ('ignore_errors', 'report:ignore_errors', 'boolean'),
  279. ('_include', 'report:include', 'list'),
  280. ('_omit', 'report:omit', 'list'),
  281. ('partial_always_list', 'report:partial_branches_always', 'regexlist'),
  282. ('partial_list', 'report:partial_branches', 'regexlist'),
  283. ('precision', 'report:precision', 'int'),
  284. ('show_missing', 'report:show_missing', 'boolean'),
  285. ('skip_covered', 'report:skip_covered', 'boolean'),
  286. ('sort', 'report:sort'),
  287. # [html]
  288. ('extra_css', 'html:extra_css'),
  289. ('html_dir', 'html:directory'),
  290. ('html_title', 'html:title'),
  291. # [xml]
  292. ('xml_output', 'xml:output'),
  293. ('xml_package_depth', 'xml:package_depth', 'int'),
  294. ]
  295. def _set_attr_from_config_option(self, cp, attr, where, type_=''):
  296. """Set an attribute on self if it exists in the ConfigParser.
  297. Returns True if the attribute was set.
  298. """
  299. section, option = where.split(":")
  300. if cp.has_option(section, option):
  301. method = getattr(cp, 'get' + type_)
  302. setattr(self, attr, method(section, option))
  303. return True
  304. return False
  305. def get_plugin_options(self, plugin):
  306. """Get a dictionary of options for the plugin named `plugin`."""
  307. return self.plugin_options.get(plugin, {})
  308. def set_option(self, option_name, value):
  309. """Set an option in the configuration.
  310. `option_name` is a colon-separated string indicating the section and
  311. option name. For example, the ``branch`` option in the ``[run]``
  312. section of the config file would be indicated with `"run:branch"`.
  313. `value` is the new value for the option.
  314. """
  315. # Check all the hard-coded options.
  316. for option_spec in self.CONFIG_FILE_OPTIONS:
  317. attr, where = option_spec[:2]
  318. if where == option_name:
  319. setattr(self, attr, value)
  320. return
  321. # See if it's a plugin option.
  322. plugin_name, _, key = option_name.partition(":")
  323. if key and plugin_name in self.plugins:
  324. self.plugin_options.setdefault(plugin_name, {})[key] = value
  325. return
  326. # If we get here, we didn't find the option.
  327. raise CoverageException("No such option: %r" % option_name)
  328. def get_option(self, option_name):
  329. """Get an option from the configuration.
  330. `option_name` is a colon-separated string indicating the section and
  331. option name. For example, the ``branch`` option in the ``[run]``
  332. section of the config file would be indicated with `"run:branch"`.
  333. Returns the value of the option.
  334. """
  335. # Check all the hard-coded options.
  336. for option_spec in self.CONFIG_FILE_OPTIONS:
  337. attr, where = option_spec[:2]
  338. if where == option_name:
  339. return getattr(self, attr)
  340. # See if it's a plugin option.
  341. plugin_name, _, key = option_name.partition(":")
  342. if key and plugin_name in self.plugins:
  343. return self.plugin_options.get(plugin_name, {}).get(key)
  344. # If we get here, we didn't find the option.
  345. raise CoverageException("No such option: %r" % option_name)
  346. def read_coverage_config(config_file, **kwargs):
  347. """Read the coverage.py configuration.
  348. Arguments:
  349. config_file: a boolean or string, see the `Coverage` class for the
  350. tricky details.
  351. all others: keyword arguments from the `Coverage` class, used for
  352. setting values in the configuration.
  353. Returns:
  354. config_file, config:
  355. config_file is the value to use for config_file in other
  356. invocations of coverage.
  357. config is a CoverageConfig object read from the appropriate
  358. configuration file.
  359. """
  360. # Build the configuration from a number of sources:
  361. # 1) defaults:
  362. config = CoverageConfig()
  363. # 2) from a file:
  364. if config_file:
  365. # Some API users were specifying ".coveragerc" to mean the same as
  366. # True, so make it so.
  367. if config_file == ".coveragerc":
  368. config_file = True
  369. specified_file = (config_file is not True)
  370. if not specified_file:
  371. config_file = ".coveragerc"
  372. for fname, our_file in [(config_file, True),
  373. ("setup.cfg", False),
  374. ("tox.ini", False)]:
  375. config_read = config.from_file(fname, our_file=our_file)
  376. is_config_file = fname == config_file
  377. if not config_read and is_config_file and specified_file:
  378. raise CoverageException("Couldn't read '%s' as a config file" % fname)
  379. if config_read:
  380. break
  381. for attr in ('_omit', '_include'):
  382. value = getattr(config, attr)
  383. if value is not None:
  384. for section in ('run', 'report'):
  385. setattr(config, section + attr, value)
  386. # 3) from environment variables:
  387. env_data_file = os.environ.get('COVERAGE_FILE')
  388. if env_data_file:
  389. config.data_file = env_data_file
  390. debugs = os.environ.get('COVERAGE_DEBUG')
  391. if debugs:
  392. config.debug.extend(d.strip() for d in debugs.split(","))
  393. # 4) from constructor arguments:
  394. config.from_args(**kwargs)
  395. # Once all the config has been collected, there's a little post-processing
  396. # to do.
  397. config.data_file = os.path.expanduser(config.data_file)
  398. config.html_dir = os.path.expanduser(config.html_dir)
  399. config.xml_output = os.path.expanduser(config.xml_output)
  400. return config_file, config