Exemplo n.º 1
0
                def solve2(self, f):
                    if all_disabled():
                        return solve(self, f)
                    
                    F = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to solve() is not in function space."
                        raise_wrapped(NotBelongs, e, msg,
                                      F=F, f=f, dp=self.repr_long(), exc=sys.exc_info())

                    try:
                        res = solve(self, f)
                        return res
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs, e,
                            'Solve failed.', self=self, f=f, exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(NotImplementedError, e,
                            'Solve not implemented for class %s.' % name, exc=sys.exc_info())
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception, e,
                            'Solve failed', f=f, self=self, exc=sys.exc_info())
Exemplo n.º 2
0
    def test_invalid_id_2(self):
        valid = [
            "cioa",
            "cioa",
            "cioa_",
            "cioa-0.4",
            "cioa-",
        ]
        invalid = [
            "cioa/",
            "cioa:",
            "cioa ",
            "cioa <",
            "$cioa",
            " cioa",
            "",
            '"',
            '""',
            "'",
            "a\\a",
        ]
        for s in invalid:
            print("TRying with %r" % s)
            if not contracts.all_disabled():
                self.assertRaises((ValueError, ContractNotRespected), Report,
                                  s)

        for s in valid:
            Report(s)
Exemplo n.º 3
0
def all_disabled():
    """
    Wraps PyContracts `all_disabled()
    <http://andreacensi.github.io/contracts/api/contracts.html#
    module-contracts.enabling>`_ function. From the PyContracts documentation:
    "Returns true if all contracts are disabled"
    """
    return contracts.all_disabled()
Exemplo n.º 4
0
def do_extra_checks():
    """ True if we want to do extra paranoid checks for functions. """
    res = not all_disabled()
    #     if _storage.first:
    #         # logger.info('do_extra_checks: %s' % res)
    #         pass
    #     _storage.first = False
    return res
Exemplo n.º 5
0
def do_extra_checks():
    return False
    res = not all_disabled()
#     if _storage.first:
#         # logger.info('do_extra_checks: %s' % res)
#         pass
#     _storage.first = False
    return res
Exemplo n.º 6
0
def parmake_job2_new_process(args):
    """ Starts the job in a new compmake process. """
    (job_id, context) = args
    compmake_bin = which("compmake")

    db = context.get_compmake_db()
    storage = db.basepath  # XXX:
    where = os.path.join(storage, "parmake_job2_new_process")
    if not os.path.exists(storage):
        try:
            os.makedirs(storage)
        except:
            pass

    out_result = os.path.join(where, "%s.results.pickle" % job_id)
    out_result = os.path.abspath(out_result)
    cmd = [compmake_bin, storage]

    if not all_disabled():
        cmd += ["--contracts"]

    cmd += [
        "--status_line_enabled",
        "0",
        "--colorize",
        "0",
        "-c",
        "make_single out_result=%s %s" % (out_result, job_id),
    ]

    cwd = os.getcwd()
    cmd_res = system_cmd_result(
        cwd, cmd, display_stdout=False, display_stderr=False, raise_on_error=False, capture_keyboard_interrupt=False
    )
    ret = cmd_res.ret

    if ret == CompmakeConstants.RET_CODE_JOB_FAILED:  # XXX:
        msg = "Job %r failed in external process" % job_id
        msg += indent(cmd_res.stdout, "stdout| ")
        msg += indent(cmd_res.stderr, "stderr| ")

        res = safe_pickle_load(out_result)
        os.unlink(out_result)
        result_dict_check(res)

        raise JobFailed.from_dict(res)

    elif ret != 0:
        msg = "Host failed while doing %r" % job_id
        msg += "\n cmd: %s" % " ".join(cmd)
        msg += "\n" + indent(cmd_res.stdout, "stdout| ")
        msg += "\n" + indent(cmd_res.stderr, "stderr| ")
        raise CompmakeBug(msg)  # XXX:

    res = safe_pickle_load(out_result)
    os.unlink(out_result)
    result_dict_check(res)
    return res
Exemplo n.º 7
0
 def checkContract(self, argumentName, value):
     # No contract to check.
     if self._contract is None:
         return
     
     # Contracts are disabled.
     if contracts.all_disabled():
         return
     
     self._contract._check_contract(value=value, context={argumentName: value}, silent=False)
