Exemplo n.º 1
0
def get_shell(shell=None):
    if shell is None:
        shell = os.path.basename(os.getenv('SHELL'))
    if shell == 'bash':
        from bash import BashShell
        return BashShell()
    ut.raise_error('Unknown shell {0!r}'.format(shell))
Exemplo n.º 2
0
 def help1(self, *modules):
     for modulename in modules:
         module = self.get_module(modulename)
         if module is None:
             ut.raise_error('Module not found: {0!r}'.format(modulename))
         s = module.help1()
         ut.put_stderr(s)
Exemplo n.º 3
0
 def add_to_modulepath(self, dirpath):
     if not op.isdir(dirpath):
         ut.raise_error('{0!r} is not a directory'.format(dirpath))
     sys_modules.modify_modulepath(self.mode, dirpath)
     the_path = sys_modules.modulepath()
     shell.register_env('PY_MODULEPATH', the_path, LOAD)
     os.environ['PY_MODULEPATH'] = the_path
Exemplo n.º 4
0
 def set_status(self, status):
     if status == 'unloaded':
         self._loaded = False
     elif status == 'loaded':
         self._loaded = True
     else:
         ut.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)
Exemplo n.º 5
0
    def process_module(self, modulename, mode, raise_e=True):

        module = self.get_module(modulename)
        if module is None:
            if raise_e:
                ut.raise_error('Unable to locate a modulefile '
                               'for {0!r}'.format(modulename))
            return

        if mode == UNLOAD and not module.loaded:
            return

        elif mode == LOAD and module.loaded:
            if ut.global_opts.force:
                # Remove the module so it can be loaded
                self.unload_module(module.name)
            else:
                if self.verbosity:
                    ut.put_stderr('Module {0!r} is already '
                                  'loaded'.format(module.name))
                return

        p = module.load(mode=mode)

        if module.type == TCL_MODULE:
            if p['stderr'] and mode not in (WHATIS, HELP):
                ut.raise_error('TCL Module failed to {0} with the '
                               'following error: {1}'.format(
                                   mode, p['stderr']))
            ut.put_stdout(p['stdout'])

        else:
            errors = module.load_errors()
            if errors:
                ut.raise_error(
                    '{0!r} failed to load with the following errors:'
                    '\n\t{1}'.format(module.name, '\n\t'.join(errors)))

            for (command, args) in module.cc:
                try:
                    getattr(self, command)(*args)
                except PrereqError as e:
                    ut.raise_error('Prerequisite {0!r} missing for '
                                   'module {1}'.format(e.args[0], module.name))
                except ConflictError as e:
                    ut.raise_error('Module {0} conflicts with loaded '
                                   'module {1}'.format(module.name, e.args[0]))

        status = {LOAD: LOADED, UNLOAD: UNLOADED}.get(mode)
        if status is not None and not module.hidden:
            module.set_status(status)

        return
Exemplo n.º 6
0
    def __init__(self):
        self.tcl_modulepath = ut.get_pathenv('MODULEPATH')
        self.py_modulepath = [p for p in ut.get_pathenv('PY_MODULEPATH')
                              if p not in self.tcl_modulepath]

        loaded_modules = ut.get_pathenv('PY_LOADEDMODULES')
        item = self.find_available(loaded_modules)
        self.avail, self._loaded, self.groups = item
        lmfiles = ut.get_pathenv('_PY_LMFILES_')

        if len(self._loaded) != len(lmfiles):
            ut.raise_error('Inconsistent LOADEDMODULES and LMFILES lengths')

        self.loaded_this_session = []
        self.unloaded_this_session = []
Exemplo n.º 7
0
    def _load(self, mode):
        """Load the module"""

        if self.type == TCL_MODULE:
            p = self.call_tcl_modulecmd(mode, self.name)
            return p

        ns_o = {}
        ns = self.namespace()
        ns['mode'] = mode
        try:
            with open(self.path) as fh:
                exec(fh.read(), ns, ns_o)

        except SyntaxError as e:
            ut.raise_error('The module file {0!r} contains python '
                           'syntax errors'.format(self.path))

        self.description = ns_o.get('description')
        self._whatis = ns_o.get('whatis')

        return
Exemplo n.º 8
0
import os

shell_name = os.path.basename(os.getenv('PY_MODULE_SHELL', os.getenv('SHELL')))
if shell_name == 'bash':
    from bash import BashShell
    shell = BashShell()
else:
    from utilities import raise_error
    raise_error('Unknown shell {0!r}'.format(shell_name))
Exemplo n.º 9
0
central_db = db_wrapper(db_path)
local_db_dir = settings.get('db_settings', 'local_db_dir')

#Is this a new user in the central db? Wipe out any existing local db if so
if not user_exists(user):
    #--------------------First Time User--------------------
    confirm = raw_input('''****************************************************
The directory %s is about to be created, if it already exists
it will be destroyed. Keep going? (y/n)''' % (local_db_dir))

    if confirm[0].lower() == 'y':
        central_db.add_user(user)
        set_up_dir(user)
        set_up_local_db(user)
    else:
        raise_error('abort', '\nSidus user creation has been cancelled.\n')
        
#do a full parse of the itunes xml
parser.parse(file_location) 

#now we have a list of Track class objects in handler.tracks,
#so we can update the local db

local_db = db_wrapper(get_db_path('local'))
central_db = db_wrapper(get_db_path('central'))

if update_main == 'update':
    main_handler = tunes_central()

for track in handler.tracks:
    #handle song...
Exemplo n.º 10
0
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:
        ut.global_opts.debug = True

    try:
        ut.global_opts.force = args.force
    except AttributeError:
        pass

    #p = PyModuleCommand(args.shell, v=args.v)
    p = PyModuleCommand(v=args.v)

    if args.c:
        try:
            cargs = args.modulefile
        except AttributeError:
            cargs = []
        height, width = ut.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)
        ut.put_stderr('\n'.join(text))
        kwargs = {'sh_out': 1}
        p = ut.call_tcl_modulecmd(shell.name, args.subparser_name, *cargs,
                                  **kwargs)
        if p['returncode'] != 0:
            ut.raise_error(p['stderr'])
        if args.subparser_name in (AVAIL, LIST, HELP, WHATIS):
            ut.put_stderr(ut.to_string(p['stderr']))
        elif bool(p['stderr']):
            error(p['stderr'])
        else:
            ut.put_stderr(ut.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