Beispiel #1
0
 def from_notebook_cell(self, cell, fh, idx = 0):
     if not hasattr(cell, 'execution_count') or cell.execution_count is None or self.no_index:
         fh.write('\n%cell {}\n'.format(cell.cell_type))
     else:
         idx += 1
         fh.write('\n%cell {} {}\n'.format(cell.cell_type,
                                           idx if self.reset_index else cell.execution_count))
     if cell.cell_type == 'code':
         if any(cell.source.startswith(x) for x in ('%run', '%restart', '%dict', '%get', '%use', '%with', '%set', '%paste', '%matplotlib', '%edit')):
             if self.remove_magic:
                 cell.source = '\n'.join(cell.source.split('\n')[1:])
         if self.add_header and not any([SOS_SECTION_HEADER.match(x) for x in cell.source.split('\n')]):
             cell.source = '[{}]\n'.format(idx if self.reset_index else cell.execution_count) + cell.source
         fh.write(cell.source.strip() + '\n')
     elif cell.cell_type == "markdown":
         fh.write('\n'.join('#! ' + x for x in cell.source.split('\n')) + '\n')
     return idx
Beispiel #2
0
 def from_notebook_cell(self, cell, fh, idx = 0):
     if not hasattr(cell, 'execution_count') or cell.execution_count is None or self.no_index:
         fh.write('\n%cell {}\n'.format(cell.cell_type))
     else:
         idx += 1
         fh.write('\n%cell {} {}\n'.format(cell.cell_type,
                                           idx if self.reset_index else cell.execution_count))
     if cell.cell_type == 'code':
         if any(cell.source.startswith(x) for x in ('%run', '%restart', '%dict', '%get', '%use', '%with', '%set', '%paste', '%matplotlib', '%edit')):
             if self.remove_magic:
                 cell.source = '\n'.join(cell.source.split('\n')[1:])
         if self.add_header and not any([SOS_SECTION_HEADER.match(x) for x in cell.source.split('\n')]):
             cell.source = '[{}]\n'.format(idx if self.reset_index else cell.execution_count) + cell.source
         fh.write(cell.source.strip() + '\n')
     elif cell.cell_type == "markdown":
         fh.write('\n'.join('#! ' + x for x in cell.source.split('\n')) + '\n')
     return idx
Beispiel #3
0
def runfile(script=None, args='', wdir='.', code=None, **kwargs):
    # this has something to do with Prefix matching rule of parse_known_args
    #
    # That is to say
    #
    #   --rep 3
    #
    # would be parsed as
    #
    #   args.workflow=3, unknown --rep
    #
    # instead of
    #
    #   args.workflow=None, unknown --rep 3
    #
    # we then have to change the parse to disable args.workflow when
    # there is no workflow option.
    if isinstance(args, str):
        args = shlex.split(args)
    if (script is None and code is None) or '-h' in args:
        parser = get_run_parser(interactive=True, with_workflow=True)
        parser.print_help()
        return
    if args and args[0].lstrip().startswith('-'):
        parser = get_run_parser(interactive=True, with_workflow=False)
        parser.error = _parse_error
        args, workflow_args = parser.parse_known_args(args)
        args.workflow = None
    else:
        parser = get_run_parser(interactive=True, with_workflow=True)
        parser.error = _parse_error
        args, workflow_args = parser.parse_known_args(args)

    # no multi-processing in interactive mode
    env.max_jobs = 1
    env.verbosity = args.verbosity
    env.__task_engine__ = 'interactive'

    #
    env.sig_mode = args.__sigmode__

    if args.__bin_dirs__:
        import fasteners
        for d in args.__bin_dirs__:
            if d == '~/.sos/bin' and not os.path.isdir(os.path.expanduser(d)):
                with fasteners.InterProcessLock('/tmp/sos_lock_bin'):
                    os.makedirs(os.path.expanduser(d))
            elif not os.path.isdir(os.path.expanduser(d)):
                raise ValueError('directory does not exist: {}'.format(d))
        os.environ['PATH'] = os.pathsep.join([os.path.expanduser(x) for x in args.__bin_dirs__]) + os.pathsep + os.environ['PATH']

    # clear __step_input__, __step_output__ etc because there is
    # no concept of passing input/outputs across cells.
    env.sos_dict.set('__step_output__', [])
    for k in ['__step_input__', '__default_output__', 'input', 'output', \
        'depends', '_input', '_output', '_depends']:
        env.sos_dict.pop(k, None)

    try:
        if script is None:
            if not code.strip():
                return
            # if there is no section header, add a header so that the block
            # appears to be a SoS script with one section
            if not any([SOS_SECTION_HEADER.match(line) for line in code.splitlines()]):
                code = '[interactive_0]\n' + code
            script = SoS_Script(content=code, global_sigil=get_default_global_sigil())
        else:
            script = SoS_Script(filename=script, global_sigil=get_default_global_sigil())
        workflow = script.workflow(args.workflow)
        executor = Interactive_Executor(workflow, args=workflow_args, config={
            'config_file': args.__config__,
            'output_dag': args.__dag__,
            'report_output': args.__report__})

        if args.__dryrun__:
            return executor.dryrun(args.__targets__)
        else:
            return executor.run(args.__targets__)
    except Exception:
        if args.verbosity and args.verbosity > 2:
            sys.stderr.write(get_traceback())
        raise
    finally:
        env.sig_mode = 'default'
        env.verbosity = 1