<commit_before>"""
The sole purpose of this module is to alter the sys.path upon 
import in such a way to get pygr from the source directory rather than
@CTB finish comment?

See the README.txt file for details on how to change the behavior.

NOTE in place builds are required:

python setup.py build_ext -i
"""

import sys, os, distutils.util, platform

import testoptions

def path_join(*args):
    "Joins and normalizes paths"
    return os.path.abspath(os.path.join(*args))

def stop(msg):
    "A fatal unrecoverable error"
    sys.stderr.write(msg + '\n')
    sys.exit()

# get the current directory of the current module
curr_dir = os.path.dirname(__file__)

# this is the extra path that needs be added
base_dir = path_join(curr_dir, '..', '..')

# get the pygr source directory
pygr_source_dir = path_join(base_dir, 'pygr')

# build specific directories
os_info = distutils.util.get_platform()
version = ".".join([str(x) for x in platform.python_version_tuple()[:2]])
lib_dir  = 'lib.%s-%s' % (os_info, version,)
temp_dir = 'temp.%s-%s' % (os_info, version,)
pygr_build_dir = path_join(base_dir, 'build', lib_dir)
pygr_temp_dir  = path_join(base_dir, 'build', temp_dir)

# we'll do a top level option parsing in this module as well
parser = testoptions.option_parser()

# parse the arguments
options, args = parser.parse_args()

# this makes it less clunky
use_pathfix = not options.no_pathfix

if use_pathfix:
    # alter the import path
    if options.builddir:
        sys.path = [ pygr_build_dir  ] + sys.path 
        required_prefix = pygr_build_dir
    else:
        sys.path = [ base_dir  ] + sys.path
        required_prefix = pygr_source_dir

###

# also, start coverage

def start_coverage():
    import figleaf
    from figleaf import annotate_html

    # Fix for figleaf misbehaving. It is adding a logger at root level 
    # and that will add a handler to all subloggers (ours as well)
    # needs to be fixed in figleaf
    import logging
    root = logging.getLogger()
    
    # remove all root handlers
    for hand in root.handlers: 
        root.removeHandler(hand)

    figleaf.start()

if options.coverage:
    start_coverage()

###

try:
    # import the main pygr module
    import pygr
except ImportError, exc:
    stop ("unable to import: %s" %  exc)
      
try:
    # import an extension module
    from pygr import cnestedlist
except ImportError, exc:
    stop("unable to import extension module: %s" %  exc )

if use_pathfix:<commit_msg>better error message upon importerrors<commit_after>"""
The sole purpose of this module is to alter the sys.path upon 
import in such a way to get pygr from the source directory rather than
@CTB finish comment?

See the README.txt file for details on how to change the behavior.

NOTE in place builds are required:

python setup.py build_ext -i
"""

import sys, os, distutils.util, platform

import testoptions

def path_join(*args):
    "Joins and normalizes paths"
    return os.path.abspath(os.path.join(*args))

def stop(msg):
    "A fatal unrecoverable error"
    sys.stderr.write(msg + '\n')
    sys.exit()

# get the current directory of the current module
curr_dir = os.path.dirname(__file__)

# this is the extra path that needs be added
base_dir = path_join(curr_dir, '..', '..')

# get the pygr source directory
pygr_source_dir = path_join(base_dir, 'pygr')

# build specific directories
os_info = distutils.util.get_platform()
version = ".".join([str(x) for x in platform.python_version_tuple()[:2]])
lib_dir  = 'lib.%s-%s' % (os_info, version,)
temp_dir = 'temp.%s-%s' % (os_info, version,)
pygr_build_dir = path_join(base_dir, 'build', lib_dir)
pygr_temp_dir  = path_join(base_dir, 'build', temp_dir)

# we'll do a top level option parsing in this module as well
parser = testoptions.option_parser()

# parse the arguments
options, args = parser.parse_args()

# this makes it less clunky
use_pathfix = not options.no_pathfix

if use_pathfix:
    # alter the import path
    if options.builddir:
        sys.path = [ pygr_build_dir  ] + sys.path 
        required_prefix = pygr_build_dir
    else:
        sys.path = [ base_dir  ] + sys.path
        required_prefix = pygr_source_dir

###

# also, start coverage

def start_coverage():
    import figleaf
    from figleaf import annotate_html

    # Fix for figleaf misbehaving. It is adding a logger at root level 
    # and that will add a handler to all subloggers (ours as well)
    # needs to be fixed in figleaf
    import logging
    root = logging.getLogger()
    
    # remove all root handlers
    for hand in root.handlers: 
        root.removeHandler(hand)

    figleaf.start()

if options.coverage:
    start_coverage()

###

try:
    # import the main pygr module
    import pygr
except ImportError, exc:
    stop ("unable to import: %s" %  exc)
      
try:
    # import an extension module
    from pygr import cnestedlist
except ImportError, exc:
    stop("""
    Unable to import extension modules: '%s'
    
    Either build in place with: python setup.py build_ext -i
    
    Or pass the -b flag to runtest.py (see runtest.py -h for more details)
    """ % exc)

if use_pathfix:
{'fraction_non_alphanumeric': 0.09287383177570094, 'fraction_numerical': 0.0003894080996884735, 'mean_word_length': 3.417024935511608, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '739557', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1642, 'n_words': 576}