summary refs log tree commit diff
path: root/pkgs/development/tools/poetry2nix/poetry2nix/overrides/shapely-rewrite.py
blob: d3b365459bd3d04a951bfe565dbd51cbad2d079f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Rewrite libc/library path references to Nix store paths
Nixpkgs uses a normal patch for this but we need to be less
sensitive to changes between versions.
"""
from textwrap import dedent
import sys
import ast
import os


with open(sys.argv[1]) as f:
    mod = ast.parse(f.read(), "geos.py")


class LibTransformer(ast.NodeTransformer):
    _lgeos_replaced = False

    def visit_If(self, node):
        if ast.unparse(node).startswith("if sys.platform.startswith('linux')"):
            return ast.parse(
                dedent(
                    """
            free = CDLL(%s).free
            free.argtypes = [c_void_p]
            free.restype = None
            """
                )
                % (lambda x: "'" + x + "'" if x else None)(os.environ.get("GEOS_LIBC"))
            )
        return node

    def visit_Assign(self, node):
        _target = node.targets[0]
        if (
            not self._lgeos_replaced
            and isinstance(_target, ast.Name)
            and _target.id == "_lgeos"
        ):
            self._lgeos_replaced = True
            return ast.parse("_lgeos = CDLL('%s')" % os.environ["GEOS_LIBRARY_PATH"])
        return node


with open(sys.argv[1], "w") as f:
    f.write(ast.unparse(LibTransformer().visit(mod)))