Example #1
0
def prerun(ctx, cmdline):
    global doc_cache

    argv = cmdline.split(' ')
    ctx.opts = util.get_opts(ctx.usage, argv=argv)

    #if not pdhdata:
    #    pdhdata = yaml_load(open(ctx.opts.flags.file))

    return doc_cache
Example #2
0
def main(argv, doc=__doc__, usage=__usage__):

    """
    Execute using docopt-mpe options.
    """

    # Process environment
    db = os.getenv( 'VOLUME_DB', __db__ )
    if db is not __db__:
        usage = usage.replace(__db__, db)
    opts = util.get_opts(doc + usage, version=get_version(), argv=argv[1:])
    opts.flags.dbref = taxus.ScriptMixin.assert_dbref(opts.flags.dbref)

    # Load configuration
    config_file = list(confparse.expand_config_path(opts.flags.config)).pop()
    settings = confparse.load_path(config_file)

    pprint(settings.todict())
    print
    for v, p in settings.volume.items():
        print v, p
    print
    for v, s in settings.volumes.items():
        print v, s
Example #3
0
commands = util.get_cmd_handlers_2(globals(), 'cmd_')
commands['help'] = util.cmd_help


### Util functions to run above functions from cmdline

def main(opts):

    """
    Execute command.
    """

    settings = opts.flags
    values = opts.args

    return util.run_commands(commands, settings, opts)

def get_version():
    return 'tasks.mpe/%s' % __version__

if __name__ == '__main__':
    import sys
    opts = util.get_opts(__description__ + '\n' + __usage__, version=get_version())
    if opts.flags.v or opts.flags.verbosity:
        log.category = 6
    if not opts.flags.project_slug:
        opts.flags.project_slug = get_project_slug(os.path.basename(os.getcwd()))
    sys.exit(main(opts))

Example #4
0

### Util functions to run above functions from cmdline

def main(opts):

    """
    Execute command.
    """

    #config = confparse.expand_config_path('domain.rc')
    opts.flags.configPath = os.path.expanduser(opts.flags.config)
    settings = util.init_config(opts.flags.configPath, dict(
            nodes = {}, interfaces = {}, domain = {}
        ), opts.flags)

    opts.default = 'info'

    return util.run_commands(commands, settings, opts)

def get_version():
    return 'domain.mpe/%s' % __version__

if __name__ == '__main__':
    import sys
    opts = util.get_opts(__doc__ + '\n' + __usage__, version=get_version())
    opts.flags.dbref = ScriptMixin.assert_dbref(opts.flags.dbref)
    sys.exit(main(opts))


Example #5
0
def pkgtest_check(args):
    '''
    This function allows to do only the checking part on an existing test output
    (i.e. 'test.fastr' and 'test.gnur' directories).
    It will try to re-create
    :param args:
    :return:
    '''
    parser = argparse.ArgumentParser(prog="pkgtest",
                                     description='FastR package testing.')
    parser.add_argument(
        '--fastr-home',
        metavar='FASTR_HOME',
        dest="fastr_home",
        type=str,
        default=None,
        required=True,
        help='The FastR standalone repo home directory (required).')
    parser.add_argument('-v',
                        '--verbose',
                        dest="verbose",
                        action="store_const",
                        const=1,
                        default=0,
                        help='Do verbose logging.')
    parser.add_argument('-V',
                        '--very-verbose',
                        dest="verbose",
                        action="store_const",
                        const=2,
                        help='Do verbose logging.')
    parser.add_argument(
        '--dump-preprocessed',
        dest="dump_preprocessed",
        action="store_true",
        help=
        'Dump processed output files where replacement filters have been applied.'
    )
    parser.add_argument('pkg_name',
                        metavar="PKG_NAME",
                        help='Package name for checking.')

    import util
    _opts = parser.parse_args(args=args, namespace=util.get_opts())

    log_format = '%(message)s'
    if _opts.verbose == 1:
        log_level = logging.DEBUG
    elif _opts.verbose == 2:
        log_level = VERY_VERBOSE
    else:
        log_level = logging.INFO
    logging.basicConfig(level=log_level, format=log_format)

    # also log to console
    console_handler = logging.StreamHandler(stream=sys.stdout)
    console_handler.setLevel(log_level)
    console_handler.setFormatter(logging.Formatter(log_format))
    logging.getLogger("").addHandler(console_handler)

    # if not :
    #     print("Missing required argument 'pkg_name'")
    #     return 1

    pkg_name = _opts.pkg_name
    fastr_testdir = _pkg_testdir("fastr", pkg_name)
    if not os.path.isdir(fastr_testdir):
        print("test directory '%s' does not exist" % fastr_testdir)
        return 1

    gnur_testdir = _pkg_testdir("gnur", pkg_name)
    if not os.path.isdir(gnur_testdir):
        print("test directory '%s' does not exist" % gnur_testdir)
        return 1

    fastr_test_info = dict()
    _get_test_outputs("fastr", pkg_name, fastr_test_info)
    return _set_test_status(fastr_test_info)
