""" Ensure that all repository are cloned and at their tip
"""
import errno
import os
import subprocess
from os.path import isdir, join
import yaml
import urllib

def read_configuration(config_path):
    with open(config_path) as config_file:
        return yaml.load(config_file.read())


def clone_repositories(config, partial_revset, repos_dir):
    cachedir = join(repos_dir, ".cache")
    try:
        os.makedirs(cachedir)
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise
    for repo_name, repo in config["repos"].items():
        clonedir = join(repos_dir, repo_name)
        print partial_revset
        for pset, setconfig in partial_revset.items():
            revset = setconfig.get('remove')
            repo_suffix = urllib.quote_plus(revset)
            partialdir = join(cachedir, "partial-{}-{}".format(repo_name, repo_suffix))
            if not isdir(partialdir):
                print(
                    "Cloning %s (%s%s) into %s (for exchange benchmarks)"
                    % (
                        repo_name,
                        clonedir,
                        " stripped of %s" % (revset,) if revset != "same" else "",
                        partialdir,
                    )
                )
                subprocess.check_call(
                    ["hg", "clone", "--noupdate", clonedir, partialdir]
                )
                if revset is not None:
                    subprocess.check_call(
                        [
                            "hg",
                            "--cwd",
                            partialdir,
                            "--config",
                            "extensions.strip=",
                            "strip",
                            "--no-backup",
                            "-r",
                            revset,
                        ]
                    )
                    subprocess.check_call(
                        ["hg", "--cwd", partialdir, "debugupdatecache"]
                    )

partial_revset = read_configuration("partial-sets.yaml")


config = read_configuration("config.yaml")
clone_repositories(config, partial_revset['partial-sets'], "repos")