MCPcopy
hub / github.com/psycopg/psycopg / run_in_container

Function run_in_container

tools/async_to_sync.py:164–195  ·  view source on GitHub ↗

Build an image and run the script in a container.

(engine: Literal["docker", "podman"])

Source from the content-addressed store, hash-verified

162
163
164def 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"""\
177FROM python:{PYVER}
178
179WORKDIR /src
180
181ADD psycopg psycopg
182RUN pip install ./psycopg[dev]
183
184ENTRYPOINT ["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
198def async_to_sync(tree: ast.AST, filepath: Path | None = None) -> ast.AST:

Callers 1

mainFunction · 0.85

Calls 3

versionFunction · 0.85
infoMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected