Skip to content
Open
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
42 changes: 42 additions & 0 deletions .github/actions/ensure-no-diff/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Ensure no changes to tracked files
description: Detect any changes to tracked files and fail the forkflow.
inputs:
pr-comment-tag:
description: |
If specified, post a comment with changes (only on pull requests).
Comments matching this tag will be edited or deleted on subsequent runs.
required: false
default: ""
comment-header:
description: Header for the PR comment
required: false
default: "Tracked files have changed:"
comment-footer:
description: Footer for the PR comment
required: false
default: ""
runs:
using: composite
steps:
- name: git diff
id: git-diff
uses: mathiasvr/[email protected]
with:
shell: bash
run: git diff --diff-algorithm=histogram --exit-code

- name: Update PR comment
if: >-
failure() &&
steps.git-diff.conclusion == 'failure' &&
github.event_name == 'pull_request' &&
inputs.pr-comment-tag != ''
uses: thollander/actions-comment-pull-request@v3
with:
comment-tag: ${{ inputs.comment-tag }}
mode: recreate
message: |
${{ inputs.comment-header }}
```diff
${{ steps.git-diff.outputs.stdout }}```
${{ inputs.comment-footer }}
23 changes: 23 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ on:
push:
branches: [main]

permissions:
pull-requests: write

jobs:
sdist:
name: Source distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
with:
# Required for dynamic version
fetch-depth: 0
fetch-tags: true

