Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# prek runs lower priorities first and equal ones concurrently, so hooks sharing a priority never write the same
# file; pre-commit ignores the key and runs hooks in file order.
ci:
autoupdate_schedule: quarterly
default_language_version:
Expand All @@ -7,38 +9,56 @@ repos:
rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # frozen: v6.0.0
hooks:
- id: end-of-file-fixer
priority: 0
- id: trailing-whitespace
priority: 1
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: a76b92851fc9eede9f9bc4073eb55202f34eb0df # frozen: 0.39.0
hooks:
- id: check-github-workflows
priority: 6
args: ["--verbose"]
- repo: https://github.com/codespell-project/codespell
rev: 57b21406f092110c18776e39b0bda50d37c945c8 # frozen: v2.4.3
hooks:
- id: codespell
priority: 6
- repo: https://github.com/tox-dev/tox-toml-fmt
rev: 5125f601c3e134347288c3d42f3b34298b7cf1be # frozen: v1.11.1
hooks:
- id: tox-toml-fmt
priority: 2
- repo: https://github.com/tox-dev/pyproject-fmt
rev: 50e0f3aee9dc5747a5f173f79a7417403c18dda6 # frozen: v2.30.3
hooks:
- id: pyproject-fmt
priority: 2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 25bcf95026b96e2dd3c0aec3090c3d6ef6ca2e6c # frozen: v0.17.3
hooks:
- id: ruff-format
- id: ruff-check
priority: 3
args: ["--fix", "--unsafe-fixes", "--exit-non-zero-on-fix"]
- repo: https://github.com/gaborbernat/simplint-pre-commit
rev: c8719dca99c13381570bc69f8955c9448fc271b7 # frozen: v0.7.1
hooks:
- id: simplint
priority: 4
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 25bcf95026b96e2dd3c0aec3090c3d6ef6ca2e6c # frozen: v0.17.3
hooks:
- id: ruff-format
priority: 5
- repo: https://github.com/google/yamlfmt
rev: 21ca5323a9c87ee37a434e0ca908efc0a89daa07 # frozen: v0.21.0
hooks:
- id: yamlfmt
priority: 2
- repo: https://github.com/executablebooks/mdformat
rev: 82912cdaea4fb830f751504486a7879c70526547 # frozen: 1.0.0
hooks:
- id: mdformat
priority: 2
args: ["--wrap", "120"]
additional_dependencies:
- mdformat-gfm==1.0.0
Expand All @@ -47,7 +67,10 @@ repos:
rev: c92afdf2c6ca336aaa8f39e2bd8d48f62dbd8e1f # frozen: v1.31.1
hooks:
- id: zizmor
priority: 6
- repo: meta
hooks:
- id: check-hooks-apply
priority: 6
- id: check-useless-excludes
priority: 6
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ lint.ignore = [
"multi-line-summary-first-line", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
"non-empty-init-module", # `__init__` module should only contain docstrings and re-exports
"single-line-implicit-string-concatenation", # Conflict with formatter
"undocumented-magic-method", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-class", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-function", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-init", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-method", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-nested-class", # undocumented-public-api (simplint SMP903) covers the public API instead
]
lint.per-file-ignores."tests/**/*.py" = [
"assert", # asserts allowed in tests
Expand Down Expand Up @@ -211,3 +217,6 @@ matrix.platforms = [ "linux_x86_64" ]
# release-creator 4.4.0 needs a seven-day cutoff exception.
# expires 2026-11-09
packages.release-creator.uploaded-prior-to = 2026-11-02T21:45:00Z

[tool.simplint]
docstring-style = "rst"
209 changes: 2 additions & 207 deletions src/devpi_process/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,215 +2,10 @@

from __future__ import annotations

import random
import socket
import string
import sys
import sysconfig
from contextlib import closing
from pathlib import Path
from subprocess import PIPE, Popen, run # ruff:ignore[suspicious-subprocess-import]
from threading import Thread
from typing import IO, TYPE_CHECKING, cast

from ._index import Index
from ._server import IndexServer
from ._version import __version__

if TYPE_CHECKING:
from collections.abc import Iterator, Sequence
from types import TracebackType
from typing import Self


def _check_call(cmd: list[str]) -> None:
run(cmd, check=True, capture_output=True) # ruff:ignore[subprocess-without-shell-equals-true]


class Index:
"""Index."""

def __init__(self, base_url: str, name: str, user: str, client_cmd_base: list[str]) -> None:
"""
Create index.

:param base_url: base url
:param name: name for the index server
:param user: the username to use
:param client_cmd_base:
"""
self._client_cmd_base = client_cmd_base
self._server_url = base_url
self.name = name
self.user = user

@property
def url(self) -> str:
"""The URL to the index server."""
return f"{self._server_url}/{self.name}/+simple/"

def use(self) -> None:
"""Use this index server."""
_check_call([*self._client_cmd_base, "use", f"{self.user}/{self.name}"])

def upload(self, *files: Path) -> None:
"""
Upload packages to the index.

:param files: the files to upload
"""
cmd = self._client_cmd_base + ["upload", "--index", self.name] + [str(i) for i in files]
_check_call(cmd)

def __repr__(self) -> str:
""":return: repr of the index"""
return f"{self.__class__.__name__}(url={self.url})"


class IndexServer:
"""A PyPI index server locally."""

def __init__(
self,
path: Path,
with_root_pypi: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument]
start_args: Sequence[str] | None = None,
) -> None:
"""
Create the local index server.

:param path: the path where to host files
:param with_root_pypi: access to upstream PyPI
:param start_args: additional arguments to start the server
"""
self.path = path
self._with_root_pypi = with_root_pypi
self._start_args: Sequence[str] = [] if start_args is None else start_args

