<commit_before>from schedules_tools.storage import ScheduleStorageBase
import os
import sys
import re
import subprocess
import datetime
import tempfile
import logging

logger = logging.getLogger(__name__)


class ScheduleStorage_cvs(ScheduleStorageBase):
    target_dir = None
    envvars = dict()
    cloned = False
    repo_url = None
    repo_name = None

    def __init__(self, opt_args=dict()):
        self.repo_name = opt_args.pop('cvs_repo_name')
        self.opt_args = opt_args
        self.envvars = os.environ
        self.envvars['CVSROOT'] = opt_args.pop('cvs_root')

    def _clone(self, target_dir=None):
        if self.cloned:
            logger.debug('Storage has been already cloned. Skipping.')
            return
        if not target_dir:
            target_dir = '/tmp'
        self.target_dir = target_dir
        tempfile.mkdtemp()
        os.makedirs(self.target_dir)

        cmd = 'cvs co {}'.format(self.repo_name)
        p = subprocess.Popen(cmd.split(), env=self.envvars, stdout=sys.stdout,
                             cwd=self.target_dir)
        p.communicate()
        assert p.returncode == 0
        self.cloned = True

    def _checkout(self, revision=None, filename=None):
        if not filename:
            filename = ''
        cmd = 'cvs update -r{revision} {filename}'.format(
            revision=revision, filename=filename)
        p = subprocess.Popen(cmd.split(), env=self.envvars, stdout=sys.stdout,
                             cwd=self.target_dir)
        p.communicate()
        assert p.returncode == 0

    def pull(self, rev=None, datetime=None, target_dir=None):
        """ Pulls from storage

        Args:
            rev: if None - pull current
            date:
            target_dir: if None, pull to tmp dir

        Returns:
            Pulled file/directory
        """
        self._clone(target_dir)
        # TODO(mpavlase): pass desired file/dir to checkout
        self._checkout(revision=rev)
        # TODO - return resulting filename

    def parse_changelog(self, filename):
        changelog = []
        cmd = 'cvs log {}'.format(filename)
        p = subprocess.Popen(cmd.split(), env=self.envvars, cwd=self.target_dir,
                             stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()

        STATE_HEAD = 'head'
        STATE_REVISION = 'revision'
        STATE_DATE_AUTHOR = 'dateauthor'
        STATE_COMMENT = 'comment'
<commit_msg>cvs storage: added CVS command 'factory' method<commit_after>from schedules_tools.storage import ScheduleStorageBase
import os
import sys
import re
import subprocess
import datetime
import tempfile
import logging

logger = logging.getLogger(__name__)


class ScheduleStorage_cvs(ScheduleStorageBase):
    target_dir = None
    cloned = False
    repo_root = None
    repo_name = None

    def _cvs_command(self, cmd):
        # -q, make cvs more quiet
        # -z9, maximum compression
        # -d, set CVSROOT
        cmd_str = 'cvs -q -z9 -d {} {}'.format(self.repo_root, cmd)
        p = subprocess.Popen(cmd_str.split(), stdout=sys.stdout,
                             cwd=self.target_dir)
        return p

    def __init__(self, opt_args=dict()):
        self.repo_name = opt_args.pop('cvs_repo_name')
        self.repo_root = opt_args.pop('cvs_root')
        self.opt_args = opt_args

    def _clone(self, target_dir=None):
        if self.cloned:
            logger.debug('Storage has been already cloned. Skipping.')
            return
        if not target_dir:
            target_dir = '/tmp'
        self.target_dir = target_dir
        tempfile.mkdtemp()
        os.makedirs(self.target_dir)

        cmd = 'co {}'.format(self.repo_name)
        p = self._cvs_command(cmd)
        p.communicate()

        assert p.returncode == 0
        self.cloned = True

        return self.target_dir

    def _checkout(self, revision=None, filename=None):
        if not filename:
            filename = ''
        cmd = 'update -r{revision} {filename}'.format(revision=revision,
                                                      filename=filename)
        p = self._cvs_command(cmd)
        p.communicate()
        assert p.returncode == 0

    def pull(self, rev=None, datetime=None, target_dir=None):
        """ Pulls from storage

        Args:
            rev: if None - pull current
            date:
            target_dir: if None, pull to tmp dir

        Returns:
            Pulled file/directory
        """
        self._clone(target_dir)
        # TODO(mpavlase): pass desired file/dir to checkout
        self._checkout(revision=rev)
        # TODO - return resulting filename

    def parse_changelog(self, filename):
        changelog = []
        cmd = 'cvs log {}'.format(filename)
        p = subprocess.Popen(cmd.split(), env=self.envvars, cwd=self.target_dir,
                             stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()

        STATE_HEAD = 'head'
        STATE_REVISION = 'revision'
        STATE_DATE_AUTHOR = 'dateauthor'
        STATE_COMMENT = 'comment'

{'fraction_non_alphanumeric': 0.09716200236499803, 'fraction_numerical': 0.0011824990145841545, 'mean_word_length': 2.079490291262136, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4714716', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1493, 'n_words': 350}