File: __init__.py

package info (click to toggle)
dune-common 2.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: cpp: 52,256; python: 3,979; sh: 1,658; makefile: 17
file content (147 lines) | stat: -rw-r--r-- 5,134 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception

import os, sys
import logging

import dune.common.module as moduleInfo
from .exceptions import CompileError, ConfigurationError
from . import cmakebuilder as builderModule

logger = logging.getLogger(__name__)

def _constructBuilder():
    from .cmakebuilder import Builder, MakefileBuilder

    env_force = os.environ.get('DUNE_FORCE_BUILD', 'FALSE').upper()
    env_save  = os.environ.get('DUNE_SAVE_BUILD' , 'FALSE').upper()

    use_cmake_builder = os.environ.get('DUNE_PY_USE_CMAKEBUILDER' , 'FALSE').upper()

    if use_cmake_builder in ('1', 'TRUE'):
        # old cmake based builder
        return Builder(force=(env_force in ('1', 'TRUE')), saveOutput=env_save)
    else:
        # new Makefile based builder
        return MakefileBuilder(force=(env_force in ('1', 'TRUE')), saveOutput=env_save)

# builder object that builds the autogenerated Python modules
# (initialize is called on first autogenerated module to be loaded)
builder = _constructBuilder()

def initializeDunePy():
    """
    This function will call the necessary method on builder to ensure dune-py exists.
    """
    # initialize builder which will create dune-py if non-existent
    builder.initialize()

def setNoDependencyCheck():
    logger.debug("Switching off dependency check - modules will always be compiled")
    builderModule.noDepCheck = True
def setDependencyCheck():
    logger.debug("Switching on dependency check")
    builderModule.noDepCheck = False
def setFlags(flags="-g",noChecks=None):
    logger.debug("Using compile flags '"+flags+"'")
    builderModule.cxxFlags = flags
    if noChecks is True:
        setNoDependencyCheck()
    elif noChecks is False:
        setDependencyCheck()
def addToFlags(pre="",post="",noChecks=None):
    setFlags(pre+" " + moduleInfo.getCXXFlags() + " "+post, noChecks)

def unsetFlags(noChecks=None):
    logger.debug("Using compile flags from configuration of dune-py")
    builderModule.cxxFlags = None
    if noChecks is True:
        setNoDependencyCheck()
    elif noChecks is False:
        setDependencyCheck()
def reset():
    unsetFlags()
    setDependencyCheck()

def path(f):
    return os.path.dirname(os.path.realpath(f))+"/"

class Constructor(object):
    def __init__(self, args, body=None, extra=None):
        self.args = args
        self.body = body
        self.extra = [] if extra is None else extra

    def register(self, cls="cls"):
        if self.body is None:
            return cls + ".def( pybind11::init( " + self.args + " )" + "".join(", " + e for e in self.extra) + " );\n"
        if self.args:
            source = cls + ".def( pybind11::init( [] ( " + ", ".join(self.args) + " ) {"
        else:
            source = cls + ".def( pybind11::init( [] () {"
        source += "\n    ".join(self.body)
        source += "\n  } )" + "".join(", " + e for e in self.extra) + " );\n"
        return source

    def __str__(self):
        return self.register()


class Method(object):
    def __init__(self, name, args, body=None, extra=None):
        self.name = name
        self.args = args
        self.body = body
        if extra is None:
            self.extra = []
        else:
            self.extra = extra

    def register(self, cls="cls"):
        if self.body is None:
            return cls + ".def( \"" + self.name + "\", " + self.args + "".join(", " + e for e in self.extra) + " );\n"
        if self.args:
            source = cls + ".def(\"" + self.name + "\", [] ( " + ", ".join(self.args) + " ) {"
        else:
            source = cls + ".def( \"" + self.name + "\", [] () {"
        source += "\n    ".join(self.body)
        source += "\n  } " + "".join(", " + e for e in self.extra) + " );\n"
        return source

    def __str__(self):
        return self.register()

class Pickler(object):
    def __init__(self, getterBody, setterBody,
                 setterArgs="pybind11::tuple t",
                 extra=["pybind11::keep_alive<1,2>()","pybind11::prepend()"]):
        self.getterBody = getterBody
        self.setterBody = setterBody
        self.setterArgs = setterArgs
        if extra is None:
            self.extra = []
        else:
            self.extra = extra

    def register(self, cls="cls"):
        source = cls + ".def( pybind11::pickle(\n";
        source += "  [](const pybind11::object &self) {\n";
        source += self.getterBody + "}\n"
        source += ", [](" + self.setterArgs + ") {\n";
        source += self.setterBody + "}\n"
        source += ")" + "".join(", " + e for e in self.extra) + " );\n"
        return source

    def __str__(self):
        return self.register()

def requiredModules(outFile=None):
    path = "dune.generated."
    mods = [m[len(path):] for m in sys.modules.keys() if path in m]
    if outFile is not None:
        try:
            with open(outFile, "w") as f:
                print("\n".join(mods), file=f)
        except TypeError:
            print("\n".join(mods), file=outFile)
    return mods