<commit_before># -*- python -*-
#
#  This file is part of the cellnopt software
#
#  Copyright (c) 2011-2012 - EMBL-EBI
#
#  File author(s): <NAME> <<EMAIL>>
#
#  Distributed under the GPLv3 License.
#  See accompanying file LICENSE.txt or copy at
#      http://www.gnu.org/licenses/gpl-3.0.html
#
#  https://pypi.python.org/pypi/cellnopt.admin
#  http://pythonhosted.org/cellnopt.admin
#
##############################################################################
# $Id: distribute.py 4207 2013-12-04 13:09:16Z cokelaer $
"""This module provides tools to check out a revision of a SVN directory
into a temporary directory and to create a tar ball out of that
SVN copy and copy the final tar ball in the directory where this script is
called. Sub directories such as .git are removed. The temporary directory is
also removed.

This is coded in the :class:`DistributeRPackage`  class. This module is also executable
provided with cellnopt.admin package called cellnopt_distribute.


"""
import os
import subprocess
import tempfile
from os.path import join as pj
import glob
import time
import sys

from easydev.tools import shellcmd
from easydev import Logging
include = ["*"]


__all__ = ["DistributeRPackage"]

class DistributeRPackage(object):
    """Class to ease distribution of CellNOptR packages from SVN


    Can be used for any SVN containing valid R packages by setting the
    repository URL

        >>> d = DistributeRPackage()
        >>> d.distribute()

    You can also use the executable provided in cellnopt.admin package itself::

        cellnopt_distribute --package CNORdt --revision HEAD

    equivalent to (if you have the sources)::

        python distribute.py --package CNORdt --revision 666
    
    Version of cellnopt.admin were based on private SVN but we moved to github and
    therefore this class is now related to the github repository only.

    In practice, this is more complicated than SVN:
    
    - cannot get a nice revision number and be able to compare
      between revisions
    - Cannot checkout a sub directory (e.g., CNORdt)

    So, we will therefore build up all packages in one go and unfortunately
    add the commit hash long number as a tag....although the --short option
    seems to be a solution.
    

    .. todo:: MEIGOR
    """
    #: mapping between package name and actual directory name 
    _valid_packages = {"CellNOptR":"CellNOptR",
                       "CNORdt":pj("CNOR_dt","CNORdt"),
                       "CNORode":pj("CNOR_ode","CNORode"),
                       "CNORfuzzy":"CNOR_fuzzy",
                       "CNORfeeder":"CNOR_add_links",
                       "MEIGOR": pj('essR','MEIGOR')}

    def __init__(self,  package, build_options="--no-build-vignettes"):
        """

        :param str package: name of a valid package (e.g., CellNOptR)
        :param revision: SVN revision (default is HEAD )
        :param build_options: additional build options for R (default is
            --no-build-vignettes)

        You can also change the logging level (e.g., self.logging.debugLevel="WARNING")


        """
        self.url = "https://github.com/cellnopt/CellNOptR"

        self.exclude = [".git"]
        self.package = package
        self.package_path = 'packages' + os.sep + package
        self.dtemp = None
        self.cwd = os.getcwd()
        self.build_options = build_options
        self.logging = Logging("INFO")

    def _get_version(self):
        data = open(self.dtemp + os.sep + self.package_path + os.sep + "DESCRIPTION", "r").read()
        res = [x.split(':')[1].strip() for x in data.split("\n") if x.startswith('Version')]
        return res[0]

    def _create_temp_directory(self):
        self.dtemp = tempfile.mkdtemp()

    def _checkout_git(self):
        self.logging.info("1. Gettting the source from GIT --------------")
        if self.dtemp == None:
            self._create_temp_directory()

        cmd = """
         git init %(directory)s;
         cd %(directory)s;
         git remote add -f origin %(repo)s true;
         echo "packages/%(package_name)s" >> .git/info/sparse-checkout;
         git pull origin master
        """
        print(self.package, self.url, self.dtemp)
        cmd = cmd % {'package_name':self.package, 'repo': self.url , 
                'directory': self.dtemp}
        
        self.logging.info(cmd,)
        try:
            ret = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
            ret.wait()
            if ret.poll()!=0:
                raise Exception
            self.logging.info('...done')
        except Exception:
            raise Exception

    @staticmethod
    def help():
        """Return usage help message"""
        print("\nPURPOSE:"+__doc__)<commit_msg>fix error with new R 3.2 versionwq<commit_after># -*- python -*-
