def whatis1(self, *modules): self.mode = WHATIS for modulename in modules: module = self.find_module_file(modulename) if module.type == TCL_MODULE: try: p = self.call_tcl_modulecmd('whatis', *modules) sh.put_stderr(p['stderr']) return except TCLModulesError: sh.raise_error('Unable to locate a modulefile ' 'for {0!r}'.format(modulename)) d = self.exec_module(module) try: string = d['whatis']().format(module_name=module.name, **d) except KeyError: string = 'loads the {0} environment'.format(module.name) sh.put_stderr(string)
def dump(self): the_path = sh.path_join([name for name in sys_modules.loaded(name=1)]) self.shell.register_env('PY_LOADEDMODULES', the_path, LOAD) the_path = sh.path_join([path for path in sys_modules.loaded(path=1)]) self.shell.register_env('_PY_LMFILES_', the_path, LOAD) s = self.shell.dump_environ() sh.put_stdout(s) if self.verbosity: loaded = ', '.join(sys_modules.loaded(name=1, this_session=1)) unloaded = ', '.join(sys_modules.unloaded(name=1, this_session=1)) msg = 'The following modules were {0}ed: {{0}}'.format(self.mode) if loaded: sh.put_stderr(msg.format(loaded)) if unloaded: sh.put_stderr(msg.format(unloaded))
def process_module(self, modulename, mode, raise_e=True): module = self.find_module_file(modulename) if module is None: if raise_e: sh.raise_error('Unable to locate a modulefile ' 'for {0!r}'.format(modulename)) return if mode == UNLOAD: if not module.loaded: return if module.type == TCL_MODULE: p = self.call_tcl_modulecmd(mode, module.name) sh.put_stdout(p['stdout']) else: # Execute the file self.exec_module(module) module.set_status(UNLOADED) return if sys_modules.is_loaded(module): if sh.global_opts.force: # Remove the module so it can be loaded self.unload_module(module.name) else: if self.verbosity: sh.put_stderr('Module {0!r} is already ' 'loaded'.format(module.name)) return # Execute the file if module.type == TCL_MODULE: p = self.call_tcl_modulecmd(mode, module.name) sh.put_stdout(p['stdout']) else: self.exec_module(module) if module.do_not_list: return module.set_status(LOADED) return
def display_loaded(self, terse=False): """Print loaded modules to the console""" height, width = sh.get_console_dims() if not self._loaded: sh.put_stderr('No modulefiles currently loaded.') return sh.put_stderr('Currently loaded modulefiles:') modules = [ '{0})&{1}'.format(i + 1, m.name) for (i, m) in enumerate(self._loaded) ] if terse: sh.put_stderr('\n'.join(modules).replace('&', ' ')) else: string = format_text_to_cols(modules, width) sh.put_stderr(string.replace('&', ' '))
def display_avail(self, terse=False): """Print available modules to the console""" height, width = sh.get_console_dims() for (d1, group) in self.groups: modules = [] for name in group: m = self.avail[name] if m.hidden: continue modules.append(m.name) if m.loaded: modules[-1] += '*' if terse: sh.put_stderr(d1 + ':') sh.put_stderr('\n'.join(x.strip() for x in modules)) else: sh.put_stderr('{0:-^{1}}'.format(d1, width)) sh.put_stderr(format_text_to_cols(modules, width) + '\n')
def main(argv=None): if argv is None: argv = sys.argv[1:] p = ArgumentParser(prog='module') p.add_argument('shell', choices=('bash', )) p.add_argument('--debug', action='store_true', default=False) p.add_argument('-v', default=MODULEVERBOSITY, type=int, help='Level of verbosity [default: %(default)s]') p.add_argument('-c', action='store_true', default=False, help=('Call the c modulecmd (must define MODULECMD ' 'environment variable)')) sub_p = p.add_subparsers(dest='subparser_name', title='subcommands', description='valid subcommands', help='sub-command help') p_avail = sub_p.add_parser('avail', help='Display available modules') p_avail.add_argument( '-t', default=False, action='store_true', help='Display available modules in terse format [default: False]') p_list = sub_p.add_parser('list', help='Display loaded modules') p_list.add_argument( '-t', default=False, action='store_true', help='Display available modules in terse format [default: False]') p_load = sub_p.add_parser('load', help='Load module[s]') p_load.add_argument('modulefile', nargs='+', help='Valid modulefile[s]') p_load.add_argument( '-f', '--force', action='store_true', default=False, help='Force load missing prerequisites [default: False]') p_uload = sub_p.add_parser('unload', help='Unload module[s]') p_uload.add_argument('modulefile', nargs='+', help='Valid modulefile[s]') p_uload.add_argument('-f', '--force', action='store_true', default=False, help='Remove loaded prerequisites [default: False]') p_purge = sub_p.add_parser('purge', help='Unload all modules') p_swap = sub_p.add_parser('swap', help='Swap modules') p_swap.add_argument('modulefile', nargs=2, help='Valid modulefile') p_help = sub_p.add_parser('help', help='Display help for module[s]') p_help.add_argument('modulefile', nargs='+', help='Valid modulefile[s]') p_what = sub_p.add_parser('whatis', help='Display short help for module[s]') p_what.add_argument('modulefile', nargs='+', help='Valid modulefile[s]') args = p.parse_args(argv) if args.debug: sh.global_opts.debug = True try: sh.global_opts.force = args.force except AttributeError: pass p = PyModuleCommand(args.shell, v=args.v) if args.c: try: cargs = args.modulefile except AttributeError: cargs = [] height, width = sh.get_console_dims() space = ' ' * 12 text = wrap( '***Warning: modules loaded/unloaded by calling TCL ' 'modules cannot be tracked with this modules package ' 'and is not recommended', width, subsequent_indent=space) sh.put_stderr('\n'.join(text)) kwargs = {'sh_out': 1} p = call_tcl_modulecmd(args.shell, args.subparser_name, *cargs, **kwargs) if p['returncode'] != 0: sh.raise_error(p['stderr']) if subcmd in (AVAIL, LIST, HELP, WHATIS): sh.put_stderr(sh.to_string(p['stderr'])) elif bool(p['stderr']): error(p['stderr']) else: sh.put_stderr(sh.to_string(p['stdout'])) elif args.subparser_name == 'avail': p.display_available_modules(terse=args.t) elif args.subparser_name == 'list': p.list_loaded_modules(terse=args.t) elif args.subparser_name == 'load': p.load_modules_from_cl(*args.modulefile) elif args.subparser_name == 'unload': p.unload_modules_from_cl(*args.modulefile) elif args.subparser_name == 'swap': p.swap_modules_from_cl(*args.modulefile) elif args.subparser_name == 'help': p.help1(*args.modulefile) elif args.subparser_name == 'whatis': p.whatis1(*args.modulefile) elif args.subparser_name == 'purge': p.purge_from_cl() return 0