Example #1
0
def download(ctx, file, remote, force):
    """Download the files/directories

    This command is generally not needed, since the restore command implies
    downloading.

    """
    file = set(file or config.git_record.readonly.keys())
    remote_names = {r[1] for r in get_refs(remote)}
    local_names = {config.git_record.readonly[fil][-1]["name"] for fil in file}
    locally_avail_names = {r[1] for r in get_refs()}
    names = local_names.copy()
    if not force:
        for name in local_names & locally_avail_names:
            local_file = {
                fil
                for fil in file
                if config.git_record.readonly[fil][-1]["name"] == name
            }
            names.remove(name)
            for fil in local_file:
                file.remove(fil)
                LOGGER.info("{} is already there"
                            " because the git file {} is available"
                            " (use --force to still download it)".format(
                                fil,
                                name,
                            ))
    local_names = names.copy()
    for name in local_names - remote_names:
        local_file = {
            fil
            for fil in file
            if config.git_record.readonly[fil][-1]["name"] == name
        }
        names.remove(name)
        for fil in local_file:
            file.remove(fil)
            LOGGER.error("{} is not available in"
                         " {} because the git file {} is missing".format(
                             fil,
                             remote,
                             name,
                         ))
    if file:
        ctx.invoke(
            git_store_fetch,
            remote=remote,
            name=["refs/git-store/" + name for name in names],
        )
        LOGGER.status("Downloaded {}".format(", ".join(file)))
Example #2
0
def _upload(ctx, file, remote, force):
    """Upload the files/directorie

    So that anyone can download them.
"""
    file = set(file or config.git_record.readonly.keys())
    remote_names = {r[1] for r in get_refs(remote)}
    local_names = {config.git_record.readonly[fil][-1]["name"] for fil in file}
    locally_avail_names = {r[1] for r in get_refs()}
    names = local_names.copy()
    for name in local_names - locally_avail_names:
        local_file = {
            fil
            for fil in file
            if config.git_record.readonly[fil][-1]["name"] == name
        }
        names.remove(name)
        for fil in local_file:
            file.remove(fil)
            LOGGER.error("{} is not there"
                         " because the git file {} is not available."
                         " It won't be uploaded to {}".format(
                             fil,
                             name,
                             remote,
                         ))
    local_names = names.copy()
    if not force:
        for name in local_names & remote_names:
            local_file = {
                fil
                for fil in file
                if config.git_record.readonly[fil][-1]["name"] == name
            }
            names.remove(name)
            for fil in local_file:
                file.remove(fil)
                LOGGER.info("{} is already in"
                            " {} because the git file {} is already there"
                            " (use --force to still upload)".format(
                                fil,
                                remote,
                                name,
                            ))
    if file:
        ctx.invoke(
            git_store_push,
            remote=remote,
            name=["refs/git-store/" + name for name in names],
        )
        LOGGER.status("Uploaded {}".format(", ".join(file)))
Example #3
0
def prune_git_files(ctx):
    """Remove all the git files not in the git-record

    It removes from git all the files that are no more references by git
    records. Use this command only if you know what you are doing.

    """
    for ref in [r[1] for r in get_refs()]:
        if ref not in [
                record["name"]
                for records in config.git_record.readonly.values()
                for record in records
        ]:
            ctx.invoke(git_store_remove, name="refs/git-store/" + ref)
Example #4
0
def show(format, all, fields, index):
    """Show the git records

    The first value is the path where the file/directory should be located. Ths
    second value is the name of the file in the git store. Since git is content
    addressable, a same git store name indicates a same content.

    """
    if index and all:
        raise click.UsageError("--index and --all are mutually exclusives")

    index = index or 1
    locally_avail_names = {
        r[1]
        for r in get_refs()
    }

    def format_records(records):
        def format_record(record):
            res = record["name"]
            if "documentation" in record and record["documentation"]:
                res += " '{}'".format(record["documentation"])
            if record["name"] in locally_avail_names:
                res += " (in here)"
            else:
                res += " (not in here)"
            if "date" in record:
                res += " (Recorded at {})".format(dateutil.parser.parse(record["date"]))
            return res

        top = records[-index]
        res = format_record(top)
        if len(records) > 1:
            res += " ({}/{})".format(index, len(records))
        if all and len(records) > 1:
            res += ". Other records: {}".format(
                ", ".join(
                    "{}: {}".format(index_ + 1, format_record(record))
                    for index_, record in enumerate(reversed(records[:-1]))
                )
            )
        return res

    with TablePrinter(fields, format) as tp:
        for key in sorted(config.git_record.readonly.keys()):
            tp.echo(key, format_records(config.git_record.readonly[key]))