#
#  This file is part of the cellnopt software
#
#  Copyright (c) 2011-2012 - EMBL-EBI
#
#  File author(s): <NAME> <<EMAIL>>
#
#  Distributed under the GPLv3 License.
#  See accompanying file LICENSE.txt or copy at
#      http://www.gnu.org/licenses/gpl-3.0.html
#
#  https://pypi.python.org/pypi/cellnopt.admin
#  http://pythonhosted.org/cellnopt.admin
#
##############################################################################
# $Id: distribute.py 4207 2013-12-04 13:09:16Z cokelaer $
"""This module provides tools to check out a revision of a SVN directory
into a temporary directory and to create a tar ball out of that
SVN copy and copy the final tar ball in the directory where this script is
called. Sub directories such as .git are removed. The temporary directory is
also removed.

This is coded in the :class:`DistributeRPackage`  class. This module is also executable
provided with cellnopt.admin package called cellnopt_distribute.


"""
import os
import subprocess
import tempfile
from os.path import join as pj
import glob
import time
import sys

from easydev.tools import shellcmd
from easydev import Logging
include = ["*"]


__all__ = ["DistributeRPackage"]

class DistributeRPackage(object):
    """Class to ease distribution of CellNOptR packages from SVN


    Can be used for any SVN containing valid R packages by setting the
    repository URL

        >>> d = DistributeRPackage()
        >>> d.distribute()

    You can also use the executable provided in cellnopt.admin package itself::

        cellnopt_distribute --package CNORdt --revision HEAD

    equivalent to (if you have the sources)::

        python distribute.py --package CNORdt --revision 666
    
    Version of cellnopt.admin were based on private SVN but we moved to github and
    therefore this class is now related to the github repository only.

    In practice, this is more complicated than SVN:
    
    - cannot get a nice revision number and be able to compare
      between revisions
    - Cannot checkout a sub directory (e.g., CNORdt)

    So, we will therefore build up all packages in one go and unfortunately
    add the commit hash long number as a tag....although the --short option
    seems to be a solution.
    

    .. todo:: MEIGOR
    """
    #: mapping between package name and actual directory name 
    _valid_packages = {"CellNOptR":"CellNOptR",
                       "CNORdt":pj("CNOR_dt","CNORdt"),
                       "CNORode":pj("CNOR_ode","CNORode"),
                       "CNORfuzzy":"CNOR_fuzzy",
                       "CNORfeeder":"CNOR_add_links",
                       "MEIGOR": pj('essR','MEIGOR')}

    def __init__(self,  package, build_options="--no-build-vignettes"):
        """

        :param str package: name of a valid package (e.g., CellNOptR)
        :param revision: SVN revision (default is HEAD )
        :param build_options: additional build options for R (default is
            --no-build-vignettes)

        You can also change the logging level (e.g., self.logging.debugLevel="WARNING")


        """
        self.url = "https://github.com/cellnopt/CellNOptR"

        self.exclude = [".git"]
        self.package = package
        self.package_path = 'packages' + os.sep + package
        self.dtemp = None
        self.cwd = os.getcwd()
        self.build_options = build_options
        self.logging = Logging("INFO")

    def _get_version(self):
        data = open(self.dtemp + os.sep + self.package_path + os.sep + "DESCRIPTION", "r").read()
        res = [x.split(':')[1].strip() for x in data.split("\n") if x.startswith('Version')]
        return res[0]

    def _create_temp_directory(self):
        self.dtemp = tempfile.mkdtemp()

    def _checkout_git(self):
        self.logging.info("1. Gettting the source from GIT --------------")
        if self.dtemp == None:
            self._create_temp_directory()

        cmd = """
         git init %(directory)s;
         cd %(directory)s;
         git remote add -f origin %(repo)s ;
         echo "packages/%(package_name)s" >> .git/info/sparse-checkout;
         git pull origin master
        """
        print(self.package, self.url, self.dtemp)
        cmd = cmd % {'package_name':self.package, 'repo': self.url , 
                'directory': self.dtemp}
        
        self.logging.info(cmd,)
        try:
            ret = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
            ret.wait()
            if ret.poll()!=0:
                raise Exception
            self.logging.info('...done')
        except Exception:
            raise Exception

    @staticmethod
    def help():
        """Return usage help message"""
        print("\nPURPOSE:"+__doc__)
{'fraction_non_alphanumeric': 0.12197168327215521, 'fraction_numerical': 0.007760880964866282, 'mean_word_length': 2.4752186588921283, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '5074211', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2709, 'n_words': 902}