-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
61 lines (46 loc) · 1.76 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import asyncio
from pathlib import Path
from asyncio.subprocess import PIPE, Process
import logging
import pystructopt
from dataclasses import dataclass, field
from typing import Union
from typing_extensions import Literal, TypeGuard, get_args
import logging
logger = logging.getLogger(__name__)
T_PYTHON_VERSIONS = Literal["3.7", "3.8", "3.9", "3.10"]
PYTHON_VERSIONS = set(get_args(T_PYTHON_VERSIONS))
@dataclass
class Opts:
python_version: Union[T_PYTHON_VERSIONS, Literal["all"]] = field(
metadata={"positional": True}
)
no_build: bool = False
async def main():
opts = pystructopt.parse(Opts)
if is_python_version(opts.python_version):
await run(opts.python_version, opts.no_build)
elif opts.python_version == "all":
tasks = [run(v, opts.no_build) for v in PYTHON_VERSIONS]
await asyncio.gather(*tasks)
else:
raise ValueError(f"Invalid python_version argument: {opts.python_version}")
def is_python_version(version: str) -> TypeGuard[T_PYTHON_VERSIONS]:
return version in PYTHON_VERSIONS
async def run(python_version: T_PYTHON_VERSIONS, no_build: bool):
mod = Path(__file__).parent.absolute().parts[-1]
tag = f"{mod}_{python_version}"
if not no_build:
cmd = f"docker build -t {tag} --build-arg PYTHON_VERSION={python_version} ."
logger.info("Build")
logger.info(cmd)
proc = await asyncio.create_subprocess_exec(*cmd.split())
if await proc.wait() != 0:
raise ValueError(cmd)
test_cmd = f"docker run --rm {tag} make test"
proc = await asyncio.create_subprocess_exec(*test_cmd.split())
if await proc.wait() != 0:
raise ValueError(test_cmd)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(main())