Skip to content

Commit

Permalink
Publish python packages
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriochaves committed Mar 6, 2024
1 parent 84449c5 commit a386163
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Publish to PyPI

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

permissions:
contents: read

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- uses: Gr1N/setup-poetry@v8
- uses: actions/cache@v2
with:
path: ~/.cache/pypoetry/virtualenvs
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}
- name: Install dependencies
run: |
poetry install
pip install toml
- name: Publish core package
run: |
cd langevals_core
PACKAGE_VERSION=$(poetry version --short)
RESPONSE=$(curl -s --head https://pypi.org/pypi/langevals_core/$PACKAGE_VERSION/json)
if [[ $RESPONSE == *"HTTP/2 200"* ]]; then
echo "langevals_core version already exists, skipping publish"
else
echo "Publishing langevals_core"
poetry build
poetry publish --username __token__ --password ${{ secrets.PYPI_API_TOKEN }}
fi
cd ..
- name: Publish evaluator subpackages
run: |
for dir in evaluators/*; do
if [ -d "$dir" ]; then
cd $dir
PACKAGE_VERSION=$(poetry version --short)
RESPONSE=$(curl -s --head https://pypi.org/pypi/langevals_$dir/$PACKAGE_VERSION/json)
if [[ $RESPONSE == *"HTTP/2 200"* ]]; then
echo "$dir version already exists, skipping publish"
else
echo "Publishing $dir"
python ../../scripts/replace_develop_dependencies.py pyproject.toml
poetry build
poetry publish --username __token__ --password ${{ secrets.PYPI_API_TOKEN }}
fi
cd ../..
fi
done
- name: Publish main package
run: |
PACKAGE_VERSION=$(poetry version --short)
RESPONSE=$(curl -s --head https://pypi.org/pypi/langevals/$PACKAGE_VERSION/json)
if [[ $RESPONSE == *"HTTP/2 200"* ]]; then
echo "langevals_core version already exists, skipping publish"
else
echo "Publishing langevals"
python scripts/replace_develop_dependencies.py pyproject.toml
poetry build
poetry publish --username __token__ --password ${{ secrets.PYPI_API_TOKEN }}
fi
44 changes: 44 additions & 0 deletions scripts/replace_develop_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys
import toml
from pathlib import Path


def get_latest_version_of_dependency(dependency_path: Path):
pyproject_path = dependency_path / "pyproject.toml"
if not pyproject_path.exists():
raise FileNotFoundError(f"pyproject.toml not found in {dependency_path}")

with open(pyproject_path, "r") as file:
data = toml.load(file)

return data["tool"]["poetry"]["version"]


def prepare_for_publishing(pyproject_path: Path):
with open(pyproject_path, "r") as file:
data = toml.load(file)

dependencies = data["tool"]["poetry"]["dependencies"]
for dep, dep_data in dependencies.items():
if isinstance(dep_data, dict) and dep_data.get("develop"):
# Assuming the path is relative to the pyproject.toml being modified
dependency_path = (pyproject_path.parent / dep_data["path"]).resolve()
latest_version = get_latest_version_of_dependency(dependency_path)
# Replace the local path with the latest version
dependencies[dep] = f"^{latest_version}"

with open(pyproject_path, "w") as file:
toml.dump(data, file)


if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py path/to/your/package/pyproject.toml")
sys.exit(1)

pyproject_path = Path(sys.argv[1])
if not pyproject_path.exists() or pyproject_path.name != "pyproject.toml":
print("The specified path does not exist or is not a pyproject.toml file.")
sys.exit(1)

prepare_for_publishing(pyproject_path)

0 comments on commit a386163

Please sign in to comment.