Skip to content

Commit 0cd7a75

Browse files
authored
Merge pull request #6 from JSv4/JSv4/work-on-build-and-dist-infra
Improve Build and Dist Infrastructure
2 parents 4ee2e99 + 54dd769 commit 0cd7a75

13 files changed

+48
-23
lines changed

.github/workflows/python-publish.yml

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# This workflow will upload a Python Package using Twine when a release is created
2-
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
3-
4-
# This workflow uses actions that are not certified by GitHub.
5-
# They are provided by a third-party and are governed by
6-
# separate terms of service, privacy policy, and support
7-
# documentation.
8-
91
name: Upload Python Package
102

113
on:
@@ -29,11 +21,9 @@ jobs:
2921
- name: Install dependencies
3022
run: |
3123
python -m pip install --upgrade pip
32-
pip install build
24+
pip install hatch
3325
- name: Build package
34-
run: python -m build
26+
run: hatch build
3527
- name: Publish package
36-
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
37-
with:
38-
user: __token__
39-
password: ${{ secrets.PYPI_API_TOKEN }}
28+
run: |
29+
hatch publish -u "__token__" -a ${{ secrets.PYPI_API_TOKEN }}

build_differ.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import re
12
import subprocess
23
import os
34
import tarfile
45
import zipfile
5-
import sys
66

77

88
def get_version():
@@ -40,10 +40,22 @@ def compress_files(source_dir, target_file):
4040
os.path.join(source_dir, '..')))
4141

4242

43+
def cleanup_old_builds(dist_dir, current_version):
44+
"""
45+
Deletes any build files ending in .zip or .tar.gz in the dist_dir with a different version tag.
46+
"""
47+
for file in os.listdir(dist_dir):
48+
if not file.endswith((f'{current_version}.zip', f'{current_version}.tar.gz', '.gitignore')):
49+
file_path = os.path.join(dist_dir, file)
50+
os.remove(file_path)
51+
print(f"Deleted old build file: {file}")
52+
4353
def main():
4454
version = get_version()
4555
print(f"Version: {version}")
4656

57+
dist_dir = "./src/python_redlines/dist/"
58+
4759
# Build for Linux
4860
print("Building for Linux...")
4961
run_command('dotnet publish ./csproj -c Release -r linux-x64 --self-contained')
@@ -58,15 +70,17 @@ def main():
5870

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

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

6779
# Compress the macOS build
6880
macos_build_dir = './csproj/bin/Release/net8.0/osx-x64'
69-
compress_files(macos_build_dir, f"./dist/osx-x64-{version}.tar.gz")
81+
compress_files(macos_build_dir, f"{dist_dir}/osx-x64-{version}.tar.gz")
82+
83+
cleanup_old_builds(dist_dir, version)
7084

7185
print("Build and compression complete.")
7286

dist/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

dist/linux-x64-0.0.1.tar.gz

-65.4 MB
Binary file not shown.

dist/osx-x64-0.0.1.tar.gz

-64.7 MB
Binary file not shown.

dist/win-x64-0.0.1.zip

-67.5 MB
Binary file not shown.

docs/quickstart.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ with open('/path/to/original.docx', 'rb') as f:
2727
with open('/path/to/modified.docx', 'rb') as f:
2828
modified_bytes = f.read()
2929

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

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

4041
```python
4142
with open('/path/to/redline_output.docx', 'wb') as f:
42-
f.write(output)
43+
f.write(output[0])
4344
```

hatch_run_build_hook.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import subprocess
2+
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
3+
4+
class HatchRunBuildHook(BuildHookInterface):
5+
PLUGIN_NAME = 'hatch-run-build'
6+
7+
def initialize(self, version, build_data):
8+
# Run the 'hatch run build' command
9+
subprocess.run(['hatch', 'run', 'build'], check=True)

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ artifacts = [
1111
"*.tar.gz",
1212
]
1313

14+
# Build hook to build the binaries for distribution...
15+
[tool.hatch.build.hooks.custom]
16+
path = "hatch_run_build_hook.py"
17+
1418
[project]
1519
name = "python-redlines"
1620
dynamic = ["version"]

src/python_redlines/__about__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: 2024-present U.N. Owen <void@some.where>
22
#
33
# SPDX-License-Identifier: MIT
4-
__version__ = "0.0.3"
4+
__version__ = "0.0.4"

src/python_redlines/bin/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*
1+
*
2+
!.gitignore

src/python_redlines/dist/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
!*
1+
*
2+
!.gitignore

src/python_redlines/engines.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
import tempfile
33
import os
44
import platform
5+
import logging
56
import zipfile
67
import tarfile
78
from pathlib import Path
89
from typing import Union, Tuple, Optional
910

1011
from .__about__ import __version__
1112

13+
logger = logging.getLogger(__name__)
14+
1215

1316
class XmlPowerToolsEngine(object):
1417
def __init__(self):
@@ -44,7 +47,7 @@ def __extract_binary(self, zip_path: str, target_path: str):
4447
zip_path: str - The path to the zip file
4548
target_path: str - The path to extract the binary to
4649
"""
47-
50+
print(f"")
4851
if zip_path.endswith('.zip'):
4952
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
5053
zip_ref.extractall(target_path)

0 commit comments

Comments
 (0)