Пример #1
0
def load_kitchens(options, args, logger):

    logger.debug('Config file not specified, trying to read "kitchen.py"')
    path = os.path.abspath(options.filename)
    if not os.path.isdir(path):
        path = os.path.dirname(path)
    if path not in sys.path:
        sys.path.insert(0, path)

    if os.path.isdir(options.filename):
        files = [os.path.join(path, f) for f in sorted(os.listdir(path)) if f.endswith('.py')]
    else:
        files = [options.filename]

    logger.debug('Processing %s as kitchen file(s)' % files)

    globs = {}
    file_found = False
    for fname in files:
        globs["__file__"] = os.path.abspath(fname)
        if os.path.exists(globs["__file__"]):
            file_found = True
            with open(fname, "rb") as fp:
                logger.debug('Compiling %s' % fname)
                source = fp.read()
                exec compile(source, fname, 'exec') in globs
            del globs['__file__']

    if not file_found:
        sys.stderr.write("Need to have 'kitchen.py' or other files specified by -f parameter")
        sys.exit(1)

    kit = Kitchen()
    logger.debug('Processing inputs')

    # place all input variables under the "input" scope
    kit.update_config({'input.' + value.split('=')[0]: value.split('=')[1] for value in options.inputs})

    roles = []
    for c in args:
        try:
            logger.debug('Adding role %s' % c)
            roles.append(globs[c])
        except KeyError:
            sys.stderr.write("Function for role '%s' not found in config\n" % c)
            sys.exit(1)
    logger.debug('Environment.config before running recipes: %s' % kit.config)
    for r in roles:
        kit.update_config({'kokki.current_role' : r.func_name})
        r(kit)

    return kit
Пример #2
0
def main():
    parser = build_parser()
    options, args = parser.parse_args()
    if not args:
        parser.error("must specify at least one command")

    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    path = os.path.abspath(options.filename)
    if not os.path.isdir(path):
        path = os.path.dirname(path)
    if path not in sys.path:
        sys.path.insert(0, path)

    if os.path.isdir(options.filename):
        files = [os.path.join(path, f) for f in sorted(os.listdir(path)) if f.endswith('.py')]
    else:
        files = [options.filename]

    globs = {}
    for fname in files:
        with open(fname, "rb") as fp:
            source = fp.read()
        exec compile(source, fname, 'exec') in globs

    kit = Kitchen()
    roles = []
    for c in args:
        try:
            roles.append(globs[c])
        except KeyError:
            sys.stderr.write("Function for role '%s' not found in config" % c)
            sys.exit(1)
    for r in roles:
        r(kit)
    kit.run()
Пример #3
0
def main():
    parser = build_parser()
    options, args = parser.parse_args()
    if not args and not options.config:
        parser.error("must specify at least one command")

    logging.basicConfig(level=logging.INFO)
    if options.verbose:
        logger = logging.getLogger('kokki')
        logger.setLevel(logging.DEBUG)

    if options.config:
        if ':' in options.config:
            format, filename = options.config.split(':', 1)
        else:
            format, filename = "yaml", options.config
        if format == "yaml":
            import yaml
            with open(options.config, "rb") as fp:
                kit = yaml.load(fp.read())
        elif format == "pickle":
            import cPickle as pickle
            with open(filename, "rb") as fp:
                kit = pickle.load(fp)
    else:
        path = os.path.abspath(options.filename)
        if not os.path.isdir(path):
            path = os.path.dirname(path)
        if path not in sys.path:
            sys.path.insert(0, path)

        if os.path.isdir(options.filename):
            files = [os.path.join(path, f) for f in sorted(os.listdir(path)) if f.endswith('.py')]
        else:
            files = [options.filename]

        globs = {}
        for fname in files:
            globs["__file__"] = os.path.abspath(fname)
            with open(fname, "rb") as fp:
                source = fp.read()
            exec compile(source, fname, 'exec') in globs
        del globs['__file__']

        kit = Kitchen()
        roles = []
        for c in args:
            try:
                roles.append(globs[c])
            except KeyError:
                sys.stderr.write("Function for role '%s' not found in config\n" % c)
                sys.exit(1)
        for r in roles:
            r(kit)

    if options.dump:
        if ':' in options.dump:
            format, filename = options.dump.split(':', 1)
        else:
            format, filename = "yaml", options.dump
        if format == "yaml":
            import yaml
            if filename == "-":
                print yaml.dump(kit)
            else:
                with open(filename, "wb") as fp:
                    yaml.dump(kit, fp)
        elif format == "pickle":
            import cPickle as pickle
            if filename == "-":
                print pickle.dumps(kit)
            else:
                with open(filename, "wb") as fp:
                    pickle.dump(kit, fp, pickle.HIGHEST_PROTOCOL)
        sys.exit(0)

    kit.run()
Пример #4
0
def main():
    parser = build_parser()
    options, args = parser.parse_args()
    if not args and not options.config:
        parser.error("must specify at least one command")

    logging.basicConfig(level=logging.INFO)
    if options.verbose:
        logger = logging.getLogger('kokki')
        logger.setLevel(logging.DEBUG)

    if options.config:
        if ':' in options.config:
            fmt, filename = options.config.split(':', 1)
        else:
            fmt, filename = "yaml", options.config
        if fmt == "yaml":
            import yaml
            with open(options.config, "rb") as fp:
                kit = yaml.load(fp.read())
        elif fmt == "pickle":
            import cPickle as pickle
            with open(filename, "rb") as fp:
                kit = pickle.load(fp)
    else:
        path = os.path.abspath(options.filename)
        if not os.path.isdir(path):
            path = os.path.dirname(path)
        if path not in sys.path:
            sys.path.insert(0, path)

        if os.path.isdir(options.filename):
            files = [
                os.path.join(path, f) for f in sorted(os.listdir(path))
                if f.endswith('.py')
            ]
        else:
            files = [options.filename]

        globs = {}
        for fname in files:
            globs["__file__"] = os.path.abspath(fname)
            with open(fname, "rb") as fp:
                source = fp.read()
            exec compile(source, fname, 'exec') in globs
        del globs['__file__']

        kit = Kitchen()
        roles = []
        for c in args:
            try:
                roles.append(globs[c])
            except KeyError:
                sys.stderr.write(
                    "Function for role '%s' not found in config\n" % c)
                sys.exit(1)
        for r in roles:
            r(kit)

    for over in options.overrides:
        name, value = over.split('=', 1)
        try:
            value = int(value)
        except ValueError:
            pass
        kit.update_config({name: value})

    if options.dump:
        if ':' in options.dump:
            fmt, filename = options.dump.split(':', 1)
        else:
            fmt, filename = "yaml", options.dump
        if fmt == "yaml":
            import yaml
            if filename == "-":
                print yaml.dump(kit)
            else:
                with open(filename, "wb") as fp:
                    yaml.dump(kit, fp)
        elif fmt == "pickle":
            import cPickle as pickle
            if filename == "-":
                print pickle.dumps(kit)
            else:
                with open(filename, "wb") as fp:
                    pickle.dump(kit, fp, pickle.HIGHEST_PROTOCOL)
        sys.exit(0)

    kit.run()