示例#1
0
    def __getattribute__(self, item):
        """

        :param item:
        :return:
        """

        abort_thread()

        if item == 'write':
            # Writing should be done to both the source buffer and the redirect
            # buffer so that they have identical copies of the same information
            # for their separate uses
            return self.write_both
        elif item == 'close':
            # The source buffer should not be closed. The redirect buffer is
            # what should be closed by calls to instances of this class
            return super(RedirectBuffer, self).__getattribute__(item)

        # Access the source buffer using a super call to prevent recursion
        source = super(RedirectBuffer, self) \
            .__getattribute__('redirection_source')

        if hasattr(source, item):
            # Preference should be given to the source buffer for all other
            # operations given that this class is a wrapper around the source
            # buffer with the only added functionality being the intercepting
            # and duplication of write operations
            return getattr(source, item)

        # If the source buffer doesn't have a particular attribute it should
        # an attribute specific to this class
        return super(RedirectBuffer, self).__getattribute__(item)
示例#2
0
 def breathe(self):
     """
     Checks the current execution state for the running step and responds
     to any changes in that state. Particular useful for checking to see
     if a step has been aborted by the user during long-running executions.
     """
     if self._step:
         threads.abort_thread()
示例#3
0
 def breathe(self):
     """
     Checks the current execution state for the running step and responds
     to any changes in that state. Particular useful for checking to see
     if a step has been aborted by the user during long-running executions.
     """
     if self._step:
         threads.abort_thread()
示例#4
0
def run(
    project: 'projects.Project',
    step: 'projects.ProjectStep',
) -> dict:
    """
    Carries out the execution of the step python source file by loading it into
    an artificially created module and then executing that module and returning
    the result.

    :param project:
        The currently open project.
    :param step:
        The project step for which the run execution will take place.
    :return:
        A dictionary containing the results of the run execution, which
        indicate whether or not the run was successful. If the run failed for
        any reason, the dictionary will contain error information for display.
    """

    target_module = create_module(project, step)
    source_code = load_step_file(step.source_path)

    try:
        code = InspectLoader.source_to_code(source_code, step.source_path)
    except SyntaxError as error:
        return render_syntax_error(project, error)

    def exec_test():
        step.test_locals = dict()
        step.test_locals.update(target_module.__dict__)
        exec(code, step.test_locals)

    try:
        set_executing(True)
        threads.abort_thread()

        if environ.modes.has(environ.modes.TESTING):
            exec_test()
        else:
            exec(code, target_module.__dict__)
        out = {'success': True}
    except threads.ThreadAbortError:
        # Raised when a user explicitly aborts the running of the step through
        # a user-interface action.
        out = {'success': False}
    except UserAbortError:
        # Raised when a user explicitly aborts the running of the step using
        # a cd.step.stop(). This behavior should be considered a successful
        # outcome as it was intentional on the part of the user that the step
        # abort running early.
        out = {'success': True}
    except Exception as error:
        out = render_error(project, error)

    set_executing(False)
    return out
示例#5
0
    def write_both(self, *args, **kwargs):
        abort_thread()

        if self.active:
            # Only write to this buffer if redirection is active. This prevents
            # race conditions from mixing buffers when attaching or removing
            # the write buffer from its sys output.
            self.last_write_time = time.time()
            super(RedirectBuffer, self).write(*args, **kwargs)

        return self.write_source(*args, **kwargs)
示例#6
0
def run(
    project: 'projects.Project',
    step: 'projects.ProjectStep',
) -> dict:
    """

    :param project:
    :param step:
    :return:
    """

    module_name = step.definition.name.rsplit('.', 1)[0]
    module = types.ModuleType(module_name)

    source_code = get_file_contents(step.source_path)

    try:
        code = InspectLoader.source_to_code(source_code, step.source_path)
    except SyntaxError as error:
        return render_syntax_error(project, source_code, error)

    setattr(module, '__file__', step.source_path)
    setattr(
        module, '__package__',
        '.'.join([project.id.replace('.', '-')] +
                 step.filename.rsplit('.', 1)[0].split(os.sep)))

    def exec_test():
        step.test_locals = dict()
        step.test_locals.update(module.__dict__)
        exec(code, step.test_locals)

    try:
        set_executing(True)
        threads.abort_thread()

        if environ.modes.has(environ.modes.TESTING):
            exec_test()
        else:
            exec(code, module.__dict__)
        out = None
    except threads.ThreadAbortError:
        out = {'success': False}
    except UserAbortError:
        out = None
    except Exception as error:
        out = render_error(project, error)

    set_executing(False)

    return {'success': True} if out is None else out
示例#7
0
def run(
        project: 'projects.Project',
        step: 'projects.ProjectStep',
) -> dict:
    """
    Carries out the execution of the step python source file by loading it into
    an artificially created module and then executing that module and returning
    the result.

    :param project:
        The currently open project.
    :param step:
        The project step for which the run execution will take place.
    :return:
        A dictionary containing the results of the run execution, which
        indicate whether or not the run was successful. If the run failed for
        any reason, the dictionary will contain error information for display.
    """

    target_module = create_module(project, step)
    source_code = load_step_file(step.source_path)

    try:
        code = InspectLoader.source_to_code(source_code, step.source_path)
    except SyntaxError as error:
        return render_syntax_error(project, error)

    def exec_test():
        step.test_locals = dict()
        step.test_locals.update(target_module.__dict__)
        exec(code, step.test_locals)

    try:
        set_executing(True)
        threads.abort_thread()

        if environ.modes.has(environ.modes.TESTING):
            exec_test()
        else:
            exec(code, target_module.__dict__)
        out = {
            'success': True,
            'stop_condition': projects.StopCondition(False, False)
        }
    except threads.ThreadAbortError:
        # Raised when a user explicitly aborts the running of the step through
        # a user-interface action.
        out = {
            'success': False,
            'stop_condition': projects.StopCondition(True, True)
        }
    except UserAbortError as error:
        # Raised when a user explicitly aborts the running of the step using
        # a cd.step.stop(). This behavior should be considered a successful
        # outcome as it was intentional on the part of the user that the step
        # abort running early.
        out = {
            'success': True,
            'stop_condition': projects.StopCondition(True, error.halt)
        }
    except Exception as error:
        out = render_error(project, error)

    set_executing(False)
    return out