<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import copy
import datetime
import os
import os.path
import shutil
import subprocess
import sys

from configparser import ConfigParser

from . import options


class Checkout(object):
    def __init__(self, branch, commit, name):
        self.branch, self.commit, self.name = branch, commit, name

    @property
    def path(self):
        return os.path.join(
            options.BASEPATH, 'checkouts',
            self.branch.repo.name, self.branch.name,
            '{0}-{1}'.format(self.name, self.commit[:11])
        )

    @classmethod
    def create(cls, branch, commit):
        name = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
        self = cls(branch, commit, name)
        self.run_in(
            ['git', 'clone', '-q', self.branch.repo.path, self.path],
            env={},
            cwd=os.path.expanduser('~deploy')
        )
        self.run_in(
            ['git', 'config', 'advice.detachedHead', 'false'],
            env={}
        )
        self.run_in(
            ['git', 'checkout', self.commit],
            env={}
        )
        self.run_in(
            ['git', 'submodule', 'update', '--init', '--recursive'],
            env={}
        )
        to_delete = []
        for root, dirs, files in os.walk(self.path):
            for d in dirs:
                if d == '.git':
                    to_delete.append(os.path.join(root, d))
        for d in to_delete:
            shutil.rmtree(d)
        return self

    @property
    def cmd_env(self):
        env = copy.deepcopy(self.branch.config.get('env', dict()))
        env.update(os.environ)
        return env

    @classmethod
    def all_for_branch(cls, branch):
        try:
            files = os.listdir(os.path.join(
                options.BASEPATH, 'checkouts', branch.repo.name, branch.name
            ))
        except FileNotFoundError:
            return
        for basename in files:<commit_msg>Add git commit to environment<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import copy
import shutil
import os.path
import datetime
import subprocess
from configparser import ConfigParser

from . import options


class Checkout(object):
    def __init__(self, branch, commit, name):
        self.branch, self.commit, self.name = branch, commit, name

    @property
    def path(self):
        return os.path.join(
            options.BASEPATH, 'checkouts',
            self.branch.repo.name, self.branch.name,
            '{0}-{1}'.format(self.name, self.commit[:11])
        )

    @classmethod
    def create(cls, branch, commit):
        name = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
        self = cls(branch, commit, name)
        self.run_in(
            ['git', 'clone', '-q', self.branch.repo.path, self.path],
            env={},
            cwd=os.path.expanduser('~deploy')
        )
        self.run_in(
            ['git', 'config', 'advice.detachedHead', 'false'],
            env={}
        )
        self.run_in(
            ['git', 'checkout', self.commit],
            env={}
        )
        self.run_in(
            ['git', 'submodule', 'update', '--init', '--recursive'],
            env={}
        )
        to_delete = []
        for root, dirs, files in os.walk(self.path):
            for d in dirs:
                if d == '.git':
                    to_delete.append(os.path.join(root, d))
        for d in to_delete:
            shutil.rmtree(d)
        return self

    @property
    def cmd_env(self):
        env = copy.deepcopy(self.branch.config.get('env', dict()))
        env.update(os.environ)
        env['GIT_COMMIT'] = self.commit
        return env

    @classmethod
    def all_for_branch(cls, branch):
        try:
            files = os.listdir(os.path.join(
                options.BASEPATH, 'checkouts', branch.repo.name, branch.name
            ))
        except FileNotFoundError:
            return
        for basename in files:
{'fraction_non_alphanumeric': 0.1376720901126408, 'fraction_numerical': 0.0025031289111389237, 'mean_word_length': 1.8101265822784811, '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': '188228', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1213, 'n_words': 277}