Skip to content

Improve Build and Dist Infrastructure #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
Expand All @@ -29,11 +21,9 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
pip install hatch
- name: Build package
run: python -m build
run: hatch build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
run: |
hatch publish -u "__token__" -a ${{ secrets.PYPI_API_TOKEN }}
22 changes: 18 additions & 4 deletions build_differ.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re
import subprocess
import os
import tarfile
import zipfile
import sys


def get_version():
Expand Down Expand Up @@ -40,10 +40,22 @@ def compress_files(source_dir, target_file):
os.path.join(source_dir, '..')))


def cleanup_old_builds(dist_dir, current_version):
"""
Deletes any build files ending in .zip or .tar.gz in the dist_dir with a different version tag.
"""
for file in os.listdir(dist_dir):
if not file.endswith((f'{current_version}.zip', f'{current_version}.tar.gz', '.gitignore')):
file_path = os.path.join(dist_dir, file)
os.remove(file_path)
print(f"Deleted old build file: {file}")

def main():
version = get_version()
print(f"Version: {version}")

dist_dir = "./src/python_redlines/dist/"

# Build for Linux
print("Building for Linux...")
run_command('dotnet publish ./csproj -c Release -r linux-x64 --self-contained')
Expand All @@ -58,15 +70,17 @@ def main():

# Compress the Linux build
linux_build_dir = './csproj/bin/Release/net8.0/linux-x64'
compress_files(linux_build_dir, f"./dist/linux-x64-{version}.tar.gz")
compress_files(linux_build_dir, f"{dist_dir}/linux-x64-{version}.tar.gz")

# Compress the Windows build
windows_build_dir = './csproj/bin/Release/net8.0/win-x64'
compress_files(windows_build_dir, f"./dist/win-x64-{version}.zip")
compress_files(windows_build_dir, f"{dist_dir}/win-x64-{version}.zip")

# Compress the macOS build
macos_build_dir = './csproj/bin/Release/net8.0/osx-x64'
compress_files(macos_build_dir, f"./dist/osx-x64-{version}.tar.gz")
compress_files(macos_build_dir, f"{dist_dir}/osx-x64-{version}.tar.gz")

cleanup_old_builds(dist_dir, version)

print("Build and compression complete.")

Expand Down
2 changes: 2 additions & 0 deletions dist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Binary file removed dist/linux-x64-0.0.1.tar.gz
Binary file not shown.
Binary file removed dist/osx-x64-0.0.1.tar.gz
Binary file not shown.
Binary file removed dist/win-x64-0.0.1.zip
Binary file not shown.
3 changes: 2 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ with open('/path/to/original.docx', 'rb') as f:
with open('/path/to/modified.docx', 'rb') as f:
modified_bytes = f.read()

# This is a tuple, bytes @ element 0
output = wrapper.run_redlines('AuthorTag', original_bytes, modified_bytes)
```

Expand All @@ -39,5 +40,5 @@ Process or save the output as needed. For example, to save the redline output to

```python
with open('/path/to/redline_output.docx', 'wb') as f:
f.write(output)
f.write(output[0])
```
9 changes: 9 additions & 0 deletions hatch_run_build_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import subprocess
from hatchling.builders.hooks.plugin.interface import BuildHookInterface

class HatchRunBuildHook(BuildHookInterface):
PLUGIN_NAME = 'hatch-run-build'

def initialize(self, version, build_data):
# Run the 'hatch run build' command
subprocess.run(['hatch', 'run', 'build'], check=True)
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ artifacts = [
"*.tar.gz",
]

# Build hook to build the binaries for distribution...
[tool.hatch.build.hooks.custom]
path = "hatch_run_build_hook.py"

[project]
name = "python-redlines"
dynamic = ["version"]
Expand Down
2 changes: 1 addition & 1 deletion src/python_redlines/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present U.N. Owen <void@some.where>
#
# SPDX-License-Identifier: MIT
__version__ = "0.0.3"
__version__ = "0.0.4"
3 changes: 2 additions & 1 deletion src/python_redlines/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*
*
!.gitignore
3 changes: 2 additions & 1 deletion src/python_redlines/dist/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
!*
*
!.gitignore
5 changes: 4 additions & 1 deletion src/python_redlines/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import tempfile
import os
import platform
import logging
import zipfile
import tarfile
from pathlib import Path
from typing import Union, Tuple, Optional

from .__about__ import __version__

logger = logging.getLogger(__name__)


class XmlPowerToolsEngine(object):
def __init__(self):
Expand Down Expand Up @@ -44,7 +47,7 @@ def __extract_binary(self, zip_path: str, target_path: str):
zip_path: str - The path to the zip file
target_path: str - The path to extract the binary to
"""

print(f"")
if zip_path.endswith('.zip'):
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(target_path)
Expand Down