<commit_before>import os
import subprocess
import logging
from utils import working_directory_keeper

logger = logging.getLogger(__name__)

SUBPROCESS_ENV = os.environ.copy()
SUBPROCESS_ENV['PYTHONPATH'] = SUBPROCESS_ENV.pop(
    'BUILDOUT_ORIGINAL_PYTHONPATH', '')

SUPPORTED = frozenset(('bzr', 'hg', 'git', 'svn'))

def hg_get_update(target_dir, url, revision, offline=False):
    """Ensure that target_dir is a clone of url at specified revision.

    If target_dir already exists, does a simple pull.
    Offline-mode: no clone nor pull, but update.
    """
    if not os.path.exists(target_dir):
        # TODO case of local url ?
        if offline:
            raise IOError("hg repository %r does not exist; cannot clone it from %r (offline mode)" % (target_dir, url))

        logger.info("CLoning %s ...", url)
        clone_cmd = ['hg', 'clone']
        if revision:
            clone_cmd.extend(['-r', revision])
        clone_cmd.extend([url, target_dir])
        subprocess.call(clone_cmd, env=SUBPROCESS_ENV)
    else:
        # TODO what if remote repo is actually local fs ?
        if not offline:
            logger.info("Pull for hg repo %r ...", target_dir)
            subprocess.call(['hg', '--cwd', target_dir, 'pull'],
                            env=SUBPROCESS_ENV)
        if revision:
            logger.info("Updating %s to revision %s",
                        target_dir, revision)
            up_cmd = ['hg', '--cwd', target_dir, 'up']
            if revision:
                up_cmd.extend(['-r', revision])
            subprocess.call(up_cmd, env=SUBPROCESS_ENV)

def bzr_get_update(target_dir, url, revision, offline=False):
    """Ensure that target_dir is a branch of url at specified revision.

    If target_dir already exists, does a simple pull.
    Offline-mode: no branch nor pull, but update.
    """
    with working_directory_keeper:
        if not os.path.exists(target_dir):
            # TODO case of local url ?
            if offline:
                raise IOError("bzr branch %s does not exist; cannot branch it from %s (offline mode)" % (target_dir, url))

            os.chdir(os.path.split(target_dir)[0])
            logger.info("Branching %s ...", url)
            branch_cmd = ['bzr', 'branch', '--stacked']
            if revision:
                branch_cmd.extend(['-r', revision])
            branch_cmd.extend([url, target_dir])
            subprocess.call(branch_cmd, env=SUBPROCESS_ENV)
        else:
            os.chdir(target_dir)
            # TODO what if bzr source is actually local fs ?
            if not offline:
                logger.info("Pull for branch %s ...", target_dir)
                subprocess.call(['bzr', 'pull'], env=SUBPROCESS_ENV)
            if revision:
                logger.info("Update to revision %s", revision)
                subprocess.call(['bzr', 'up', '-r', revision])

def git_get_update(target_dir, url, revision, offline=False):
    """Ensure that target_dir is a branch of url at specified revision.

    If target_dir already exists, does a simple pull.
    Offline-mode: no branch nor pull, but update.
    """
    rev_str = revision

    with working_directory_keeper:
        if not os.path.exists(target_dir):
            # TODO case of local url ?
            if offline:
                raise IOError("git repository %s does not exist; cannot clone it from %s (offline mode)" % (target_dir, url))<commit_msg>Stopped chdir in Bzr case<commit_after>import os
import subprocess
import logging
from utils import working_directory_keeper

logger = logging.getLogger(__name__)

SUBPROCESS_ENV = os.environ.copy()
SUBPROCESS_ENV['PYTHONPATH'] = SUBPROCESS_ENV.pop(
    'BUILDOUT_ORIGINAL_PYTHONPATH', '')

SUPPORTED = frozenset(('bzr', 'hg', 'git', 'svn'))

def hg_get_update(target_dir, url, revision, offline=False):
    """Ensure that target_dir is a clone of url at specified revision.

    If target_dir already exists, does a simple pull.
    Offline-mode: no clone nor pull, but update.
    """
    if not os.path.exists(target_dir):
        # TODO case of local url ?
        if offline:
            raise IOError("hg repository %r does not exist; cannot clone it from %r (offline mode)" % (target_dir, url))

        logger.info("CLoning %s ...", url)
        clone_cmd = ['hg', 'clone']
        if revision:
            clone_cmd.extend(['-r', revision])
        clone_cmd.extend([url, target_dir])
        subprocess.call(clone_cmd, env=SUBPROCESS_ENV)
    else:
        # TODO what if remote repo is actually local fs ?
        if not offline:
            logger.info("Pull for hg repo %r ...", target_dir)
            subprocess.call(['hg', '--cwd', target_dir, 'pull'],
                            env=SUBPROCESS_ENV)
        if revision:
            logger.info("Updating %s to revision %s",
                        target_dir, revision)
            up_cmd = ['hg', '--cwd', target_dir, 'up']
            if revision:
                up_cmd.extend(['-r', revision])
            subprocess.call(up_cmd, env=SUBPROCESS_ENV)

def bzr_get_update(target_dir, url, revision, offline=False):
    """Ensure that target_dir is a branch of url at specified revision.

    If target_dir already exists, does a simple pull.
    Offline-mode: no branch nor pull, but update.
    """
    if not os.path.exists(target_dir):
        # TODO case of local url ?
        if offline:
            raise IOError("bzr branch %s does not exist; cannot branch it from %s (offline mode)" % (target_dir, url))

        logger.info("Branching %s ...", url)
        branch_cmd = ['bzr', 'branch', '--stacked']
        if revision:
            branch_cmd.extend(['-r', revision])
        branch_cmd.extend([url, target_dir])
        subprocess.call(branch_cmd, env=SUBPROCESS_ENV)
    else:
        # TODO what if bzr source is actually local fs ?
        if not offline:
            logger.info("Pull for branch %s ...", target_dir)
            subprocess.call(['bzr', 'pull', '-d', target_dir],
                            env=SUBPROCESS_ENV)
        if revision:
            logger.info("Update to revision %s", revision)
            subprocess.call(['bzr', 'up', '-d', target_dir, '-r', revision],
                            env=SUBPROCESS_ENV)


def git_get_update(target_dir, url, revision, offline=False):
    """Ensure that target_dir is a branch of url at specified revision.

    If target_dir already exists, does a simple pull.
    Offline-mode: no branch nor pull, but update.
    """
    rev_str = revision

    with working_directory_keeper:
        if not os.path.exists(target_dir):
            # TODO case of local url ?
            if offline:
                raise IOError("git repository %s does not exist; cannot clone it from %s (offline mode)" % (target_dir, url))
{'fraction_non_alphanumeric': 0.11247051886792453, 'fraction_numerical': 0.00014740566037735848, 'mean_word_length': 2.3539298072170043, 'pattern_counts': {'":': 0, '<': 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': '2106130', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1978, 'n_words': 622}