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
|
# -*- coding: UTF-8 -*-
# Copyright 2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
# Author: Saleem Abdulrasool <compnerd@gentoo.org>
# Maintainer: Gentoo Java Herd <java@gentoo.org>
# Java Subsystem Configuration Utility for Gentoo Linux
# ChangeLog
# Saleem A. <compnerd@gentoo.org>
# December 30, 2004 - Initial Rewrite
# - Based on the collective works of the following:
# {karltk,axxo,aether}@gentoo.org
__version__ = '$Revision: 2.0$'[11:-1]
import VM,Package
import os,glob,re
from Errors import *
class EnvironmentManager:
virtual_machines = {}
packages = []
def __init__(self):
# Get JAVA_HOME
environ_path = [
os.path.join(os.environ.get('HOME'), '.gentoo', 'java'),
os.path.join('/', 'etc', 'env.d', '20java')
]
self.JAVA_HOME = None
for file in environ_path:
try:
stream = open(file, 'r')
except IOError:
continue
read = stream.readline()
while read:
if read.strip().startswith('JAVA_HOME'):
stream.close()
self.JAVA_HOME = read.split('=', 1)[-1].strip()
break
else:
read = stream.readline()
stream.close()
# Collect the Virtual Machines
# TODO: MAKE THIS MODULAR!
if os.path.isdir('/etc/env.d/java'):
try:
count = 1
for file in os.listdir('/etc/env.d/java'):
conf = os.path.join('/etc/env.d/java', file)
if file.startswith("20"):
vm = None
try:
vm = VM.VM(conf)
except InvalidConfigError:
pass
except PermissionError:
pass
if vm.query('JAVA_HOME') == self.JAVA_HOME:
vm.set_active()
self.virtual_machines[count] = vm
count += 1
except OSError:
pass
# Collect the packages
# TODO: MAKE THIS MODULAR!
packages_path = os.path.join('/', 'usr', 'share', '*', 'package.env')
for package in iter(glob.glob(packages_path)):
self.packages.append(Package.Package(package,os.path.basename(os.path.dirname(package))))
def get_active_vm(self):
vm_list = self.get_virtual_machines()
for count in iter(vm_list):
if vm_list[count].active:
return vm_list[count]
raise RuntimeError, "No java vm could be found."
def get_virtual_machines(self):
return self.virtual_machines
def find_vm(self, name):
found = []
for id, vm in self.virtual_machines.iteritems():
if vm.name().startswith(name):
found.append(vm)
return found
def get_packages(self):
return self.packages
def query_packages(self, packages, query):
results = []
for package in iter(self.get_packages()):
if package.name in packages:
value = package.query(query)
if value:
results.append(value)
packages.remove(package.name)
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
elif machine == vm.name():
return vm
# Check if the vm is specified via JAVA_HOME
elif machine == vm.query('JAVA_HOME'):
return vm
# Check if vm is specified by partial name
elif 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().iteritems():
if item in ENV_VARS:
stream.write(render % (item,value))
except IOError:
raise PermissionError
except EnvironmentUndefinedError:
raise EnvironmentUndefinedError
def set_vm(self, vm, sh_env_file, csh_env_file=None):
# Create the SH environment file
if sh_env_file is not None:
try:
stream = open(sh_env_file, 'w')
except IOError:
raise PermissionError
try:
self.create_env_entry(vm, stream, "%s=%s\n")
except IOError:
stream.close()
raise PermissionError
except EnvironmentUndefinedError:
stream.close();
raise EnvironmentUndefinedError
stream.close()
# Create the CSH environment file
if csh_env_file is not None:
try:
stream = open(csh_env_file, 'w')
except IOError:
raise PermissionError
try:
create_env_entry(vm, stream, "setenv %s %s\n")
except IOError:
stream.close()
raise PermissionError
stream.close()
def clean_classpath(self, env_file):
if os.path.isfile(env_file):
try:
os.remove(env_file)
except IOError:
raise PermissionError
def set_classpath(self, env_file, pkgs):
classpath = self.query_packages(pkgs, "CLASSPATH")
classpath = re.sub(':+', ':', classpath)
classpath.strip(':')
if os.path.isfile(env_file):
try:
os.remove(env_file)
except IOError:
raise PermissionError
try:
stream = open(env_file, 'w')
except IOError:
raise PermissionError
stream.write("CLASSPATH=%s\n" % (classpath))
stream.close()
def append_classpath(self, env_file, pkgs):
classpath = self.query_packages(pkgs, "CLASSPATH")
classpath = re.sub(':+', ':', classpath)
classpath.strip(':')
oldClasspath = ''
if os.path.isfile(env_file):
try:
stream = open(env_file, 'r')
except IOError:
raise PermissionError
read = stream.readline()
while read:
if read.strip().startswith('CLASSPATH'):
stream.close()
oldClasspath = read.split('=', 1)[-1].strip()
break
else:
read = stream.readline()
stream.close()
classpath = oldClasspath + ':' + classpath
if os.path.isfile(env_file):
try:
os.remove(env_file)
except IOError:
raise PermissionError
try:
stream = open(env_file, 'w')
except IOError:
raise PermissionError
stream.write("CLASSPATH=%s\n" % (classpath))
stream.close()
# vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3:
|