Packaging/dev-tools/docs update #244
70 changed files with 1518 additions and 1327 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
---
|
---
|
||||||
name: Codespell
|
name: Codespell
|
||||||
|
|
||||||
on: workflow_dispatch
|
on: [push, pull_request, workflow_dispatch]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -10,13 +10,13 @@ permissions:
|
||||||
jobs:
|
jobs:
|
||||||
codespell:
|
codespell:
|
||||||
name: Check for spelling errors
|
name: Check for spelling errors
|
||||||
runs-on: ubuntu-latest
|
runs-on: debian-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Codespell
|
- name: Codespell
|
||||||
uses: codespell-project/actions-codespell@v2
|
uses: https://github.com/codespell-project/actions-codespell@v2
|
||||||
with:
|
with:
|
||||||
ignore_words_list: crate
|
ignore_words_list: crate
|
||||||
|
|
|
||||||
36
.forgejo/workflows/mypy-pr.yml
Normal file
36
.forgejo/workflows/mypy-pr.yml
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
name: Type annotation (PR)
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
paths:
|
||||||
|
- 'dump_things_service/**.py'
|
||||||
|
- '!**/tests/**.py'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check:
|
||||||
|
runs-on: debian-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout project
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install uv
|
||||||
|
uses: astral-sh/setup-uv@v6
|
||||||
|
- name: Install hatch
|
||||||
|
run: uv tool install hatch
|
||||||
|
- name: Get Python changed files
|
||||||
|
id: changed-py-files
|
||||||
|
uses: https://github.com/tj-actions/changed-files@v46
|
||||||
|
with:
|
||||||
|
files: |
|
||||||
|
*.py
|
||||||
|
**/*.py
|
||||||
|
- name: Type check changed files
|
||||||
|
if: steps.changed-py-files.outputs.any_changed == 'true'
|
||||||
|
run: |
|
||||||
|
# get any type stubs that mypy thinks it needs
|
||||||
|
# run mypy on the modified files only, and do not even follow imports.
|
||||||
|
# this results is a fairly superficial test, but given the overall
|
||||||
|
# state of annotations, we strive to become more correct incrementally
|
||||||
|
# with focused error reports, rather than barfing a huge complaint
|
||||||
|
# that is unrelated to the changeset someone has been working on.
|
||||||
|
# run on the oldest supported Python version
|
||||||
|
hatch run types:mypy --install-types --non-interactive --python-version 3.11 --follow-imports skip --pretty --show-error-context ${{ steps.changed-py-files.outputs.all_changed_files }}
|
||||||
17
.forgejo/workflows/ruff.yml
Normal file
17
.forgejo/workflows/ruff.yml
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
name: Ruff
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
ruff:
|
||||||
|
name: Code linting
|
||||||
|
runs-on: debian-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout project
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install uv
|
||||||
|
uses: astral-sh/setup-uv@v6
|
||||||
|
- name: Install hatch
|
||||||
|
run: uv tool install hatch
|
||||||
|
- name: Check code
|
||||||
|
run: hatch check code
|
||||||
|
- name: Check formatting
|
||||||
|
run: hatch check fmt
|
||||||
|
|
@ -12,9 +12,6 @@ jobs:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up Python
|
|
||||||
uses: actions/setup-python@v5
|
|
||||||
|
|
||||||
- name: Install uv
|
- name: Install uv
|
||||||
uses: astral-sh/setup-uv@v6
|
uses: astral-sh/setup-uv@v6
|
||||||
|
|
||||||
|
|
@ -23,14 +20,14 @@ jobs:
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
hatch run tests:run \
|
hatch test \
|
||||||
--ignore=dump_things_service/tests/test_generators.py \
|
--ignore=dump_things_service/tests/test_generators.py \
|
||||||
--ignore=dump_things_service/tests/test_ifabsent_patch.py
|
--ignore=dump_things_service/tests/test_ifabsent_patch.py
|
||||||
|
|
||||||
- name: Run generator tests
|
- name: Run generator tests
|
||||||
run: |
|
run: |
|
||||||
hatch run tests:run dump_things_service/tests/test_generators.py
|
hatch test dump_things_service/tests/test_generators.py
|
||||||
|
|
||||||
- name: Run ifabsent-patch tests
|
- name: Run ifabsent-patch tests
|
||||||
run: |
|
run: |
|
||||||
hatch run tests:run dump_things_service/tests/test_ifabsent_patch.py
|
hatch test dump_things_service/tests/test_ifabsent_patch.py
|
||||||
|
|
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -3,3 +3,5 @@ dist/**
|
||||||
tmp/**
|
tmp/**
|
||||||
**/__pycache__
|
**/__pycache__
|
||||||
**/.hypothesis
|
**/.hypothesis
|
||||||
|
.*.swp
|
||||||
|
dump_things_service/_version.py
|
||||||
|
|
|
||||||
31
.readthedocs.yaml
Normal file
31
.readthedocs.yaml
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
# .readthedocs.yaml
|
||||||
|
# Read the Docs configuration file
|
||||||
|
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
|
||||||
|
|
||||||
|
# Required
|
||||||
|
version: 2
|
||||||
|
|
||||||
|
# Set the OS, Python version and other tools you might need
|
||||||
|
build:
|
||||||
|
os: ubuntu-24.04
|
||||||
|
tools:
|
||||||
|
python: "3.12"
|
||||||
|
|
||||||
|
# Build documentation in the "docs/" directory with Sphinx
|
||||||
|
sphinx:
|
||||||
|
configuration: docs/conf.py
|
||||||
|
|
||||||
|
# Optionally build your docs in additional formats such as PDF and ePub
|
||||||
|
# formats:
|
||||||
|
# - pdf
|
||||||
|
# - epub
|
||||||
|
|
||||||
|
# Optional but recommended, declare the Python requirements required
|
||||||
|
# to build your documentation
|
||||||
|
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
|
||||||
|
python:
|
||||||
|
install:
|
||||||
|
- method: pip
|
||||||
|
path: .
|
||||||
|
extra_requirements:
|
||||||
|
- docs
|
||||||
|
|
@ -123,7 +123,7 @@
|
||||||
3. The top-level mapping `admin_tokens` was added.
|
3. The top-level mapping `admin_tokens` was added.
|
||||||
|
|
||||||
- Configuration files are no longer read when the service is started. Instead
|
- Configuration files are no longer read when the service is started. Instead
|
||||||
the service reads its configuration from the store, if it is present. Thw tool
|
the service reads its configuration from the store, if it is present. The tool
|
||||||
(`dump-things-load-config`) can read an existing configuration
|
(`dump-things-load-config`) can read an existing configuration
|
||||||
file and manifest the described configuration on a running dump-things server.
|
file and manifest the described configuration on a running dump-things server.
|
||||||
It supports pre version 6 config files and converts them to the new
|
It supports pre version 6 config files and converts them to the new
|
||||||
|
|
|
||||||
|
|
@ -594,7 +594,7 @@ Most endpoints require a *collection*. These correspond to the names of the "dat
|
||||||
|
|
||||||
The service provides the following user endpoints (In addition to user endpoints, there exist endpoints for curators. To view them, check the `/docs`-path in an installed service):
|
The service provides the following user endpoints (In addition to user endpoints, there exist endpoints for curators. To view them, check the `/docs`-path in an installed service):
|
||||||
|
|
||||||
- `POST /maintenance`: this endpoint allows to set a collection into mantenance mode.
|
- `POST /maintenance`: this endpoint allows to set a collection into maintenance mode.
|
||||||
In maintenance mode, only tokens with curator-privileges can access the collection.
|
In maintenance mode, only tokens with curator-privileges can access the collection.
|
||||||
The posted data is a JSON that contains the name of the collection and whether the maintenance state should be active or not, for example:
|
The posted data is a JSON that contains the name of the collection and whether the maintenance state should be active or not, for example:
|
||||||
```json
|
```json
|
||||||
|
|
|
||||||
2
docs/.gitignore
vendored
Normal file
2
docs/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
_build
|
||||||
|
generated
|
||||||
20
docs/Makefile
Normal file
20
docs/Makefile
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Minimal makefile for Sphinx documentation
|
||||||
|
#
|
||||||
|
|
||||||
|
# You can set these variables from the command line, and also
|
||||||
|
# from the environment for the first two.
|
||||||
|
SPHINXOPTS ?= --fail-on-warning
|
||||||
|
SPHINXBUILD ?= sphinx-build
|
||||||
|
SOURCEDIR = .
|
||||||
|
BUILDDIR = _build
|
||||||
|
|
||||||
|
# Put it first so that "make" without argument is like "make help".
|
||||||
|
help:
|
||||||
|
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||||
|
|
||||||
|
.PHONY: help Makefile
|
||||||
|
|
||||||
|
# Catch-all target: route all unknown targets to Sphinx using the new
|
||||||
|
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
||||||
|
%: Makefile
|
||||||
|
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||||
0
docs/_static/.gitkeep
vendored
Normal file
0
docs/_static/.gitkeep
vendored
Normal file
48
docs/conf.py
Normal file
48
docs/conf.py
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
# Configuration file for the Sphinx documentation builder.
|
||||||
|
#
|
||||||
|
# For the full list of built-in configuration values, see the documentation:
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
||||||
|
|
||||||
|
# -- Project information -----------------------------------------------------
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
|
||||||
|
|
||||||
|
import dump_things_service
|
||||||
|
|
||||||
|
project = 'dump-things-server'
|
||||||
|
copyright = '2025-2026, Christian Mönch'
|
||||||
|
author = 'Christian Mönch'
|
||||||
|
release = dump_things_service.__version__
|
||||||
|
|
||||||
|
# -- General configuration ---------------------------------------------------
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
||||||
|
|
||||||
|
extensions = [
|
||||||
|
'sphinx.ext.autosummary',
|
||||||
|
'sphinx.ext.autodoc',
|
||||||
|
'sphinx_autodoc_typehints',
|
||||||
|
'sphinx.ext.viewcode',
|
||||||
|
]
|
||||||
|
|
||||||
|
templates_path = ['_templates']
|
||||||
|
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
||||||
|
|
||||||
|
primary_domain = 'py'
|
||||||
|
autoclass_content = "both"
|
||||||
|
|
||||||
|
typehints_use_signature = True
|
||||||
|
typehints_use_signature_return = True
|
||||||
|
|
||||||
|
# we build some docstrings from loguru. define some no-op substitutions
|
||||||
|
# to avoid errors
|
||||||
|
rst_prolog = """
|
||||||
|
.. |Logger| replace:: Logger
|
||||||
|
.. |add| replace:: add
|
||||||
|
.. |sys.stderr| replace:: sys.stderr
|
||||||
|
.. |str.format| replace:: str.format
|
||||||
|
"""
|
||||||
|
|
||||||
|
# -- Options for HTML output -------------------------------------------------
|
||||||
|
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
||||||
|
|
||||||
|
html_theme = 'sphinx_rtd_theme'
|
||||||
|
html_static_path = ['_static']
|
||||||
10
docs/index.rst
Normal file
10
docs/index.rst
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
The `dump-thing-server` documentation
|
||||||
|
=====================================
|
||||||
|
|
||||||
|
HERE BE CONTENT...
|
||||||
|
|
||||||
|
Indices and tables
|
||||||
|
==================
|
||||||
|
|
||||||
|
* :ref:`genindex`
|
||||||
|
* :ref:`search`
|
||||||
|
|
@ -20,8 +20,9 @@ from starlette.status import (
|
||||||
HTTP_503_SERVICE_UNAVAILABLE,
|
HTTP_503_SERVICE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from dump_things_service._version import __version__
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'Format',
|
|
||||||
'HTTP_200_OK',
|
'HTTP_200_OK',
|
||||||
'HTTP_201_CREATED',
|
'HTTP_201_CREATED',
|
||||||
'HTTP_300_MULTIPLE_CHOICES',
|
'HTTP_300_MULTIPLE_CHOICES',
|
||||||
|
|
@ -37,6 +38,8 @@ __all__ = [
|
||||||
'HTTP_503_SERVICE_UNAVAILABLE',
|
'HTTP_503_SERVICE_UNAVAILABLE',
|
||||||
'JSON',
|
'JSON',
|
||||||
'YAML',
|
'YAML',
|
||||||
|
'Format',
|
||||||
|
'__version__',
|
||||||
'config_file_name',
|
'config_file_name',
|
||||||
'reserved_collection_names',
|
'reserved_collection_names',
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
import enum
|
import enum
|
||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
|
from collections.abc import Callable, Iterable
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from pathlib import (
|
from pathlib import (
|
||||||
Path,
|
Path,
|
||||||
PurePosixPath,
|
PurePosixPath,
|
||||||
)
|
)
|
||||||
from typing import (
|
from typing import (
|
||||||
Callable,
|
|
||||||
Iterable,
|
|
||||||
Literal,
|
Literal,
|
||||||
cast,
|
cast,
|
||||||
)
|
)
|
||||||
|
|
@ -17,7 +16,8 @@ from fastapi import HTTPException
|
||||||
from pydantic import (
|
from pydantic import (
|
||||||
BaseModel,
|
BaseModel,
|
||||||
ConfigDict,
|
ConfigDict,
|
||||||
Field, ValidationError,
|
Field,
|
||||||
|
ValidationError,
|
||||||
)
|
)
|
||||||
from yaml.scanner import ScannerError
|
from yaml.scanner import ScannerError
|
||||||
|
|
||||||
|
|
@ -27,12 +27,11 @@ from dump_things_service import (
|
||||||
)
|
)
|
||||||
from dump_things_service.audit.gitaudit import GitAuditBackend
|
from dump_things_service.audit.gitaudit import GitAuditBackend
|
||||||
from dump_things_service.backends.record_dir import (
|
from dump_things_service.backends.record_dir import (
|
||||||
_RecordDirStore,
|
|
||||||
RecordDirStore,
|
RecordDirStore,
|
||||||
|
_RecordDirStore,
|
||||||
)
|
)
|
||||||
from dump_things_service.exceptions import ConfigError
|
from dump_things_service.exceptions import ConfigError
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('dump_things_service')
|
logger = logging.getLogger('dump_things_service')
|
||||||
|
|
||||||
g_abstract_configuration = None
|
g_abstract_configuration = None
|
||||||
|
|
@ -103,7 +102,9 @@ class CollectionConfig(BaseModel):
|
||||||
curated: PurePosixPath
|
curated: PurePosixPath
|
||||||
schema_location: str = Field(alias='schema')
|
schema_location: str = Field(alias='schema')
|
||||||
incoming: PurePosixPath | None = None
|
incoming: PurePosixPath | None = None
|
||||||
backend: RecordDirBackendConfig | SQLiteBackendConfig = RecordDirBackendConfig(type='record_dir+stl')
|
backend: RecordDirBackendConfig | SQLiteBackendConfig = RecordDirBackendConfig(
|
||||||
|
type='record_dir+stl'
|
||||||
|
)
|
||||||
auth_sources: list[ForgejoAuthSpec | ConfigAuthSpec] = [ConfigAuthSpec()]
|
auth_sources: list[ForgejoAuthSpec | ConfigAuthSpec] = [ConfigAuthSpec()]
|
||||||
audit_backends: list[GitAuditBackendConfig] = []
|
audit_backends: list[GitAuditBackendConfig] = []
|
||||||
submission_tags: TagSpec = TagSpec()
|
submission_tags: TagSpec = TagSpec()
|
||||||
|
|
@ -211,9 +212,7 @@ def get_config_backends(
|
||||||
|
|
||||||
if config_backend is None:
|
if config_backend is None:
|
||||||
config_backend = RecordDirStore(
|
config_backend = RecordDirStore(
|
||||||
config_path,
|
config_path, mapping_functions[MappingMethod.digest_md5], 'yaml'
|
||||||
mapping_functions[MappingMethod.digest_md5],
|
|
||||||
'yaml'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
audit_path = store_path / config_audit_path
|
audit_path = store_path / config_audit_path
|
||||||
|
|
@ -244,7 +243,7 @@ def read_config(
|
||||||
if record_info
|
if record_info
|
||||||
else Configuration(
|
else Configuration(
|
||||||
type='collections',
|
type='collections',
|
||||||
version = 2,
|
version=2,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except ValidationError as ve:
|
except ValidationError as ve:
|
||||||
|
|
@ -259,7 +258,7 @@ def get_config() -> Configuration:
|
||||||
if not g_abstract_configuration:
|
if not g_abstract_configuration:
|
||||||
msg = 'Configuration not yet loaded'
|
msg = 'Configuration not yet loaded'
|
||||||
raise RuntimeError(msg)
|
raise RuntimeError(msg)
|
||||||
return cast(Configuration, g_abstract_configuration)
|
return cast('Configuration', g_abstract_configuration)
|
||||||
|
|
||||||
|
|
||||||
def store_config(
|
def store_config(
|
||||||
|
|
@ -274,7 +273,7 @@ def store_config(
|
||||||
config_backend.add_record(
|
config_backend.add_record(
|
||||||
iri=dump_things_config_iri,
|
iri=dump_things_config_iri,
|
||||||
class_name='DumpThingsConfig',
|
class_name='DumpThingsConfig',
|
||||||
json_object=json_object
|
json_object=json_object,
|
||||||
)
|
)
|
||||||
audit_backend.add_record(
|
audit_backend.add_record(
|
||||||
record=json_object,
|
record=json_object,
|
||||||
|
|
@ -314,10 +313,9 @@ def check_label(
|
||||||
from dump_things_service.utils import get_on_disk_labels
|
from dump_things_service.utils import get_on_disk_labels
|
||||||
|
|
||||||
"""Check that a label exists in a collection configuration or on disk"""
|
"""Check that a label exists in a collection configuration or on disk"""
|
||||||
if (
|
if label not in get_config_labels(
|
||||||
label not in get_config_labels(abstract_config, collection)
|
abstract_config, collection
|
||||||
and label not in get_on_disk_labels(store_path, abstract_config, collection)
|
) and label not in get_on_disk_labels(store_path, abstract_config, collection):
|
||||||
):
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTP_404_NOT_FOUND,
|
status_code=HTTP_404_NOT_FOUND,
|
||||||
detail=f"No incoming label: '{label}' in collection: '{collection}'.",
|
detail=f"No incoming label: '{label}' in collection: '{collection}'.",
|
||||||
|
|
@ -336,10 +334,7 @@ def get_config_labels(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_default_token_name(
|
def get_default_token_name(abstract_config: Configuration, collection: str) -> str:
|
||||||
abstract_config: Configuration,
|
|
||||||
collection: str
|
|
||||||
) -> str:
|
|
||||||
check_collection(abstract_config, collection)
|
check_collection(abstract_config, collection)
|
||||||
return abstract_config.collections[collection].default_token
|
return abstract_config.collections[collection].default_token
|
||||||
|
|
||||||
|
|
@ -377,7 +372,6 @@ def get_token_infos_for_collection(
|
||||||
abstract_config: Configuration,
|
abstract_config: Configuration,
|
||||||
collection_name: str,
|
collection_name: str,
|
||||||
) -> Iterable[tuple[str, TokenConfig, TokenCollectionConfig]]:
|
) -> Iterable[tuple[str, TokenConfig, TokenCollectionConfig]]:
|
||||||
|
|
||||||
yield from {
|
yield from {
|
||||||
(token_name, token_config, token_collection_config)
|
(token_name, token_config, token_collection_config)
|
||||||
for token_name, token_config in abstract_config.tokens.items()
|
for token_name, token_config in abstract_config.tokens.items()
|
||||||
|
|
@ -391,7 +385,6 @@ def get_token_config_for_representation_and_collection(
|
||||||
collection_name: str,
|
collection_name: str,
|
||||||
token_representation: str,
|
token_representation: str,
|
||||||
) -> tuple[str, TokenConfig, TokenCollectionConfig] | None:
|
) -> tuple[str, TokenConfig, TokenCollectionConfig] | None:
|
||||||
|
|
||||||
token_info = get_token_info_by_representation(
|
token_info = get_token_info_by_representation(
|
||||||
abstract_config=abstract_config,
|
abstract_config=abstract_config,
|
||||||
token_representation=token_representation,
|
token_representation=token_representation,
|
||||||
|
|
@ -421,7 +414,6 @@ def get_default_token_config(
|
||||||
abstract_config: Configuration,
|
abstract_config: Configuration,
|
||||||
collection: str,
|
collection: str,
|
||||||
) -> TokenConfig | None:
|
) -> TokenConfig | None:
|
||||||
|
|
||||||
default_token_name = get_collection_config_by_name(
|
default_token_name = get_collection_config_by_name(
|
||||||
abstract_config,
|
abstract_config,
|
||||||
collection,
|
collection,
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ from dump_things_service.abstract_config import (
|
||||||
)
|
)
|
||||||
from dump_things_service.instance_state import InstanceState
|
from dump_things_service.instance_state import InstanceState
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('dump_things_service')
|
logger = logging.getLogger('dump_things_service')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ committed.
|
||||||
|
|
||||||
Changes are annotated with a time stamp and a user-id
|
Changes are annotated with a time stamp and a user-id
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
@ -23,12 +24,11 @@ import yaml
|
||||||
from datalad_core.git_utils import apply_changeset
|
from datalad_core.git_utils import apply_changeset
|
||||||
from datalad_core.repo import Repo
|
from datalad_core.repo import Repo
|
||||||
from datalad_core.runners import (
|
from datalad_core.runners import (
|
||||||
call_git,
|
|
||||||
CommandError,
|
CommandError,
|
||||||
|
call_git,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import AuditBackend
|
from dump_things_service.audit import AuditBackend
|
||||||
|
|
||||||
|
|
||||||
index_file_name = 'gitaudit_index.log'
|
index_file_name = 'gitaudit_index.log'
|
||||||
|
|
||||||
|
|
@ -56,7 +56,6 @@ class FlushingThread(Thread):
|
||||||
|
|
||||||
|
|
||||||
class GitAuditBackend(AuditBackend):
|
class GitAuditBackend(AuditBackend):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
path: Path,
|
path: Path,
|
||||||
|
|
@ -69,7 +68,8 @@ class GitAuditBackend(AuditBackend):
|
||||||
self.lock = Lock()
|
self.lock = Lock()
|
||||||
self.last_flush_time = 0
|
self.last_flush_time = 0
|
||||||
if auto_flush_timeout < 1:
|
if auto_flush_timeout < 1:
|
||||||
raise ValueError('auto_flush_timeout must be greater or equal to 1')
|
msg = 'auto_flush_timeout must be greater or equal to 1'
|
||||||
|
raise ValueError(msg)
|
||||||
self.flushing_thread = FlushingThread(self, auto_flush_timeout)
|
self.flushing_thread = FlushingThread(self, auto_flush_timeout)
|
||||||
self.flushing_thread.start()
|
self.flushing_thread.start()
|
||||||
self._init_repo()
|
self._init_repo()
|
||||||
|
|
@ -125,32 +125,42 @@ class GitAuditBackend(AuditBackend):
|
||||||
# the records
|
# the records
|
||||||
changes = []
|
changes = []
|
||||||
yaml_location, log_location = map(str, self._get_location_for(record_id)[1:])
|
yaml_location, log_location = map(str, self._get_location_for(record_id)[1:])
|
||||||
commit_hashes = call_git(
|
commit_hashes = (
|
||||||
|
call_git(
|
||||||
['log', '--format=%H', '--', log_location],
|
['log', '--format=%H', '--', log_location],
|
||||||
cwd=self.path,
|
cwd=self.path,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
).decode().splitlines()
|
)
|
||||||
|
.decode()
|
||||||
|
.splitlines()
|
||||||
|
)
|
||||||
for commit_hash in commit_hashes:
|
for commit_hash in commit_hashes:
|
||||||
log_diff_lines = call_git(
|
log_diff_lines = (
|
||||||
|
call_git(
|
||||||
['show', '--format=%b', commit_hash, '--', log_location],
|
['show', '--format=%b', commit_hash, '--', log_location],
|
||||||
cwd=self.path,
|
cwd=self.path,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
).decode().splitlines()
|
)
|
||||||
|
.decode()
|
||||||
|
.splitlines()
|
||||||
|
)
|
||||||
# Get the log entry
|
# Get the log entry
|
||||||
log_line = tuple(
|
log_line = next(filter(
|
||||||
filter(
|
|
||||||
lambda l: not l.startswith('+++') and l.startswith('+'),
|
lambda l: not l.startswith('+++') and l.startswith('+'),
|
||||||
log_diff_lines,
|
log_diff_lines,
|
||||||
)
|
))[1:]
|
||||||
)[0][1:]
|
|
||||||
log_entry = json.loads(log_line)
|
log_entry = json.loads(log_line)
|
||||||
|
|
||||||
# Get the YAML diff
|
# Get the YAML diff
|
||||||
yaml_diff_lines = call_git(
|
yaml_diff_lines = (
|
||||||
|
call_git(
|
||||||
['show', '--format=%b', commit_hash, '--', yaml_location],
|
['show', '--format=%b', commit_hash, '--', yaml_location],
|
||||||
cwd=self.path,
|
cwd=self.path,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
).decode().splitlines()
|
)
|
||||||
|
.decode()
|
||||||
|
.splitlines()
|
||||||
|
)
|
||||||
yaml_diff = '\n'.join(filter(lambda l: l != '', yaml_diff_lines)) + '\n'
|
yaml_diff = '\n'.join(filter(lambda l: l != '', yaml_diff_lines)) + '\n'
|
||||||
|
|
||||||
# Get the YAML content
|
# Get the YAML content
|
||||||
|
|
@ -247,7 +257,7 @@ class GitAuditBackend(AuditBackend):
|
||||||
) -> bytes:
|
) -> bytes:
|
||||||
try:
|
try:
|
||||||
return call_git(
|
return call_git(
|
||||||
['cat-file', '-p', f'master:{str(path)}'],
|
['cat-file', '-p', f'master:{path!s}'],
|
||||||
cwd=self.path,
|
cwd=self.path,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
)
|
)
|
||||||
|
|
@ -290,7 +300,7 @@ class GitAuditBackend(AuditBackend):
|
||||||
record_id: str,
|
record_id: str,
|
||||||
) -> tuple[str, Path, Path]:
|
) -> tuple[str, Path, Path]:
|
||||||
base = hashlib.sha1(record_id.encode()).hexdigest()
|
base = hashlib.sha1(record_id.encode()).hexdigest()
|
||||||
dir_1, dir_2, name = base[0:3], base[3:6], base[6:]
|
dir_1, dir_2, _name = base[0:3], base[3:6], base[6:]
|
||||||
location_dir = Path(dir_1) / Path(dir_2)
|
location_dir = Path(dir_1) / Path(dir_2)
|
||||||
return (
|
return (
|
||||||
base,
|
base,
|
||||||
|
|
@ -321,8 +331,8 @@ class GitAuditBackend(AuditBackend):
|
||||||
if not self.index_path.exists():
|
if not self.index_path.exists():
|
||||||
self._rebuild_index()
|
self._rebuild_index()
|
||||||
|
|
||||||
with open(self.index_path, 'rt') as f:
|
with open(self.index_path) as f:
|
||||||
self.index = set(line.strip() for line in f.readlines())
|
self.index = {line.strip() for line in f}
|
||||||
|
|
||||||
def _add_to_index(
|
def _add_to_index(
|
||||||
self,
|
self,
|
||||||
|
|
@ -333,16 +343,20 @@ class GitAuditBackend(AuditBackend):
|
||||||
self.index.add(record_id)
|
self.index.add(record_id)
|
||||||
|
|
||||||
def _rebuild_index(self):
|
def _rebuild_index(self):
|
||||||
tree_entries = call_git(
|
tree_entries = (
|
||||||
|
call_git(
|
||||||
['ls-tree', '-r', 'master:'],
|
['ls-tree', '-r', 'master:'],
|
||||||
cwd=self.path,
|
cwd=self.path,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
).decode().splitlines()
|
)
|
||||||
with open(self.index_path, 'wt') as f:
|
.decode()
|
||||||
|
.splitlines()
|
||||||
|
)
|
||||||
|
with open(self.index_path, 'w') as f:
|
||||||
for line in tree_entries:
|
for line in tree_entries:
|
||||||
if not line.endswith('.yaml'):
|
if not line.endswith('.yaml'):
|
||||||
continue
|
continue
|
||||||
flag, object_type, object_hash, file_name = line.split(maxsplit=3)
|
_flag, _object_type, object_hash, _file_name = line.split(maxsplit=3)
|
||||||
record = yaml.safe_load(
|
record = yaml.safe_load(
|
||||||
call_git(
|
call_git(
|
||||||
['show', object_hash],
|
['show', object_hash],
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ def _get_audit_log_lines(backend: GitAuditBackend, record_id: str) -> list[str]:
|
||||||
|
|
||||||
|
|
||||||
def test_gitaudit_basic(tmp_path_factory):
|
def test_gitaudit_basic(tmp_path_factory):
|
||||||
tmp_path = tmp_path_factory.mktemp("gitaudit_backend")
|
tmp_path = tmp_path_factory.mktemp('gitaudit_backend')
|
||||||
|
|
||||||
backend = GitAuditBackend(tmp_path)
|
backend = GitAuditBackend(tmp_path)
|
||||||
|
|
||||||
|
|
@ -44,14 +44,13 @@ def test_gitaudit_basic(tmp_path_factory):
|
||||||
# Check that the changes are reported
|
# Check that the changes are reported
|
||||||
changes = backend.get_audit_log(record_id)
|
changes = backend.get_audit_log(record_id)
|
||||||
assert len(changes) == 4
|
assert len(changes) == 4
|
||||||
assert tuple(map(lambda e: e[0:2], changes.values())) == tuple(
|
assert tuple(e[0:2] for e in changes.values()) == tuple(
|
||||||
(f'committer_{100 + i}@x.org', f'author_{i}@y.org')
|
(f'committer_{100 + i}@x.org', f'author_{i}@y.org') for i in range(4)
|
||||||
for i in range(4)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_gitaudit_identical_change(tmp_path_factory):
|
def test_gitaudit_identical_change(tmp_path_factory):
|
||||||
tmp_path = tmp_path_factory.mktemp("gitaudit_backend")
|
tmp_path = tmp_path_factory.mktemp('gitaudit_backend')
|
||||||
|
|
||||||
backend = GitAuditBackend(tmp_path)
|
backend = GitAuditBackend(tmp_path)
|
||||||
|
|
||||||
|
|
@ -59,13 +58,13 @@ def test_gitaudit_identical_change(tmp_path_factory):
|
||||||
backend.add_record(
|
backend.add_record(
|
||||||
record={'pid': record_id},
|
record={'pid': record_id},
|
||||||
committer_id='committer_b@x.org',
|
committer_id='committer_b@x.org',
|
||||||
author_id = 'author_b@y.org',
|
author_id='author_b@y.org',
|
||||||
)
|
)
|
||||||
|
|
||||||
backend.add_record(
|
backend.add_record(
|
||||||
record={'pid': record_id},
|
record={'pid': record_id},
|
||||||
committer_id='committer_b@x.org',
|
committer_id='committer_b@x.org',
|
||||||
author_id = 'author_b@y.org',
|
author_id='author_b@y.org',
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check that there is only one entry in the audit log
|
# Check that there is only one entry in the audit log
|
||||||
|
|
@ -83,7 +82,7 @@ def test_gitaudit_identical_change(tmp_path_factory):
|
||||||
|
|
||||||
|
|
||||||
def test_gitaudit_huge_log(tmp_path_factory):
|
def test_gitaudit_huge_log(tmp_path_factory):
|
||||||
tmp_path = tmp_path_factory.mktemp("gitaudit_backend")
|
tmp_path = tmp_path_factory.mktemp('gitaudit_backend')
|
||||||
|
|
||||||
backend = GitAuditBackend(tmp_path)
|
backend = GitAuditBackend(tmp_path)
|
||||||
|
|
||||||
|
|
@ -96,7 +95,7 @@ def test_gitaudit_huge_log(tmp_path_factory):
|
||||||
backend.add_record(
|
backend.add_record(
|
||||||
record={'pid': record_id, 'content': f'j:{j}, i:{i}'},
|
record={'pid': record_id, 'content': f'j:{j}, i:{i}'},
|
||||||
committer_id='committer@x.org',
|
committer_id='committer@x.org',
|
||||||
author_id = 'author@y.org',
|
author_id='author@y.org',
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check that the changes are reported
|
# Check that the changes are reported
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ determine:
|
||||||
- the incoming_label to be used with the token
|
- the incoming_label to be used with the token
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue