|
| 1 | +import shutil |
1 | 2 | import json |
2 | 3 | import pathlib |
3 | 4 | import re |
@@ -118,3 +119,51 @@ def _validate_coverage(path: Path) -> str: |
118 | 119 | f"Invalid database, the database is missing the following tables {missing}" |
119 | 120 | ) |
120 | 121 | return "" |
| 122 | + |
| 123 | + |
| 124 | +@nox.session(name="artifacts:copy", python=False) |
| 125 | +def copy_artifacts(session: Session) -> None: |
| 126 | + """ |
| 127 | + Copy artifacts to the current directory |
| 128 | + """ |
| 129 | + |
| 130 | + def get_suffix() -> str: |
| 131 | + versions = getattr(PROJECT_CONFIG, "python_versions", None) |
| 132 | + pivot = versions[0] if versions else "3.9" |
| 133 | + return f"-python{pivot}" |
| 134 | + |
| 135 | + dir = Path(session.posargs[0]) |
| 136 | + suffix = _python_version_suffix() |
| 137 | + _combine_coverage(session, dir, f"coverage{suffix}*/.coverage") |
| 138 | + _copy_artifacts( |
| 139 | + dir, |
| 140 | + f"lint{suffix}/.lint.txt", |
| 141 | + f"lint{suffix}/.lint.json", |
| 142 | + f"security{suffix}/.security.json", |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +def _python_version_suffix() -> str: |
| 147 | + versions = getattr(PROJECT_CONFIG, "python_versions", None) |
| 148 | + pivot = versions[0] if versions else "3.9" |
| 149 | + return f"-python{pivot}" |
| 150 | + |
| 151 | + |
| 152 | +def _combine_coverage(session: Session, dir: Path, pattern: str): |
| 153 | + """ |
| 154 | + pattern: glob pattern, e.g. "*.coverage" |
| 155 | + """ |
| 156 | + if args := [f for f in dir.glob(pattern) if f.exists()]: |
| 157 | + session.run("coverage", "combine", "--keep", *args) |
| 158 | + else: |
| 159 | + print(f"Could not find any file {dir}/{pattern}") |
| 160 | + |
| 161 | + |
| 162 | +def _copy_artifacts(dir: Path, *files: str): |
| 163 | + for file in files: |
| 164 | + path = dir / file |
| 165 | + if path.exists(): |
| 166 | + print(f"Copying file {path}") |
| 167 | + shutil.copy(path, ".") |
| 168 | + else: |
| 169 | + print(f"File not found {path}") |
0 commit comments