Skip to content

Commit db4567f

Browse files
committed
Replace generated folder with .g extension; save all stubs in SCM
1 parent bfedf26 commit db4567f

File tree

13 files changed

+4949
-28
lines changed

13 files changed

+4949
-28
lines changed

.github/workflows/build.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,16 @@ jobs:
7373
with:
7474
name: package-wheels-${{matrix.os}}
7575
path: ./wheelhouse/*.whl
76+
77+
- uses: tj-actions/verify-changed-files@v20
78+
id: verify-changed-files
79+
if: github.event_name == 'pull_request'
80+
with:
81+
fail-if-changed: true
82+
83+
- uses: parkerbxyz/suggest-changes@v3
84+
if: steps.verify-changed-files.outcome == 'failure'
85+
with:
86+
event: REQUEST_CHANGES
87+
comment: |
88+
Some source files have been changed after building wheels. Consider committing the following changes:

CMakeLists.txt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ install(TARGETS ${extension} LIBRARY DESTINATION . COMPONENT ${extension})
6262
# generate-enums.py ----------------------------------------------------------------------------------------------------
6363
find_package(Python3 COMPONENTS Interpreter REQUIRED)
6464
add_custom_target(_generate_enums
65-
COMMAND "${Python3_EXECUTABLE}" scripts/generate-enums.py
65+
COMMAND "${Python3_EXECUTABLE}" scripts/generate-enums.py --output "src/enums.g.inc"
6666
DEPENDS "scripts/generate-enums.py"
67-
BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/src/generated/enums.inc"
67+
BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/src/enums.g.inc"
6868
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
69-
COMMENT "Generating generated/enums.inc"
69+
COMMENT "Generating src/enums.g.inc"
7070
VERBATIM
7171
)
7272
add_dependencies(${extension} _generate_enums)
@@ -91,18 +91,16 @@ add_custom_command(TARGET ${extension} POST_BUILD
9191
VERBATIM
9292
)
9393

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

107105
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/zint-stubs" DESTINATION . COMPONENT ${extension})
108106
# ----------------------------------------------------------------------------------------------------------------------

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ classifiers = [
3232
]
3333

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

3739
[project.urls]
3840
Documentation = "https://zint-bindings.readthedocs.io/"
@@ -68,7 +70,7 @@ requires = [
6870
"pip >= 24", # For installing script dependencies
6971
# Requirements from src/zint/scripts/generate-stub.requirements.txt
7072
"pybind11-stubgen == 2.5.5",
71-
"typing-extensions >= 4.6",
73+
"typing-extensions >= 4.6; python_version < '3.12'",
7274
"yapf",
7375
]
7476
build-backend = "scikit_build_core.build"

scripts/generate-enums.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Generate src/generated/enums.hpp file. Run this from the root directory."""
22

3+
import argparse
34
import io
45
import json
56
import re
67
import sys
78
from dataclasses import dataclass
8-
from textwrap import dedent
99

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

6868
if suffix is not None:
6969
assert name.endswith(suffix)
70-
name = name[: -len(suffix)]
70+
name = name[:-len(suffix)]
7171

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

108108

109+
def cli():
110+
parser = argparse.ArgumentParser()
111+
parser.add_argument("-o", "--output", default="-", help="output file")
112+
113+
return parser
114+
115+
109116
def main():
110117
with open("external/zint/backend/zint.h", "r", encoding="utf-8") as f:
111118
source = f.read()
@@ -193,11 +200,21 @@ def main():
193200
),
194201
]
195202

203+
result = io.StringIO()
204+
result.write("// Generated by generate-enums.py\n")
205+
for enum in enums:
206+
write_enum_macro(result, enum)
207+
208+
result = result.getvalue()
209+
196210
# Write files ------------------------------------------------------------------------------------------------------
197-
# Write enums.inc
198-
with open("src/generated/enums.inc", "w", encoding="utf-8", newline="\n") as fd:
199-
for enum in enums:
200-
write_enum_macro(fd, enum)
211+
args = cli().parse_args()
212+
213+
if args.output == "-":
214+
sys.stdout.write(result)
215+
else:
216+
with open(args.output, "w", encoding="utf-8", newline="\n") as fp:
217+
fp.write(result)
201218

202219

203220
if __name__ == "__main__":
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pybind11-stubgen == 2.5.5
2-
typing-extensions >= 4.6
2+
typing-extensions >= 4.6; python_version < '3.12'
33
yapf
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Generated by generate-enums.py
12
ENUM_BEGIN(Symbology, int, "enum.Enum", "Values for `Symbol.symbology`")
23
ENUM_VALUE(Symbology, CODE11, 1, "Code 11")
34
ENUM_VALUE(Symbology, C25STANDARD, 2, "2 of 5 Standard (Matrix)")

src/enums.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
} \
99
;
1010

11-
#include "generated/enums.inc"
11+
#include "enums.g.inc"
1212

1313
#undef ENUM_BEGIN
1414
#undef ENUM_VALUE
@@ -20,7 +20,7 @@
2020
#define ENUM_VALUE(enum_name, name, value, docstring) .add(#name, enum_name::name, docstring)
2121
#define ENUM_END(enum_name) ;
2222

23-
#include "generated/enums.inc"
23+
#include "enums.g.inc"
2424

2525
#undef ENUM_BEGIN
2626
#undef ENUM_VALUE

0 commit comments

Comments
 (0)