def verify_repository_is_working(step):
    if Dist.is_centos_family(Dist.from_name(CONF.feature.dist)):
        branch = 'test/update-system-rpm'
        commits = ["@update-system @rpm @postinst", "@update-system @rpm @fatal-error"]
    elif Dist.is_debian_family(Dist.from_name(CONF.feature.dist)):
        branch = 'test/update-system-deb'
        commits = ["@update-system @deb @postinst", "@update-system @deb @fatal-error"]
    elif CONF.feature.dist.startswith('win'):
        branch = 'test/update-system-win'
        commits = ["@update-system @win @postinst", "@update-system @win @fatal-error"]
    else:
        raise AssertionError('Don\'t know what branch use!')
    LOG.info('Used scalarizr branch is %s' % branch)
    setattr(world, 'scalarizr_branch', branch)

    if not os.path.isdir(SCALARIZR_REPO_PATH):
        LOG.info('Clone scalarizr repo')
        exit_code = subprocess.call(['git', 'clone', '-b', branch, SCALARIZR_GITHUB_PATH, SCALARIZR_REPO_PATH],
                                    stderr=subprocess.PIPE)
        if not exit_code == 0:
            raise AssertionError('Error in git clone!')

    os.chdir(SCALARIZR_REPO_PATH)

    LOG.info('Merge feature/update-system to this repository')

    merge_proc = subprocess.Popen(['git', 'merge', '-m', 'merge parent branch', 'origin/feature/update-system'],
                                  stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    merge_proc.wait()
    LOG.debug('Merge process result: %s' % merge_proc.stdout.read())

    if merge_proc.returncode != 0:
        raise AssertionError('Something wrong in git merge, please see log: %s' % merge_proc.stdout.read())

    last_revision = subprocess.check_output(['git', 'log', '--pretty=oneline', '-1']).splitlines()[0].split()[0].strip()
    LOG.info('Last pushed revision: %s' % last_revision)
    setattr(world, 'last_scalarizr_revision', last_revision)

    git_log = subprocess.check_output(['git', 'log', '--pretty=oneline', '-20']).splitlines()
    LOG.info('Get latest commits from git history: %s' % git_log)
    flags = {}
    for log in git_log:
        commit, message = log.split(' ', 1)
        message = message.strip()
        for m in commits:
            if message.startswith('Revert "%s"' % m) or message.startswith("Revert '%s'" % m):
                flags[m] = True
            elif message.startswith(m) and not flags.get(m, False):
                # revert last commit
                LOG.info('Revert broken commit "%s %s"' % (commit, message))
                subprocess.call(['git', 'revert', commit, '-n', '--no-edit'])
                subprocess.call(['git', 'commit', '-a', '-m', "Revert '%s'" % message])
                last_revision = subprocess.check_output(['git', 'log', '--pretty=oneline', '-1']).splitlines()[0].split()[0].strip()
                LOG.info('Last pushed revision: %s' % last_revision)
                setattr(world, 'last_scalarizr_revision', last_revision)
                flags[m] = True
        if len(flags) == len(commits):
            break
    LOG.debug('Push changes to working branch')
    subprocess.call(['git', 'push'])
def broke_scalarizr_branch(step, comment):
    LOG.debug('Git work dir: %s' % SCALARIZR_REPO_PATH)
    os.chdir(SCALARIZR_REPO_PATH)

    if Dist.is_centos_family(Dist.from_name(CONF.feature.dist)):
        tag = '@rpm'
    elif Dist.is_debian_family(Dist.from_name(CONF.feature.dist)):
        tag = '@deb'
    elif CONF.feature.dist.startswith('win'):
        tag = '@win'

    comment = comment.split()
    comment.insert(-1, tag)
    comment = ' '.join(comment)

    command = ['git', 'log', '--pretty=oneline', "--grep=Revert '%s'" % comment]
    LOG.debug('Execute grep by git: %s' % command)
    git_log = subprocess.check_output(command, stderr=subprocess.PIPE).strip().splitlines()
    if not git_log:
        command = ['git', 'log', '--pretty=oneline', '--grep=Revert "%s"' % comment]
        LOG.debug('Execute grep by git: %s' % command)
        git_log = subprocess.check_output(command, stderr=subprocess.PIPE).strip().splitlines()
    LOG.info('Get latest commits from git history (in broke step): %s' % git_log)
    splitted_log = git_log[0].split(' ', 2)
    commit = splitted_log[0]
    message = splitted_log[-1].replace('"', '').replace("'", '')
    LOG.info('Revert commit "%s %s" for brake repository' % (commit, message))
    subprocess.call(['git', 'revert', commit, '-n', '--no-edit'])
    subprocess.call(['git', 'commit', '-a', '-m', '%s' % message])
    last_revision = subprocess.check_output(['git', 'log', '--pretty=oneline', '-1']).splitlines()[0].split()[0].strip()
    LOG.info('Last pushed revision: %s' % last_revision)
    setattr(world, 'last_scalarizr_revision', last_revision)
    subprocess.call(['git', 'push'])
    time.sleep(60)