예제 #1
0
def _construct_provided_module(name, data, url, commit):
    # At this point the @commmit part should be removed from url so:
    # either url should not have an @,
    # or the @ should be for [email protected]
    assert "@" not in url or url.rindex(".") > url.rindex("@")

    module = OrderedDict()
    module["name"] = name
    if "description" not in data:
        user_error(
            "missing required key 'description' in module definition: %s" %
            pretty(data))
    module["description"] = data["description"]
    module["url"] = url
    module["commit"] = commit
    subdirectory = data.get("subdirectory")
    if subdirectory:
        module["subdirectory"] = subdirectory
    dependencies = data.get("dependencies")
    if dependencies:
        module["dependencies"] = dependencies
    if "steps" not in data:
        user_error("missing required key 'steps' in module definition: %s" %
                   pretty(data))
    module["steps"] = data["steps"]
    module["added_by"] = "cfbs add"
    return module
예제 #2
0
 def check_existence(self, modules: list):
     for module in modules:
         assert isinstance(module, Module)
         if not self.exists(module):
             user_error("Module '%s'%s does not exist" % (
                 module.name,
                 " version '%s'" % module.version if module.version else "",
             ))
예제 #3
0
파일: main.py 프로젝트: nickanderson/cpm
def main() -> int:
    args = get_args()
    set_log_level(args.loglevel)

    if args.version:
        print(f"cfbs {version()}")
        return 0

    if not args.command:
        user_error("Usage: cfbs COMMAND")

    # Commands you can run outside a cfbs repo:
    if args.command == "init":
        return commands.init_command()
    if args.command == "search":
        return commands.search_command(args.args)
    if args.command == "pretty":
        return commands.pretty_command(args.args)

    if not commands.is_cfbs_repo():
        user_error("This is not a cfbs repo, to get started, type: cfbs init")

    if args.command == "status":
        return commands.status_command()
    if args.command == "add":
        return commands.add_command(args.args)
    if args.command == "download":
        return commands.download_command(args.force)
    if args.command == "build":
        return commands.build_command()
    if args.command == "install":
        return commands.install_command(args.args)

    user_error(f"Command '{args.command}' not found")
예제 #4
0
파일: main.py 프로젝트: cfengine/cfbs
def main() -> int:
    args = get_args()
    init_logging(args.loglevel)

    if args.version:
        print("cfbs %s" % version())
        return 0

    if not args.command:
        print_help()
        print("")
        user_error("No command given")

    if args.non_interactive and args.command not in (
            "init",
            "add",
            "remove",
            "clean",
            "update",
    ):
        user_error("The option --non-interactive is not for cfbs %s" %
                   (args.command))

    if args.non_interactive:
        print("""
Warning: The --non-interactive option is only meant for testing (!)
         DO NOT run commands with --non-interactive as part of your deployment
         pipeline. Instead, run cfbs commands manually, commit the resulting
         cfbs.json and only run cfbs build + cfbs install when deploying your
         policy set. Thank you for your cooperation.
""".strip())

    # Commands you can run outside a cfbs repo:
    if args.command == "help":
        print_help()
        return 0

    CFBSConfig.get_instance(args.index, args.non_interactive)

    if args.command == "init":
        return commands.init_command(index=args.index,
                                     non_interactive=args.non_interactive)

    if args.command == "search":
        return commands.search_command(args.args)
    if args.command == "pretty":
        return commands.pretty_command(args.args, args.check, args.keep_order)
    if args.command == "validate":
        return commands.validate_command()
    if args.command in ("info", "show"):
        return commands.info_command(args.args)

    if not is_cfbs_repo():
        user_error("This is not a cfbs repo, to get started, type: cfbs init")

    if args.command == "status":
        return commands.status_command()
    if args.command == "add":
        return commands.add_command(
            args.args,
            checksum=args.checksum,
        )
    if args.command == "remove":
        return commands.remove_command(args.args)
    if args.command == "clean":
        return commands.clean_command()
    if args.command == "download":
        return commands.download_command(args.force)
    if args.command == "build":
        return commands.build_command()
    if args.command == "install":
        return commands.install_command(args.args)
    if args.command == "update":
        return commands.update_command(args.args)

    print_help()
    user_error("Command '%s' not found" % args.command)
예제 #5
0
파일: build.py 프로젝트: cfengine/cfbs
def _perform_build_step(module, step, max_length):
    step = step.split(" ")
    operation, args = step[0], step[1:]
    source = module["_directory"]
    counter = module["_counter"]
    destination = "out/masterfiles"

    prefix = "%03d %s :" % (counter, pad_right(module["name"], max_length))

    if operation == "copy":
        src, dst = args
        if dst in [".", "./"]:
            dst = ""
        print("%s copy '%s' 'masterfiles/%s'" % (prefix, src, dst))
        src, dst = os.path.join(source, src), os.path.join(destination, dst)
        cp(src, dst)
    elif operation == "run":
        shell_command = " ".join(args)
        print("%s run '%s'" % (prefix, shell_command))
        sh(shell_command, source)
    elif operation == "delete":
        files = [args] if type(args) is str else args
        assert len(files) > 0
        as_string = " ".join(["'%s'" % f for f in files])
        print("%s delete %s" % (prefix, as_string))
        for file in files:
            rm(os.path.join(source, file))
    elif operation == "json":
        src, dst = args
        if dst in [".", "./"]:
            dst = ""
        print("%s json '%s' 'masterfiles/%s'" % (prefix, src, dst))
        if not os.path.isfile(os.path.join(source, src)):
            user_error("'%s' is not a file" % src)
        src, dst = os.path.join(source, src), os.path.join(destination, dst)
        extras, original = read_json(src), read_json(dst)
        if not extras:
            print("Warning: '%s' looks empty, adding nothing" %
                  os.path.basename(src))
        if original:
            merged = merge_json(original, extras)
        else:
            merged = extras
        write_json(dst, merged)
    elif operation == "append":
        src, dst = args
        if dst in [".", "./"]:
            dst = ""
        print("%s append '%s' 'masterfiles/%s'" % (prefix, src, dst))
        src, dst = os.path.join(source, src), os.path.join(destination, dst)
        if not os.path.exists(dst):
            touch(dst)
        assert os.path.isfile(dst)
        sh("cat '%s' >> '%s'" % (src, dst))
    elif operation == "directory":
        src, dst = args
        if dst in [".", "./"]:
            dst = ""
        print("{} directory '{}' 'masterfiles/{}'".format(prefix, src, dst))
        dstarg = dst  # save this for adding .cf files to inputs
        src, dst = os.path.join(source, src), os.path.join(destination, dst)
        defjson = os.path.join(destination, "def.json")
        merged = read_json(defjson)
        if not merged:
            merged = {}
        if "classes" not in merged:
            merged["classes"] = {}
        if "services_autorun_bundles" not in merged["classes"]:
            merged["classes"]["services_autorun_bundles"] = ["any"]
        inputs = []
        for root, dirs, files in os.walk(src):
            for f in files:
                if f.endswith(".cf"):
                    inputs.append(os.path.join(dstarg, f))
                    cp(os.path.join(root, f),
                       os.path.join(destination, dstarg, f))
                elif f == "def.json":
                    extra = read_json(os.path.join(root, f))
                    if extra:
                        merged = merge_json(merged, extra)
                else:
                    cp(os.path.join(root, f),
                       os.path.join(destination, dstarg, f))
        if "inputs" in merged:
            merged["inputs"].extend(inputs)
        else:
            merged["inputs"] = inputs
        write_json(defjson, merged)
    else:
        user_error("Unknown build step operation: %s" % operation)