Esempio n. 1
0
File: Worker.py Progetto: azag0/caf
 def __init__(self, myid, root, url, dry=False, limit=None, debug=False):
     super().__init__(myid, root, dry, limit, debug)
     conf = Configuration(os.environ['HOME'] + '/.config/caf/conf.yaml')
     self.curl = conf.get('curl')
     self.pushover = conf.get('pushover')
     self.url = url + '?caller=' + socket.gethostname()
     self.url_state = {}
     self.url_putback = {}
     self.has_warned = False
     signal(SIGXCPU, self.signal_handler)
Esempio n. 2
0
File: Caf.py Progetto: azag0/caf
def list_remotes(caf, _):
    """
    List remotes.

    Usage:
        caf list remotes
    """
    remote_conf = Configuration()
    remote_conf.update(caf.conf.get('remotes', {}))
    print(remote_conf)
Esempio n. 3
0
File: Caf.py Progetto: azag0/caf
 def __init__(self, libpath):
     super().__init__('caf')
     self.conf = Configuration('.caf/conf.yaml')
     self.conf.set_global(Configuration('{}/.config/caf/conf.yaml'
                                        .format(os.environ['HOME'])))
     for cscriptname in ['cscript', 'cscript.py']:
         if Path(cscriptname).is_file():
             break
     else:
         cscriptname = None
     with timing('reading cscript'):
         try:
             self.cscript = load_module(cscriptname, self.commands[('unpack',)]._func) \
                 if cscriptname else object()
         except RuntimeError:
             error('There was an error while reading cscript.')
     self.out = Path(getattr(self.cscript, 'out', 'build'))
     self.cache = Path(getattr(self.cscript, 'cache', '.caf/db'))
     self.top = Path(getattr(self.cscript, 'top', '.'))
     self.cellar = self.cache/cellar
     self.brewery = self.cache/brewery
     self.remotes = {name: Remote(r['host'], r['path'], self.top)
                     for name, r in self.conf.get('remotes', {}).items()}
     self.libpath = libpath
Esempio n. 4
0
File: Caf.py Progetto: azag0/caf
class Caf(CLI):
    def __init__(self, libpath):
        super().__init__('caf')
        self.conf = Configuration('.caf/conf.yaml')
        self.conf.set_global(Configuration('{}/.config/caf/conf.yaml'
                                           .format(os.environ['HOME'])))
        for cscriptname in ['cscript', 'cscript.py']:
            if Path(cscriptname).is_file():
                break
        else:
            cscriptname = None
        with timing('reading cscript'):
            try:
                self.cscript = load_module(cscriptname, self.commands[('unpack',)]._func) \
                    if cscriptname else object()
            except RuntimeError:
                error('There was an error while reading cscript.')
        self.out = Path(getattr(self.cscript, 'out', 'build'))
        self.cache = Path(getattr(self.cscript, 'cache', '.caf/db'))
        self.top = Path(getattr(self.cscript, 'top', '.'))
        self.cellar = self.cache/cellar
        self.brewery = self.cache/brewery
        self.remotes = {name: Remote(r['host'], r['path'], self.top)
                        for name, r in self.conf.get('remotes', {}).items()}
        self.libpath = libpath

    def __call__(self, argv):
        log_caf(argv)
        try:
            super().__call__(argv)  # try CLI as if local
        except CLIExit as e:  # store exception to reraise below if remote fails as well
            cliexit = e
        else:
            print_timing()
            return  # finished
        # the local CLI above did not succeed
        # make a usage without local CLI
        usage = '\n'.join(l for l in str(self).splitlines() if 'caf COMMAND' not in l)
        try:  # remote CLI failed as well, reraise CLIExit
            args = docopt(usage, argv=argv[1:], options_first=True, help=False)  # parse local
        except DocoptExit:
            raise cliexit
        rargv = [argv[0], args['COMMAND']] + args['ARGS']  # remote argv
        try:  # try CLI as if remote
            rargs = self.parse(rargv)  # remote parsed arguments
        except DocoptExit:  # remote CLI failed as well, reraise CLIExit
            raise cliexit
        if 'work' in rargs:
            if rargs['--queue']:  # substitute URL
                url = self.get_queue_url(rargs['--queue'], 'get')
                if url:
                    rargv = [arg if arg != rargs['--queue'] else url for arg in rargv]
            elif rargs['--last']:
                with open('.caf/LAST_QUEUE') as f:
                    queue_url = f.read().strip()
                last_index = rargv.index('--last')
                rargv = rargv[:last_index] + ['--queue', queue_url] + rargv[last_index+1:]
        remotes = self.proc_remote(args['REMOTE'])  # get Remote objects
        if args['COMMAND'] in ['init', 'build', 'work']:
            for remote in remotes:
                remote.update()
        if 'work' in rargs and not rargs['build'] and not args['--no-check']:
            for remote in remotes:
                remote.check(self.out)
        for remote in remotes:
            remote.command(' '.join(arg if ' ' not in arg else repr(arg)
                                    for arg in rargv[1:]))
            if 'work' in rargs and rargs['build'] and not args['--no-check']:
                remote.check(self.out)

    def __format__(self, fmt):
        if fmt == 'header':
            return 'Caf -- Calculation framework.'
        elif fmt == 'usage':
            s = """\
            Usage:
                caf COMMAND [ARGS...]
                caf [--no-check] REMOTE COMMAND [ARGS...]
            """.rstrip()
            return dedent(s)
        elif fmt == 'options':
            s = """\
            Options:
                --no-check           Do not check remote cellar.
            """.rstrip()
            return dedent(s)
        else:
            return super().__format__(fmt)

    def get_queue_url(self, queue, action):
        if 'queue' in self.conf:
            if action == 'submit':
                if queue in self.conf['queue']:
                    return '{0[host]}/token/{0[token]}/submit'.format(self.conf['queue'][queue])
            elif action == 'get':
                host, queue = queue.split(':', 1)
                if host in self.conf['queue']:
                    return '{0[host]}/token/{0[token]}/queue/{1}/get' \
                        .format(self.conf['queue'][host], queue)
            elif action == 'append':
                host, queue = queue.split(':', 1)
                if host in self.conf['queue']:
                    return '{0[host]}/token/{0[token]}/queue/{1}/append' \
                        .format(self.conf['queue'][host], queue)

    def finalize(self, sig, frame):
        print_timing()
        sys.exit()

    def proc_remote(self, remotes):
        if remotes == 'all':
            remotes = self.remotes.values()
        else:
            try:
                remotes = [self.remotes[r] for r in remotes.split(',')]
            except KeyError as e:
                error('Remote "{}" is not defined'.format(e.args[0]))
        return remotes