self.host, self.port = "localhost", _find_free_port()
self._passwd = "".join(random.choices(string.ascii_letters, k=8)) # ruff:ignore[suspicious-non-cryptographic-random-usage]

scripts_dir = sysconfig.get_path("scripts")
if scripts_dir is None:
msg = "could not get scripts folder of host interpreter" # pragma: no cover
raise RuntimeError(msg) # pragma: no cover

def _exe(name: str) -> str:
return str(Path(scripts_dir) / f"{name}{'.exe' if sys.platform == 'win32' else ''}")

self._init: str = _exe("devpi-init")
self._server: str = _exe("devpi-server")
self._client: str = _exe("devpi")

self._server_dir = self.path / "server"
self._client_dir = self.path / "client"
self._indexes: dict[str, Index] = {}
self._process: Popen[str] | None = None
self._has_use = False
self._stdout_drain: Thread | None = None

@property
def user(self) -> str:
"""Username of the index server."""
return "root"

def __enter__(self) -> Self:
""":return: start the index server"""
self._create_and_start_server()
self._setup_client()
return self

def _create_and_start_server(self) -> None:
self._server_dir.mkdir(exist_ok=True)
server_at = str(self._server_dir)
# 1. create the server
cmd = [self._init, "--serverdir", server_at]
cmd.extend(("--role", "standalone", "--root-passwd", self._passwd))
if self._with_root_pypi is False:
cmd.append("--no-root-pypi")
_check_call(cmd)
# 2. start the server
cmd = [self._server, "--serverdir", server_at, "--port", str(self.port)]
cmd.extend(self._start_args)
self._process = Popen(cmd, stdout=PIPE, universal_newlines=True) # ruff:ignore[subprocess-without-shell-equals-true]
stdout = self._drain_stdout()
for line in stdout: # pragma: no branch # will always loop at least once
if "serving at url" in line:

def _keep_draining() -> None:
for _ in stdout:
pass

# important to keep draining the stdout, otherwise once the buffer is full Windows blocks the process
self._stdout_drain = Thread(target=_keep_draining, name="tox-test-stdout-drain")
self._stdout_drain.start()
break

def _drain_stdout(self) -> Iterator[str]:
process = cast("Popen[str]", self._process)
stdout = cast("IO[str]", process.stdout)
while True:
if process.poll() is not None: # pragma: no cover
print(f"devpi server with pid {process.pid} at {self._server_dir} died") # ruff:ignore[print]
break
yield stdout.readline()

def _setup_client(self) -> None:
"""Create a user on the server and authenticate it."""
self._client_dir.mkdir(exist_ok=True)
base = ["--clientdir", str(self._client_dir)]
_check_call([self._client, "use", *base, self.url])
_check_call([self._client, "login", *base, self.user, "--password", self._passwd])

def create_index(self, name: str, *args: str) -> Index:
"""
Create an index on the server.

:param name: with name
:param args: additional arguments
:return: the created index
"""
if name in self._indexes: # pragma: no cover
msg = f"index {name} already exists"
raise ValueError(msg)
base = [self._client, "--clientdir", str(self._client_dir)]
_check_call([*base, "index", "-c", name, *args])
index = Index(f"{self.url}/{self.user}", name, self.user, base)
self._indexes[name] = index
return index

def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""
Stop the index server.

:param exc_type:
:param exc_val:
:param exc_tb:
"""
if self._process is not None: # pragma: no cover # defend against devpi startup fail
self._process.terminate()
if self._stdout_drain is not None and self._stdout_drain.is_alive(): # pragma: no cover # devpi startup fail
self._stdout_drain.join()

@property
def url(self) -> str:
"""URL to the index server."""
return f"http://{self.host}:{self.port}"

def __repr__(self) -> str:
""":return: repr of the index server"""
return f"{self.__class__.__name__}(url={self.url}, indexes={list(self._indexes)})"


def _find_free_port() -> int:
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as socket_handler:
socket_handler.bind(("", 0))
return cast("int", socket_handler.getsockname()[1])


__all__ = [
"Index",
"IndexServer",
Expand Down
55 changes: 55 additions & 0 deletions src/devpi_process/_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from __future__ import annotations

from subprocess import run # ruff:ignore[suspicious-subprocess-import] # no shell, argument lists only
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path


class Index:
"""Index."""

def __init__(self, base_url: str, name: str, user: str, client_cmd_base: list[str]) -> None:
"""
Create index.

:param base_url: server URL up to the user segment, the index URL is built from it
:param name: name for the index server
:param user: the username to use
:param client_cmd_base: the devpi client command, with its client directory, that runs index commands
"""
self._client_cmd_base = client_cmd_base
self._server_url = base_url
self.name = name
self.user = user

def __repr__(self) -> str:
"""Return the repr of the index."""
return f"{self.__class__.__name__}(url={self.url})"

@property
def url(self) -> str:
"""The URL to the index server."""
return f"{self._server_url}/{self.name}/+simple/"

def use(self) -> None:
"""Use this index server."""
_check_call([*self._client_cmd_base, "use", f"{self.user}/{self.name}"])

def upload(self, *files: Path) -> None:
"""
Upload packages to the index.

:param files: the files to upload
"""
_check_call(self._client_cmd_base + ["upload", "--index", self.name] + [str(i) for i in files])


def _check_call(cmd: list[str]) -> None:
run(cmd, check=True, capture_output=True) # ruff:ignore[subprocess-without-shell-equals-true] # no shell, argument list only


__all__ = [
"Index",
]
Loading