Skip to content

Instantly share code, notes, and snippets.

@eli-collins
Created November 23, 2016 16:05
Show Gist options
  • Save eli-collins/ef91add2bc635ccaf61738cd805cb18b to your computer and use it in GitHub Desktop.
Save eli-collins/ef91add2bc635ccaf61738cd805cb18b to your computer and use it in GitHub Desktop.
Helper for running tox
#!/usr/bin/env python
"""
Quick script that returns list of TOX envs, after applying pattern expansion.
Usage example:
$ toxmatch {foo,bar}-py{2,3}
foo-py2,foo-py3,bar-py2,bar-py3
"""
from __future__ import print_function
import sys
import subprocess
try:
from itertools import filter
except ImportError:
pass
from tox.config import _expand_envstr
def main(pattern):
reqs = set(frozenset(elem.split("-")) for elem in _expand_envstr(pattern))
def match(env):
factors = set(env.split("-"))
return any(factors.issuperset(req) for req in reqs)
envs = subprocess.check_output(["tox", "-l"]).splitlines()
print(",".join(env for env in envs if match(env)))
if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))
@anentropic
Copy link

anentropic commented Apr 5, 2018

these both seem to have a problem with signature def main(pattern) vs calling main(*sys.argv[1:]) leading to TypeError: main() takes exactly 1 argument (2 given)

but basically works good

"""
Usage example:

    $ toxmatch {foo,bar}-py{2,3}
    foo-py2,foo-py3,bar-py2,bar-py3

    $ toxmatch py2
    foo-py2,bar-py2,baz-py2

    $ toxmatch foo
    foo-py2,foo-py3
"""
from __future__ import print_function
import sys

from tox.config import parseconfig, _expand_envstr


def main(pattern):
    reqs = set(frozenset(elem.split("-")) for elem in _expand_envstr(pattern))

    def match(env):
        factors = set(env.split("-"))
        return any(factors.issuperset(req) for req in reqs)

    print(
        ",".join(
            env
            for env in parseconfig([]).envlist
            if match(env)
        )
    )


if __name__ == "__main__":
    sys.exit(main(sys.argv[1]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment