diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 02b2ab2..8f01474 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -100,6 +100,26 @@ jobs: restore-keys: | ${{ runner.os }}-buildx- + - name: Get Version for Docker Build + id: get_version + run: | + # Ensure we have tags + git fetch --tags --force + + # For tagged builds, use the exact tag without v prefix for PACKAGE_VERSION + if [[ "$GITHUB_REF" == refs/tags/* ]]; then + TAG=${GITHUB_REF#refs/tags/} + VERSION="${TAG#v}" + echo "Using tag version: $VERSION" + else + # Use git version without v prefix + VERSION=$(git describe --tags --always 2>/dev/null | sed 's/^v//' || echo "0.1.0") + echo "Using git version: $VERSION" + fi + + # Output for GitHub Actions + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT + - name: Build Docker Image - Python ${{ matrix.python-version }} uses: docker/build-push-action@v5 with: @@ -108,6 +128,7 @@ jobs: tags: gpt-po-translator:py${{ matrix.python-version }} build-args: | PYTHON_VERSION=${{ matrix.python-version }} + VERSION=${{ steps.get_version.outputs.VERSION }} cache-from: type=gha cache-to: type=gha,mode=max @@ -164,6 +185,17 @@ jobs: uses: actions/setup-python@v5 with: python-version: '3.x' + + - name: Set Package Version for PyPI + if: matrix.python-version == '3.11' + run: | + # Get tag name without 'refs/tags/' prefix + TAG=${GITHUB_REF#refs/tags/} + # Remove 'v' prefix if present + VERSION="${TAG#v}" + # Set as environment variable + echo "PACKAGE_VERSION=$VERSION" >> $GITHUB_ENV + echo "Using version $VERSION for PyPI package" - name: Install dependencies if: matrix.python-version == '3.11' @@ -227,12 +259,12 @@ jobs: # For tagged builds, use the exact tag if [[ "$GITHUB_REF" == refs/tags/* ]]; then TAG=${GITHUB_REF#refs/tags/} - # Keep the v prefix for git tags - VERSION="$TAG" + # Remove v prefix if present for Docker build arg + VERSION="${TAG#v}" echo "Using tag version: $VERSION" else - # Use git version with v prefix - VERSION=$(git describe --tags --always 2>/dev/null || echo "v0.1.0") + # Use git version without v prefix + VERSION=$(git describe --tags --always 2>/dev/null | sed 's/^v//' || echo "0.1.0") echo "Using git version: $VERSION" fi @@ -250,4 +282,4 @@ jobs: PYTHON_VERSION=${{ matrix.python-version }} VERSION=${{ steps.get_version.outputs.VERSION }} cache-from: type=gha - cache-to: type=gha,mode=max \ No newline at end of file + cache-to: type=gha,mode=max diff --git a/.gitignore b/.gitignore index 2c4675e..90e7d35 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ dist/ __pycache__ build/ db.sqlite3 -.venv \ No newline at end of file +.venv +python_gpt_po/version.py diff --git a/Dockerfile b/Dockerfile index f6943ed..30834d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,9 @@ FROM python:${PYTHON_VERSION}-slim # Accept version as build arg ARG VERSION="0.1.0" +# Set as environment variable for setup.py to use +ENV PACKAGE_VERSION=${VERSION} +ENV PYTHONPATH=/app WORKDIR /app @@ -13,11 +16,12 @@ RUN apt-get update && apt-get install -y git && apt-get clean && rm -rf /var/lib COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -# Copy source code for installation +# Copy source code COPY . . -# Install the package -RUN pip install --no-cache-dir . +# Create a simple wrapper script +RUN echo '#!/bin/bash\npython -m python_gpt_po.main "$@"' > /usr/local/bin/gpt-po-translator && \ + chmod +x /usr/local/bin/gpt-po-translator # Create a wrapper script to allow more flexibility COPY docker-entrypoint.sh /usr/local/bin/ diff --git a/README.md b/README.md index 490d369..76c9881 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ git clone https://github.com/pescheckit/python-gpt-po.git cd python-gpt-po python3 -m venv .venv source .venv/bin/activate -pip install -r requirements +pip install -r requirements.txt python -m python_gpt_po.main --folder test --lang nl --bulk --provider="deepseek" --list-models ``` diff --git a/build.sh b/build.sh deleted file mode 100644 index 87c3e52..0000000 --- a/build.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e - -# Get version from git -VERSION=$(./version.sh) -echo "Building with version: $VERSION" - -# Build Docker image with version from git -docker build --build-arg VERSION="$VERSION" -t gpt-po-translator:$VERSION . \ No newline at end of file diff --git a/setup.py b/setup.py index 7b75a38..0605b67 100644 --- a/setup.py +++ b/setup.py @@ -3,8 +3,8 @@ This script is used to install the package, dependencies, and the man page. """ -import json import os +import subprocess from setuptools import find_packages, setup @@ -16,17 +16,34 @@ def get_version(): - """ - Get version from git or version.json (for Docker builds). - - Returns: - dict or bool: Version configuration - """ - if os.path.exists('version.json'): - # In Docker build environment, use the version file - with open('version.json', encoding='utf-8') as version_file: - return json.load(version_file)['version'] - return True # Use SCM version + """Get version from git or environment variable.""" + # Check for Docker environment + if 'PACKAGE_VERSION' in os.environ: + return os.environ.get('PACKAGE_VERSION') + + # Check for CI/CD environment variable + if 'GITHUB_REF' in os.environ and os.environ['GITHUB_REF'].startswith('refs/tags/'): + # Extract version from tag (strip 'v' prefix if present) + return os.environ['GITHUB_REF'].split('/')[-1].lstrip('v') + + # Try getting from git + try: + # Get version from git describe, but normalize it to be PEP 440 compliant + version = subprocess.check_output(['git', 'describe', '--tags', '--always']).decode('utf-8').strip() + + # Handle version format from git describe + if '-' in version: + # Format like v0.3.5-5-gd9775d7, convert to 0.3.5.dev5+gd9775d7 + tag, commits, commit_hash = version.lstrip('v').split('-') + version = f"{tag}.dev{commits}+{commit_hash}" + elif version.startswith('v'): + # Just a tagged version like v0.3.5 + version = version[1:] + + return version + except (subprocess.SubprocessError, FileNotFoundError): + # Fallback version + return "0.1.0" def install_man_pages(): @@ -44,8 +61,7 @@ def install_man_pages(): setup( name='gpt-po-translator', - use_scm_version=get_version(), - setup_requires=['setuptools-scm==8.1.0'], + version=get_version(), author='Bram Mittendorff', author_email='bram@pescheck.io', description='A CLI tool for translating .po files using GPT models.', diff --git a/version.sh b/version.sh old mode 100644 new mode 100755 index 0e73497..ab5fb27 --- a/version.sh +++ b/version.sh @@ -1,6 +1,19 @@ #!/bin/bash # Get version from git GIT_VERSION=$(git describe --tags --always 2>/dev/null || echo "0.1.0") -# Strip any leading 'v' if present -VERSION="${GIT_VERSION#v}" -echo $VERSION \ No newline at end of file + +# Clean up version for PEP 440 compliance +if [[ "$GIT_VERSION" == *-*-* ]]; then + # Format: v0.3.5-5-gd9775d7 -> 0.3.5.dev5+gd9775d7 + VERSION=$(echo "$GIT_VERSION" | sed -E 's/^v?([0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)-g([a-f0-9]+)/\1.dev\2+\3/') +else + # Simple version, just remove v prefix if present + VERSION="${GIT_VERSION#v}" +fi + +# If first argument is "docker", return Docker-friendly version (replace + with -) +if [ "$1" = "docker" ]; then + echo "${VERSION//+/-}" +else + echo "$VERSION" +fi