adamreeve commented on code in PR #41721:
URL: https://github.com/apache/arrow/pull/41721#discussion_r1607262484


##########
c_glib/tools/gen-version-header.py:
##########
@@ -0,0 +1,150 @@
+#!/usr/bin/env python3
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+import argparse
+from io import TextIOBase
+from pathlib import Path
+
+
+def main():
+    parser = argparse.ArgumentParser(
+            description="Generate C header with version macros")
+    parser.add_argument(
+            "--library",
+            required=True,
+            help="The library name to use in macro prefixes")
+    parser.add_argument(
+            "--version",
+            required=True,
+            help="The library version number")
+    parser.add_argument(
+            "--input",
+            type=Path,
+            required=True,
+            help="Path to the input template file")
+    parser.add_argument(
+            "--output",
+            type=Path,
+            required=True,
+            help="Path to the output file to generate")
+    parser.add_argument(
+            "--version-library",
+            default="GARROW",
+            help="The library name prefix to use in MIN_REQUIRED and 
MAX_ALLOWED checks")
+
+    args = parser.parse_args()
+    with open(args.input, "r", encoding="utf-8") as input_file, \
+            open(args.output, "w", encoding="utf-8") as output_file:
+        write_header(input_file, output_file, args.library, args.version, 
args.version_library)
+
+
+def write_header(
+        input_file: TextIOBase,
+        output_file: TextIOBase,
+        library_name: str,
+        version: str,
+        version_library: str):
+    if "-" in version:
+        version, version_tag = version.split("-")
+    else:
+        version_tag = ""
+    version_major, version_minor, version_micro = [int(v) for v in 
version.split(".")]
+
+    availability_macros = generate_availability_macros(library_name, 
version_library)
+
+    replacements = {
+            "VERSION_MAJOR": str(version_major),
+            "VERSION_MINOR": str(version_minor),
+            "VERSION_MICRO": str(version_micro),
+            "VERSION_TAG": version_tag,
+            "AVAILABILITY_MACROS": availability_macros,
+    }
+
+    replacements = dict(
+            (f"@{key}@", replacement) for (key, replacement) in 
replacements.items())
+
+    for line in input_file:
+        if "@" in line:
+            for key, replacement in replacements.items():
+                line = line.replace(key, replacement)
+        output_file.write(line)

Review Comment:
   :+1: that's cool, I didn't know `re.sub` could take a function.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to