<commit_before>"""
This bootstrap module contains code for ensuring that the astropy_helpers
package will be importable by the time the setup.py script runs.  It also
includes some workarounds to ensure that a recent-enough version of setuptools
is being used for the installation.

This module should be the first thing imported in the setup.py of distributions
that make use of the utilities in astropy_helpers.  If the distribution ships
with its own copy of astropy_helpers, this module will first attempt to import
from the shipped copy.  However, it will also check PyPI to see if there are
any bug-fix releases on top of the current version that may be useful to get
past platform-specific bugs that have been fixed.  When running setup.py, use
the ``--offline`` command-line option to disable the auto-upgrade checks.

When this module is imported or otherwise executed it automatically calls a
main function that attempts to read the project's setup.cfg file, which it
checks for a configuration section called ``[ah_bootstrap]`` the presences of
that section, and options therein, determine the next step taken:  If it
contains an option called ``auto_use`` with a value of ``True``, it will
automatically call the main function of this module called
`use_astropy_helpers` (see that function's docstring for full details).
Otherwise no further action is taken and by default the system-installed version
of astropy-helpers will be used (however, ``ah_bootstrap.use_astropy_helpers``
may be called manually from within the setup.py script).

This behavior can also be controlled using the ``--auto-use`` and
``--no-auto-use`` command-line flags. For clarity, an alias for
``--no-auto-use`` is ``--use-system-astropy-helpers``, and we recommend using
the latter if needed.

Additional options in the ``[ah_boostrap]`` section of setup.cfg have the same
names as the arguments to `use_astropy_helpers`, and can be used to configure
the bootstrap script when ``auto_use = True``.

See https://github.com/astropy/astropy-helpers for more details, and for the
latest version of this module.
"""

import contextlib
import errno
import io
import locale
import os
import re
import subprocess as sp
import sys

from distutils import log
from distutils.debug import DEBUG

try:
    from ConfigParser import ConfigParser, RawConfigParser
except ImportError:
    from configparser import ConfigParser, RawConfigParser

import pkg_resources

from setuptools import Distribution
from setuptools.package_index import PackageIndex

# TODO: Maybe enable checking for a specific version of astropy_helpers?
DIST_NAME = 'astropy-helpers'
PACKAGE_NAME = 'astropy_helpers'
UPPER_VERSION_EXCLUSIVE = None

# Defaults for other options
DOWNLOAD_IF_NEEDED = True
INDEX_URL = 'https://pypi.python.org/simple'
USE_GIT = True
OFFLINE = False
AUTO_UPGRADE = True

# A list of all the configuration options and their required types
CFG_OPTIONS = [
    ('auto_use', bool), ('path', str), ('download_if_needed', bool),
    ('index_url', str), ('use_git', bool), ('offline', bool),
    ('auto_upgrade', bool)
]

# Start off by parsing the setup.cfg file

if os.path.exists('setup.cfg'):

    SETUP_CFG = ConfigParser()

    try:
        SETUP_CFG.read('setup.cfg')
    except Exception as e:
        if DEBUG:
            raise

        log.error(
            "Error reading setup.cfg: {0!r}\n{1} will not be "
            "automatically bootstrapped and package installation may fail."
            "\n{2}".format(e, PACKAGE_NAME, _err_help_msg))
        SETUP_CFG = {}

else:

    SETUP_CFG = {}

__minimum_python_version__ = (3, 5)

