From 05196f6899aff3795a86a9dabfd1eed30522ecaf Mon Sep 17 00:00:00 2001 From: Bram Mittendorff Date: Wed, 2 Apr 2025 01:24:05 +0200 Subject: [PATCH 1/7] Fixed get_verions --- setup.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index 7b75a38..de111ba 100644 --- a/setup.py +++ b/setup.py @@ -16,17 +16,22 @@ 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 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: + import subprocess + version = subprocess.check_output(['git', 'describe', '--tags', '--always']).decode('utf-8').strip() + if version.startswith('v'): + version = version[1:] + return version + except: + # Fallback version + return "0.1.0" def install_man_pages(): From e9178ab18d6b112f36bd9b8051c9475845126d5f Mon Sep 17 00:00:00 2001 From: bram Date: Wed, 2 Apr 2025 11:06:36 +0200 Subject: [PATCH 2/7] Fixed docker version issue --- .github/workflows/ci-cd.yml | 42 ++++++++++++++++++++++++++++++++----- .gitignore | 3 ++- Dockerfile | 2 ++ README.md | 2 +- build.sh | 9 -------- setup.py | 10 +++++---- version.sh | 2 +- 7 files changed, 49 insertions(+), 21 deletions(-) delete mode 100644 build.sh mode change 100644 => 100755 version.sh 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..56b528b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,8 @@ FROM python:${PYTHON_VERSION}-slim # Accept version as build arg ARG VERSION="0.1.0" +# Add this line to convert the ARG to an ENV +ENV PACKAGE_VERSION=${VERSION} WORKDIR /app 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 de111ba..1bd6d7b 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,6 @@ This script is used to install the package, dependencies, and the man page. """ -import json import os from setuptools import find_packages, setup @@ -17,6 +16,10 @@ def get_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) @@ -29,7 +32,7 @@ def get_version(): if version.startswith('v'): version = version[1:] return version - except: + except (subprocess.SubprocessError, FileNotFoundError): # Fallback version return "0.1.0" @@ -49,8 +52,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..a4b96bd --- a/version.sh +++ b/version.sh @@ -3,4 +3,4 @@ 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 +echo $VERSION From a3be4d48c9e3e25c154af8480e2a159391d8eed0 Mon Sep 17 00:00:00 2001 From: bram Date: Wed, 2 Apr 2025 11:12:10 +0200 Subject: [PATCH 3/7] Fixed version --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1bd6d7b..3ce6497 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,8 @@ def get_version(): # Try getting from git try: import subprocess - version = subprocess.check_output(['git', 'describe', '--tags', '--always']).decode('utf-8').strip() + # Get only the latest tag without additional commit info + version = subprocess.check_output(['git', 'describe', '--tags', '--abbrev=0']).decode('utf-8').strip() if version.startswith('v'): version = version[1:] return version From 9687d42cdb8eb029c0eaa0ea8ff9f446c5c3a4cd Mon Sep 17 00:00:00 2001 From: bram Date: Wed, 2 Apr 2025 11:13:55 +0200 Subject: [PATCH 4/7] Added import to top --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 3ce6497..27bf587 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ """ import os +import subprocess from setuptools import find_packages, setup @@ -27,7 +28,6 @@ def get_version(): # Try getting from git try: - import subprocess # Get only the latest tag without additional commit info version = subprocess.check_output(['git', 'describe', '--tags', '--abbrev=0']).decode('utf-8').strip() if version.startswith('v'): From e16054540b84583e2fe75cc513b65b5749f2023b Mon Sep 17 00:00:00 2001 From: bram Date: Wed, 2 Apr 2025 11:20:08 +0200 Subject: [PATCH 5/7] Fixed setup --- Dockerfile | 9 +++++---- setup.py | 15 +++++++++++---- version.sh | 19 ++++++++++++++++--- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 56b528b..059e69f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM python:${PYTHON_VERSION}-slim # Accept version as build arg ARG VERSION="0.1.0" -# Add this line to convert the ARG to an ENV +# Set as environment variable for setup.py to use ENV PACKAGE_VERSION=${VERSION} WORKDIR /app @@ -15,11 +15,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 . +# Instead of pip install which is failing, just create the entry point +RUN ln -s /app/python_gpt_po/main.py /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/setup.py b/setup.py index 27bf587..35a485e 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,6 @@ import os import subprocess - from setuptools import find_packages, setup with open('README.md', encoding='utf-8') as f: @@ -28,10 +27,18 @@ def get_version(): # Try getting from git try: - # Get only the latest tag without additional commit info - version = subprocess.check_output(['git', 'describe', '--tags', '--abbrev=0']).decode('utf-8').strip() - if version.startswith('v'): + # 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 diff --git a/version.sh b/version.sh index a4b96bd..ab5fb27 100755 --- 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 + +# 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 From 8de486d5d79d3b1b00f0e321811333b095f0c1cb Mon Sep 17 00:00:00 2001 From: bram Date: Wed, 2 Apr 2025 11:21:52 +0200 Subject: [PATCH 6/7] Added packages --- Dockerfile | 5 +++-- setup.py | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 059e69f..82cb014 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ FROM python:${PYTHON_VERSION}-slim ARG VERSION="0.1.0" # Set as environment variable for setup.py to use ENV PACKAGE_VERSION=${VERSION} +ENV PYTHONPATH=/app WORKDIR /app @@ -18,8 +19,8 @@ RUN pip install --no-cache-dir -r requirements.txt # Copy source code COPY . . -# Instead of pip install which is failing, just create the entry point -RUN ln -s /app/python_gpt_po/main.py /usr/local/bin/gpt-po-translator && \ +# Create a simple wrapper script +RUN echo '#!/bin/bash\npython /app/python_gpt_po/main.py "$@"' > /usr/local/bin/gpt-po-translator && \ chmod +x /usr/local/bin/gpt-po-translator # Create a wrapper script to allow more flexibility diff --git a/setup.py b/setup.py index 35a485e..0605b67 100644 --- a/setup.py +++ b/setup.py @@ -5,6 +5,7 @@ import os import subprocess + from setuptools import find_packages, setup with open('README.md', encoding='utf-8') as f: From a99a510b53cd83c6b573f0f1e87e169122b180fb Mon Sep 17 00:00:00 2001 From: bram Date: Wed, 2 Apr 2025 11:25:24 +0200 Subject: [PATCH 7/7] Fixed issue with entrypoint --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 82cb014..30834d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,7 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . # Create a simple wrapper script -RUN echo '#!/bin/bash\npython /app/python_gpt_po/main.py "$@"' > /usr/local/bin/gpt-po-translator && \ +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