Exemplo n.º 8
0
    def checkContract(self, argumentName, value):
        # No contract to check.
        if self._contract is None:
            return

        # Contracts are disabled.
        if contracts.all_disabled():
            return

        self._contract._check_contract(value=value,
                                       context={argumentName: value},
                                       silent=False)
Exemplo n.º 9
0
                def get_implementations_f_r2(self, f, r):
                    if all_disabled():
                        return get_implementations_f_r(self, f, r)
                    
                    
                    F = self.get_fun_space()
                    R = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in function space."
                        raise_wrapped(NotBelongs, e, msg,
                                      F=F, f=f, dp=self.repr_long(), exc=sys.exc_info())
                    try:
                        R.belongs(r)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in R space."
                        raise_wrapped(NotBelongs, e, msg,
                                      R=R, r=r, dp=self.repr_long(), exc=sys.exc_info())

                    try:
                        res = get_implementations_f_r(self, f, r)
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs, e,
                            'Solve failed.', self=self, f=f, exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(NotImplementedError, e,
                            'Solve not implemented for class %s.' % name)
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception, e,
                            'Solve failed', f=f, self=self, exc=sys.exc_info())
                        
                    M = self.get_imp_space()
                    try:
                        for m in res:
                            M.belongs(m)
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs, e,
                                      'Result of get_implementations_f_r not in M.',
                                      self=self, m=m, M=M, exc=sys.exc_info())

                    return res
Exemplo n.º 10
0
                def solve2(self, f):
                    if all_disabled():
                        return solve(self, f)

                    F = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to solve() is not in function space."
                        raise_wrapped(NotBelongs,
                                      e,
                                      msg,
                                      F=F,
                                      f=f,
                                      dp=self.repr_long(),
                                      exc=sys.exc_info())

                    try:
                        res = solve(self, f)
                        return res
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs,
                                      e,
                                      'Solve failed.',
                                      self=self,
                                      f=f,
                                      exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(NotImplementedError,
                                      e,
                                      'Solve not implemented for class %s.' %
                                      name,
                                      exc=sys.exc_info())
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception,
                                      e,
                                      'Solve failed',
                                      f=f,
                                      self=self,
                                      exc=sys.exc_info())
Exemplo n.º 11
0
                def get_implementations_f_r2(self, f, r):
                    if all_disabled():
                        return get_implementations_f_r(self, f, r)

                    F = self.get_fun_space()
                    R = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in function space."
                        raise_wrapped(NotBelongs,
                                      e,
                                      msg,
                                      F=F,
                                      f=f,
                                      dp=self.repr_long(),
                                      exc=sys.exc_info())
                    try:
                        R.belongs(r)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in R space."
                        raise_wrapped(NotBelongs,
                                      e,
                                      msg,
                                      R=R,
                                      r=r,
                                      dp=self.repr_long(),
                                      exc=sys.exc_info())

                    try:
                        res = get_implementations_f_r(self, f, r)
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs,
                                      e,
                                      'Solve failed.',
                                      self=self,
                                      f=f,
                                      exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(
                            NotImplementedError, e,
                            'Solve not implemented for class %s.' % name)
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception,
                                      e,
                                      'Solve failed',
                                      f=f,
                                      self=self,
                                      exc=sys.exc_info())

                    M = self.get_imp_space()
                    try:
                        for m in res:
                            M.belongs(m)
                    except NotBelongs as e:
                        raise_wrapped(
                            NotBelongs,
                            e,
                            'Result of get_implementations_f_r not in M.',
                            self=self,
                            m=m,
                            M=M,
                            exc=sys.exc_info())

                    return res
