<commit_before>import logging
import os
import shutil
import subprocess
import sys
from setuptools import setup
from setuptools.extension import Extension

# Use Cython if available.
try:
    from Cython.Build import cythonize
except ImportError:
    cythonize = None

logging.basicConfig()
log = logging.getLogger()

# python -W all setup.py ...
if 'all' in sys.warnoptions:
    log.level = logging.DEBUG

def check_output(cmd):
    # since subprocess.check_output doesn't exist in 2.6
    # we wrap it here.
    try:
        out = subprocess.check_output(cmd)
        return out.decode('utf')
    except AttributeError:
        # For some reasone check_output doesn't exist
        # So fall back on Popen
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        out, err = p.communicate()
        return out

def copy_data_tree(datadir, destdir):
    try:
        shutil.rmtree(destdir)
    except OSError:
        pass
    shutil.copytree(datadir, destdir)

# Parse the version from the fiona module.
with open('fiona/__init__.py', 'r') as f:
    for line in f:
        if line.find("__version__") >= 0:
            version = line.split("=")[1].strip()
            version = version.strip('"')
            version = version.strip("'")
            continue

# Fiona's auxiliary files are UTF-8 encoded and we'll specify this when
# reading with Python 3+
open_kwds = {}
if sys.version_info > (3,):
    open_kwds['encoding'] = 'utf-8'

with open('VERSION.txt', 'w', **open_kwds) as f:
    f.write(version)

with open('README.rst', **open_kwds) as f:
    readme = f.read()

with open('CREDITS.txt', **open_kwds) as f:
    credits = f.read()

with open('CHANGES.txt', **open_kwds) as f:
    changes = f.read()


def copy_gdalapi(gdalversion):
    if gdalversion[0] == u'1':
        log.info("Building Fiona for gdal 1.x: {}".format(gdal_output[3]))
        shutil.copy('fiona/ogrext1.pyx', 'fiona/ogrext.pyx')
        shutil.copy('fiona/ograpi1.pxd', 'fiona/ograpi.pxd')
    else:
        log.info("Building Fiona for gdal 2.x: {}".format(gdal_output[3]))
        shutil.copy('fiona/ogrext2.pyx', 'fiona/ogrext.pyx')
        shutil.copy('fiona/ograpi2.pxd', 'fiona/ograpi.pxd')

if '--gdalversion' in sys.argv:
    index = sys.argv.index('--gdalversion')
    sys.argv.pop(index)
    gdalversion = sys.argv.pop(index)
    copy_gdalapi(gdalversion)

# By default we'll try to get options via gdal-config. On systems without,
# options will need to be set in setup.cfg or on the setup command line.
include_dirs = []
library_dirs = []
libraries = []
extra_link_args = []
gdal_output = [None] * 4

try:
    gdal_config = os.environ.get('GDAL_CONFIG', 'gdal-config')
    for i, flag in enumerate(("--cflags", "--libs", "--datadir", "--version")):
        gdal_output[i] = check_output([gdal_config, flag]).strip()

    for item in gdal_output[0].split():
        if item.startswith("-I"):
            include_dirs.extend(item[2:].split(":"))
    for item in gdal_output[1].split():
        if item.startswith("-L"):
            library_dirs.extend(item[2:].split(":"))
        elif item.startswith("-l"):
            libraries.append(item[2:])
        else:
            # e.g. -framework GDAL
            extra_link_args.append(item)

    copy_gdalapi(gdal_output[3])

except Exception as e:
    if os.name == "nt":
        log.info(("Building on Windows requires extra options to setup.py to locate needed GDAL files.\n"
                   "More information is available in the README."))
    else:
        log.warning("Failed to get options via gdal-config: %s", str(e))

    # Conditionally copy the GDAL data. To be used in conjunction with
    # the bdist_wheel command to make self-contained binary wheels.
    if os.environ.get('PACKAGE_DATA'):
        try:
            shutil.rmtree('fiona/gdal_data')
        except OSError:
            pass
        shutil.copytree(datadir, 'fiona/gdal_data')
if os.environ.get('PACKAGE_DATA'):
    destdir = 'fiona/gdal_data'
    if gdal_output[2]:
        log.info("Copying gdal data from %s" % gdal_output[2])
        copy_data_tree(gdal_output[2], destdir)
    else:
        # check to see if GDAL_DATA is defined
        gdal_data = os.environ.get('GDAL_DATA', None)
        if gdal_data:
            log.info("Copying gdal data from %s" % gdal_data)
            copy_data_tree(gdal_data, destdir)

    # Conditionally copy PROJ.4 data. 
    projdatadir = os.environ.get('PROJ_LIB', '/usr/local/share/proj')
    if os.path.exists(projdatadir):
        log.info("Copying proj data from %s" % projdatadir)
        copy_data_tree(projdatadir, 'fiona/proj_data')

ext_options = dict(
    include_dirs=include_dirs,
    library_dirs=library_dirs,
    libraries=libraries,
    extra_link_args=extra_link_args)

