コード例 #1
0
def _parser_callback(args):
    if args.instance_path and not path.isdir(args.instance_path):
        raise argparse.ArgumentTypeError(
            "Directory {} is not a valid path!".format(args.instance_path))

    if args.workers < 1:
        print("{} is not a positive number!".format(args.workers))
        return 1

    storage_old = Storage.create(args.instance_path)
    comparer = TemplateComparer()
    instance_comparer = InstanceComparer()

    if args.template:
        service_template_new = args.template.name
    else:
        print("Template file for update was not supplied.")
        return 1

    try:
        if args.inputs:
            inputs_new = yaml.safe_load(args.inputs)
        else:
            inputs_new = {}
    except YAMLError as e:
        print("Invalid inputs: {}".format(e))
        return 1

    workdir_old = get_workdir(storage_old)

    try:
        with tempfile.TemporaryDirectory() as temp_path:
            storage_new = Storage.create(temp_path)
            storage_new.write_json(inputs_new, "inputs")
            storage_new.write(service_template_new, "root_file")
            workdir_new = get_workdir(storage_new)

            instance_diff = diff_instances(storage_old, workdir_old,
                                           storage_new, workdir_new, comparer,
                                           instance_comparer, args.verbose)

            update(storage_old,
                   workdir_old,
                   storage_new,
                   workdir_new,
                   instance_comparer,
                   instance_diff,
                   args.verbose,
                   args.workers,
                   overwrite=True)

    except ParseError as e:
        print("{}: {}".format(e.loc, e))
        return 1
    except DataError as e:
        print(str(e))
        return 1

    return 0
コード例 #2
0
ファイル: diff.py プロジェクト: xlab-si/xopera-opera
def _parser_callback(args):
    if args.instance_path and not path.isdir(args.instance_path):
        raise argparse.ArgumentTypeError(
            f"Directory {args.instance_path} is not a valid path!")

    storage_old = Storage.create(args.instance_path)
    comparer = TemplateComparer()

    if args.template:
        service_template_new_path = Path(args.template.name)
    else:
        print("Template file for comparison was not supplied.")
        return 1

    if storage_old.exists("root_file"):
        service_template_old_path = Path(storage_old.read("root_file"))
    else:
        print("There is no root_file in storage.")
        return 1

    if storage_old.exists("inputs"):
        inputs_old = storage_old.read_json("inputs")
    else:
        inputs_old = {}

    try:
        if args.inputs:
            inputs_new = yaml.safe_load(args.inputs)
        else:
            inputs_new = {}
    except yaml.YAMLError as e:
        print(f"Invalid inputs: {e}")
        return 1

    workdir_old = get_workdir(storage_old)
    workdir_new = Path(service_template_new_path.parent)

    try:
        if args.template_only:
            template_diff = diff_templates(service_template_old_path,
                                           workdir_old, inputs_old,
                                           service_template_new_path,
                                           workdir_new, inputs_new, comparer,
                                           args.verbose)
        else:
            instance_comparer = InstanceComparer()
            with tempfile.TemporaryDirectory() as temp_path:
                storage_new = Storage.create(temp_path)
                storage_new.write_json(inputs_new, "inputs")
                storage_new.write(str(service_template_new_path), "root_file")
                template_diff = diff_instances(storage_old, workdir_old,
                                               storage_new, workdir_new,
                                               comparer, instance_comparer,
                                               args.verbose)
        outputs = template_diff.outputs()
        if args.output:
            save_outputs(outputs, args.format, args.output)
        else:
            print(format_outputs(outputs, args.format))
    except ParseError as e:
        print(f"{e.loc}: {e}")
        return 1
    except DataError as e:
        print(str(e))
        return 1

    return 0