Exemplo n.º 12
0
    def execute(self, spool):
        db = self.db
        # Todo: check its this one

        storage = os.path.abspath(db.basepath)

        # create a new spool directory for each execution
        # otherwise we get confused!

        self.stderr = os.path.join(spool, '%s.stderr' % self.job_id)
        self.stdout = os.path.join(spool, '%s.stdout' % self.job_id)
        self.retcode = os.path.join(spool, '%s.retcode' % self.job_id)
        self.out_results = os.path.join(spool,
                                        '%s.results.pickle' % self.job_id)

        if os.path.exists(self.stderr):
            os.remove(self.stderr)

        if os.path.exists(self.stdout):
            os.remove(self.stdout)

        if os.path.exists(self.retcode):
            os.remove(self.retcode)

        if os.path.exists(self.out_results):
            os.remove(self.out_results)

        options = []
        cwd = os.path.abspath(os.getcwd())
        variables = dict(SGE_O_WORKDIR=cwd,
                         PYTHONPATH=os.getenv('PYTHONPATH', '') + ':' + cwd)

        # nice-looking name
        self.sge_job_name = 'cm%s-%s' % (os.getpid(), self.job_id)
        # Note that we get the official "id" later and we store it in
        # self.sge_id
        options.extend(
            ['-v', ",".join('%s=%s' % x for x in variables.items())])
        # XXX: spaces
        options.extend(['-e', self.stderr])
        options.extend(['-o', self.stdout])
        options.extend(['-N', self.sge_job_name])
        options.extend(['-wd', cwd])

        options.extend(['-V'])  # pass all environment

        options.extend(['-terse'])

        compmake_bin = SGEJob.get_compmake_bin()

        compmake_options = [
            compmake_bin,
            storage,
            '--retcodefile',
            self.retcode,
            '--status_line_enabled',
            '0',
            '--colorize',
            '0',
            '-c',
            '"make_single out_result=%s %s"' % (self.out_results, self.job_id),
        ]

        if not all_disabled():
            compmake_options += ['--contracts']

        # XXX: spaces in variable out_result

        write_stdin = ' '.join(compmake_options)

        cmd = ['qsub'] + options

        res = system_cmd_result(cwd,
                                cmd,
                                display_stdout=False,
                                display_stderr=True,
                                raise_on_error=True,
                                write_stdin=write_stdin,
                                capture_keyboard_interrupt=False)

        self.sge_id = res.stdout.strip()

        self.already_read = False
        self.npolls = 0

        self.told_you_ready = False
Exemplo n.º 13
0
 def testInvalidShapes(self):
     for shape in Test.invalid_shapes:
         v = np.zeros(shape=shape, dtype="uint8")
         print("Trying with %s" % str(v.shape))
         if not contracts.all_disabled():
             self.assertRaises(Exception, Image_from_array, v)
Exemplo n.º 14
0
def contract(*arg, **kwargs):
    """
        Decorator for adding contracts to functions.

        It is smart enough to support functions with variable number of
        arguments and keyword arguments.

        There are three ways to specify the contracts. In order of precedence:

        - As arguments to this decorator. For example: ::

              @contract(a='int,>0',b='list[N],N>0',returns='list[N]')
              def my_function(a, b):
                  # ...
                  pass

        - As annotations (supported only in Python 3): ::

              @contract
              def my_function(a:'int,>0', b:'list[N],N>0') -> 'list[N]':
                  # ...
                  pass

        - Using ``:type:`` and ``:rtype:`` tags in the function's docstring: ::

              @contract
              def my_function(a, b):
                  """

    if not HAVE_CONTRACTS:
        if isinstance(arg[0], types.FunctionType):
            return arg[0]
        else:
            return lambda f: f

    # this bit tags function as decorated
    def tag_and_decorate(function, **kwargs):
        @functools.wraps(function)
        def __functools_wrap(function, **kwargs):
            new_f = contracts.main.contracts_decorate(function, **kwargs)
            cargs = copy.deepcopy(kwargs)
            new_f.contract_kwargs = cargs or dict()
            new_f.decorated = 'contract'
            return new_f
        return __functools_wrap(function, **kwargs)

    # OK, this is black magic. You are not expected to understand this.
    if arg:
        if isinstance(arg[0], types.FunctionType):
            # We were called without parameters
            function = arg[0]
            function.__doc__ = fixup_docstring(function.__doc__)
            if contracts.all_disabled():
                return function
            try:
                return tag_and_decorate(function, **kwargs)
            except contracts.ContractSyntaxError as e:
                # Erase the stack
                raise contracts.ContractSyntaxError(e.error, e.where)
        else:
            msg = ('I expect that  contracts() is called with '
                   'only keyword arguments (passed: %r)' % arg)
            raise contracts.ContractException(msg)
    else:
        function.__doc__ = fixup_docstring(function.__doc__)
        # We were called *with* parameters.
        if contracts.all_disabled():
            def tmp_wrap(function):
                return function
        else:
            def tmp_wrap(function):
                try:
                    return tag_and_decorate(function, **kwargs)
                except contracts.ContractSyntaxError as e:
                    # Erase the stack
                    raise contracts.ContractSyntaxError(e.error, e.where)
        return tmp_wrap
