<commit_before>#Copyright ReportLab Europe Ltd. 2000-2008
#see license.txt for license details
__version__='''$Id$'''
__doc__="""Provides support for the test suite.

The test suite as a whole, and individual tests, need to share
certain support functions.  We have to put these in here so they
can always be imported, and so that individual tests need to import
nothing more than "reportlab.whatever..."
"""

import sys, os, string, fnmatch, copy, re
from ConfigParser import ConfigParser
import unittest

# Helper functions.
def isWritable(D):
    try:
        fn = '00DELETE.ME'
        f = open(fn, 'w')
        f.write('test of writability - can be deleted')
        f.close()
        if os.path.isfile(fn):
            os.remove(fn)
            return 1
    except:
        return 0

_OUTDIR = None
RL_HOME = None
testsFolder = None
def setOutDir(name):
    """Is it a writable file system distro being invoked within
    test directory?  If so, can write test output here.  If not,
    it had better go in a temp directory.  Only do this once per
    process"""
    global _OUTDIR, RL_HOME, testsFolder
    if _OUTDIR: return _OUTDIR
    D = [d[9:] for d in sys.argv if d.startswith('--outdir=')]
    if D:
        _OUTDIR = D[-1]
        try:
            os.makedirs(_OUTDIR)
        except:
            pass
        map(sys.argv.remove,D)
    else:
        assert name=='__main__',"setOutDir should only be called in the main script"
        scriptDir=os.path.dirname(sys.argv[0])
        if not scriptDir: scriptDir=os.getcwd()
        _OUTDIR = scriptDir

    if not isWritable(_OUTDIR):
        _OUTDIR = get_rl_tempdir('reportlab_test')

    import reportlab
    RL_HOME=reportlab.__path__[0]
    if not os.path.isabs(RL_HOME): RL_HOME=os.path.normpath(os.path.abspath(RL_HOME))
    topDir = os.path.dirname(RL_HOME)
    testsFolder = os.path.join(topDir,'tests')
    if not os.path.isdir(testsFolder):
        testsFolder = os.path.join(os.path.dirname(topDir),'tests')
    if not os.path.isdir(testsFolder):
        testsFolder = None
    return _OUTDIR

def outputfile(fn):
    """This works out where to write test output.  If running
    code in a locked down file system, this will be a
    temp directory; otherwise, the output of 'test_foo.py' will
    normally be a file called 'test_foo.pdf', next door.
    """
    D = setOutDir(__name__)
    if fn: D = os.path.join(D,fn)
    return D

def printLocation(depth=1):
    if sys._getframe(depth).f_locals.get('__name__')=='__main__':
        outDir = outputfile('')
        if outDir!=_OUTDIR:
            print 'Logs and output files written to folder "%s"' % outDir

def makeSuiteForClasses(*classes):
    "Return a test suite with tests loaded from provided classes."

    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for C in classes:
        suite.addTest(loader.loadTestsFromTestCase(C))
    return suite

def getCVSEntries(folder, files=1, folders=0):
    """Returns a list of filenames as listed in the CVS/Entries file.
<commit_msg>reportlab: another attempt at fixing testsFolder<commit_after>#Copyright ReportLab Europe Ltd. 2000-2008
#see license.txt for license details
__version__='''$Id$'''
__doc__="""Provides support for the test suite.

The test suite as a whole, and individual tests, need to share
certain support functions.  We have to put these in here so they
can always be imported, and so that individual tests need to import
nothing more than "reportlab.whatever..."
"""

import sys, os, string, fnmatch, copy, re
from ConfigParser import ConfigParser
import unittest

# Helper functions.
def isWritable(D):
    try:
        fn = '00DELETE.ME'
        f = open(fn, 'w')
        f.write('test of writability - can be deleted')
        f.close()
        if os.path.isfile(fn):
            os.remove(fn)
            return 1
    except:
        return 0

_OUTDIR = None
RL_HOME = None
testsFolder = None
def setOutDir(name):
    """Is it a writable file system distro being invoked within
    test directory?  If so, can write test output here.  If not,
    it had better go in a temp directory.  Only do this once per
    process"""
    global _OUTDIR, RL_HOME, testsFolder
    if _OUTDIR: return _OUTDIR
    D = [d[9:] for d in sys.argv if d.startswith('--outdir=')]
    if D:
        _OUTDIR = D[-1]
        try:
            os.makedirs(_OUTDIR)
        except:
            pass
        map(sys.argv.remove,D)
    else:
        assert name=='__main__',"setOutDir should only be called in the main script"
        scriptDir=os.path.dirname(sys.argv[0])
        if not scriptDir: scriptDir=os.getcwd()
        _OUTDIR = scriptDir

    if not isWritable(_OUTDIR):
        _OUTDIR = get_rl_tempdir('reportlab_test')

    import reportlab
    RL_HOME=reportlab.__path__[0]
    if not os.path.isabs(RL_HOME): RL_HOME=os.path.normpath(os.path.abspath(RL_HOME))
    topDir = os.path.dirname(RL_HOME)
    testsFolder = os.path.join(topDir,'tests')
    if not os.path.isdir(testsFolder):
        testsFolder = os.path.join(os.path.dirname(topDir),'tests')
    if not os.path.isdir(testsFolder):
        if name=='__main__':
            scriptDir=os.path.dirname(sys.argv[0])
            if not scriptDir: scriptDir=os.getcwd()
            testsFolder = os.path.abspath(scriptDir)
        else:
            testsFolder = None
    return _OUTDIR

def outputfile(fn):
    """This works out where to write test output.  If running
    code in a locked down file system, this will be a
    temp directory; otherwise, the output of 'test_foo.py' will
    normally be a file called 'test_foo.pdf', next door.
    """
    D = setOutDir(__name__)
    if fn: D = os.path.join(D,fn)
    return D

def printLocation(depth=1):
    if sys._getframe(depth).f_locals.get('__name__')=='__main__':
        outDir = outputfile('')
        if outDir!=_OUTDIR:
            print 'Logs and output files written to folder "%s"' % outDir

def makeSuiteForClasses(*classes):
    "Return a test suite with tests loaded from provided classes."

    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for C in classes:
        suite.addTest(loader.loadTestsFromTestCase(C))
    return suite

def getCVSEntries(folder, files=1, folders=0):
    """Returns a list of filenames as listed in the CVS/Entries file.

{'fraction_non_alphanumeric': 0.10066582117945466, 'fraction_numerical': 0.006182625237793279, 'mean_word_length': 2.863441518677281, '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': '2338501', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1962, 'n_words': 639}