Exemple #1
0
def validate_register(args):
    if not args.category:
        error("Requires a category for action \"{}\"".format(args.subcmd))
    if not args.edit and not args.targets:
        error(
            "Specify a target to register, or use --edit to use external editor"
        )
Exemple #2
0
def start(args):
    handlers = get_commands_handlers()
    if args.subcmd in handlers.keys():
        ret = handlers[args.subcmd](args)
        if ret is None:
            return 0
        else:
            return ret
    else:
        error("Subcommand not recognized: {}".format(args.subcmd))
Exemple #3
0
def validate_args(args, parser):
    if (not args.subcmd) and (not hasattr(args, "category")):
        parser.print_help()
        sys.exit(0)

    if args.subcmd in REQUIRES_CAT and REQUIRES_CAT[args.subcmd]:
        if not args.category[0] in get_categories_list():
            error("Category \"{}\" does not exist".format(args.category[0]))
    if not args.subcmd:
        parser.print_help()
    elif args.subcmd in SUBCMD_FCTS.keys():
        # Validate args, call function validate_<name>
        SUBCMD_FCTS[args.subcmd][1](args)
    else:
        error("Subcommand {} not recognized".format(args.subcmd))
Exemple #4
0
def __restic_backup(category, excl, incl):
    all_excludes = generate_excludes(excl)
    out_repo = get_category_repodir(category)
    if not os.path.exists(os.path.join(out_repo, "config")):
        progress("Init restic repository...", heading=category)
        if not os.path.isdir(out_repo):
            os.mkdir(out_repo)
        ret = call_cmdline("RESTIC_PASSWORD_FILE={} restic init -r {}".format(
            gcst.MEMORY_PWD, out_repo))
    progress("Backup in progress...", heading=category)
    ret = call_cmdline(
        "RESTIC_PASSWORD_FILE={} restic backup -r {} {} -- {}".format(
            gcst.MEMORY_PWD, out_repo, all_excludes,
            " ".join([p for p in incl if os.path.exists(p)])))
    if ret:
        error("Restic command failed for category {}".format(category))

    progress("Success", heading=category)
Exemple #5
0
def __edit_list_in_plaintext(fname, key_name, name="unknown", **kwargs):
    data = extract_from_file(fname)
    if key_name not in data.keys():
        error("Key {} not found in \"{}\" config file, cannot edit".format(
            key_name, name))

    tmpdir = get_tmp_dir()
    tmp_fname = os.path.join("/tmp/", tmpdir, key_name)
    dump_to_user(tmp_fname, data[key_name])

    call_cmdline(GlobalConstants.EDITOR_CMD + tmp_fname,
                 stdout=None,
                 stderr=None)

    user_input = load_user_input(tmp_fname, **kwargs)
    rm_tmp_dir(tmpdir)
    data[key_name] = user_input
    export_to_file(fname, data)
    return data
Exemple #6
0
def validate_config(args):
    if not args.category:
        error("Requires a category for action \"{}\"".format(args.subcmd))
    if len(args.category) > 1:
        error("Cannot perform \"{}\" action on more than 1 category".format(
            args.subcmd))
    if not args.config_cmd:
        error("Requires a command")
Exemple #7
0
def validate_exclude(args):
    if not args.exclude_type:
        error("Need to specify what type of exclusion will you use")
    if not args.category:
        error("Requires a category for action \"{}\"".format(args.subcmd))
    if not args.edit and not args.excludes:
        error(
            "Specify a rule to exclude, or use --edit to use external editor")
Exemple #8
0
def edit_list_in_plaintext(fname,
                           keys_names,
                           name="unknown",
                           recurs=0,
                           dirname=None,
                           **kwargs):
    if len(keys_names) > 1:
        data = extract_from_file(fname)
        key_name = keys_names.pop(0)
        if key_name not in data.keys():
            error(
                "Key {} not found in \"{}\" config file (recursion {})".format(
                    key_name, name, recurs))
        if dirname is None:
            dirname = get_tmp_dir()

        tmp_fname = os.path.join("/tmp/", dirname, key_name)
        export_to_file(tmp_fname, data[key_name])
        ret = edit_list_in_plaintext(tmp_fname,
                                     keys_names,
                                     name=name,
                                     recurs=recurs + 1,
                                     dirname=dirname,
                                     **kwargs)

        data[key_name] = extract_from_file(tmp_fname)
        rm_tmp_dir(dirname)
        export_to_file(fname, data)
        return ret
    elif len(keys_names) == 1:
        ret = __edit_list_in_plaintext(fname,
                                       keys_names[0],
                                       name=name,
                                       **kwargs)
    else:
        error("No keys passed as arguments")
    return ret
Exemple #9
0
def validate_edit(args):
    if not args.category or len(args.category) > 1:
        error("Requires a category for action \"{}\"".format(args.subcmd))
    if not check_category_exist(args.category[0]):
        error("Category {} does not exist".format(args.category[0]))
Exemple #10
0
def validate_prune(args):
    if args.keep < 1:
        error("Must keep at least 1 snapshot")
Exemple #11
0
def rm_tmp_dir(dirname):
    if dirname[:4] == "mem.":
        shutil.rmtree(os.path.join("/tmp/", dirname))
    else:
        error("Attempt to remove directory not temporary")
Exemple #12
0
def __validate_register(reg):
    if (GlobalConstants.INCLUDE_TEXT
            not in reg.keys()) or (GlobalConstants.EXCLUDE_TEXT
                                   not in reg.keys()):
        error("Register file corrupted")
Exemple #13
0
def __validate_load_userinp(data):
    if any([f in data for f in GlobalConstants.CONFIG_FORBIDDEN_CHARS]):
        error("Error while loading user configuration, forbidden char used")
    data.replace("\\ ", " ").replace(" ", "\\ ")
Exemple #14
0
def validate_dump_to_user(data):
    if type(data) != list:
        error("Cannot edit other configurations than simple string lists")