Exemplo n.º 15
0
def contract(*arg, **kwargs):
    """
        Decorator for adding contracts to functions.

        It is smart enough to support functions with variable number of
        arguments and keyword arguments.

        There are three ways to specify the contracts. In order of precedence:

        - As arguments to this decorator. For example: ::

              @contract(a='int,>0',b='list[N],N>0',returns='list[N]')
              def my_function(a, b):
                  # ...
                  pass

        - As annotations (supported only in Python 3): ::

              @contract
              def my_function(a:'int,>0', b:'list[N],N>0') -> 'list[N]':
                  # ...
                  pass

        - Using ``:type:`` and ``:rtype:`` tags in the function's docstring: ::

              @contract
              def my_function(a, b):
                  """

    if not HAVE_CONTRACTS:
        if isinstance(arg[0], types.FunctionType):
            return arg[0]
        else:
            return lambda f: f

    # this bit tags function as decorated
    def tag_and_decorate(function, **kwargs):
        @functools.wraps(function)
        def __functools_wrap(function, **kwargs):
            new_f = contracts.main.contracts_decorate(function, **kwargs)
            cargs = copy.deepcopy(kwargs)
            new_f.contract_kwargs = cargs or dict()
            new_f.decorated = 'contract'
            return new_f

        return __functools_wrap(function, **kwargs)

    # OK, this is black magic. You are not expected to understand this.
    if arg:
        if isinstance(arg[0], types.FunctionType):
            # We were called without parameters
            function = arg[0]
            function.__doc__ = fixup_docstring(function.__doc__)
            if contracts.all_disabled():
                return function
            try:
                return tag_and_decorate(function, **kwargs)
            except contracts.ContractSyntaxError as e:
                # Erase the stack
                raise contracts.ContractSyntaxError(e.error, e.where)
        else:
            msg = ('I expect that  contracts() is called with '
                   'only keyword arguments (passed: %r)' % arg)
            raise contracts.ContractException(msg)
    else:
        function.__doc__ = fixup_docstring(function.__doc__)
        # We were called *with* parameters.
        if contracts.all_disabled():

            def tmp_wrap(function):
                return function
        else:

            def tmp_wrap(function):
                try:
                    return tag_and_decorate(function, **kwargs)
                except contracts.ContractSyntaxError as e:
                    # Erase the stack
                    raise contracts.ContractSyntaxError(e.error, e.where)

        return tmp_wrap
Exemplo n.º 16
0
    def __init__(cls, name, bases, dct):  # @NoSelf
        ABCMeta.__init__(cls, name, bases, dct)

        if all_disabled():
            pass
        else:
            #             print('Adding checks on Primitive %s: Switches.disable_all = %s' % (name, Switches.disable_all))
            from mcdp_dp.primitive import NotSolvableNeedsApprox
            from mcdp_dp.primitive import WrongUseOfUncertain

            if 'solve' in cls.__dict__:
                solve = cls.__dict__['solve']

                def solve2(self, f):
                    if all_disabled():
                        return solve(self, f)

                    F = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to solve() is not in function space."
                        raise_wrapped(NotBelongs,
                                      e,
                                      msg,
                                      F=F,
                                      f=f,
                                      dp=self.repr_long(),
                                      exc=sys.exc_info())

                    try:
                        res = solve(self, f)
                        return res
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs,
                                      e,
                                      'Solve failed.',
                                      self=self,
                                      f=f,
                                      exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(NotImplementedError,
                                      e,
                                      'Solve not implemented for class %s.' %
                                      name,
                                      exc=sys.exc_info())
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception,
                                      e,
                                      'Solve failed',
                                      f=f,
                                      self=self,
                                      exc=sys.exc_info())

                setattr(cls, 'solve', solve2)

            if 'get_implementations_f_m' in cls.__dict__:
                get_implementations_f_r = cls.__dict__[
                    'get_implementations_f_r']

                def get_implementations_f_r2(self, f, r):
                    if all_disabled():
                        return get_implementations_f_r(self, f, r)

                    F = self.get_fun_space()
                    R = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in function space."
                        raise_wrapped(NotBelongs,
                                      e,
                                      msg,
                                      F=F,
                                      f=f,
                                      dp=self.repr_long(),
                                      exc=sys.exc_info())
                    try:
                        R.belongs(r)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in R space."
                        raise_wrapped(NotBelongs,
                                      e,
                                      msg,
                                      R=R,
                                      r=r,
                                      dp=self.repr_long(),
                                      exc=sys.exc_info())

                    try:
                        res = get_implementations_f_r(self, f, r)
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs,
                                      e,
                                      'Solve failed.',
                                      self=self,
                                      f=f,
                                      exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(
                            NotImplementedError, e,
                            'Solve not implemented for class %s.' % name)
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception,
                                      e,
                                      'Solve failed',
                                      f=f,
                                      self=self,
                                      exc=sys.exc_info())

                    M = self.get_imp_space()
                    try:
                        for m in res:
                            M.belongs(m)
                    except NotBelongs as e:
                        raise_wrapped(
                            NotBelongs,
                            e,
                            'Result of get_implementations_f_r not in M.',
                            self=self,
                            m=m,
                            M=M,
                            exc=sys.exc_info())

                    return res

                setattr(cls, 'get_implementations_f_r',
                        get_implementations_f_r2)
