|
| 1 | +import functools |
| 2 | +import nox |
| 3 | +import configparser |
| 4 | +import braceexpand |
| 5 | +import itertools |
| 6 | + |
| 7 | +MAX_JOBS = 50 |
| 8 | + |
| 9 | +def expand_factors(value): |
| 10 | + for line in value.splitlines(): |
| 11 | + line = line.strip() |
| 12 | + |
| 13 | + if not line or line.startswith("#"): |
| 14 | + continue |
| 15 | + |
| 16 | + for expanded in braceexpand.braceexpand(line): |
| 17 | + yield expanded |
| 18 | + |
| 19 | + |
| 20 | +def find_dependencies(deps, env): |
| 21 | + parts = env.split("-") |
| 22 | + |
| 23 | + for combo in itertools.product((True, False), repeat=len(parts)): |
| 24 | + key = "-".join(parts[i] for i, include in enumerate(combo) if include) |
| 25 | + |
| 26 | + if key in deps: |
| 27 | + yield deps[key] |
| 28 | + |
| 29 | +def parse_tox(): |
| 30 | + config = configparser.ConfigParser() |
| 31 | + config.read("tox.ini") |
| 32 | + |
| 33 | + dependencies = {} |
| 34 | + |
| 35 | + for declaration in expand_factors(config['testenv']['deps']): |
| 36 | + if declaration.startswith("-r "): |
| 37 | + continue |
| 38 | + |
| 39 | + env_matcher, dependency = declaration.split(":", 1) |
| 40 | + dependencies[env_matcher.strip()] = dependency |
| 41 | + |
| 42 | + jobs = {} |
| 43 | + |
| 44 | + for env in expand_factors(config['tox']['envlist']): |
| 45 | + python_version, integration, framework_version, *_ = ( |
| 46 | + list(env.split("-")) + [None, None] |
| 47 | + ) |
| 48 | + |
| 49 | + python_version_jobs = jobs.setdefault(python_version, []) |
| 50 | + for job in python_version_jobs: |
| 51 | + if job.setdefault(integration, framework_version) == framework_version: |
| 52 | + break |
| 53 | + else: |
| 54 | + python_version_jobs.append({integration: framework_version}) |
| 55 | + |
| 56 | + return dependencies, jobs |
| 57 | + |
| 58 | + |
| 59 | +def generate_sessions(_locals): |
| 60 | + dependencies, jobs = parse_tox() |
| 61 | + |
| 62 | + for python_version, batches in jobs.items(): |
| 63 | + for batch_name, integrations in enumerate(batches): |
| 64 | + job_name = f"{python_version}-{batch_name}" |
| 65 | + deps = [] |
| 66 | + for integration, version in integrations.items(): |
| 67 | + deps.extend(find_dependencies( |
| 68 | + dependencies, f"{python_version}-{integration}-{version}" |
| 69 | + )) |
| 70 | + |
| 71 | + def func(session, deps=deps): |
| 72 | + session.install("-r", "test-requirements.txt", *deps) |
| 73 | + |
| 74 | + assert python_version.startswith("py") |
| 75 | + nox_python_version = "pypy" if python_version == "pypy" else python_version[2:] |
| 76 | + |
| 77 | + _locals[job_name] = nox.session( |
| 78 | + func=func, reuse_venv=True, python=nox_python_version, name=job_name |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +generate_sessions(locals()) |
0 commit comments