<commit_before>"""
Helper file to generate package version numbers on the fly

Wouldn't it be great if every time you bundled a python package, it was tagged
with a unique version number that reflected the current git ref? It's almost
like that's a feature that should be available without stupid hacks.

But it's not. So here's how the stupid hacks work.

When you run ``python setup.py``, if you are running it from inside of a git
repository this script with generate a unique version number and embed it in an
auto-generated file in your package. By default the file is named
'_version.py', and you should add it to your ``.gitignore``. Since this is a
python file and it's in your package, it will get bundled up and distributed
with your package. Then during the installation process, this script will
recognize that it is not longer being run from within a git repository and find
the version number from the file it generated earlier.

"""
import locale
import os
import re

import subprocess


# This version regex was constructed from the regex-ish description here:
# http://www.python.org/dev/peps/pep-0440/#public-version-identifiers
VERSION_SCHEME = re.compile(r'^\d+(\.\d+)+'
                            r'((a|b|c|rc)\d+)?'
                            r'(\.post\d+)?'
                            r'(\.dev\d+)?$')
GIT_DESCRIBE = ('git', 'describe')
GIT_DESCRIBE_ARGS = ('--tags', '--dirty', '--abbrev=40', '--long')


def find_package(path):
    """
    Find the directory that contains the python package in a repository

    Parameters
    ----------
    path : str
        The path to the repository

    Returns
    -------
    package_dir : str
        The name of the directory that contains the python package

    Raises
    ------
    error : :class:`IOError`
        If a single package cannot be found

    """
    dirname = os.path.basename(path)
    if os.path.isdir(os.path.join(path, dirname)):
        return dirname
    candidates = []
    for filename in os.listdir(path):
        init = os.path.join(path, filename, '__init__.py')
        if os.path.exists(init):
            candidates.append(filename)
    if len(candidates) == 1:
        return candidates[0]
    elif len(candidates) == 0:
        raise IOError("No package found in repo '%s'" % path)
    else:
        raise IOError("Multiple possible packages found in repo '%s'! "
                      "Please specify one." % path)


def parse_constants(filename):
    """ Parse python constants from a file """
    if not os.path.exists(filename):
        return {
            'source_label': 'NOTAG',
            'version': '0.0.0',
        }
    constants = {}
    with open(filename, 'r') as infile:
        for line in infile:
            components = line.split('=')
            if len(components) <= 1:
                continue
            key = components[0].strip(' _')
            value = '='.join(components[1:]).strip().strip('\'\"')
            constants[key] = value
    return constants<commit_msg>Updating the comments in version_helper.py<commit_after>"""
Helper file to generate package version numbers on the fly

You're probably already using git tags to tag releases of your project. If you
aren't, you really should. Wouldn't it be great if your python package
automatically updated its version number based on the most recent git tag? You
know, so you don't have to do it manually all the time? It's almost like
that's a feature that should be available without stupid hacks.

But it's not. So here's how the stupid hacks work.

When you run ``python setup.py``, if you are running it from inside of a git
repository this script with generate a unique version number and embed it in an
auto-generated file in your package. By default the file is named
'_version.py', and you should add it to your ``.gitignore``. Since this is a
python file and it's in your package, it will get bundled up and distributed
with your package. Then during the installation process, this script will
recognize that it is no longer being run from within a git repository and find
the version number from the file it generated earlier.

To use version_helper, first tag a release commit in your repository::

    git tag 0.1.0

Then add the following to your setup.py file::

    from setuptools import setup
    from version_helper import git_version

    setup(
        name='mypackage',
        ...
        **git_version())

You're done! That's it! To view the auto-generated version number of your
package, run::

    python setup.py -V

"""
import locale
import os
import re

import subprocess


# This version regex was constructed from the regex-ish description here:
# http://www.python.org/dev/peps/pep-0440/#public-version-identifiers
VERSION_SCHEME = re.compile(r'^\d+(\.\d+)+'
                            r'((a|b|c|rc)\d+)?'
                            r'(\.post\d+)?'
                            r'(\.dev\d+)?$')
GIT_DESCRIBE = ('git', 'describe')
GIT_DESCRIBE_ARGS = ('--tags', '--dirty', '--abbrev=40', '--long')


def find_package(path):
    """
    Find the directory that contains the python package in a repository

    Parameters
    ----------
    path : str
        The path to the repository

    Returns
    -------
    package_dir : str
        The name of the directory that contains the python package

    Raises
    ------
    error : :class:`IOError`
        If a single package cannot be found

    """
    dirname = os.path.basename(path)
    if os.path.isdir(os.path.join(path, dirname)):
        return dirname
    candidates = []
    for filename in os.listdir(path):
        init = os.path.join(path, filename, '__init__.py')
        if os.path.exists(init):
            candidates.append(filename)
    if len(candidates) == 1:
        return candidates[0]
    elif len(candidates) == 0:
        raise IOError("No package found in repo '%s'" % path)
    else:
        raise IOError("Multiple possible packages found in repo '%s'! "
                      "Please specify one." % path)


def parse_constants(filename):
    """ Parse python constants from a file """
    if not os.path.exists(filename):
        return {
            'source_label': 'NOTAG',
            'version': 'unknown',
        }
    constants = {}
    with open(filename, 'r') as infile:
        for line in infile:
            components = line.split('=')
            if len(components) <= 1:
                continue
            key = components[0].strip(' _')
            value = '='.join(components[1:]).strip().strip('\'\"')
            constants[key] = value
    return constants
{'fraction_non_alphanumeric': 0.10711030082041932, 'fraction_numerical': 0.004557885141294439, 'mean_word_length': 2.47571277719113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '610562', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1793, 'n_words': 732}