-
Notifications
You must be signed in to change notification settings - Fork 0
/
modulecmd.py
203 lines (160 loc) · 5.64 KB
/
modulecmd.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
#
# 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 argparse
import sys
import os
from module import ModuleError, Module, ModuleDb, ModuleEnv
from modulecfg import LOADEDMODULES
from moduleutil import splitid, print_centered, print_columns
def avail(args):
"""
List available modules.
"""
moduledb = ModuleDb()
if not args.module:
args.module = [':']
for moduleid in args.module:
name,version = splitid(moduleid)
if name.startswith(':'):
for category, matches in moduledb.avail_category(name[1:]):
print_centered('category: ' + category)
print_columns(matches)
else:
title = 'name: %s*/*' % name
if version:
title = '%s%s*' % (title, version)
print_centered(title)
print_columns(moduledb.avail(name,version))
def list_loaded(args):
"""
List currently loaded modules.
"""
moduleids = os.getenv(LOADEDMODULES)
if not moduleids:
return
moduleids = [m for m in moduleids.split(':') if m]
print_columns(moduleids)
def load(args):
"""
Load modules, unloading any versions that are already loaded.
aliases: add switch swap
"""
env = ModuleEnv()
db = ModuleDb()
for moduleid in args.module:
name,version = splitid(moduleid)
try:
Module.load(db.lookup(name),env,version)
except ModuleError as e:
e.warn()
env.dump()
def unload(args):
"""
Unload modules, if the specified version is already loaded.
aliases: rm remove
"""
env = ModuleEnv()
db = ModuleDb()
for moduleid in args.module:
name,version = splitid(moduleid)
try:
Module.unload(db.lookup(name),env,strict=True)
except ModuleError as e:
e.warn()
env.dump()
def show(args):
"""
Show the environment changes that will be made when loading the module.
aliases: display
"""
env = ModuleEnv()
db = ModuleDb()
for moduleid in args.module:
name,version = splitid(moduleid)
try:
Module.show(db.lookup(name),env,version)
except ModuleError as e:
e.warn()
env.dump(sys.stderr)
def help(args):
"""
Print additional information about a module.
aliases: whatis
"""
# Doesn't use ModuleEnv or Module
name,version = splitid(args.module)
try:
ModuleDb().lookup(name).help(version)
except ModuleError as e:
e.warn()
def list_bin(args):
"""
List the programs provided by the module.
"""
db = ModuleDb()
for moduleid in args.module:
name,version = splitid(moduleid)
try:
Module.list_bin(db.lookup(name),version)
except ModuleError as e:
e.warn()
def alias_subcommand(argv):
"""
Substitute aliases for each command.
"""
i = 1 # subcommand is at argument index 1
if len(argv) > i:
if argv[i] in ('add','switch','swap'): argv[i] = 'load'
elif argv[i] in ('rm','remove'): argv[i] = 'unload'
elif argv[i] == 'display': argv[i] = 'show'
elif argv[i] == 'whatis': argv[i] = 'help'
def main():
parser = argparse.ArgumentParser(prog='modulecmd')
subparsers = parser.add_subparsers(title='subcommands')
avail_parser = subparsers.add_parser('avail', help=avail.__doc__)
avail_parser.add_argument('module',nargs='*')
avail_parser.set_defaults(func=avail)
load_parser = subparsers.add_parser('load', help=load.__doc__)
load_parser.add_argument('module',nargs='+')
load_parser.set_defaults(func=load)
unload_parser = subparsers.add_parser('unload', help=unload.__doc__)
unload_parser.add_argument('module',nargs='+')
unload_parser.set_defaults(func=unload)
list_parser = subparsers.add_parser('list', help=list_loaded.__doc__)
list_parser.set_defaults(func=list_loaded)
show_parser = subparsers.add_parser('show', help=show.__doc__)
show_parser.add_argument('module',nargs='+')
show_parser.set_defaults(func=show)
help_parser = subparsers.add_parser('help', help=help.__doc__)
help_parser.add_argument('module')
help_parser.set_defaults(func=help)
bin_parser = subparsers.add_parser('bin', help=list_bin.__doc__)
bin_parser.add_argument('module',nargs='+')
bin_parser.set_defaults(func=list_bin)
alias_subcommand(sys.argv)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()
# vim:ts=4:shiftwidth=4:expandtab: