Пример #1
0
def execute_notebook(nb):
    km = BlockingKernelManager()
    km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w'))
    km.start_channels()
    # run %pylab inline, because some notebooks assume this
    # even though they shouldn't
    km.shell_channel.execute("pass")
    km.shell_channel.get_msg()
    while True:
        try:
            km.sub_channel.get_msg(timeout=1)
        except Empty:
            break
    
    successes = 0
    failures = 0
    errors = 0
    prompt_number = 1
    for ws in nb.worksheets:
        for cell in ws.cells:
            cell.prompt_number = prompt_number
            if cell.cell_type != 'code':
                continue
            try:
                outs = run_cell(km, cell)
            except Exception as e:
                print "failed to run cell:", repr(e)
                print cell.input
                errors += 1
                continue
            
            cell.outputs = outs
            prompt_number += 1
    km.shutdown_kernel()
    del km
Пример #2
0
def run_notebook(nb):
    km = BlockingKernelManager()
    km.start_kernel(stderr=open(os.devnull, 'w'))
    km.start_channels()
    shell = km.shell_channel
    # simple ping:
    shell.execute("pass")
    shell.get_msg()

    cells = 0
    failures = 0
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type != 'code':
                continue
            shell.execute(cell.input)
            # wait for finish, maximum 20s
            reply = shell.get_msg(timeout=20)['content']
            if reply['status'] == 'error':
                failures += 1
                print "\nFAILURE:"
                print cell.input
                print '-----'
                print "raised:"
                print '\n'.join(reply['traceback'])
            cells += 1
            sys.stdout.write('.')

    print
    print "ran notebook %s" % nb.metadata.name
    print "    ran %3i cells" % cells
    if failures:
        print "    %3i cells raised exceptions" % failures
    km.shutdown_kernel()
    del km
Пример #3
0
def run_notebook(nb):
    km = BlockingKernelManager()
    km.start_kernel(stderr=open(os.devnull, 'w'))
    km.start_channels()
    shell = km.shell_channel
    # simple ping:
    shell.execute("pass")
    shell.get_msg()
    
    cells = 0
    failures = 0
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type != 'code':
                continue
            shell.execute(cell.input)
            # wait for finish, maximum 20s
            reply = shell.get_msg(timeout=20)['content']
            if reply['status'] == 'error':
                failures += 1
                print "\nFAILURE:"
                print cell.input
                print '-----'
                print "raised:"
                print '\n'.join(reply['traceback'])
            cells += 1
            sys.stdout.write('.')

    print
    print "ran notebook %s" % nb.metadata.name
    print "    ran %3i cells" % cells
    if failures:
        print "    %3i cells raised exceptions" % failures
    km.shutdown_kernel()
    del km
Пример #4
0
def test_notebook(nb):
    km = BlockingKernelManager()
    km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w'))
    km.start_channels()
    # run %pylab inline, because some notebooks assume this
    # even though they shouldn't
    km.shell_channel.execute("pass")
    km.shell_channel.get_msg()
    while True:
        try:
            km.sub_channel.get_msg(timeout=1)
        except Empty:
            break
    
    successes = 0
    failures = 0
    errors = 0
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type != 'code':
                continue
            try:
                outs = run_cell(km, cell)
            except Exception as e:
                print "failed to run cell:", repr(e)
                print cell.input
                errors += 1
                continue
            
            failed = False
            for out, ref in zip(outs, cell.outputs):
                if not compare_outputs(out, ref):
                    failed = True
            if failed:
                failures += 1
            else:
                successes += 1
            sys.stdout.write('.')

    print
    print "tested notebook %s" % nb.metadata.name
    print "    %3i cells successfully replicated" % successes
    if failures:
        print "    %3i cells mismatched output" % failures
    if errors:
        print "    %3i cells failed to complete" % errors
    km.shutdown_kernel()
    del km
Пример #5
0
def new_kernel():
    """start a kernel in a subprocess, and wait for it to be ready
    
    Returns
    -------
    kernel_manager: connected KernelManager instance
    """
    KM = BlockingKernelManager()

    KM.start_kernel(stdout=PIPE, stderr=PIPE)
    KM.start_channels()
    
    # wait for kernel to be ready
    KM.shell_channel.execute("import sys")
    KM.shell_channel.get_msg(block=True, timeout=5)
    flush_channels(KM)
    try:
        yield KM
    finally:
        KM.stop_channels()
        KM.shutdown_kernel()
Пример #6
0
def new_kernel():
    """start a kernel in a subprocess, and wait for it to be ready
    
    Returns
    -------
    kernel_manager: connected KernelManager instance
    """
    KM = BlockingKernelManager()

    KM.start_kernel(stdout=PIPE, stderr=PIPE)
    KM.start_channels()

    # wait for kernel to be ready
    KM.shell_channel.execute("import sys")
    KM.shell_channel.get_msg(block=True, timeout=5)
    flush_channels(KM)
    try:
        yield KM
    finally:
        KM.stop_channels()
        KM.shutdown_kernel()
Пример #7
0
class IPyNbFile(pytest.File):
    def collect(self):
        with self.fspath.open() as f:
            self.nb = reads(f.read(), 'json')

            cell_num = 0

            for ws in self.nb.worksheets:
                for cell in ws.cells:
                    if cell.cell_type == "code":
                        yield IPyNbCell(self.name, self, cell_num, cell)
                        cell_num += 1

    def setup(self):
        self.km = BlockingKernelManager()
        self.km.start_kernel(stderr=open(os.devnull, 'w'))
        self.km.start_channels()
        self.shell = self.km.shell_channel

    def teardown(self):
        self.km.shutdown_kernel()
        del self.shell
        del self.km
Пример #8
0
def test_notebook(nb, halt_on_error=True, km=None):
    our_kernel = km is None
    if our_kernel:
        km = BlockingKernelManager()
        km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w'))
        km.start_channels()
    print km.connection_file
    # run %pylab inline, because some notebooks assume this
    # even though they shouldn't
    km.shell_channel.execute("pass")
    km.shell_channel.get_msg()
    km.restart_kernel()
    km.shell_channel.get_msg()
    while True:
        try:
            km.sub_channel.get_msg(timeout=1)
        except Empty:
            break
    
    successes = 0
    failures = 0
    errors = 0
    halted = False
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type != 'code':
                continue
            try:
                outs, exec_count = run_cell(km, cell)
            except Exception as e:
                log.critical( "failed to run cell:"+ repr(e))
                log.critical("tested notebook %s" % nb.metadata.name)
                print cell.input
                errors += 1
                continue
            c = '.'
            for out in outs:
                if out['output_type'] == 'pyerr':
                    c = 'E'
            if log.getEffectiveLevel() < logging.FATAL:
                sys.stderr.write(c)
            
            failed = False
            #for out, ref in zip(outs, cell.outputs):
            #    if not compare_outputs(out, ref):
            #        failed = True
            if failed:
                failures += 1
            else:
                successes += 1
            cell.outputs = outs
            cell.prompt_number = exec_count
            if c == 'E' and halt_on_error:
                log.info("halting on error")
                halted = True
                # XXX: this next part is specific to nbexplode
                save_intermediate(km, ws, cell, nb.metadata.name)
                break;

    if log.getEffectiveLevel() <= logging.WARNING:
        sys.stderr.write('\n')
    log.info("tested notebook %s" % nb.metadata.name)
    log.info("    %3i cells successfully replicated" % successes)
    if failures:
        log.info("    %3i cells mismatched output" % failures)
    if errors:
        log.info("    %3i cells failed to complete" % errors)
    if our_kernel:
        km.shutdown_kernel()
        del km
    return nb, halted
Пример #9
0
    if args.saveonerror:
        # XXX: hack - just dange an attribute we can check against
        km.saveonerror = True

    halt_on_error = not args.all
    halt_notebooks  = not args.allnotebooks
    for ipynb in args.inputs:
        log.info('Running '+ ipynb)
        with open(ipynb) as f:
            nb = reads(f.read(), 'json')
        # here, before running the notebook, it'd be nice if we verified it's
        # metadata.name, and adjusted it according to the current filename, so
        # let's do that
        nb.metadata.name = os.path.basename(os.path.splitext(ipynb)[0])
        output_nb, halted = test_notebook(nb, halt_on_error, km)
        nb_as_json = writes(output_nb, 'json')
        if args.stdout:
            sys.stdout.write(nb_as_json)
        else:
            outfile = args.prefix+ipynb
            with open(outfile, 'w') as f:
                f.write(nb_as_json)
            if log.getEffectiveLevel() < logging.CRITICAL:
                print outfile
            log.info("Wrote file " + outfile)
        if halted and halt_notebooks:
            break
    km.shutdown_kernel()
    del km
    sys.exit(halted and halt_notebooks)