def add_to_modulepath(self, dirpath): if not op.isdir(dirpath): sh.raise_error('{0!r} is not a directory'.format(dirpath)) sys_modules.modify_modulepath(self.mode, dirpath) the_path = sys_modules.modulepath() self.shell.register_env('PY_MODULEPATH', the_path, LOAD) os.environ['PY_MODULEPATH'] = the_path
def set_status(self, status): if status == UNLOADED: self._loaded = False elif status == LOADED: self._loaded = True else: sh.raise_error('Unknown module status {0!r}'.format(status)) if self._loaded and self.on_load: self.on_load(self) elif not self._loaded and self.on_unload: self.on_unload(self)
def __init__(self): self.tcl_modulepath = sh.get_pathenv('MODULEPATH') self.py_modulepath = [ p for p in sh.get_pathenv('PY_MODULEPATH') if p not in self.tcl_modulepath ] loaded_modules = sh.get_pathenv('PY_LOADEDMODULES') item = self.find_available(loaded_modules) self.avail, self._loaded, self.groups = item lmfiles = sh.get_pathenv('_PY_LMFILES_') if len(self._loaded) != len(lmfiles): sh.raise_error('Inconsistent LOADEDMODULES and LMFILES lengths') self.loaded_this_session = [] self.unloaded_this_session = []
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 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 exec1(self, mode, namespace): output = {'_error': False} namespace['module_name'] = self.name namespace['module_path'] = self.path namespace['mode'] = mode try: with open(self.path) as fh: exec(fh.read(), namespace, output) except SyntaxError as e: sh.raise_error('Failed to read module {0!r} with the following ' 'error: {1}'.format(self.name, e.args[0])) except PrereqError as e: sh.raise_error('Prerequisite {0!r} missing for ' 'module {1}'.format(e.args[0], self.name)) except ConflictError as e: sh.raise_error('Module {0!r} conflicts with ' '{1!r}'.format(e.args[0], self.name)) self.do_not_list = bool(output.get('do_not_list', 0)) return
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