<commit_before>#!/usr/bin/env python

#
#    Copyright (c) 2010 <NAME>
#
#    This file is part of pyzmq.
#
#    pyzmq is free software; you can redistribute it and/or modify it under
#    the terms of the Lesser GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    pyzmq is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    Lesser GNU General Public License for more details.
#
#    You should have received a copy of the Lesser GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    The `configure` subcommand is copied and adaped from h5py
#    h5py source used under the New BSD license
#
#    h5py: <http://code.google.com/p/h5py/>
#    BSD license: <http://www.opensource.org/licenses/bsd-license.php>
#

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import with_statement

import os, sys, shutil
from traceback import print_exc

from distutils.core import setup, Command
from distutils.ccompiler import get_default_compiler
from distutils.extension import Extension
from distutils.command.sdist import sdist
from distutils.command.build_ext import build_ext

from unittest import TextTestRunner, TestLoader
from glob import glob
from os.path import splitext, basename, join as pjoin

from subprocess import Popen, PIPE
import logging

try:
    from configparser import ConfigParser
except:
    from ConfigParser import ConfigParser

try:
    import nose
except ImportError:
    nose = None

# local script imports:
import detect

#-----------------------------------------------------------------------------
# Flags
#-----------------------------------------------------------------------------
# ignore unused-function and strict-aliasing warnings, of which there
# will be many from the Cython generated code:
# note that this is only for gcc-style compilers
if get_default_compiler() in ('unix', 'mingw32'):
    ignore_common_warnings=True
else:
    ignore_common_warnings=False

release = False # flag for whether to include *.c in package_data

# the minimum zeromq version this will work against:
min_zmq = (2,1,0)
#-----------------------------------------------------------------------------
# Configuration (adapted from h5py: http://h5py.googlecode.com)
#-----------------------------------------------------------------------------
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler(sys.stderr))
# --- Convenience functions --------------------------------------------------

def debug(what):
    pass

def fatal(instring, code=1):
    logger.error("Fatal: "+instring)
    exit(code)

def warn(instring):
    logger.error("Warning: "+instring)

def localpath(*args):
    return os.path.abspath(reduce(pjoin, (os.path.dirname(__file__),)+args))

def loadpickle(name):
    """ Load object from pickle file, or None if it can't be opened """
    import pickle
    name = pjoin('conf', name)
    try:
        f = open(name,'rb')
    except IOError:
        # raise
        return None
    try:
        return pickle.load(f)
    except Exception:
        # raise
        return None
    finally:
        f.close()

def savepickle(name, data):
    """ Save to pickle file, exiting if it can't be written """
    import pickle
    if not os.path.exists('conf'):
        os.mkdir('conf')
    name = pjoin('conf', name)
    try:
        f = open(name, 'wb')
    except IOError:
        fatal("Can't open pickle file \"%s\" for writing" % name)
    try:
        pickle.dump(data, f, 0)
    finally:
        f.close()

def v_str(v_tuple):
    """turn (2,0,1) into '2.0.1'."""
    return ".".join(str(x) for x in v_tuple)

# --- Try to discover path ---

def discover_settings():
    """ Discover custom settings for ZMQ path"""

    def get_eargs():
        """ Look for options in environment vars """

        settings = {}

        zmq = os.environ.get("ZMQ_DIR", '')
        if zmq != '':
            debug("Found environ var ZMQ_DIR=%s" % zmq)
            settings['zmq'] = zmq

        return settings

    def get_cfg_args():
        """ Look for options in setup.cfg """

        settings = {}
        zmq = ''
        if not os.path.exists('setup.cfg'):
            return settings
        cfg = ConfigParser()
        cfg.read('setup.cfg')
        if 'build_ext' in cfg.sections() and \
                    cfg.has_option('build_ext', 'include_dirs'):
            includes = cfg.get('build_ext', 'include_dirs')
            include = includes.split(os.pathsep)[0]
            if include.endswith('include') and os.path.isdir(include):
                zmq = include[:-8]
        if zmq != '':
            debug("Found ZMQ=%s in setup.cfg" % zmq)
            settings['zmq'] = zmq

        return settings

    def get_cargs():
        """ Look for global options in the command line """
        settings = loadpickle('buildconf.pickle')
        if settings is None:  settings = {}
        for arg in sys.argv[:]:
            if arg.find('--zmq=') == 0:
                zmq = arg.split('=')[-1]
                if zmq.lower() == 'default':
                    settings.pop('zmq', None)
                else:
                    settings['zmq'] = zmq
                sys.argv.remove(arg)
        savepickle('buildconf.pickle', settings)
        return settings

    settings = get_cfg_args()       # lowest priority
    settings.update(get_eargs())
    settings.update(get_cargs())    # highest priority
    return settings.get('zmq')

ZMQ = None
for cmd in ['install', 'build', 'build_ext', 'configure']:
    if cmd in sys.argv:
        ZMQ = discover_settings()
        break

if ZMQ is not None and not os.path.exists(ZMQ):
    warn("ZMQ directory \"%s\" does not appear to exist" % ZMQ)

# --- compiler settings -------------------------------------------------

if sys.platform.startswith('win'):
    COMPILER_SETTINGS = {
        'libraries'     : ['libzmq'],
        'include_dirs'  : [],
        'library_dirs'  : [],
    }
    if ZMQ is not None:
        COMPILER_SETTINGS['include_dirs'] += [pjoin(ZMQ, 'include')]
        COMPILER_SETTINGS['library_dirs'] += [pjoin(ZMQ, 'lib')]<commit_msg>always discover_settings<commit_after>#!/usr/bin/env python