if sys.version_info < __minimum_python_version__:

    # We used package_name in the package template for a while instead of name
    if SETUP_CFG.has_option('metadata', 'name'):
        parent_package = SETUP_CFG.get('metadata', 'name')
    elif SETUP_CFG.has_option('metadata', 'package_name'):
        parent_package = SETUP_CFG.get('metadata', 'package_name')
    else:
        parent_package = None

    if parent_package is None:<commit_msg>Fix case where setup.cfg file is missing<commit_after>"""
This bootstrap module contains code for ensuring that the astropy_helpers
package will be importable by the time the setup.py script runs.  It also
includes some workarounds to ensure that a recent-enough version of setuptools
is being used for the installation.

This module should be the first thing imported in the setup.py of distributions
that make use of the utilities in astropy_helpers.  If the distribution ships
with its own copy of astropy_helpers, this module will first attempt to import
from the shipped copy.  However, it will also check PyPI to see if there are
any bug-fix releases on top of the current version that may be useful to get
past platform-specific bugs that have been fixed.  When running setup.py, use
the ``--offline`` command-line option to disable the auto-upgrade checks.

When this module is imported or otherwise executed it automatically calls a
main function that attempts to read the project's setup.cfg file, which it
checks for a configuration section called ``[ah_bootstrap]`` the presences of
that section, and options therein, determine the next step taken:  If it
contains an option called ``auto_use`` with a value of ``True``, it will
automatically call the main function of this module called
`use_astropy_helpers` (see that function's docstring for full details).
Otherwise no further action is taken and by default the system-installed version
of astropy-helpers will be used (however, ``ah_bootstrap.use_astropy_helpers``
may be called manually from within the setup.py script).

This behavior can also be controlled using the ``--auto-use`` and
``--no-auto-use`` command-line flags. For clarity, an alias for
``--no-auto-use`` is ``--use-system-astropy-helpers``, and we recommend using
the latter if needed.

Additional options in the ``[ah_boostrap]`` section of setup.cfg have the same
names as the arguments to `use_astropy_helpers`, and can be used to configure
the bootstrap script when ``auto_use = True``.

See https://github.com/astropy/astropy-helpers for more details, and for the
latest version of this module.
"""

import contextlib
import errno
import io
import locale
import os
import re
import subprocess as sp
import sys

from distutils import log
from distutils.debug import DEBUG

try:
    from ConfigParser import ConfigParser, RawConfigParser
except ImportError:
    from configparser import ConfigParser, RawConfigParser

import pkg_resources

from setuptools import Distribution
from setuptools.package_index import PackageIndex

# TODO: Maybe enable checking for a specific version of astropy_helpers?
DIST_NAME = 'astropy-helpers'
PACKAGE_NAME = 'astropy_helpers'
UPPER_VERSION_EXCLUSIVE = None

# Defaults for other options
DOWNLOAD_IF_NEEDED = True
INDEX_URL = 'https://pypi.python.org/simple'
USE_GIT = True
OFFLINE = False
AUTO_UPGRADE = True

# A list of all the configuration options and their required types
CFG_OPTIONS = [
    ('auto_use', bool), ('path', str), ('download_if_needed', bool),
    ('index_url', str), ('use_git', bool), ('offline', bool),
    ('auto_upgrade', bool)
]

# Start off by parsing the setup.cfg file

SETUP_CFG = ConfigParser()

if os.path.exists('setup.cfg'):

    try:
        SETUP_CFG.read('setup.cfg')
    except Exception as e:
        if DEBUG:
            raise

        log.error(
            "Error reading setup.cfg: {0!r}\n{1} will not be "
            "automatically bootstrapped and package installation may fail."
            "\n{2}".format(e, PACKAGE_NAME, _err_help_msg))

__minimum_python_version__ = (3, 5)

if sys.version_info < __minimum_python_version__:

    # We used package_name in the package template for a while instead of name
    if SETUP_CFG.has_option('metadata', 'name'):
        parent_package = SETUP_CFG.get('metadata', 'name')
    elif SETUP_CFG.has_option('metadata', 'package_name'):
        parent_package = SETUP_CFG.get('metadata', 'package_name')
    else:
        parent_package = None

    if parent_package is None:
{'fraction_non_alphanumeric': 0.07613397602274131, 'fraction_numerical': 0.001235941169200346, 'mean_word_length': 4.244329228775113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '5954730', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2232, 'n_words': 976}