]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Make the script to build the binary package portable
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 28 Jun 2021 13:32:23 +0000 (14:32 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 30 Jun 2021 00:48:41 +0000 (01:48 +0100)
.github/workflows/packages.yml
tools/build/copy_to_binary.py [new file with mode: 0755]
tools/build/copy_to_binary.sh [deleted file]

index b26e8b3640a6de6fba65d405d9cfab0147cc7c73..62fc1fb20890566a1b2479a548d9bb348a04c609 100644 (file)
@@ -20,7 +20,7 @@ jobs:
         uses: docker/setup-qemu-action@v1
 
       - name: Create the binary package source tree
-        run: ./tools/build/copy_to_binary.sh
+        run: python3 ./tools/build/copy_to_binary.py
 
       - name: Build wheels
         uses: pypa/cibuildwheel@v1.12.0
diff --git a/tools/build/copy_to_binary.py b/tools/build/copy_to_binary.py
new file mode 100755 (executable)
index 0000000..ae8422d
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+# Create the psycopg-binary package by renaming and patching psycopg-c
+
+import os
+import re
+import shutil
+from pathlib import Path
+from typing import Union
+
+curdir = Path(__file__).parent
+pdir = curdir / "../.."
+target = pdir / "psycopg_binary"
+
+if target.exists():
+    raise Exception(f"path {target} already exists")
+
+
+def sed_i(pattern: str, repl: str, filename: Union[str, Path]) -> None:
+    with open(filename, "rb") as f:
+        data = f.read()
+    newdata = re.sub(pattern.encode("utf8"), repl.encode("utf8"), data)
+    if newdata != data:
+        with open(filename, "wb") as f:
+            f.write(newdata)
+
+
+shutil.copytree(pdir / "psycopg_c", target)
+shutil.move(str(target / "psycopg_c"), str(target / "psycopg_binary"))
+sed_i("psycopg-c", "psycopg-binary", target / "setup.cfg")
+sed_i(
+    r"__impl__\s*=.*", '__impl__ = "binary"', target / "psycopg_binary/pq.pyx"
+)
+for dirpath, dirnames, filenames in os.walk(target):
+    for filename in filenames:
+        if os.path.splitext(filename)[1] not in (".pyx", ".pxd", ".py"):
+            continue
+        sed_i(r"\bpsycopg_c\b", "psycopg_binary", Path(dirpath) / filename)
diff --git a/tools/build/copy_to_binary.sh b/tools/build/copy_to_binary.sh
deleted file mode 100755 (executable)
index 147f7ff..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/bin/bash
-
-# Create the psycopg-binary package by renaming and patching psycopg-c
-# This script is designed to run
-
-set -euo pipefail
-set -x
-
-dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-pdir="$( cd "${dir}/../.." && pwd )"
-target="${pdir}/psycopg_binary"
-
-cp -r "${pdir}/psycopg_c" "${target}"
-mv "${target}"/{psycopg_c,psycopg_binary}/
-sed -i 's/psycopg-c/psycopg-binary/' "${target}"/setup.cfg
-sed -i "s/__impl__[[:space:]]*=.*/__impl__ = 'binary'/" \
-    "${target}"/psycopg_binary/pq.pyx
-find "${target}" -name \*.pyx -or -name \*.pxd -or -name \*.py \
-    | xargs sed -i 's/\bpsycopg_c\b/psycopg_binary/'