Support verbose showconfig in tox siblings

Unfortunately, when tox combines --showconfig with verbosity options
like -vv, some non-config output gets streamed to stdout before the
configuration is emitted. Filter this preamble in
tox_install_sibling_packages by discarding any initial lines of
output before the first section heading.

Also extend get_envlist() to deal with the fact that additional
verbosity adds a [tox] section in the --showconfig output, which it
was previously relying on to determine whether the config had been
filtered to a subset of env sections. Instead also check the
tox.args string to determine whether a -e option was passed on the
command line.

Change-Id: Iafeb88eaf9a596603ad4d2134a4574345d5189ab
This commit is contained in:
Jeremy Stanley 2021-08-30 16:49:51 +00:00
parent 3d449a07df
commit bc8776bad4
1 changed files with 33 additions and 3 deletions

View File

@ -52,6 +52,12 @@ try:
except ImportError:
import ConfigParser as configparser
# Workaround for lack of configparser.read_string() on Python 2.7
try:
from io import StringIO
except ImportError:
from StringIO import StringIO
import os
import ast
import subprocess
@ -265,7 +271,13 @@ def install_siblings(envdir, projects, package_name, constraints):
def get_envlist(tox_config):
envlist = []
if 'tox' in tox_config.sections():
# This is overly LBYL to deal with differences in older Python 2.7
# ConfigParser which would necessitate a fairly large number of exceptions
# if we wanted to do a simple try/except with the get() instead
if (
'tox' in tox_config.sections() and 'env' in
tox_config.options('tox') and "'-e" not in
tox_config.get('tox', 'args')):
envlist_default = ast.literal_eval(
tox_config.get('tox', 'envlist_default'))
tox_args = ast.literal_eval(tox_config.get('tox', 'args'))
@ -278,7 +290,8 @@ def get_envlist(tox_config):
envlist.append(testenv)
else:
for section in tox_config.sections():
envlist.append(section.split(':')[1])
if section.startswith('testenv:'):
envlist.append(section.split(':')[1])
return envlist
@ -298,8 +311,25 @@ def main():
projects = module.params['projects']
tox_show_config = module.params.get('tox_show_config')
# Filter out any leading verbose output lines before the config
with open(tox_show_config) as tox_raw_config:
tox_clean_config = ''
discard = True
for line in tox_raw_config:
if not discard:
# Normal operation, tested first for efficiency
tox_clean_config += line
elif line.startswith('['):
# Once we see a section heading, stop discarding
discard = False
tox_clean_config += line
tox_config = configparser.RawConfigParser()
tox_config.read(tox_show_config)
# Workaround for lack of configparser.read_string() on Python 2.7
try:
tox_config.read_string(tox_clean_config)
except AttributeError:
tox_config.readfp(StringIO(unicode(tox_clean_config))) # noqa: F821
envlist = get_envlist(tox_config)