Skip to content

Commit

Permalink
Migrate from python-on-whales to native Docker cli calls
Browse files Browse the repository at this point in the history
  • Loading branch information
KSmanis committed May 8, 2021
1 parent 5b59b4b commit e0d475f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ repos:
rev: v2.14.0
hooks:
- id: pyupgrade
args: ["--py37-plus"]
args: ["--py36-plus"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.812
hooks:
Expand Down
67 changes: 41 additions & 26 deletions pipautocompile/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import shlex
import subprocess # nosec
import sys
import tempfile
from pathlib import Path
from typing import Iterable, List, Tuple, Union

import click
from python_on_whales import docker

DEFAULT_BUILD_STAGE = "build-deps"
DEFAULT_PIP_COMPILE_ARGS = (
Expand Down Expand Up @@ -75,10 +75,8 @@ def cli(build_stage: str, pip_compile_args: Tuple[str, ...]):

for spec in _find_spec_files():
click.secho(f"Compiling {spec}...", fg="yellow", bold=True)
basename_in = spec.name
basename_out = spec.with_suffix(".txt").name
spec_dir = spec.resolve(strict=True).parent
build_dir = spec_dir if basename_in == "requirements.in" else spec_dir.parent
build_dir = spec_dir if spec.name == "requirements.in" else spec_dir.parent

env = {
"CUSTOM_COMPILE_COMMAND": _shell_quote(("pip-autocompile", *sys.argv[1:]))
Expand All @@ -88,37 +86,54 @@ def cli(build_stage: str, pip_compile_args: Tuple[str, ...]):
regex=fr"^FROM \S+ AS {build_stage}$",
)
if has_build_stage:
image_id = docker.build(context_path=build_dir, target=build_stage).id
docker.run(
image=image_id,
command=[
with tempfile.TemporaryDirectory() as temp_dir:
iidfile = Path(temp_dir) / "iidfile"
subprocess.check_call( # nosec
(
"docker",
"build",
"--iidfile",
iidfile,
"--target",
build_stage,
build_dir,
),
env={**os.environ, "DOCKER_BUILDKIT": "1"},
)
image_id = iidfile.read_text()

output = subprocess.check_output( # nosec
(
"docker",
"run",
*(f"--env={k}={v}" for k, v in env.items()),
"--rm",
"--volume",
f"{spec_dir}:/app/:ro",
"--volume",
"pip-autocompile-cache-pip:/root/.cache/pip/",
"--volume",
"pip-autocompile-cache-pip-tools:/root/.cache/pip-tools/",
"--workdir",
"/app/",
image_id,
"sh",
"-c",
"""
set -eux;
python -m venv /opt/venv/;
/opt/venv/bin/pip install pip-tools;
/opt/venv/bin/pip-compile {pip_compile_args} {basename_in};
chown "$(stat -c '%u:%g' {basename_in})" {basename_out};
chmod "$(stat -c '%a' {basename_in})" {basename_out};
pip install pip-tools >&2;
pip-compile {args} -o- {spec_name};
""".format(
basename_in=_shell_quote(basename_in),
basename_out=_shell_quote(basename_out),
pip_compile_args=_shell_quote(pip_compile_args),
args=_shell_quote(pip_compile_args),
spec_name=_shell_quote(spec.name),
),
],
envs=env,
remove=True,
volumes=[
(spec_dir, "/app/"),
("pip-autocompile-cache-pip", "/root/.cache/pip/"),
("pip-autocompile-cache-pip-tools", "/root/.cache/pip-tools/"),
],
workdir="/app/",
),
env={**os.environ, "DOCKER_BUILDKIT": "1"},
)
spec.with_suffix(".txt").write_bytes(output)
else:
subprocess.check_call( # nosec
["pip-compile", *pip_compile_args, basename_in],
["pip-compile", *pip_compile_args, spec.name],
cwd=spec_dir,
env={**os.environ, **env},
)
5 changes: 2 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ classifiers =
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Topic :: Software Development :: Build Tools

[options]
packages = find:
python_requires = >=3.7
python_requires = >=3.6
setup_requires = setuptools_scm
install_requires =
click
pip-tools
python-on-whales

[options.extras_require]
dev = pre-commit
Expand Down

0 comments on commit e0d475f

Please sign in to comment.