aboutsummaryrefslogtreecommitdiff
path: root/src/gjl
blob: b5eafbe8e7a83bc5340aad5b76fb78ccee7cada9 (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
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright 2004-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from java_config_2.OutputFormatter import *
from java_config_2.EnvironmentManager import *
from java_config_2.VersionManager import *

from optparse import OptionParser, make_option, OptionValueError
import os
from os.path import basename
import sys

def get_pkg_args(package):
    missing_deps = set()

    classpath = manager.build_dep_path([package.name()], "CLASSPATH", missing_deps)
    library = manager.build_dep_path([package.name()], "LIBRARY_PATH", missing_deps)

    if len(missing_deps) > 0:
        for dep in missing_deps:
            printer._printError("Dependency package %s was not found!" % dep)

    return ':'.join(classpath), ':'.join(library)

def abort(msg):
    printer._printError(msg)
    sys.exit(1)

def get_vm(pkg):
    return verman.get_vm(pkg.query('VM'))

def get_args(pkg):
    args=""

    classpath, library = get_pkg_args(pkg)
    if classpath:
        envcp = os.getenv('CLASSPATH')

        if envcp:
            classpath = ':'.join((envcp, classpath))

        args += ' -classpath %s' % (classpath)

    envlp = os.getenv('LD_LIBRARY_PATH')
    envjlp = os.getenv('JAVA_LIBRARY_PATH')

    libdir = pkg.query('LIBDIR')
    if libdir:
        newlibrary = manager.eprefix + '/' + libdir
    else:
        # Old, obsolete fallback path for packages that where
        # installed before java-utils-2.eclass recorded LIBDIR.
        # Bug #917326.
        newlibrary = manager.eprefix + '/lib:'+ manager.eprefix + '/usr/lib'

    if library:
        newlibrary = ':'.join((library, newlibrary))
    if envjlp:
        newlibrary = ':'.join((newlibrary, envjlp))
    if envlp:
        newlibrary = ':'.join((newlibrary, envlp))

    args += ' -Djava.library.path="%s"' % (newlibrary)

    if args:
        return args
    else:
        return None

def get_env(package):
    env = manager.build_dep_env_vars([package.name()], set())
    return env

def get_jar(pkg, gjar):
    jars = pkg.classpath()
    if jars:
        for jar in jars.split(':'):
            if gjar == basename(jar):
                return jar
            if normpath(gjar) == normpath(jar):
                return gjar
    return None

def normpath(mypath):
    newpath = os.path.normpath(mypath)
    if newpath.startswith('//'):
        return newpath[1:]
    return newpath

if __name__ == '__main__':
    import java_config_2
    usage =  "%prog [options]\n\n"
    usage += f"Java Utility Version {java_config_2.version}\n"
    usage += "Copyright 2004-2023 Gentoo Authors\n"
    usage += "Distributed under the terms of the GNU General Public License v2\n"
    usage += "Please contact the Gentoo Java Project <java@gentoo.org> with problems."

    options_list = [
                     make_option ("-p", "--package",  action="store", type="string", dest="package", help="The package"),
                     make_option ("-v", "--get-vm",   action="store_true", dest="get_vm"),
                     make_option ("-a", "--get-args", action="store_true", dest="get_args"),
                     make_option ("-j", "--get-jar", action="store", type="string", dest="jar")
                   ]

    parser = OptionParser(usage, options_list)
    (options, args) = parser.parse_args()

    global printer, manager, verman
    printer = OutputFormatter(True, True)
    manager = EnvironmentManager(os.getenv('ROOT', ''), java_config_2.eprefix)
    verman = VersionManager(manager)

    if not options.package:
        abort("Too dumb todo anything without -p")

    pkg = manager.get_package(options.package)
    if not pkg:
        abort("Invalid package: %s" % ( options.package ) )

    if options.get_vm:
        vm = get_vm(pkg)
        if vm:
            manager.set_active_vm(vm)
            print('gjl_vm="%s"' % ( vm ))

    if options.get_args:
        args = get_args(pkg)
        if args:
            print('gjl_args="%s";' % ( args ))
        env = get_env(pkg)
        for k, v in env.items():
            if 'PATH' in k:
                print('export %s="%s:${%s}"; %s=${%s%%:};' % ( k, v, k, k, k ))
            else:
                print('export %s="%s";' % ( k, v ))

    if options.jar:
        jar = get_jar(pkg, options.jar)
        if jar:
            print('gjl_starte="-jar %s"' % ( jar ))
        else:
            abort("Couldn't find %s" % ( options.jar ) )

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