aboutsummaryrefslogtreecommitdiff
blob: 47d5313e3f16539ce496b40d66805951a9d35c60 (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
# -*- coding: utf-8 -*-
# Copyright 2004-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

from .FileParser import *
from .Errors import *
import os


# Dont accept env files without these variables
NEEDED_VARS = [ "JAVA_HOME", "PROVIDES_TYPE", "PROVIDES_VERSION" ]


class VM:
    def __init__(self, file):
        self.file = file
        self.config = EnvFileParser(file).get_config()
        self.check_for_needed_vars()

    def __eq__(self, other):
        return self.version() == other.version()

    def __ne__(self, other):
        return self.version() != other.version()

    def __lt__(self, other):
        return self.version() < other.version()

    def __gt__(self, other):
        return self.version() > other.version()

    def __le__(self, other):
        return self.version() <= other.version()

    def __ge__(self, other):
        return self.version() >= other.version()

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

    def check_for_needed_vars(self):
        for var in NEEDED_VARS:
            if var not in self.config:
                raise InvalidVMError("Missing: %s" % var)

    def get_config(self):
        return self.config

    def query(self, var):
        if var in self.config:
            return self.config[var]
        else:
            raise EnvironmentUndefinedError

    def filename(self):
        return self.file

    def name(self):
        return os.path.basename(self.file)

    def is_build_only(self):
        try:
            if self.query('BUILD_ONLY').upper() == 'TRUE':
                return True
        except:
            return False

    def is_jre(self):
        return self.is_type("JRE")

    def is_jdk(self):
        return self.is_type("JDK")

    def is_type(self, type):
        if type.upper() in [t.upper() for t in self.query('PROVIDES_TYPE').split(' ')]:
            return True
        else:
            return False

    def type(self):
        return self.query('PROVIDES_TYPE')

    def version(self):
        return self.query('PROVIDES_VERSION')

    def find_exec(self, executable):
        path = self.query('PATH')
        paths = path.split(':')

        for path in paths:
            path = os.path.join(path, executable)
            if os.path.isfile(path):
                if not os.access(path, os.X_OK):
                    raise PermissionError
                else:
                    return path
            else:
                raise PermissionError
        return None

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