Exemplo n.º 17
0
    "all_disabled",
]

show_info = DuckietownConstants.debug_show_package_import_info

if on_duckiebot():
    using_fake_contracts = True
    if show_info:
        logger.warning("Contracts are disabled becaused we are on Duckiebot.")
else:
    try:
        # use PyContracts if installed
        # noinspection PyUnresolvedReferences
        from contracts import contract, all_disabled

        if all_disabled():
            if show_info:
                logger.warning(
                    "Using PyContracts, but it was disabled by the user.")
        else:
            if show_info:
                logger.warning("Using PyContracts.")
        using_fake_contracts = False
    except ImportError:
        if show_info:
            logger.warning(
                "Contracts are disabled becaused PyContracts not found.")
        using_fake_contracts = True

if using_fake_contracts:
Exemplo n.º 18
0
def contract(*arg, **kwargs):
    '''
        Decorator for adding contracts to functions.

        It is smart enough to support functions with variable number of
        arguments and keyword arguments.

        There are three ways to specify the contracts. In order of precedence:

        - As arguments to this decorator. For example: ::

              @contract(a='int,>0',b='list[N],N>0',returns='list[N]')
              def my_function(a, b):
                  # ...
                  pass

        - As annotations (supported only in Python 3): ::

              @contract
              def my_function(a:'int,>0', b:'list[N],N>0') -> 'list[N]':
                  # ...
                  pass

        - Using ``:type:`` and ``:rtype:`` tags in the function's docstring: ::

              @contract
              def my_function(a, b):
                  """Function description.
                      :type a: int,>0
                      :type b: list[N],N>0
                      :rtype: list[N]
                  """
                  pass

        **Signature and docstrings**: The signature of the decorated
        function is conserved. By default, the docstring is modified
        by adding ``:type:`` and ``:rtype:`` definitions. To avoid that,
        pass ``modify_docstring=False`` as a parameter.


        **Contracts evaluation**: Note that all contracts for the arguments
        and the return values
        are evaluated in the same context. This make it possible to use
        common variables in the contract expression. For example, in the
        example above, the return value is constrained to be a list of the same
        length (``N``) as the parameter ``b``.

        **Using docstrings** Note that, by convention, those annotations must
        be parseable as RestructuredText. This is relevant if you are using
        Sphinx.
        If the contract string has special RST characters in it, like ``*``,
        you can include it in double ticks. `pycontracts` will remove
        the double ticks before interpreting the string.

        For example, the two annotations in this docstring are equivalent
        for `pycontracts`, but the latter is better for Sphinx: ::

              """ My function

                  :param a: First parameter
                  :type a: list(tuple(str,*))

                  :param b: First parameter
                  :type b: ``list(tuple(str,*))``
              """

        :raise: ContractException, if arguments are not coherent
        :raise: ContractSyntaxError
    '''

    # this bit tags function as decorated
    def tag_and_decorate(function, **kwargs):
        @functools.wraps(function)
        def __functools_wrap(function, **kwargs):
            new_f = contracts.main.contracts_decorate(function, **kwargs)
            cargs = copy.deepcopy(kwargs)
            new_f.contract_kwargs = cargs or dict()
            new_f.decorated = 'contract'
            return new_f
        return __functools_wrap(function, **kwargs)

    # OK, this is black magic. You are not expected to understand this.
    if arg:
        if isinstance(arg[0], types.FunctionType):
            # We were called without parameters
            function = arg[0]
            function.__doc__ = fixup_docstring(function.__doc__)
            if contracts.all_disabled():
                return function
            try:
                return tag_and_decorate(function, **kwargs)
            except contracts.ContractSyntaxError as e:
                # Erase the stack
                raise contracts.ContractSyntaxError(e.error, e.where)
        else:
            msg = ('I expect that  contracts() is called with '
                   'only keyword arguments (passed: %r)' % arg)
            raise contracts.ContractException(msg)
    else:
        function.__doc__ = fixup_docstring(function.__doc__)
        # We were called *with* parameters.
        if contracts.all_disabled():
            def tmp_wrap(function):
                return function
        else:
            def tmp_wrap(function):
                try:
                    return tag_and_decorate(function, **kwargs)
                except contracts.ContractSyntaxError as e:
                    # Erase the stack
                    raise contracts.ContractSyntaxError(e.error, e.where)
        return tmp_wrap
