aboutsummaryrefslogtreecommitdiff
blob: 9e64e4e9339c9b4c5d8133537ef41503796a620d (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# -*- coding: UTF-8 -*-
# Copyright 2004-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from .OutputFormatter import *
from .Package import *
from .Virtual import *
from .VM import *
from .Errors import *
from itertools import chain

import sys
import re
import os

from os.path import basename, dirname
from glob import glob


class EnvironmentManager(object):
    """This is the central class, which manages all information from the 'environment'"""

    def __init__(self, root='', eprefix=''):
        self.all_packages_loaded = False
        self.packages = {}
        self.virtuals = {}
        self.virtuals_pref = None
        self.virtual_machines = None
        self.active_vm = None

        self.eprefix = eprefix
        self.eroot = root + eprefix

        # Location of the vm env files
        self.vms_path = self.eroot + '/usr/share/java-config-2/vm'
        # Location of the package env files to load
        self.pkg_path = self.eroot + '/usr/share/%s/package.env'
        self.virtual_path = self.eroot + '/usr/share/java-config-2/virtuals/'

        self.system_config_path = self.eroot + "/etc/java-config-2/"

    def load_vms(self):
        """Load all the vm files, and check for correctness"""
        self.virtual_machines = {}

        if os.path.isdir(self.vms_path):
            count = 1
            filelist = os.listdir(self.vms_path)
            filelist.sort()
            for file in filelist:
                conf = os.path.join(self.vms_path,file)
                vm = None

                try:
                    vm = VM(conf)
                except (InvalidConfigError, PermissionError):
                    continue
                except InvalidVMError as ex:
                    printer = OutputFormatter()
                    printer._printAlert("Invalid vm configuration file found: %s\nJava-config 2 requires some new variables, please update all your jdk/jre:  file\n(%s)" % ( conf, ex ))
                    continue

                self.virtual_machines[count] = vm
                count += 1

    def load_package(self, name):
        try:
            name = name.replace(':', '-')
            pkg = Package(name, sorted (glob (self.pkg_path % name ), reverse=True)[0])
            self.packages[name] = pkg
            return pkg
        except (IndexError, InvalidConfigError):
            try:
                #Try load Virtual instead of Package.
                pkg = Virtual( name, self, self.virtual_path + name )
                self.packages[name] = pkg
                self.virtuals[name] = pkg
                return pkg
            except InvalidConfigError:
                raise UnexistingPackageError(name)

    def load_packages(self):
        for package in iter(glob(self.pkg_path % "*" )):
            name = basename(dirname(package))
            if name in self.packages:
                continue
            self.packages[name] = Package(name, package)

        self.all_packages_loaded = True

        for virtual in iter(glob(self.virtual_path + '*')):
            virt = Virtual(basename(virtual), self, virtual)
            self.packages[virt.name()] = virt
            self.virtuals[virt.name()] = virt

    def get_virtuals_pref(self):
        if self.virtuals_pref is None:
            self.load_virtuals_pref()
        return self.virtuals_pref

    def load_virtuals_pref(self):
        self.virtuals_pref = EnvFileParser(self.system_config_path + "virtuals")

    def load_active_vm(self):
        vm_name = os.getenv("GENTOO_VM")
        if vm_name:
            vm = self.get_vm(vm_name)
            if vm:
                self.active_vm = vm
                return vm

        for link in self.vm_links():
            if os.path.islink(link):
                vm_name = basename(os.readlink(link))
                vm = self.get_vm(vm_name)
                if vm:
                    self.active_vm = vm
                    return vm
        raise InvalidVMError("Unable to determine valid Java VM!")

    def set_active_vm(self, vm):
        self.active_vm = vm

    def get_active_vm(self):
        if self.active_vm is None:
            self.load_active_vm()
        return self.active_vm

    def get_virtual_machines(self):
        if self.virtual_machines is None:
            self.load_vms()
        return self.virtual_machines

    def find_vm(self, name):
        found = []
        for id, vm in self.get_virtual_machines().items():
            # match either exact given string or the unversioned part - bug #288695
            if not name or len(name) == 0:
                found.append(vm)
            elif vm.name() == name:
                found.append(vm)
            elif vm.name() == (name + "-" + vm.version()):
                found.append(vm)
        return found

    def get_package(self, pkgname):
        try:
            return self.packages[pkgname]
        except KeyError:
            if not self.all_packages_loaded:
                return self.load_package(pkgname)

    def get_packages(self):
        """
        Returns a dictionary of Packages indexed by their names.
        For java-config-3 we probably want to change this to return
        the list of packages directly.
        """
        if not self.all_packages_loaded:
            self.load_packages()
        return self.packages

    def get_virtuals(self):
        if self.virtuals is None:
            self.load_packages()
        return self.virtuals

    def get_virtual(self, virtname):
        return self.get_package(virtname)

    def query_packages(self, packages, query):
        results = []

        for package in packages:
            pkg = self.get_package(package)
            if pkg:
                value = pkg.query(query)
                if value:
                    results.append(value)
            else:
                raise UnexistingPackageError(package)

        return results

    def get_vm(self, machine):
        vm_list = self.get_virtual_machines()
        selected = None

        for count in iter(vm_list):
            vm = vm_list[count]

            if str(machine).isdigit():
                if int(machine) is count:
                    return vm
            else:
                # Check if the vm is specified via env file
                if machine == vm.filename():
                    return vm

                # Check if the vm is specified by name
                if machine == vm.name():
                    return vm

                # Check if the vm is specified via JAVA_HOME
                if machine == vm.query('JAVA_HOME'):
                    return vm

                # Check if vm is specified by partial name
                if vm.name().startswith(machine):
                    selected = vm

        if selected:
            return selected
        else:
            return None

    def create_env_entry(self, vm, stream, render="%s=%s\n"):
        stream.write("# Autogenerated by java-config\n")
        stream.write("# Java Virtual Machine: %s\n\n" % vm.query('VERSION'))

        try:
            ENV_VARS = vm.query('ENV_VARS')
            for (item, value) in vm.get_config().items():
                if item in ENV_VARS:
                    stream.write(render % (item, value))
        except IOError:
            raise PermissionError
        except EnvironmentUndefinedError:
            raise EnvironmentUndefinedError

    def vm_links(self):
        # Don't try to use user-vm if HOME is undefined
        if os.environ.get('HOME') == None:
            return [ self.system_vm_link() ]
        else:
            return [ self.user_vm_link(), self.system_vm_link() ]

    def user_vm_link(self):
        return  os.path.join(os.environ.get('HOME'), '.gentoo' + self.eprefix + '/java-config-2/current-user-vm')

    def system_vm_link(self):
        return self.eroot + '/etc/java-config-2/current-system-vm'

    def system_vm_name(self):
        link = self.system_vm_link()
        if os.path.islink(link):
            return basename(os.readlink(link))
        else:
            return None

    def add_path_elements(self, elements, path):
        if elements:
            for p in elements.split(':'):
                if p != '' and p not in path:
                    path.append(p)

    def build_path(self, pkgs, query):
        path = []
        for lpath in self.query_packages(pkgs, query):
            self.add_path_elements(lpath, path)

        return path

    def get_pkg_deps(self, pkg):
        """
        Returns list of package's deps and optional deps.
        Filters out optional deps that are not present.
        """
        deps = []
        if hasattr(pkg, 'get_packages') and pkg.use_all_available():
            vps = pkg.get_packages()
            for vp in vps:
                try:
                    vp_pkg = self.get_package(vp)
                    vp_deps = self.get_pkg_deps(vp_pkg)
                    for dep in vp_deps:
                        deps.append(dep)
                except UnexistingPackageError:
                    continue
        else:
            deps = pkg.deps();
            for opt_dep in pkg.opt_deps():
                try:
                    self.get_package(opt_dep[-1])
                    deps.append(opt_dep)
                except UnexistingPackageError:
                    continue
        return deps

    def add_dep_classpath(self, pkg, dep, classpath):
        pkg_cp = pkg.classpath()
        if pkg_cp:
            if not dep or len(dep) == 1:
                self.add_path_elements(pkg_cp, classpath)
            else:
                for cp in pkg_cp.split(':'):
                    if basename(cp) == dep[0] and cp not in classpath:
                        classpath.append(cp)

    def build_dep_path(self, pkgs, query, missing_deps):
        path = []

        unresolved = set()
        resolved = set()

        for p in pkgs[:]:
            pkg = self.get_package(p)
            if pkg:
                pkgs.remove(p)
                lpath = pkg.query(query)
                self.add_path_elements(lpath, path)
                unresolved.add(pkg)

        while len(unresolved) > 0:
            pkg = unresolved.pop()
            resolved.add(pkg)

            if query != "CLASSPATH":
                lpath = pkg.query(query)
                self.add_path_elements(lpath, path)
            for dep in self.get_pkg_deps(pkg):
                p = self.get_package(dep[-1])

                if p:
                    if p not in resolved:
                        unresolved.add(p)
                    if query == "CLASSPATH":
                        self.add_dep_classpath(p, dep, path)
                else:
                    missing_deps.add(dep[-1])

        return path

    def add_pkg_env_vars(self, pkg, env):
        """
        Adds variables declared in `pkg`'s package.env via ENV_VARS
        into the dictionary `env`
        """
        env_vars = pkg.query("ENV_VARS")
        if (env_vars):
            for var in env_vars.split(' '):
                val = pkg.query(var)
                assert val
                if (var not in env):
                    env[var] = val

    def build_dep_env_vars(self, pkgs, missing_deps):
        """
        Returns a dictionary of variables declared via ENV_VARS in
        package.env of all packages in list `pkgs` and all dependencies.
        Encountered missing dependencies are recorded in `missing_deps`.
        """
        env = {}

        unresolved = set()
        resolved = set()

        for p in pkgs:
            pkg = self.get_package(p)
            if pkg:
                if hasattr(pkg, 'is_vm') and pkg.is_vm():
                    continue
                pkgs.remove(p)
                unresolved.add(pkg)

        while len(unresolved) > 0:
            pkg = unresolved.pop()
            resolved.add(pkg)

            self.add_pkg_env_vars(pkg, env)

            for dep in self.get_pkg_deps(pkg):
                p = self.get_package(dep[-1])

                if p:
                    if p not in resolved:
                        if hasattr(p, 'is_vm') and p.is_vm():
                            continue
                        unresolved.add(p)
                else:
                    missing_deps.add(dep[-1])
        return env

    def have_provider(self, virtuals, virtualMachine, versionManager):
        result=True
        storeVM = self.get_active_vm()
        self.set_active_vm(virtualMachine)
        try:
            for virtualKey in virtuals.split():
                if self.get_package(virtualKey):
                    try:
                        self.get_package(virtualKey).get_provider().classpath()
                        result= (result and True)
                        continue
                    except AttributeError:
                        if not self.get_package(virtualKey).get_available_vms().count(virtualMachine.name()) > 0:
                            result = False
                        else:
                            result=result and True
        finally:
            self.set_active_vm(storeVM)
        return result

# vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap: