mpcdf_common.py 9.77 KB
Newer Older
1
2
from __future__ import print_function

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
3
__all__ = ["valid_mpi", "valid_cuda", "project_meta", "get_attribute", "get_attribute_values", "set_attribute", "mpcdf_enable_repositories", "mpcdf_setup_repos"]
4
5
6
7

import osc
import osc.conf
import osc.core
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
8
import osc.oscerr
9

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
10
from functools import partial
11
12
13
14
15
16
17
18
19
from xml.etree import ElementTree


def valid_mpi(compiler, mpi):
    """
    It might be possible to use Intel MPI libararies and compilers from
    different Parallel Studio packages, but I currently do not want to support
    it
    """
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
20
21
22
    if compiler == "intel_18_0":
        if mpi.startswith("impi"):
            return mpi == "impi_2018_1"
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    if compiler == "intel_17_0":
        if mpi.startswith("impi"):
            return mpi == "impi_2017_3"
    if compiler == "intel_16_0":
        if mpi.startswith("impi"):
            return mpi == "impi_5_1_3"
    return True


def valid_cuda(cuda, compiler):
    """
    The CUDA distribution only works with certain gcc versions,
    this little function takes care that only supported combinations
    are allowed
    """
    if cuda == "cuda_8":
        return compiler == "gcc_5"
    return False


Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
43
44
45
def project_meta(api_url, project):
    return ElementTree.fromstringlist(osc.core.show_project_meta(api_url, project))

46

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
47
def get_attribute(api_url, project, package, attribute, with_project=False):
48
49
    attribute_meta = osc.core.show_attribute_meta(api_url, project, package, None, attribute, False, with_project)
    if attribute_meta is None:
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
50
        raise osc.oscerr.APIError("Cannot fetch value for attribute '{0}' from {1}".format(attribute, (project, package)))
51
52
53
54

    root = ElementTree.fromstringlist(attribute_meta)
    attribute = root.find("./attribute")
    if attribute is not None:
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
55
        return root
56
57
58
    else:
        raise Exception("Attribute not set")

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
59

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
60
61
62
63
64
65
66
def get_attribute_values(api_url, project, package, attribute, with_project=False):
    root = get_attribute(api_url, project, package, attribute, with_project)
    attribute = root.find("./attribute")
    return list(value.text for value in attribute.findall("./value"))


def set_attribute(api_url, path, attribute):
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
67
    attr_meta = ElementTree.tostring(attribute, encoding=osc.core.ET_ENCODING)
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
68
69

    url = osc.core.makeurl(api_url, ["source"] + list(path) + ["_attribute"])
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
70
    resp = ElementTree.fromstringlist(osc.core.streamfile(url, osc.core.http_POST, data=attr_meta))
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
71
    if resp.find("./summary").text != "Ok":
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
72
        raise osc.oscerr.APIError("Could not store attribute")
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
73

74
75
76
77
78

def mpcdf_enable_repositories(api_url, project, package, verbose=False):

    root = ElementTree.fromstringlist(osc.core.show_package_meta(api_url, project, package))
    build = root.find("./build")
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
79
80
81
82
83
    if build is None:
        build = ElementTree.SubElement(root, "build")
    else:
        for enable in build.findall("./enable"):
            build.remove(enable)
84

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
85
86
87
    compilers = get_attribute_values(api_url, project, package, "MPCDF:compiler_modules", with_project=True)
    mpis = get_attribute_values(api_url, project, package, "MPCDF:mpi_modules", with_project=True)
    cudas = get_attribute_values(api_url, project, package, "MPCDF:cuda_modules", with_project=True)
88
89
90
91
92
93
94
95
96
97

    def enable(name):
        node = ElementTree.Element("enable")
        node.set("repository", name)
        node.tail = "\n    "
        build.insert(0, node)
        if verbose:
            print("Enabling", name)

    try:
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
98
        enable_repos = get_attribute_values(api_url, project, package, "MPCDF:enable_repositories")
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
99
    except Exception:
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        if verbose:
            print("Warning: Could not get attribute MPCDF:enable_repositories for package {0}".format(package))
        return False

    for flag in enable_repos:

        if flag == "system":
            enable("System")

        if flag == "compilers":
            for compiler in compilers:
                enable(compiler)

        if flag == "mpi":
            for compiler in compilers:
                for mpi in mpis:
                    if valid_mpi(compiler, mpi):
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
117
                        enable(mpi + "_" + compiler)
118
119
120
121
122
123
124
125
126
127
128
129
130

        if flag == "cuda":
            for cuda in cudas:
                for compiler in compilers:
                    if valid_cuda(cuda, compiler):
                        enable(cuda + "_" + compiler)

        if flag == "cuda_mpi":
            for cuda in cudas:
                for compiler in compilers:
                    if valid_cuda(cuda, compiler):
                        for mpi in mpis:
                            if valid_mpi(compiler, mpi):
131
                                enable(cuda + "_" + mpi + "_" + compiler)
132

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
133
134
    if len(build.getchildren()) > 0:
        build.getchildren()[-1].tail = "\n  "
135

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
136
    pkg_meta = ElementTree.tostring(root, encoding=osc.core.ET_ENCODING)
