def main():
    from sys import stdin
    import yaml
    config = yaml.safe_load(stdin)
    config = BranchConfig.wrap(config)
    config.normalize()
    if not config.check_trunk_is_recent():
        print("The trunk is not based on a very recent commit")
        print("Consider using one of the following:")
        print(git_recent_tags())
        exit(1)
    args = set(sys.argv[2:])
    verbose = '-v' in args
    do_push = '--no-push' not in args
    args.discard('-v')
    args.discard('--no-push')
    if not args:
        args = set('fetch sync rebuild'.split())
    with DisableGitHooks(), ShVerbose(verbose):
        if 'fetch' in args:
            fetch_remote(config)
        if 'sync' in args:
            sync_local_copies(config, push=do_push)
        if 'rebuild' in args:
            rebuild_staging(config, push=do_push)
예제 #2
0
def git_check_merge(branch1, branch2, git=None):
    """
    returns True if branch1 would auto-merge cleanly into branch2,
    False if the merge requires human assistance

    Thanks to http://stackoverflow.com/a/501461/240553

    """
    git = git or get_git()
    with ShVerbose(False):
        orig_branch = git_current_branch(git)
        git.checkout(branch2)
        is_behind = git.log('{0}..{1}'.format(branch2, branch1),
                            max_count=1).strip()
        if is_behind:
            try:
                git.merge('--no-commit', '--no-ff', branch1).strip()
            except sh.ErrorReturnCode_1:
                # git merge returns 1 when there's a conflict
                return False
            else:
                return True
            finally:
                git.merge('--abort')
                git.checkout(orig_branch)
        else:
            return True
예제 #3
0
def main():
    from sys import stdin
    import yaml
    config = yaml.load(stdin)
    config = BranchConfig.wrap(config)
    config.normalize()
    args = set(sys.argv[1:])
    verbose = '-v' in args
    do_push = '--no-push' not in args
    args.discard('-v')
    args.discard('--no-push')
    if not args:
        args = set('fetch sync rebuild'.split())
    with DisableGitHooks(), ShVerbose(verbose):
        if 'fetch' in args:
            fetch_remote(config)
        if 'sync' in args:
            sync_local_copies(config, push=do_push)
        if 'rebuild' in args:
            rebuild_staging(config, push=do_push)
예제 #4
0
def main():
    import argparse
    import yaml

    parser = argparse.ArgumentParser(
        description='Rebuild the deploy branch for an environment')
    parser.add_argument("env", help="Name of the environment")
    parser.add_argument("actions", nargs="*")
    parser.add_argument("--commcare-hq-root",
                        help="Path to cloned commcare-hq repository",
                        default=os.environ.get("COMMCARE_HQ_ROOT"))
    parser.add_argument("-v", "--verbose")
    parser.add_argument(
        "--no-push",
        action="store_true",
        help="Do not push the changes to remote git repository.")
    args = parser.parse_args()

    if not args.commcare_hq_root:
        print(
            red("Path to commcare-hq repository must be provided.\n"
                "Use '--commcare-hq-root=[path]' or set the 'COMMCARE_HQ_ROOT' environment variable."
                ))
        exit(1)

    config_path = os.path.join("environments", args.env, "deploy_branches.yml")

    git = get_git()
    print("Fetching master")
    git.fetch("origin", "master")
    if not args.no_push:
        print("Checking branch config for modifications")
        if git.diff("origin/master", "--", config_path):
            print(
                red("'{}' on this branch different from the one on master".
                    format(config_path)))
            exit(1)

    with open(config_path) as config_yaml:
        config = yaml.safe_load(config_yaml)

    if "trunk" in config:
        config = BranchConfig.wrap(config)
        config.normalize()
        repositories = {"dimagi/commcare-hq": config}
    elif "dimagi/commcare-hq" in config:
        repositories = {
            repo: BranchConfig.wrap(repo_config)
            for repo, repo_config in config.items()
        }
        for repo, repo_config in repositories.items():
            repo_config.normalize()
            if repo == "dimagi/commcare-hq":
                repo_config.root = os.path.abspath(args.commcare_hq_root)
            else:
                env_var = "{}_ROOT".format(re.sub("[/-]", "_", repo).upper())
                code_root = os.environ.get(env_var)
                if not code_root:
                    code_root = raw_input(
                        "Please supply the location of the '{}' repo: ".format(
                            repo))

                if not code_root or not os.path.exists(code_root):
                    print(
                        red("Repo path must be supplied. "
                            "Consider setting the '{}' environment variable".
                            format(env_var)))
                    exit(1)
                repo_config.root = os.path.abspath(code_root)
    else:
        print(red("Unexpected format for config file."))
        exit(1)

    for config in repositories.values():
        if not config.check_trunk_is_recent(config.root):
            print("The trunk is not based on a very recent commit")
            print("Consider using one of the following:")
            print(git_recent_tags(config.root))
            exit(1)
    if not args.actions:
        args.actions = 'fetch sync rebuild'.split()
    push = not args.no_push
    with DisableGitHooks(), ShVerbose(args.verbose):
        for repo, config in repositories.items():
            print("\nRebuilding '{}' branch in '{}' repo.".format(
                config.name, repo))
            if 'fetch' in args.actions:
                fetch_remote(config)
            if 'sync' in args.actions:
                sync_local_copies(config, push=push)
            if 'rebuild' in args.actions:
                rebuild_staging(config, push=push)
예제 #5
0
        except sh.ErrorReturnCode_1:
            self.already_disabled = True
        else:
            sh.mv(self.path, self.hidden_path)

    def __exit__(self, exc_type, exc_val, exc_tb):
        if not self.already_disabled:
            sh.mv(self.hidden_path, self.path)


if __name__ == '__main__':
    from sys import stdin
    import yaml
    config = yaml.load(stdin)
    config = BranchConfig.wrap(config)
    config.normalize()
    args = set(sys.argv[1:])
    verbose = '-v' in args
    args.discard('-v')
    if not args:
        args = set('fetch sync check rebuild'.split())
    with DisableGitHooks(), ShVerbose(verbose):
        if 'fetch' in args:
            fetch_remote(config)
        if 'sync' in args:
            sync_local_copies(config)
        if 'check' in args:
            check_merges(config)
        if 'rebuild' in args:
            rebuild_staging(config)