Exemplo n.º 19
0
def parmake_job2_new_process(args):
    """ Starts the job in a new compmake process. """
    (job_id, context) = args
    compmake_bin = which('compmake')

    db = context.get_compmake_db()
    storage = db.basepath  # XXX:
    where = os.path.join(storage, 'parmake_job2_new_process')
    if not os.path.exists(storage):
        try:
            os.makedirs(storage)
        except:
            pass

    out_result = os.path.join(where, '%s.results.pickle' % job_id)
    out_result = os.path.abspath(out_result)
    cmd = [compmake_bin, storage]

    if not all_disabled():
        cmd += ['--contracts']

    cmd += [
        '--status_line_enabled',
        '0',
        '--colorize',
        '0',
        '-c',
        'make_single out_result=%s %s' % (out_result, job_id),
    ]

    cwd = os.getcwd()
    cmd_res = system_cmd_result(cwd,
                                cmd,
                                display_stdout=False,
                                display_stderr=False,
                                raise_on_error=False,
                                capture_keyboard_interrupt=False)
    ret = cmd_res.ret

    if ret == CompmakeConstants.RET_CODE_JOB_FAILED:  # XXX:
        msg = 'Job %r failed in external process' % job_id
        msg += indent(cmd_res.stdout, 'stdout| ')
        msg += indent(cmd_res.stderr, 'stderr| ')

        res = safe_pickle_load(out_result)
        os.unlink(out_result)
        result_dict_check(res)

        raise JobFailed.from_dict(res)

    elif ret != 0:
        msg = 'Host failed while doing %r' % job_id
        msg += '\n cmd: %s' % " ".join(cmd)
        msg += '\n' + indent(cmd_res.stdout, 'stdout| ')
        msg += '\n' + indent(cmd_res.stderr, 'stderr| ')
        raise CompmakeBug(msg)  # XXX:

    res = safe_pickle_load(out_result)
    os.unlink(out_result)
    result_dict_check(res)
    return res