#
#    Copyright (c) 2010 <NAME>
#
#    This file is part of pyzmq.
#
#    pyzmq is free software; you can redistribute it and/or modify it under
#    the terms of the Lesser GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    pyzmq is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    Lesser GNU General Public License for more details.
#
#    You should have received a copy of the Lesser GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    The `configure` subcommand is copied and adaped from h5py
#    h5py source used under the New BSD license
#
#    h5py: <http://code.google.com/p/h5py/>
#    BSD license: <http://www.opensource.org/licenses/bsd-license.php>
#

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import with_statement

import os, sys, shutil
from traceback import print_exc

from distutils.core import setup, Command
from distutils.ccompiler import get_default_compiler
from distutils.extension import Extension
from distutils.command.sdist import sdist
from distutils.command.build_ext import build_ext

from unittest import TextTestRunner, TestLoader
from glob import glob
from os.path import splitext, basename, join as pjoin

from subprocess import Popen, PIPE
import logging

try:
    from configparser import ConfigParser
except:
    from ConfigParser import ConfigParser

try:
    import nose
except ImportError:
    nose = None

# local script imports:
import detect

#-----------------------------------------------------------------------------
# Flags
#-----------------------------------------------------------------------------
# ignore unused-function and strict-aliasing warnings, of which there
# will be many from the Cython generated code:
# note that this is only for gcc-style compilers
if get_default_compiler() in ('unix', 'mingw32'):
    ignore_common_warnings=True
else:
    ignore_common_warnings=False

release = False # flag for whether to include *.c in package_data

# the minimum zeromq version this will work against:
min_zmq = (2,1,0)
#-----------------------------------------------------------------------------
# Configuration (adapted from h5py: http://h5py.googlecode.com)
#-----------------------------------------------------------------------------
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler(sys.stderr))
# --- Convenience functions --------------------------------------------------

def debug(what):
    pass

def fatal(instring, code=1):
    logger.error("Fatal: "+instring)
    exit(code)

def warn(instring):
    logger.error("Warning: "+instring)

def localpath(*args):
    return os.path.abspath(reduce(pjoin, (os.path.dirname(__file__),)+args))

def loadpickle(name):
    """ Load object from pickle file, or None if it can't be opened """
    import pickle
    name = pjoin('conf', name)
    try:
        f = open(name,'rb')
    except IOError:
        # raise
        return None
    try:
        return pickle.load(f)
    except Exception:
        # raise
        return None
    finally:
        f.close()

def savepickle(name, data):
    """ Save to pickle file, exiting if it can't be written """
    import pickle
    if not os.path.exists('conf'):
        os.mkdir('conf')
    name = pjoin('conf', name)
    try:
        f = open(name, 'wb')
    except IOError:
        fatal("Can't open pickle file \"%s\" for writing" % name)
    try:
        pickle.dump(data, f, 0)
    finally:
        f.close()

def v_str(v_tuple):
    """turn (2,0,1) into '2.0.1'."""
    return ".".join(str(x) for x in v_tuple)

# --- Try to discover path ---

def discover_settings():
    """ Discover custom settings for ZMQ path"""

    def get_eargs():
        """ Look for options in environment vars """

        settings = {}

        zmq = os.environ.get("ZMQ_DIR", '')
        if zmq != '':
            debug("Found environ var ZMQ_DIR=%s" % zmq)
            settings['zmq'] = zmq

        return settings

    def get_cfg_args():
        """ Look for options in setup.cfg """

        settings = {}
        zmq = ''
        if not os.path.exists('setup.cfg'):
            return settings
        cfg = ConfigParser()
        cfg.read('setup.cfg')
        if 'build_ext' in cfg.sections() and \
                    cfg.has_option('build_ext', 'include_dirs'):
            includes = cfg.get('build_ext', 'include_dirs')
            include = includes.split(os.pathsep)[0]
            if include.endswith('include') and os.path.isdir(include):
                zmq = include[:-8]
        if zmq != '':
            debug("Found ZMQ=%s in setup.cfg" % zmq)
            settings['zmq'] = zmq

        return settings

    def get_cargs():
        """ Look for global options in the command line """
        settings = loadpickle('buildconf.pickle')
        if settings is None:  settings = {}
        for arg in sys.argv[:]:
            if arg.find('--zmq=') == 0:
                zmq = arg.split('=')[-1]
                if zmq.lower() == 'default':
                    settings.pop('zmq', None)
                else:
                    settings['zmq'] = zmq
                sys.argv.remove(arg)
        savepickle('buildconf.pickle', settings)
        return settings

    settings = get_cfg_args()       # lowest priority
    settings.update(get_eargs())
    settings.update(get_cargs())    # highest priority
    return settings.get('zmq')

ZMQ = discover_settings()

if ZMQ is not None and not os.path.exists(ZMQ):
    warn("ZMQ directory \"%s\" does not appear to exist" % ZMQ)

# --- compiler settings -------------------------------------------------

if sys.platform.startswith('win'):
    COMPILER_SETTINGS = {
        'libraries'     : ['libzmq'],
        'include_dirs'  : [],
        'library_dirs'  : [],
    }
    if ZMQ is not None:
        COMPILER_SETTINGS['include_dirs'] += [pjoin(ZMQ, 'include')]
        COMPILER_SETTINGS['library_dirs'] += [pjoin(ZMQ, 'lib')]
{'fraction_non_alphanumeric': 0.19211899384783115, 'fraction_numerical': 0.004361031072346391, 'mean_word_length': 1.9494717501148369, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1900717', 'n_tokens_mistral': 3661, 'n_tokens_neox': 3573, 'n_words': 1122}