Example #6
0

### Util functions to run above functions from cmdline

def main(opts):

    """
    Execute command.
    """

    settings = opts.flags
    opts.default = ['balance', 'verify']
    return util.run_commands(commands, settings, opts)

def get_version():
    return 'budget.mpe/%s' % __version__

argument_handlers = {
        'DATE': lambda v: datetime.strptime(v, '%Y-%m-%d'),
        'YEAR-MONTH': lambda v: datetime.strptime(v, '%Y-%m')
}

if __name__ == '__main__':
    import sys
    opts = util.get_opts(__usage__, meta=argument_handlers, version=get_version())
    opts.flags.dbref = taxus.ScriptMixin.assert_dbref(opts.flags.dbref)
    sys.exit(main(opts))



Example #7
0
### Transform cmd_ function names to nested dict

commands = util.get_cmd_handlers(globals(), 'cmd_')
commands['help'] = util.cmd_help


### Util functions to run above functions from cmdline

def main(opts):

    """
    Execute command.
    """

    settings = opts.flags
    opts.default = 'info'
    return util.run_commands(commands, settings, opts)

def get_version():
    return 'project.mpe/%s' % __version__


if __name__ == '__main__':
    import sys
    opts = util.get_opts(__doc__, version=get_version())
    opts.flags.dbref = ScriptMixin.assert_dbref(opts.flags.dbref)
    sys.exit(main(opts))


Example #8
0
        # Normal execution
        return handlers[func](ctx)


if __name__ == '__main__':
    import sys, os
    ctx = confparse.Values(dict(
        usage=__doc__,
        path_exists=os.path.exists,
        sep=confparse.Values(dict(
            line=os.linesep
        )),
        out=sys.stdout,
        inp=sys.stdin,
        err=sys.stderr,
        opts=util.get_opts(__doc__)
    ))
    ctx['in'] = ctx['inp']
    if not ctx.opts.cmds:
        ctx.opts.cmds = ['dump']
    if ctx.opts.flags.no_detect_format:
        ctx.opts.flags.detect_format = False
    else:
        ctx.opts.flags.detect_format = True
    # TODO: ctx.opts.flags.no_json_string
    try:
        sys.exit( main( ctx.opts.cmds[0], ctx ) )
    except Exception as err:
        if not ctx.opts.flags.quiet:
            import traceback
            tb = traceback.format_exc()
Example #9
0
    """
    Execute command.
    """

    settings = opts.flags
    return util.run_commands(commands, settings, opts)

def get_version():
    return 'db_sa.mpe/%s' % __version__

if __name__ == '__main__':
    import sys
    from pprint import pformat

    opts = util.get_opts(__usage__, version=get_version())

    # Override dbref setting from schema
    if opts.flags.v or opts.flags.verbosity:
        log.category = 6
        # FIXME: log.category log.test()
    #if opts.flags.v
    #    print opts.flags.v
    #if opts.flags.verbosity:
    #    print opts.flags.verbosity


    # schema corresponds to module name
    if opts.args.schema:
        log.note("Using schema %r", opts.args.schema)
        schema = __import__(os.path.splitext(opts.args.schema)[0])
