Beispiel #1
0
def test_2():
    "Test fmtxt/table.py"
    exa_dir = os.path.join(examples_dir, 'fmtxt')
    exa_file = os.path.join(exa_dir, 'table.py')

    # find required files
    with open(exa_file) as fid:
        text = fid.read()
    filenames = re.findall("# requires: (\w+.\w+)", text)
    text = text.replace("n_samples = 1000", "n_samples = 2")

    # copy all files to temporary dir
    tempdir = mkdtemp()
    dst = os.path.join(tempdir, 'table.py')
    with open(dst, 'w') as fid:
        fid.write(text)
    for filename in filenames:
        src = os.path.join(exa_dir, filename)
        shutil.copy(src, tempdir)

    # execute example
    logging.info("executing from %s" % tempdir)
    plot.configure(show=False)
    os.chdir(tempdir)
    execfile('table.py', {})

    # delete temporary files
    shutil.rmtree(tempdir)
def test_2():
    "Test examples/experiment/sample_experiment_sessions.py"
    exa_dir = os.path.join(examples_dir, "experiment")
    exa_file = os.path.join(exa_dir, "sample_experiment_sessions.py")

    # find required files
    with open(exa_file) as fid:
        text = fid.read()
    filenames = re.findall("# requires: (\w+.\w+)", text)
    text = text.replace("n_samples = 1000", "n_samples = 2")

    # copy all files to temporary dir
    tempdir = mkdtemp()
    dst = os.path.join(tempdir, "sample_experiment_sessions.py")
    with open(dst, "w") as fid:
        fid.write(text)
    for filename in filenames:
        src = os.path.join(exa_dir, filename)
        shutil.copy(src, tempdir)

    # execute example
    logging.info("executing from %s" % tempdir)
    plot.configure(show=False)
    os.chdir(tempdir)
    execfile("sample_experiment_sessions.py", {})

    # delete temporary files
    shutil.rmtree(tempdir)
Beispiel #3
0
def run_example(example_path, name):
    "Run the example script at ``filename``"
    dirname, example_filename = os.path.split(example_path)

    # read example
    with open(example_path) as fid:
        text = fid.read()

    # check for explicit skip
    if re.findall("^# skip test", text, re.MULTILINE):
        return

    # check for required modules
    required_modules = re.findall("^# requires: (\w+)", text, re.MULTILINE)
    for module in required_modules:
        try:
            __import__(module)
        except ImportError:
            raise SkipTest("required module not available: %s" % module)

    # check for required datasets
    required_datasets = re.findall("^# dataset: (\w+)", text, re.MULTILINE)
    for dataset in required_datasets:
        if not DATASETS[dataset]:
            raise SkipTest("required dataset not available: %s" % dataset)

    # find required files
    required_files = re.findall("^# file: (\w+.\w+)", text, re.MULTILINE)

    # reduce computational load
    text = text.replace("n_samples = 1000", "n_samples = 2")

    # copy all files to temporary dir
    tempdir = mkdtemp()
    try:
        logging.info("Tempdir for %s at %s" % (name, tempdir))
        for filename in required_files:
            src = os.path.join(dirname, filename)
            logging.info(" Copying %s" % (filename, ))
            shutil.copy(src, tempdir)

        # execute example
        os.chdir(tempdir)
        logging.info(" Executing %s" % (name, ))
        plot.configure(show=False)
        exec(text, {})
    finally:
        # delete temporary files
        # FIXME:  on Windows (Appveyor) this raises a WindowsError indicating
        # that the folder is being used by another process
        if os.name != 'nt':
            shutil.rmtree(tempdir)