- uses: actions/setup-python@v6
with:
python-version-file: pyproject.toml
Expand All @@ -33,6 +41,11 @@ jobs:
name: package-sdist
path: dist/*.tar.gz

- name: Ensure no changes to tracked files
uses: ./.github/actions/ensure-no-diff
with:
pr-comment-tag: ${{ github.job }}

wheels:
name: Binary wheels
runs-on: ${{ matrix.os }}
Expand All @@ -47,6 +60,11 @@ jobs:

steps:
- uses: actions/checkout@v5
with:
# Required for dynamic version
fetch-depth: 0
fetch-tags: true

- uses: actions/setup-python@v6
with:
python-version-file: pyproject.toml
Expand All @@ -73,3 +91,8 @@ jobs:
with:
name: package-wheels-${{matrix.os}}
path: ./wheelhouse/*.whl

- name: Ensure no changes to tracked files
uses: ./.github/actions/ensure-no-diff
with:
pr-comment-tag: ${{ github.job }}/${{matrix.os}}
28 changes: 13 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ install(TARGETS ${extension} LIBRARY DESTINATION . COMPONENT ${extension})
# generate-enums.py ----------------------------------------------------------------------------------------------------
find_package(Python3 COMPONENTS Interpreter REQUIRED)
add_custom_target(_generate_enums
COMMAND "${Python3_EXECUTABLE}" scripts/generate-enums.py
COMMAND "${Python3_EXECUTABLE}" scripts/generate-enums.py --output "src/enums.g.inc"
DEPENDS "scripts/generate-enums.py"
BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/src/generated/enums.inc"
BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/src/enums.g.inc"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "Generating generated/enums.inc"
COMMENT "Generating src/enums.g.inc"
VERBATIM
)
add_dependencies(${extension} _generate_enums)
Expand All @@ -91,18 +91,16 @@ add_custom_command(TARGET ${extension} POST_BUILD
VERBATIM
)

if(Python3_VERSION_MINOR STREQUAL 13) # Update this when Python 3.14 is released
# Copy stubs into the source directory so they are visible in source control and PRs
add_custom_command(TARGET ${extension} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_BINARY_DIR}/zint-stubs/__init__.pyi"
"src/generated/stubs.pyi"
BYPRODUCTS "src/generated/stubs.pyi"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "Updating reference stub file"
VERBATIM
)
endif()
# Copy stubs into the source directory so they are visible in source control and PRs
add_custom_command(TARGET ${extension} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_BINARY_DIR}/zint-stubs/__init__.pyi"
"src/stubs/stub-py3${Python3_VERSION_MINOR}.g.pyi"
BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/src/stubs/stub-py3${Python3_VERSION_MINOR}.g.pyi"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "Updating reference stub file"
VERBATIM
)

install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/zint-stubs" DESTINATION . COMPONENT ${extension})
# ----------------------------------------------------------------------------------------------------------------------
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ classifiers = [
]

requires-python = ">=3.9"
dependencies = []
dependencies = [
"typing-extensions >= 4.6; python_version < '3.12'", # For the stub file
]

[project.urls]
Documentation = "https://zint-bindings.readthedocs.io/"
Expand Down Expand Up @@ -68,7 +70,7 @@ requires = [
"pip >= 24", # For installing script dependencies
# Requirements from src/zint/scripts/generate-stub.requirements.txt
"pybind11-stubgen == 2.5.5",
"typing-extensions >= 4.6",
"typing-extensions >= 4.6; python_version < '3.12'",
"yapf",
]
build-backend = "scikit_build_core.build"
Expand Down
31 changes: 24 additions & 7 deletions scripts/generate-enums.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Generate src/generated/enums.hpp file. Run this from the root directory."""

import argparse
import io
import json
import re
import sys
from dataclasses import dataclass
from textwrap import dedent

re_base_define = r"^#define (\w+) +((?:0x)?\d+)"
"""`#define BARCODE_CODE128 0x01`"""
Expand Down Expand Up @@ -63,11 +63,11 @@ def parse_enum_values(
name = m[1]
if prefix is not None:
assert name.startswith(prefix)
name = name[len(prefix) :]
name = name[len(prefix):]

if suffix is not None:
assert name.endswith(suffix)
name = name[: -len(suffix)]
name = name[:-len(suffix)]

# Macro value
value = m[2]
Expand Down Expand Up @@ -106,6 +106,13 @@ def write_enum_macro(fd, enum: EnumInfo):
fd.write(f"ENUM_END({enum.name})\n\n")


def cli():
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", default="-", help="output file")

return parser


def main():
with open("external/zint/backend/zint.h", "r", encoding="utf-8") as f:
source = f.read()
Expand Down Expand Up @@ -193,11 +200,21 @@ def main():
),
]

result = io.StringIO()
result.write("// Generated by generate-enums.py\n")
for enum in enums:
write_enum_macro(result, enum)

result = result.getvalue()

# Write files ------------------------------------------------------------------------------------------------------
# Write enums.inc
with open("src/generated/enums.inc", "w", encoding="utf-8", newline="\n") as fd:
for enum in enums:
write_enum_macro(fd, enum)
args = cli().parse_args()

if args.output == "-":
sys.stdout.write(result)
else:
with open(args.output, "w", encoding="utf-8", newline="\n") as fp:
fp.write(result)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-stub.requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pybind11-stubgen == 2.5.5
typing-extensions >= 4.6
typing-extensions >= 4.6; python_version < '3.12'
yapf
1 change: 1 addition & 0 deletions src/generated/enums.inc → src/enums.g.inc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Generated by generate-enums.py
ENUM_BEGIN(Symbology, int, "enum.Enum", "Values for `Symbol.symbology`")
ENUM_VALUE(Symbology, CODE11, 1, "Code 11")
ENUM_VALUE(Symbology, C25STANDARD, 2, "2 of 5 Standard (Matrix)")
Expand Down
4 changes: 2 additions & 2 deletions src/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
} \
;

#include "generated/enums.inc"
#include "enums.g.inc"

#undef ENUM_BEGIN
#undef ENUM_VALUE
Expand All @@ -20,7 +20,7 @@
#define ENUM_VALUE(enum_name, name, value, docstring) .add(#name, enum_name::name, docstring)
#define ENUM_END(enum_name) ;

#include "generated/enums.inc"
#include "enums.g.inc"

#undef ENUM_BEGIN
#undef ENUM_VALUE
Expand Down
Loading
Loading