-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
472 lines (362 loc) · 15.2 KB
/
module.py
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#
# PyModules - Software Environments for Research Computing Clusters
#
# Copyright 2012-2013, Brown University, Providence, RI. All Rights Reserved.
#
# This file is part of PyModules.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose other than its incorporation into a
# commercial product is hereby granted without fee, provided that the
# above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of Brown University not be used in
# advertising or publicity pertaining to distribution of the software
# without specific, written prior permission.
#
# BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
# PARTICULAR PURPOSE. IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
# ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import ConfigParser
import os
import pickle
import sys
import sqlite3 as sqlite
from modulecfg import *
from moduleutil import splitid, localize, info, print_columns
class ModuleError(Exception):
""" Base class for all module exceptions """
def __init__(self, msg, type=None):
super(ModuleError,self).__init__(msg)
self.warning = msg + messages.get(type, '')
def warn(self):
""" Print a warning message. """
print >>sys.stderr, "module: warning:", self.warning
class Module:
""" Encapsulates all of the logic for manipulating a module """
def __init__(self,modulefile):
""" Initializes a module from a modulefile """
self.name = os.path.basename(modulefile)
self.defaults = defaults.copy()
self.defaults['name'] = self.name
config = ConfigParser.SafeConfigParser(self.defaults)
config.optionxform = str
try:
config.read(modulefile)
except ConfigParser.MissingSectionHeaderError:
raise ModuleError("no versions specified in '%s'" % modulefile)
except ConfigParser.ParsingError as e:
raise ModuleError(
"error parsing modulefile '%s'\n%s" % (
modulefile, e))
sections = config.sections()
if not sections:
raise ModuleError("no versions specified in '%s'" % modulefile)
self.default_version = config.get(sections[0],'version');
self.versions = []
self.actions = {}
self.data = {}
self.paths = {}
for section in sections:
version = config.get(section,'version')
self.versions.append(version)
if config.getboolean(section,'default'):
self.default_version = version
try:
self.actions[version] = []
self.data[version] = {}
self.paths[version] = []
for key,val in config.items(section):
if key.partition(' ')[0] in ('set', 'append', 'prepend'):
if '"' in val:
raise ModuleError("found illegal '\"' character in action for '%s':\n %s = %s" % (modulefile,key,val))
self.actions[version].append((key,val))
if key.partition(' ')[2] == 'PATH':
self.paths[version] += val.split(':')
else:
self.data[version][key] = val
except ConfigParser.InterpolationError as e:
raise ModuleError(
"error interpreting modulefile '%s'\n %s" % (
modulefile, str(e)))
self.versions.sort()
def help(self,version=None):
""" Prints helpful information about this module """
version = self.__pick_version(version)
print >>sys.stderr, \
'\nName: ', self.name, \
'\nVersions: ', ', '.join(self.versions), \
'\nDefault: ', self.default_version, \
'\nURL: ', self.data[version].get('url', ''), \
'\nBrief: ', self.data[version].get('brief', ''), \
'\n\n', self.data[version].get('usage', '')
if 'loadmsg' in self.data[version]:
print >>sys.stderr, \
"\nLoad Message:\n%s" % self.data[version]['loadmsg']
def show(self,env,version=None):
""" Prints the environment changes to standard error """
version = self.__pick_version(version)
for key,val in self.actions[version]:
action = key.split(' ',1)
if action[0] == 'set': env.set(action[1],val)
elif action[0] == 'append': env.append(action[1],val)
elif action[0] == 'prepend': env.prepend(action[1],val)
env.append(LOADEDMODULES,'/'.join([self.name,version]))
def load(self,env,version=None):
""" Loads this module and modifies the caller's environment """
self.unload(env)
version = self.__pick_version(version)
info("loading '%s/%s'" % (self.name, version))
if 'loadmsg' in self.data[version]:
print >>sys.stderr, \
"module: %s: %s" % (
self.name, self.data[version]['loadmsg'])
for key,val in self.actions[version]:
action = key.split(' ',1)
if action[0] == 'set': env.set(action[1],val)
elif action[0] == 'append': env.append(action[1],val)
elif action[0] == 'prepend': env.prepend(action[1],val)
env.append(LOADEDMODULES,'/'.join([self.name,version]))
def unload(self,env,strict=False):
""" Unloads this module and resets the caller's environment """
version = self.__pick_loaded()
if not version:
if strict:
raise ModuleError("'%s' is not currently loaded" % self.name)
else:
return # Fail silenty
info("unloading '%s/%s'" % (self.name, version))
for key,val in self.actions[version]:
action = key.split(' ',1)
if action[0] == 'set':
env.unset(action[1])
elif action[0] == 'append':
for item in val.split(':'):
env.remove(action[1],item)
elif action[0] == 'prepend':
for item in val.split(':'):
env.remove(action[1],item)
env.remove(LOADEDMODULES,'/'.join([self.name,version]))
def list_bin(self,version):
""" List the executables provided by the module. """
version = self.__pick_version(version)
# Lambda function tests if a path is an executable file
is_exe = lambda f: os.path.isfile(f) and os.access(f, os.X_OK)
for path in self.paths[version]:
path = localize(path)
os.chdir(path)
programs = filter(is_exe, os.listdir(path))
if programs:
print >>sys.stderr, "%s:" % path
print_columns(sorted(programs))
def __pick_version(self,version):
""" Picks the version of the module based on the request """
if not version:
return self.default_version
elif not version in self.versions:
raise ModuleError(
"unknown version '%s/%s'" % (self.name, version),
type='unknown')
else: return version
def __pick_loaded(self):
""" Picks the version of the module based on the loaded modules """
moduleids = os.getenv(LOADEDMODULES)
if moduleids:
for moduleid in moduleids.split(':'):
name,version = splitid(moduleid)
if name == self.name:
return version
return None
class ModuleDb:
""" Encapsulates the database of modules """
def __init__(self):
self.conn = None
self.connect()
def connect(self, dbfile=MODULEDB):
""" Initializes connections to the sqlite database """
try:
if self.conn:
self.conn.close()
self.conn = sqlite.connect(dbfile, isolation_level=None)
except sqlite.OperationalError as e:
raise ModuleError(
"can't connect to database '%s' (sqlite3 error: %s)" % (
dbfile, e))
self.conn.row_factory = sqlite.Row
def rebuild(self,path):
""" Rebuilds the database with the modulefiles in the path """
# Since rebuild can take some time, write the new database to a
# temporary path to prevent service interruption.
tmpfile = MODULEDB + '~'
if os.path.exists(tmpfile):
os.unlink(tmpfile)
self.connect(tmpfile)
self.conn.execute("""
CREATE TABLE modules (
name TEXT PRIMARY KEY,
data BLOB)""")
self.conn.execute("""
CREATE TABLE moduleids (
name TEXT,
version TEXT,
PRIMARY KEY (name,version))""")
self.conn.execute("""
CREATE TABLE categories (
category TEXT,
name TEXT,
version TEXT,
PRIMARY KEY (category,name,version))""")
for modulefile in os.listdir(path):
if not modulefile.startswith('.'):
self.insert(os.path.join(path,modulefile))
# Move temporary database in place ...
self.conn.close()
os.chmod(tmpfile, moduleperm)
os.rename(tmpfile, MODULEDB)
# ... and reestablish the connection.
self.connect()
def insert(self,modulefile,force=False):
""" Inserts the modulefile as a Module into the database """
try:
module = Module(modulefile)
except ModuleError as e:
e.warn()
return
blob = sqlite.Binary(pickle.dumps(module,pickle.HIGHEST_PROTOCOL))
self.conn.execute('BEGIN')
if force:
self.conn.execute(
"REPLACE INTO modules VALUES (?,?)",
(module.name,blob))
self.conn.execute(
"DELETE FROM moduleids WHERE name = ?",
(module.name,))
self.conn.execute(
"DELETE FROM categories WHERE name = ?",
(module.name,))
else:
try:
self.conn.execute(
"INSERT INTO modules VALUES (?,?)",
(module.name,blob))
except sqlite.IntegrityError:
raise ModuleError(
"duplicate module already in database for '%s'" % \
module.name)
for version in module.versions:
self.conn.execute(
"INSERT INTO moduleids VALUES (?,?)",
(module.name,version))
for category in module.data[version].get('category', '(none)').split(','):
self.conn.execute(
"INSERT INTO categories VALUES (?,?,?)",
(category,module.name,version))
self.conn.execute('COMMIT')
os.chmod(modulefile, moduleperm)
def lookup(self,name):
""" Return the module with the specified name from the database """
cursor = self.conn.execute("""
SELECT name, data
FROM modules
WHERE name = ?""",(name,))
result = cursor.fetchone()
if result: return pickle.loads(str(result['data']))
else: raise ModuleError("unknown module '%s'" % name, 'unknown')
def avail(self,name='',version=''):
""" Return a list of the modules in the database matching the
specified name and version. """
cursor = self.conn.execute("""
SELECT name, version
FROM moduleids
WHERE name LIKE ? AND version LIKE ?
ORDER BY name """,
(name+'%','%'+version+'%'))
return map('/'.join, cursor)
def avail_category(self,category=''):
""" Return a list of modules organized by category. """
cursor = self.conn.execute("""
SELECT category, name, version
FROM categories
WHERE category LIKE ?
ORDER BY category, name """,
(category+'%',))
category = None
matches = []
for row in cursor:
if row[0] != category:
category = row[0]
matches.append((category, []))
matches[-1][1].append(row[1]+'/'+row[2])
return matches
class ModuleEnv:
""" Encapsulates an environment that multiple modules can alter """
def __init__(self):
self._env = dict()
self._env_unset = set()
def get(self,variable):
""" Gets an environment variable from the cache """
try:
return self._env[variable]
except KeyError:
return os.getenv(variable)
def dump(self,out=sys.stdout):
""" Prints the environment variable cache line by line """
if MODULESHELL == 'bash':
unsetfmt = "unset {0};"
setfmt = "export {0}=\"{1}\";"
elif MODULESHELL == 'csh':
unsetfmt = "unsetenv {0};"
setfmt = "setenv {0} \"{1}\";"
else:
raise NotImplementedError(MODULESHELL)
for var in self._env_unset:
print >>out, unsetfmt.format(var)
for var,val in self._env.iteritems():
print >>out, setfmt.format(var,val)
def set(self,variable,value):
""" Sets the environment variable to the specified value """
if variable == 'MANPATH' and (not value or value[-1] != ':'):
value += ':'
self._env[variable] = localize(value)
def unset(self,variable,value=None):
""" Unsets/resets the environment variable to the specified value """
self._env_unset.add(variable)
if value: self.set(variable,localize(value))
def append(self,variable,path):
""" Appends the path to the environment variable """
path = localize(path)
paths = self.get(variable)
if not paths:
paths = path
else:
paths = paths.split(':')
paths = [x for x in paths if x != path]
paths.append(path)
paths = ':'.join(paths)
self.set(variable,paths)
def prepend(self,variable,path):
""" Prepends the path to the environment variable """
path = localize(path)
paths = self.get(variable)
if not paths:
paths = path
else:
paths = paths.split(':')
paths = [x for x in paths if x != path]
paths.insert(0,path)
paths = ':'.join(paths)
self.set(variable,paths)
def remove(self,variable,path):
""" Removes the path from the environment variable """
path = localize(path)
paths = self.get(variable)
if paths:
paths = paths.split(':')
paths = [x for x in paths if x != path]
paths = ':'.join(paths)
self.set(variable,paths)
# vim:ts=4:shiftwidth=4:expandtab: