<commit_before>import os
from time import sleep
import sys
import subprocess
import hashlib
import argparse


def tag(name):
    subprocess.check_output(['git', 'tag', name, '-m', 'version created for staging deploy'])
    subprocess.check_output(['git', 'push', 'origin-travis', name])


def merge(source, merge_to):
    res1 = subprocess.check_output(['git', 'status']).decode('utf-8').strip()

    print("status on master is" + res1)
    subprocess.check_output(
        ['git', 'checkout', merge_to])

    subprocess.check_output(['git', 'stash'])
    res = subprocess.check_output(['git', 'status']).decode('utf-8').strip()
    print("status on prod is " + res)

    res2 = subprocess.check_output(
        ['git', 'merge', source, '-m', 'merged']).decode('utf-8').strip()
    print(res2)
    subprocess.check_output(
        ['git', 'push', 'origin-travis', merge_to]).decode('utf-8').strip()
    subprocess.check_output(['git', 'stash', 'pop'])


def get_git_version():
    version = os.environ.get("TRAVIS_COMMIT", "")[:7]
    if not version:
        version = subprocess.check_output(
            ['git', '-C', os.path.dirname(__file__), 'describe']).decode('utf-8').strip()
        version = version[:7]
        diff = subprocess.check_output(
            ['git', '-C', os.path.dirname(__file__), 'diff', '--no-ext-diff'])
        if diff:
            version += '-patch' + hashlib.sha1(diff).hexdigest()[:7]
    return "v-" + version


def update_version(version, branch):
    filename = 'buildout.cfg'
    regex = 's/encoded_version.*/encoded_version = %s/' % (version)

    print("updated buildout.cfg with version", version)
    subprocess.check_output(
        ['sed', '-i', regex, filename])
    commit_with_previous_msg(filename, branch)


def commit_with_previous_msg(filename, branch):
    print("adding file to git")
    subprocess.check_output(
        ['git', 'add', filename])

    msg = parse(previous_git_commit())

    print("git commit -m " + msg)
    subprocess.check_output(
        ['git', 'commit', '-m', 'version bump + ' + msg])

    subprocess.check_output(
        ['git', 'push', 'origin-travis', branch])


def previous_git_commit():
    return subprocess.check_output(
        ['git', 'log', '-1']
    ).decode('utf-8').strip()


def parse(commit):
    author, msg = "", ""
    # parse up some commit lines
    commit_lines = commit.split('\n')
    author = commit_lines[1].split(":")[1].strip()
    msg = " ".join(l.strip() for l in commit_lines[3:] if l)

    return "%s - %s" % (author, msg)


def deploy(deploy_to=None):
    '''
    run eb deploy and show the output
    '''
    print("start deployment to elastic beanstalk deploy to is %s" % str(deploy_to))

    wait = [20,40,60,120,120,120]
    for time in wait:
        try:
            if not deploy_to:
                p = subprocess.Popen(['eb', 'deploy'], stderr=subprocess.PIPE)
            else:
                p = subprocess.Popen(['eb', 'deploy', deploy_to], stderr=subprocess.PIPE)
        except Exception:
            # we often get errors due to timeouts
            sleep(time)
        else:
            break

    while True:
        out = p.stderr.read(1)
        out = out.decode('utf-8')
        if out == '' and p.poll() is not None:
            break
        if out != '':
            sys.stdout.write(out)
            sys.stdout.flush()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="update version if relevant and deploy"
    )
    parser.add_argument('--prod', action="store_true", help="deploy to prod")
    args = parser.parse_args()
    branch = os.environ.get("TRAVIS_BRANCH")
    merge_to = os.environ.get("tibanna_merge", "").strip()
    deploy_to = os.environ.get("tibanna_deploy", "").strip()

    if not args.prod:
        print("not production")
        try:
            if deploy_to == 'fourfront-staging':

                print("deploy to staging")
                ver = get_git_version()
                # checkout correct branch
                print("checkout master")
                subprocess.check_output(
                    ['git', 'checkout', branch])

                print("update version")
                update_version(ver, branch)
                if merge_to:
                    print("merge from %s to %s" % (branch, merge_to))
                    merge(branch, merge_to)
                    print("tag it")
                    tag(ver)
        except Exception as e:
            # this can all go wrong if somebody pushes during the build<commit_msg>adding new environments to list of "merge_to_production" environments<commit_after>import os
from time import sleep
import sys
import subprocess
import hashlib
import argparse


def tag(name):
    subprocess.check_output(['git', 'tag', name, '-m', 'version created for staging deploy'])
    subprocess.check_output(['git', 'push', 'origin-travis', name])


def merge(source, merge_to):
    res1 = subprocess.check_output(['git', 'status']).decode('utf-8').strip()

    print("status on master is" + res1)
    subprocess.check_output(
        ['git', 'checkout', merge_to])

    subprocess.check_output(['git', 'stash'])
    res = subprocess.check_output(['git', 'status']).decode('utf-8').strip()
    print("status on prod is " + res)

    res2 = subprocess.check_output(
        ['git', 'merge', source, '-m', 'merged']).decode('utf-8').strip()
    print(res2)
    subprocess.check_output(
        ['git', 'push', 'origin-travis', merge_to]).decode('utf-8').strip()
    subprocess.check_output(['git', 'stash', 'pop'])


def get_git_version():
    version = os.environ.get("TRAVIS_COMMIT", "")[:7]
    if not version:
        version = subprocess.check_output(
            ['git', '-C', os.path.dirname(__file__), 'describe']).decode('utf-8').strip()
        version = version[:7]
        diff = subprocess.check_output(
            ['git', '-C', os.path.dirname(__file__), 'diff', '--no-ext-diff'])
        if diff:
            version += '-patch' + hashlib.sha1(diff).hexdigest()[:7]
    return "v-" + version


def update_version(version, branch):
    filename = 'buildout.cfg'
    regex = 's/encoded_version.*/encoded_version = %s/' % (version)

    print("updated buildout.cfg with version", version)
    subprocess.check_output(
        ['sed', '-i', regex, filename])
    commit_with_previous_msg(filename, branch)


def commit_with_previous_msg(filename, branch):
    print("adding file to git")
    subprocess.check_output(
        ['git', 'add', filename])

    msg = parse(previous_git_commit())

    print("git commit -m " + msg)
    subprocess.check_output(
        ['git', 'commit', '-m', 'version bump + ' + msg])

    subprocess.check_output(
        ['git', 'push', 'origin-travis', branch])


def previous_git_commit():
    return subprocess.check_output(
        ['git', 'log', '-1']
    ).decode('utf-8').strip()


def parse(commit):
    author, msg = "", ""
    # parse up some commit lines
    commit_lines = commit.split('\n')
    author = commit_lines[1].split(":")[1].strip()
    msg = " ".join(l.strip() for l in commit_lines[3:] if l)

    return "%s - %s" % (author, msg)


def deploy(deploy_to=None):
    '''
    run eb deploy and show the output
    '''
    print("start deployment to elastic beanstalk deploy to is %s" % str(deploy_to))

    wait = [20, 40, 60, 120, 120, 120, 120]
    for time in wait:
        try:
            if not deploy_to:
                p = subprocess.Popen(['eb', 'deploy'], stderr=subprocess.PIPE)
            else:
                p = subprocess.Popen(['eb', 'deploy', deploy_to], stderr=subprocess.PIPE)
        except Exception:
            # we often get errors due to timeouts
            sleep(time)
        else:
            break

    while True:
        out = p.stderr.read(1)
        out = out.decode('utf-8')
        if out == '' and p.poll() is not None:
            break
        if out != '':
            sys.stdout.write(out)
            sys.stdout.flush()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="update version if relevant and deploy"
    )
    parser.add_argument('--prod', action="store_true", help="deploy to prod")
    args = parser.parse_args()
    branch = os.environ.get("TRAVIS_BRANCH")
    merge_to = os.environ.get("tibanna_merge", "").strip()
    deploy_to = os.environ.get("tibanna_deploy", "").strip()

    if not args.prod:
        print("not production")
        try:
            if deploy_to in ['fourfront-staging', 'fourfront-webprod', 'fourfront-webprod2']:
                print("deploy to staging")
                ver = get_git_version()
                # checkout correct branch
                print("checkout master")
                subprocess.check_output(
                    ['git', 'checkout', branch])

                print("update version")
                update_version(ver, branch)
                if merge_to:
                    print("merge from %s to %s" % (branch, merge_to))
                    merge(branch, merge_to)
                    print("tag it")
                    tag(ver)
        except Exception as e:
            # this can all go wrong if somebody pushes during the build
{'fraction_non_alphanumeric': 0.15151185555797259, 'fraction_numerical': 0.008048727430933217, 'mean_word_length': 2.4580669424595714, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942823', 'n_tokens_mistral': 2879, 'n_tokens_neox': 2761, 'n_words': 687}