Exemplo n.º 20
0
    def execute(self, spool):
        db = self.db
        # Todo: check its this one

        storage = os.path.abspath(db.basepath)

        # create a new spool directory for each execution
        # otherwise we get confused!

        self.stderr = os.path.join(spool, '%s.stderr' % self.job_id)
        self.stdout = os.path.join(spool, '%s.stdout' % self.job_id)
        self.retcode = os.path.join(spool, '%s.retcode' % self.job_id)
        self.out_results = os.path.join(spool,
                                        '%s.results.pickle' % self.job_id)

        if os.path.exists(self.stderr):
            os.remove(self.stderr)

        if os.path.exists(self.stdout):
            os.remove(self.stdout)

        if os.path.exists(self.retcode):
            os.remove(self.retcode)

        if os.path.exists(self.out_results):
            os.remove(self.out_results)

        options = []
        cwd = os.path.abspath(os.getcwd())
        variables = dict(SGE_O_WORKDIR=cwd,
                         PYTHONPATH=os.getenv('PYTHONPATH', '') + ':' + cwd)

        # nice-looking name
        self.sge_job_name = 'cm%s-%s' % (os.getpid(), self.job_id)
        # Note that we get the official "id" later and we store it in
        # self.sge_id
        options.extend(
            ['-v', ",".join('%s=%s' % x for x in variables.items())])
        # XXX: spaces
        options.extend(['-e', self.stderr])
        options.extend(['-o', self.stdout])
        options.extend(['-N', self.sge_job_name])
        options.extend(['-wd', cwd])

        options.extend(['-V'])  # pass all environment

        options.extend(['-terse'])

        compmake_bin = SGEJob.get_compmake_bin()

        compmake_options = [
            compmake_bin, storage,
            '--retcodefile', self.retcode,
            '--status_line_enabled', '0',
            '--colorize', '0',
            '-c',
            '"make_single out_result=%s %s"' % (self.out_results, self.job_id),
        ]

        if not all_disabled():
            compmake_options += ['--contracts']

        # XXX: spaces in variable out_result

        write_stdin = ' '.join(compmake_options)

        cmd = ['qsub'] + options

        res = system_cmd_result(cwd, cmd,
                                display_stdout=False,
                                display_stderr=True,
                                raise_on_error=True,
                                write_stdin=write_stdin,
                                capture_keyboard_interrupt=False)

        self.sge_id = res.stdout.strip()

        self.already_read = False
        self.npolls = 0

        self.told_you_ready = False
Exemplo n.º 21
0
    def __init__(cls, name, bases, dct):  # @NoSelf
        ABCMeta.__init__(cls, name, bases, dct)

        if all_disabled():
            pass
        else:
#             print('Adding checks on Primitive %s: Switches.disable_all = %s' % (name, Switches.disable_all))
            from mcdp_dp.primitive import NotSolvableNeedsApprox
            from mcdp_dp.primitive import WrongUseOfUncertain

            if 'solve' in cls.__dict__:
                solve = cls.__dict__['solve']

                def solve2(self, f):
                    if all_disabled():
                        return solve(self, f)
                    
                    F = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to solve() is not in function space."
                        raise_wrapped(NotBelongs, e, msg,
                                      F=F, f=f, dp=self.repr_long(), exc=sys.exc_info())

                    try:
                        res = solve(self, f)
                        return res
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs, e,
                            'Solve failed.', self=self, f=f, exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(NotImplementedError, e,
                            'Solve not implemented for class %s.' % name, exc=sys.exc_info())
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception, e,
                            'Solve failed', f=f, self=self, exc=sys.exc_info())

                setattr(cls, 'solve', solve2)

            if 'get_implementations_f_m' in cls.__dict__:
                get_implementations_f_r = cls.__dict__['get_implementations_f_r']

                def get_implementations_f_r2(self, f, r):
                    if all_disabled():
                        return get_implementations_f_r(self, f, r)
                    
                    
                    F = self.get_fun_space()
                    R = self.get_fun_space()
                    try:
                        F.belongs(f)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in function space."
                        raise_wrapped(NotBelongs, e, msg,
                                      F=F, f=f, dp=self.repr_long(), exc=sys.exc_info())
                    try:
                        R.belongs(r)
                    except NotBelongs as e:
                        msg = "Function passed to get_implementations_f_r() is not in R space."
                        raise_wrapped(NotBelongs, e, msg,
                                      R=R, r=r, dp=self.repr_long(), exc=sys.exc_info())

                    try:
                        res = get_implementations_f_r(self, f, r)
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs, e,
                            'Solve failed.', self=self, f=f, exc=sys.exc_info())
                    except NotImplementedError as e:
                        raise_wrapped(NotImplementedError, e,
                            'Solve not implemented for class %s.' % name)
                    except NotSolvableNeedsApprox:
                        raise
                    except WrongUseOfUncertain:
                        raise
                    except Exception as e:
                        raise_wrapped(Exception, e,
                            'Solve failed', f=f, self=self, exc=sys.exc_info())
                        
                    M = self.get_imp_space()
                    try:
                        for m in res:
                            M.belongs(m)
                    except NotBelongs as e:
                        raise_wrapped(NotBelongs, e,
                                      'Result of get_implementations_f_r not in M.',
                                      self=self, m=m, M=M, exc=sys.exc_info())

                    return res

                setattr(cls, 'get_implementations_f_r', get_implementations_f_r2)