Python sh.git() Examples

The following are 15 code examples of sh.git(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sh , or try the search function .
Example #1
Source File: benchmark.py    From cstar_perf with Apache License 2.0 6 votes vote down vote up
def build_stress(stress_revision, name=None):
    # Build a stress revision

    try:
        git_id = sh.git('--git-dir={home}/fab/cassandra.git'
                        .format(home=HOME), 'rev-parse', stress_revision).strip()
    except sh.ErrorReturnCode:
        raise AssertionError('Invalid stress_revision: {}'.format(stress_revision))

    path = os.path.join(CASSANDRA_STRESS_PATH, git_id)
    if not os.path.exists(path):
        logger.info("Building cassandra-stress '{}' in '{}'.".format(stress_revision, path))
        os.makedirs(path)
        sh.tar(
            sh.git("--git-dir={home}/fab/cassandra.git".format(home=HOME), "archive", git_id),
            'x', '-C', path
        )
        antcmd('-Dbasedir={}'.format(path), '-f', '{}/build.xml'.format(path),
               'realclean', 'jar', _env={"JAVA_TOOL_OPTIONS": "-Dfile.encoding=UTF8",
                                         "JAVA_HOME": JAVA_HOME})

    name = name if name else stress_revision
    return {name: git_id} 
Example #2
Source File: flamegraph.py    From cstar_perf with Apache License 2.0 6 votes vote down vote up
def setup(flamegraph_directory, flamegraph_path, perf_map_agent_path, java_home):
    """Setup deps for flamegraph"""

    # Create the flamegraph directory and clean the directory
    if not os.path.exists(flamegraph_directory):
        os.mkdir(flamegraph_directory)
    for f in os.listdir(flamegraph_directory):
        file_path = os.path.join(flamegraph_directory, f)
        sh.sudo.rm(file_path)

    if not os.path.exists(perf_map_agent_path):
        sh.git('clone', 'https://github.com/jrudolph/perf-map-agent', perf_map_agent_path)
        sh.cmake('.', _cwd=perf_map_agent_path, _env={'JAVA_HOME': java_home})
        sh.make(_cwd=perf_map_agent_path)

    if not os.path.exists(flamegraph_path):
        sh.git('clone', 'https://github.com/brendangregg/FlameGraph', flamegraph_path) 
Example #3
Source File: test_repositories.py    From DLRN with Apache License 2.0 6 votes vote down vote up
def test_clone_no_fallback(self, sh_mock):
        config = configparser.RawConfigParser()
        config.read("projects.ini")
        config.set('DEFAULT', 'fallback_to_master', '0')
        self.config = ConfigOptions(config)
        # We need to redefine the mock object again, to use a side effect
        # that will fail in the git checkout call. A bit convoluted, but
        # it works
        with mock.patch.object(sh.Command, '__call__') as new_mock:
            new_mock.side_effect = _aux_sh
            self.assertRaises(sh.ErrorReturnCode_1, repositories.refreshrepo,
                              'url', 'path', branch='branch')
            expected = [mock.call('url', 'path'),
                        mock.call('origin'),
                        mock.call('-f', 'branch')]
            self.assertEqual(new_mock.call_args_list, expected) 
Example #4
Source File: benchmark.py    From cstar_perf with Apache License 2.0 5 votes vote down vote up
def download_and_build_spark_cassandra_stress(stress_node=None):
    dse_home = 'DSE_HOME={dse_path}'.format(dse_path=dse.get_dse_path())
    dse_resources = 'DSE_RESOURCES={dse_resources_path}'.format(dse_resources_path=os.path.join(dse.get_dse_path(), 'resources'))
    spark_cassandra_stress_git = 'https://github.com/datastax/spark-cassandra-stress.git'
    git_clone_spark_cass_stress_command = 'git clone -b master --single-branch ' \
                                          '{spark_cass_stress_git} ' \
                                          '{spark_cass_stress_path}'.format(spark_cass_stress_git=spark_cassandra_stress_git,
                                                                            spark_cass_stress_path=get_spark_cassandra_stress_path(stress_node=stress_node))
    build_command = './gradlew jar -Pagainst=dse;'
    full_build_command = 'cd {spark_cass_stress_path}; TERM=dumb {dse_home} {dse_resources} {build_cmd}'.format(
        spark_cass_stress_path=get_spark_cassandra_stress_path(),
        dse_home=dse_home,
        dse_resources=dse_resources,
        build_cmd=build_command
    )

    if stress_node:
        with common.fab.settings(hosts=stress_node):
            execute(fab.run, 'rm -rf {spark_cass_stress_path}'.format(spark_cass_stress_path=get_spark_cassandra_stress_path(stress_node=stress_node)))
            execute(fab.run, git_clone_spark_cass_stress_command)
            execute(fab.run, full_build_command)
    else:
        shutil.rmtree(get_spark_cassandra_stress_path(), ignore_errors=True)
        logger.info('Installing Spark-Cassandra-Stress from {spark_cass_stress_git}'.format(spark_cass_stress_git=spark_cassandra_stress_git))
        proc = subprocess.Popen(git_clone_spark_cass_stress_command, shell=True)
        proc.wait()
        assert proc.returncode == 0, 'Installing Spark-Cassandra-Stress from {spark_cass_stress_git} ' \
                                     'did not complete successfully'.format(spark_cass_stress_git=spark_cassandra_stress_git)
        logger.info('Building Spark-Cassandra-Stress using {full_build_command}'.format(full_build_command=full_build_command))
        proc = subprocess.Popen(full_build_command, shell=True)
        proc.wait()
        assert proc.returncode == 0, 'Building Spark-Cassandra-Stress using {full_build_command} ' \
                                     'did not complete successfully'.format(full_build_command=full_build_command) 
Example #5
Source File: test_repositories.py    From DLRN with Apache License 2.0 5 votes vote down vote up
def test_clone_if_not_cloned(self, sh_mock):
        repositories.refreshrepo('url', 'path', branch='branch')
        expected = [mock.call(sh.git.clone, 'url', 'path'),
                    mock.call(sh.git.fetch, 'origin'),
                    mock.call(sh.git.checkout, '-f', 'branch'),
                    mock.call(sh.git.reset, '--hard', 'origin/branch'),
                    mock.call(sh.git.log, '--pretty=format:%H %ct', '-1', '.')]
        self.assertEqual(sh_mock.call_args_list, expected) 
Example #6
Source File: test_repositories.py    From DLRN with Apache License 2.0 5 votes vote down vote up
def test_dont_clone_if_cloned(self, path_mock, shutil_mock, sh_mock):
        repositories.refreshrepo('url', 'path', branch='branch')
        expected = [mock.call(sh.git, 'remote', '-v'),
                    mock.call(sh.git.clone, 'url', 'path'),
                    mock.call(sh.git.fetch, 'origin'),
                    mock.call(sh.git.checkout, '-f', 'branch'),
                    mock.call(sh.git.reset, '--hard', 'origin/branch'),
                    mock.call(sh.git.log, '--pretty=format:%H %ct', '-1', '.')]
        self.assertEqual(sh_mock.call_args_list, expected) 
Example #7
Source File: test_repositories.py    From DLRN with Apache License 2.0 5 votes vote down vote up
def test_dont_fetch_if_local_repo_exists(self, path_mock, sh_mock):
        repositories.refreshrepo('url', 'path', branch='branch', local=True)
        expected = [mock.call(sh.git.log, '--pretty=format:%H %ct', '-1', '.')]
        self.assertEqual(sh_mock.call_args_list, expected) 
Example #8
Source File: test_repositories.py    From DLRN with Apache License 2.0 5 votes vote down vote up
def test_clone_fetch_if_local_repo_missing(self, sh_mock):
        repositories.refreshrepo('url', 'path', branch='branch', local=True)
        expected = [mock.call(sh.git.clone, 'url', 'path'),
                    mock.call(sh.git.fetch, 'origin'),
                    mock.call(sh.git.checkout, '-f', 'branch'),
                    mock.call(sh.git.reset, '--hard', 'origin/branch'),
                    mock.call(sh.git.log, '--pretty=format:%H %ct', '-1', '.')]
        self.assertEqual(sh_mock.call_args_list, expected) 
Example #9
Source File: git.py    From gitver with Apache License 2.0 5 votes vote down vote up
def __git_raw(*args):
    """
    @return sh.RunningCommand
    Proxies the specified git command+args and returns it
    """
    return sh.git(args) 
Example #10
Source File: git.py    From gitver with Apache License 2.0 5 votes vote down vote up
def __git(*args):
    """
    Proxies the specified git command+args and returns a cleaned up version
    of the stdout buffer.
    """
    return __git_raw(*args).stdout.replace('\n', '') 
Example #11
Source File: test_util.py    From gilt with MIT License 5 votes vote down vote up
def test_run_command(capsys):
    cmd = sh.git.bake(version=True)
    util.run_command(cmd)

    result, _ = capsys.readouterr()
    assert "" == result 
Example #12
Source File: test_util.py    From gilt with MIT License 5 votes vote down vote up
def test_run_command_with_debug(temp_dir, capsys):
    cmd = sh.git.bake(version=True)
    util.run_command(cmd, debug=True)

    result, _ = capsys.readouterr()
    x = "COMMAND: {} --version".format(sh.git)
    assert x in result
    x = "PWD: {}".format(temp_dir)
    assert x in result 
Example #13
Source File: utils.py    From ansible-tools with Apache License 2.0 5 votes vote down vote up
def update_devel(self):
        if not os.path.exists(self.develdir):
            logger.debug('git clone %s %s' % (self.DEVEL_URL, self.develdir))
            git('clone', self.DEVEL_URL, self.develdir)
        else:
            cmd = 'cd %s; git fetch -a; git pull --rebase origin devel' % self.develdir
            logger.debug(cmd)
            run_command(cmd) 
Example #14
Source File: gittools.py    From extratools with MIT License 4 votes vote down vote up
def status(path: str = '.') -> Optional[Mapping[str, Any]]:
    try:
        output = sh.git(
            "status", "-s", "-b", "--porcelain=2",
            _cwd=os.path.expanduser(path)
        )
    except:
        return None

    head = None
    upstream = None

    ahead, behind = 0, 0

    modified: List[str] = []
    untracked: List[str] = []

    for line in output.rstrip('\n').splitlines():
        if line.startswith('#'):
            if line.startswith("# branch.oid "):
                oid = line.rsplit(' ', 1)[1]
            if line.startswith("# branch.head "):
                branch = line.rsplit(' ', 1)[1]
                if branch != "(detached)":
                    head = branch
            elif line.startswith("# branch.upstream "):
                branch = line.rsplit(' ', 1)[1]
                if branch != "(detached)":
                    upstream = branch
            elif line.startswith("# branch.ab "):
                ahead, behind = [abs(int(x)) for x in line.rsplit(' ', 2)[1:]]
        elif line.startswith('?'):
            untracked.append(line.rsplit(' ', -1)[1])
        elif not line.startswith('!'):
            vals = line.split(' ')

            s, _, _, u = vals[2]
            flag = s == 'S' and u == 'U'

            (untracked if flag else modified).append(vals[-1])

    return {
        "path": path,
        "oid": oid,
        "branch": {
            "head": head,
            "upstream": upstream
        },
        "commits": {
            "ahead": ahead,
            "behind": behind
        },
        "files": {
            "modified": modified,
            "untracked": untracked
        }
    } 
Example #15
Source File: gittools.py    From extratools with MIT License 4 votes vote down vote up
def status(path: str = '.') -> Optional[Mapping[str, Any]]:
    try:
        output = sh.git(
            "status", "-s", "-b", "--porcelain=2",
            _cwd=os.path.expanduser(path)
        )
    except:
        return None

    head = None
    upstream = None

    ahead, behind = 0, 0

    modified: List[str] = []
    untracked: List[str] = []

    for line in output.rstrip('\n').splitlines():
        if line.startswith('#'):
            if line.startswith("# branch.oid "):
                oid = line.rsplit(' ', 1)[1]
            if line.startswith("# branch.head "):
                branch = line.rsplit(' ', 1)[1]
                if branch != "(detached)":
                    head = branch
            elif line.startswith("# branch.upstream "):
                branch = line.rsplit(' ', 1)[1]
                if branch != "(detached)":
                    upstream = branch
            elif line.startswith("# branch.ab "):
                ahead, behind = [abs(int(x)) for x in line.rsplit(' ', 2)[1:]]
        elif line.startswith('?'):
            untracked.append(line.rsplit(' ', -1)[1])
        elif not line.startswith('!'):
            vals = line.split(' ')

            s, _, _, u = vals[2]
            flag = s == 'S' and u == 'U'

            (untracked if flag else modified).append(vals[-1])

    return {
        "path": path,
        "oid": oid,
        "branch": {
            "head": head,
            "upstream": upstream
        },
        "commits": {
            "ahead": ahead,
            "behind": behind
        },
        "files": {
            "modified": modified,
            "untracked": untracked
        }
    }