137
138
139

    osc.core.edit_meta("pkg", (project, package), data=pkg_meta)
    return True
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
140
141


Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
142
143
144
145
146
147
148
149
150
def mpcdf_setup_repos(api_url, project, distribution, parent=None, packages=None, dry_run=False):
    if parent:
        for attribute in ["MPCDF:compiler_modules", "MPCDF:mpi_modules", "MPCDF:cuda_modules"]:
            try:
                get_attribute_values(api_url, project, None, attribute)
            except Exception:
                print("Copying attribute '{0}' from parent project".format(attribute))
                set_attribute(api_url, (project,), get_attribute(api_url, parent, None, attribute))

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
151
152
153
154
155
156
157
    compilers = list(get_attribute_values(api_url, project, None, "MPCDF:compiler_modules"))
    mpis = list(get_attribute_values(api_url, project, None, "MPCDF:mpi_modules"))
    cudas = list(get_attribute_values(api_url, project, None, "MPCDF:cuda_modules"))

    distributions = project_meta(api_url, "distributions")
    dist_repo = distributions.find('./repository[@name="{0}"]'.format(distribution))
    if dist_repo is None:
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
158
        raise osc.oscerr.WrongArgs("No repository '{0}' is defined in project 'distributions' on the server".format(distribution))
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    architectures = list(arch.text for arch in dist_repo.findall("./arch"))

    root = project_meta(api_url, project)

    prjconf = osc.core.show_project_conf(api_url, project)
    start_marker = "# Autogenerated by osc mpcdf_setup_repos, do not edit till end of section\n"
    end_marker = "# End of autogenerated section\n"

    try:
        start = prjconf.index(start_marker)
        end = prjconf.index(end_marker)
    except ValueError:
        start = None
        end = len(prjconf)

    prjconf_head = "".join(prjconf[:start])
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
175
    prjconf_tail = "".join(prjconf[end + 1:])
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
176
177
178
179
180
181
182
183
184
185
186
187
188
    prjconf = [start_marker]

    # Remove existing repositories
    for repo in root.findall("./repository"):
        root.remove(repo)

    def repo(name, *dependencies, **kwargs):
        is_mpi = kwargs.pop("mpi", False)
        is_cuda = kwargs.pop("cuda", False)
        if kwargs:
            raise Exception("Invalid argument")
        r = ElementTree.SubElement(root, "repository")
        r.set("name", name)
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
189
190
        r.text = "\n    "

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
191
192
193
194
        def path(project, repo):
            p = ElementTree.SubElement(r, "path")
            p.set("project", project)
            p.set("repository", repo)
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
195
            p.tail = "\n    "
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

        if parent:
            path(parent, name)
        for dep_project, dep_repo in dependencies:
            path(dep_project, dep_repo)

        for arch in architectures:
            a = ElementTree.SubElement(r, "arch")
            a.text = arch
            a.tail = "\n    "
        a.tail = "\n  "
        r.tail = "\n  "

        prjconf.append("%if %_repository == {0}".format(name))
        prjconf.append("Macros:")
        prjconf.append("%is_mpi_repository {0}".format(1 if is_mpi else 0))
        prjconf.append("%is_cuda_repository {0}".format(1 if is_cuda else 0))
        prjconf.append(":Macros")
        prjconf.append("%endif")
        prjconf.append("")

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
217
218
219
220
    if parent:
        repo("System")
    else:
        repo("System", ("distributions", distribution))
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

    for compiler in compilers:
        repo(compiler, (project, "System"))

        for mpi in filter(partial(valid_mpi, compiler), mpis):
            repo(mpi + "_" + compiler, (project, compiler), mpi=True)

    for cuda in cudas:
        for compiler in filter(partial(valid_cuda, cuda), compilers):
            repo(cuda + "_" + compiler, (project, compiler), cuda=True)
            for mpi in filter(partial(valid_mpi, compiler), mpis):
                repo(cuda + "_" + mpi + "_" + compiler,
                     (project, cuda + "_" + compiler),
                     (project, mpi + "_" + compiler),
                     mpi=True, cuda=True)

    # Remove build configuration
    build = root.find("./build")
    if build is None:
        build = ElementTree.Element("build")
        build.text = "\n    "
        disable = ElementTree.SubElement(build, "disable")
        disable.tail = "\n  "
        build.tail = "\n  "
        root.insert(list(root).index(root.find("./repository")), build)

    root.getchildren()[-1].tail = "\n"
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
248
    prj = ElementTree.tostring(root, encoding=osc.core.ET_ENCODING)
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
249
250
251
252
253
254
255
256
257
258

    prjconf.append(end_marker)
    prjconf = prjconf_head + "\n".join(prjconf) + prjconf_tail

    if dry_run:
        print("osc meta prj {0} -F - <<EOF\n{1}\nEOF\n".format(project, prj))
        print("osc meta prjconf {0} -F - <<EOF\n{1}\nEOF\n".format(project, prjconf))
    else:
        # First set-up the <enable/> flags, that way no
        # spurious builds are launched
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
259
260
261
        if packages is None:
            packages = osc.core.meta_get_packagelist(api_url, project)
        for package in packages:
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
262
263
264
265
266
267
268
269
270
            print("Updating repositories for", package)
            if not mpcdf_enable_repositories(api_url, project, package):
                print("ATTENTION: Not changing unmanaged package {0}".format(package))

        # Update repositories
        print("Updating prj meta")
        osc.core.edit_meta("prj", project, data=prj)
        print("Updating prjconf meta")
        osc.core.edit_meta("prjconf", project, data=prjconf)