Build an image and run the script in a container.
(engine: Literal["docker", "podman"])
| 162 | |
| 163 | |
| 164 | def run_in_container(engine: Literal["docker", "podman"]) -> int: |
| 165 | """ |
| 166 | Build an image and run the script in a container. |
| 167 | """ |
| 168 | tag = f"async-to-sync:{version('ast_comments')}-{PYVER}" |
| 169 | |
| 170 | # Check if the image we want is present. |
| 171 | cmdline = [engine, "inspect", tag, "-f", "{{ .Id }}"] |
| 172 | try: |
| 173 | sp.check_call(cmdline, stdout=sp.DEVNULL, stderr=sp.DEVNULL) |
| 174 | except sp.CalledProcessError: |
| 175 | logger.info("building container image with %s", engine) |
| 176 | containerfile = f"""\ |
| 177 | FROM python:{PYVER} |
| 178 | |
| 179 | WORKDIR /src |
| 180 | |
| 181 | ADD psycopg psycopg |
| 182 | RUN pip install ./psycopg[dev] |
| 183 | |
| 184 | ENTRYPOINT ["tools/async_to_sync.py"] |
| 185 | """ |
| 186 | cmdline = [engine, "build", "--network=host", "--tag", tag, "-f", "-"] |
| 187 | cmdline += [str(PROJECT_DIR)] |
| 188 | sp.run(cmdline, check=True, text=True, input=containerfile) |
| 189 | |
| 190 | cmdline = sys.argv[1:] |
| 191 | cmdline.remove(f"--{engine}") |
| 192 | cmdline = [engine, "run", "--rm", "-v", f"{PROJECT_DIR}:/src", tag] + cmdline |
| 193 | logger.info("running in container image %s (%s)", tag, engine) |
| 194 | sp.check_call(cmdline) |
| 195 | return 0 |
| 196 | |
| 197 | |
| 198 | def async_to_sync(tree: ast.AST, filepath: Path | None = None) -> ast.AST: |