Example #10
0
def main(argv, doc=__doc__, usage=__usage__):

    """
    Execute using docopt-mpe options.

        prog [opts] [CTX] ( FILE... | DIR... )

    """

    # Process environment
    db = os.getenv( 'FINFO_DB', __db__ )
    if db is not __db__:
        usage = usage.replace(__db__, db)

    ctx = confparse.Values(dict(
        opts = util.get_opts(doc + usage, version=get_version(), argv=argv[1:])
    ))
    ctx.opts.flags.dbref = taxus.ScriptMixin.assert_dbref(ctx.opts.flags.dbref)
    # Load configuration
    ctx.config_file = list(confparse.expand_config_path(ctx.opts.flags.config)).pop()
    ctx.settings = settings = confparse.load_path(ctx.config_file)
    # Load SA session
    ctx.sa = get_session(ctx.opts.flags.dbref)

    # DEBUG: pprint(ctx.settings.todict())

    # Process arguments
    dirs = []
    # Shift paths from ctx arg
    if ctx.opts.args.CTX and os.path.exists(ctx.opts.args.CTX):
        ctx.opts.args.FILE.append(ctx.opts.args.CTX)
        ctx.opts.args.CTX = None

    # Sort out dirs from files
    for arg in ctx.opts.args.FILE:
        if os.path.isdir(arg):
            ctx.opts.args.FILE.remove(arg)
            dirs.append(arg)
        elif os.path.isfile(arg):
            pass
        else:
            log.note("Unhandled path %r" % arg)
    ctx.opts.args.DIR = dirs

    # Set default path context
    if ctx.opts.flags.name:
        assert not ctx.opts.args.CTX
        ctx.opts.args.CTX = ctx.opts.flags.name

    elif not ctx.opts.args.CTX:
        ctx.opts.args.CTX = 'current'


    # XXX: create prefixes object on context
    ctx.prefixes = confparse.Values(dict(
        map= settings.finfo['prefix-map'],
        env={},
        map_={}
    ))
    if 'homedir' not in ctx.prefixes.map:
        ctx.prefixes.map['homedir'] = 'HOME=%s' % os.path.expanduser('~')
    if 'current' not in ctx.prefixes.map:
        ctx.prefixes.map['current'] = '$PWD:$HOME'
    if 'pwd' not in ctx.prefixes.map:
        ctx.prefixes.map['pwd'] = 'PWD=%s' % os.path.abspath('.')

    for prefix, path in ctx.prefixes.map.items():
        if '=' in path:
            envvar, path = path.split('=')
            if envvar in ctx.prefixes.env:
                assert ctx.prefixes.env[envvar] == prefix, (
                        ctx.prefixes.env[envvar], prefix )
            ctx.prefixes.env[envvar] = prefix

    # Pre-pocess binds from env flags

    if not isinstance(ctx.opts.flags.env, list):
        ctx.opts.flags.env = [ ctx.opts.flags.env ]

    for env_map in ctx.opts.flags.env:
        envvar, prefix = env_map.split('=')
        if envvar in ctx.prefixes.env:
            assert prefix == ctx.prefixes.env[envvar]
        else:
            ctx.prefixes.env[envvar] = prefix

        envvalue = os.getenv(envvar, None)
        if envvalue:
            ctx.prefixes.map[prefix] = "%s=%s" % ( envvar, envvalue )
            #ctx.prefixes.map_[prefix] = envvalue.split(':')

    # Post-process prefixes after passed flags, and resolve all values
    for prefix, spec in ctx.prefixes.map.items():
        if '=' in spec:
            envvar, spec = spec.split('=')
            if envvar in ctx.prefixes.env:
                assert ctx.prefixes.env[envvar] == prefix, (
                        ctx.prefixes.env[envvar], prefix )
            ctx.prefixes.env[envvar] = prefix

        specs = spec.split(':')
        set_ = []

        for idx, path in enumerate(specs):
            path = os.path.expanduser(path)
            if varname.match(path):
                refpref = ctx.prefixes.env[path[1:]]
                #refpath = ctx.prefixes.map[]
                path = '#prefixes/'+refpref

            elif '$' in path:
                pass
            #else:
            #    path = '#prefixes/'+prefix+':'+str(idx)

            set_.append(path)

        ctx.prefixes.map_[prefix] = set_

    ctx.pathrefs = ctx.prefixes.map_[ctx.opts.args.CTX]

    #DEBUG:
    #print ctx.opts.todict()
    #print pformat(ctx.prefixes.todict())
    #print pformat(ctx.pathrefs)

    # Preprocess filters to regex
    if 'FILTER' not in ctx.opts.args:
        ctx.opts.args.FILTER = []

    if not ctx.opts.args.FILTER:
        ctx.opts.args.FILTER = default_filters
    if ctx.opts.flags.documents:
        ctx.opts.args.FILTER = doc_filters + ctx.opts.args.FILTER
    for idx, filter in enumerate(ctx.opts.args.FILTER):
        if isinstance(filter, str):
            ctx.opts.args.FILTER[idx] = fnmatch.translating(filter)

    # Resolve FILE/DIR arguments
    files, dirs = [], []
    for arg in ctx.opts.args.FILE + ctx.opts.args.DIR:
        r = find_local(arg, search_path(ctx.pathrefs, ctx))
        if not r: continue
        prefix, file, dir = r
        if not dir:
            raise Exception("No path for %s" % arg)
        elif file:
            files.append((prefix, file))
        else:
            dirs.append((prefix, dir))

    print("Resolved arguments to %s dirs, %s files" % ( len(dirs), len(files) ))

    # XXX: if not ctx.opts.flags.directories:

    if ctx.opts.flags.recurse:
        # Resolve all dirs to file lists
        for p, d in dirs:
            for top, path_dirs, path_files in os.walk(os.path.join(p, d)):
                for path_dir in list(path_dirs):
                    for filter in ctx.opts.args.FILTER:
                        if not filter.match(os.path.basename(path_dir)):
                            path_dirs.remove(path_dir)
                            break

                if top.startswith('./'):
                    top = top[2:]

                for path_file in list(path_files):
                    filter = None
                    for filter in ctx.opts.args.FILTER:
                        if filter.match(os.path.basename(path_file)):
                            break
                        else:
                            continue
                    if not filter.match(os.path.basename(path_file)):
                        path_files.remove(path_file)
                    if path_file not in path_files:
                        continue
                    files.append((p, os.path.join(top, path_file)))

    print("Continue with %s files" % len(files))

    mfadapter = None
    res.persistence.PersistedMetaObject.stores['metafile'] = mfadapter


    prefix = None
    for p, f in files:

        if ctx.opts.flags.auto_prefix:

            prefixes = find_prefixes(p, ctx)
            assert prefixes # FIXME: how come only use first??
            prefix = prefixes.next()
            assert len(ctx.prefixes.map_[prefix]) == 1, prefix
            name = f[len(ctx.prefixes.map_[prefix][0])+1:]

        else:
            prefix = ctx.opts.args.CTX
            name = f[len(p)+1:]

        ref = prefix+':'+name

        if ctx.opts.flags.names_only:
            print ref

        else:
            # TODO: get INode through context? Also add mediatype & parameters
            # resolver. But needs access to finfo ctx..
            record = taxus.INode.get_instance(name=ref, _sa=ctx.sa)

            # GNU/Linux: -bi = --brief --mime
            # Darwin/BSD: -bI = --brief --mime
            #mediatype = lib.cmd('file --brief --mime "%s"', path).strip()
            # XXX: see basename-reg?

            #if ctx.opts.flags.update == True:

            # TODO: repopulate metadata;

            mf = res.metafile.Metafile(f)

            assert mf.date_accessed
            record.date_accessed = mf.date_accessed
            assert mf.date_modified
            record.date_modified = mf.date_modified

            if not record.node_id:
                ctx.sa.add(record)

            print record, record.date_updated, record.date_modified
            #sys.exit()

            if ctx.opts.flags.update:
                ctx.sa.commit()