# When building from a repo, Cython is required.
if os.path.exists("MANIFEST.in"):
    log.info("MANIFEST.in found, presume a repo, cythonizing...")
    if not cythonize:
        log.critical(<commit_msg>exclude clean as suggested by @sebastic<commit_after>import logging
import os
import shutil
import subprocess
import sys
from setuptools import setup
from setuptools.extension import Extension

# Use Cython if available.
try:
    from Cython.Build import cythonize
except ImportError:
    cythonize = None

logging.basicConfig()
log = logging.getLogger()

# python -W all setup.py ...
if 'all' in sys.warnoptions:
    log.level = logging.DEBUG

def check_output(cmd):
    # since subprocess.check_output doesn't exist in 2.6
    # we wrap it here.
    try:
        out = subprocess.check_output(cmd)
        return out.decode('utf')
    except AttributeError:
        # For some reasone check_output doesn't exist
        # So fall back on Popen
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        out, err = p.communicate()
        return out

def copy_data_tree(datadir, destdir):
    try:
        shutil.rmtree(destdir)
    except OSError:
        pass
    shutil.copytree(datadir, destdir)

# Parse the version from the fiona module.
with open('fiona/__init__.py', 'r') as f:
    for line in f:
        if line.find("__version__") >= 0:
            version = line.split("=")[1].strip()
            version = version.strip('"')
            version = version.strip("'")
            continue

# Fiona's auxiliary files are UTF-8 encoded and we'll specify this when
# reading with Python 3+
open_kwds = {}
if sys.version_info > (3,):
    open_kwds['encoding'] = 'utf-8'

with open('VERSION.txt', 'w', **open_kwds) as f:
    f.write(version)

with open('README.rst', **open_kwds) as f:
    readme = f.read()

with open('CREDITS.txt', **open_kwds) as f:
    credits = f.read()

with open('CHANGES.txt', **open_kwds) as f:
    changes = f.read()


def copy_gdalapi(gdalversion):
    if gdalversion[0] == u'1':
        log.info("Building Fiona for gdal 1.x: {}".format(gdal_output[3]))
        shutil.copy('fiona/ogrext1.pyx', 'fiona/ogrext.pyx')
        shutil.copy('fiona/ograpi1.pxd', 'fiona/ograpi.pxd')
    else:
        log.info("Building Fiona for gdal 2.x: {}".format(gdal_output[3]))
        shutil.copy('fiona/ogrext2.pyx', 'fiona/ogrext.pyx')
        shutil.copy('fiona/ograpi2.pxd', 'fiona/ograpi.pxd')

if '--gdalversion' in sys.argv:
    index = sys.argv.index('--gdalversion')
    sys.argv.pop(index)
    gdalversion = sys.argv.pop(index)
    copy_gdalapi(gdalversion)

# By default we'll try to get options via gdal-config. On systems without,
# options will need to be set in setup.cfg or on the setup command line.
include_dirs = []
library_dirs = []
libraries = []
extra_link_args = []
gdal_output = [None] * 4

try:
    gdal_config = os.environ.get('GDAL_CONFIG', 'gdal-config')
    for i, flag in enumerate(("--cflags", "--libs", "--datadir", "--version")):
        gdal_output[i] = check_output([gdal_config, flag]).strip()

    for item in gdal_output[0].split():
        if item.startswith("-I"):
            include_dirs.extend(item[2:].split(":"))
    for item in gdal_output[1].split():
        if item.startswith("-L"):
            library_dirs.extend(item[2:].split(":"))
        elif item.startswith("-l"):
            libraries.append(item[2:])
        else:
            # e.g. -framework GDAL
            extra_link_args.append(item)

    copy_gdalapi(gdal_output[3])

except Exception as e:
    if os.name == "nt":
        log.info(("Building on Windows requires extra options to setup.py to locate needed GDAL files.\n"
                   "More information is available in the README."))
    else:
        log.warning("Failed to get options via gdal-config: %s", str(e))

    # Conditionally copy the GDAL data. To be used in conjunction with
    # the bdist_wheel command to make self-contained binary wheels.
    if os.environ.get('PACKAGE_DATA'):
        try:
            shutil.rmtree('fiona/gdal_data')
        except OSError:
            pass
        shutil.copytree(datadir, 'fiona/gdal_data')
if os.environ.get('PACKAGE_DATA'):
    destdir = 'fiona/gdal_data'
    if gdal_output[2]:
        log.info("Copying gdal data from %s" % gdal_output[2])
        copy_data_tree(gdal_output[2], destdir)
    else:
        # check to see if GDAL_DATA is defined
        gdal_data = os.environ.get('GDAL_DATA', None)
        if gdal_data:
            log.info("Copying gdal data from %s" % gdal_data)
            copy_data_tree(gdal_data, destdir)

    # Conditionally copy PROJ.4 data. 
    projdatadir = os.environ.get('PROJ_LIB', '/usr/local/share/proj')
    if os.path.exists(projdatadir):
        log.info("Copying proj data from %s" % projdatadir)
        copy_data_tree(projdatadir, 'fiona/proj_data')

ext_options = dict(
    include_dirs=include_dirs,
    library_dirs=library_dirs,
    libraries=libraries,
    extra_link_args=extra_link_args)

# When building from a repo, Cython is required.
if os.path.exists("MANIFEST.in") and "clean" not in sys.argv:
    log.info("MANIFEST.in found, presume a repo, cythonizing...")
    if not cythonize:
        log.critical(
{'fraction_non_alphanumeric': 0.12870890136327184, 'fraction_numerical': 0.005813953488372093, 'mean_word_length': 2.8851246105919004, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, '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': '5460247', 'n_tokens_mistral': 3456, 'n_tokens_neox': 3417, 'n_words': 841}