Skip to content

Storage

The following snippet will build on the pip upload example to store the uploaded packages and metadata.

File structure

packages/
└── atht-core
    └── 0.15.0
        ├── bdist_wheel
        │   └── atht_core-0.15.0-py3-none-any.whl
        ├── .metadata.json
        └── sdist
            └── atht_core-0.15.0.tar.gz

Code

import json
from pathlib import Path

root_path = Path("/srv/packages")

def callback(package):
    package_path = root_path / package.name
    version_path = package_path / package.version
    filetype_path = version_path / package.filetype
    filename = filetype_path / package.filename

    version_metadata_path = version_path / ".metadata.json"
    if not version_metadata_path.exists():
        version_path.mkdir(exist_ok=True, parents=True)
        with open(version_metadata_path, "w") as f:
            meta = package.model_dump(exclude="content", exclude_none=True)
            f.write(json.dumps(meta))

    filetype_path.mkdir(exist_ok=True, parents=True)
    with open(filename, "wb") as f:
        f.write(package.content)

    return "OK", 200