Example #1
0
 def test_this_file(self):
     self.assertEquals(_this_file, __file__.replace('.pyc','.py'))
     # Note that in some versions of PyPy, this can return <module>
     # instead of the normal <string>
     self.assertIn(run([
         sys.executable,'-c',
         'from pyomo.common.fileutils import this_file;'
         'print(this_file())'
     ])[1].strip(), ['<string>','<module>'])
     self.assertEquals(run(
         [sys.executable],
         stdin='from pyomo.common.fileutils import this_file;'
         'print(this_file())'
     )[1].strip(), '<stdin>')
Example #2
0
 def test_this_file(self):
     self.assertEquals(_this_file, __file__.replace('.pyc', '.py'))
     # Note that in some versions of PyPy, this can return <module>
     # instead of the normal <string>
     self.assertIn(
         run([
             sys.executable, '-c',
             'from pyomo.common.fileutils import this_file;'
             'print(this_file())'
         ])[1].strip(), ['<string>', '<module>'])
     self.assertEquals(
         run([sys.executable],
             stdin='from pyomo.common.fileutils import this_file;'
             'print(this_file())')[1].strip(), '<stdin>')
Example #3
0
    def _execute_command(self,command):
        """
        Execute the command
        """

        start_time = time.time()

        try:
            if 'script' in command:
                _input = command.script
            else:
                _input = None
            [rc, log] = run(
                command.cmd,
                stdin = _input,
                timelimit = self._timelimit if self._timelimit is None else self._timelimit + max(1, 0.01*self._timelimit),
                env   = command.env,
                tee   = self._tee,
                define_signal_handlers = self._define_signal_handlers
             )
        except WindowsError:
            err = sys.exc_info()[1]
            msg = 'Could not execute the command: %s\tError message: %s'
            raise ApplicationError(msg % (command.cmd, err))
        sys.stdout.flush()

        self._last_solve_time = time.time() - start_time

        return [rc,log]
Example #4
0
    def _get_version(self):
        """
        Returns a tuple describing the solver executable version.
        """
        solver_exec = self.executable()
        if (solver_exec, 'version') in self._solver_info_cache:
            return self._solver_info_cache[(solver_exec, 'version')]

        if solver_exec is None:
            ver = _extract_version('')
        else:
            f = StringIO()
            results = run([solver_exec],
                          stdin=('from gurobipy import *; '
                                 'print(gurobi.version()); exit()'),
                          ostream=f)
            ver = None
            try:
                ver = tuple(eval(f.getvalue().strip()))
                while (len(ver) < 4):
                    ver += (0, )
            except SyntaxError:
                ver = _extract_version('')
        ver = ver[:4]
        self._solver_info_cache[(solver_exec, 'version')] = ver
        return ver
Example #5
0
    def _execute_command(self,command):
        """
        Execute the command
        """

        start_time = time.time()

        try:
            if 'script' in command:
                _input = command.script
            else:
                _input = None
            [rc, log] = run(
                command.cmd,
                stdin = _input,
                timelimit = self._timelimit if self._timelimit is None else self._timelimit + max(1, 0.01*self._timelimit),
                env   = command.env,
                tee   = self._tee
             )
        except WindowsError:
            err = sys.exc_info()[1]
            msg = 'Could not execute the command: %s\tError message: %s'
            raise ApplicationError(msg % (command.cmd, err))
        sys.stdout.flush()

        self._last_solve_time = time.time() - start_time

        return [rc,log]
Example #6
0
def configure_cbc():
    global _cbc_compiled_with_asl
    global _cbc_version
    global _cbc_old_version
    if _cbc_compiled_with_asl is not None:
        return
    # manually look for the cbc executable to prevent the
    # CBC.execute() from logging an error when CBC is missing
    executable = Executable("cbc")
    if not executable:
        return
    cbc_exec = executable.path()
    results = run([cbc_exec, "-stop"], timelimit=1)
    _cbc_version = _extract_version(results[1])
    results = run([cbc_exec, "dummy", "-AMPL", "-stop"], timelimit=1)
    _cbc_compiled_with_asl = not ('No match for AMPL' in results[1])
    if _cbc_version is not None:
        _cbc_old_version = _cbc_version < (2, 7, 0, 0)
Example #7
0
def run_entry_point(package, script, args=[], **kwds):
    """Run the named entry point script from the specified package

    Args:
        package (str): The name of the Python package
        script (str): The name of the entry point script to run
        args (list): The list of command line arguments to pass the script
        **kwds: Any additional arguments to pass to pyutilib.subprocess.run
    """
    package = str(package)
    script = str(script)
    assert type(args) is list
    cmdLine \
        = "import pkg_resources,sys; "\
        "sys.argv=%r; "\
        "sys.exit(pkg_resources.load_entry_point(%r,'console_scripts',%r)())" \
        % ( [script]+args, package, script )
    run([sys.executable, '-c', cmdLine], **kwds)
Example #8
0
 def _get_version(self):
     """
     Returns a tuple describing the solver executable version.
     """
     solver_exec = self.executable()
     if solver_exec is None:
         return _extract_version('')
     results = run([solver_exec, '-c', 'quit'], timelimit=1)
     return _extract_version(results[1])
Example #9
0
 def _run_bad_conversion_test(self, *args, **kwds):
     cmd, output_dir = self._get_cmd(*args, **kwds)
     outfile = output_dir + ".out"
     rc = run(cmd, outfile=outfile)
     self.assertNotEqual(rc[0], 0)
     self._assert_contains(
         outfile,
         b"ValueError: One or more deterministic parts of the problem found in file"
     )
     shutil.rmtree(output_dir, ignore_errors=True)
     os.remove(outfile)
Example #10
0
 def test_unsupported_variable_bounds(self):
     cmd, output_dir = self._get_cmd(
         join(thisdir, "model_unsupported_variable_bounds.py"))
     outfile = output_dir + ".out"
     rc = run(cmd, outfile=outfile)
     self.assertNotEqual(rc[0], 0)
     self._assert_contains(
         outfile, b"ValueError: The SMPS writer does not currently support "
         b"stochastic variable bounds. Invalid annotation type: "
         b"StochasticVariableBoundsAnnotation")
     shutil.rmtree(output_dir, ignore_errors=True)
     os.remove(outfile)
Example #11
0
 def _get_version(self):
     """
     Returns a tuple describing the solver executable version.
     """
     solver_exec = self.executable()
     if solver_exec is None:
         return _extract_version('')
     results = run([solver_exec, "--version"], timelimit=1)
     # 'PICO --version' seems to print 'pebble <version>, PICO <version>
     # we don't wan't the pebble version being advertised so we split
     # the string at the comma before extracting a version number. It
     # also exits with a nonzero return code so don't bother checking it.
     return _extract_version(results[1].split(',')[1])
Example #12
0
 def test_bad_component_type(self):
     cmd, output_dir = self._get_cmd(
         join(thisdir, "model_bad_component_type.py"))
     outfile = output_dir + ".out"
     rc = run(cmd, outfile=outfile)
     self.assertNotEqual(rc[0], 0)
     self._assert_contains(
         outfile, b"TypeError: Declarations "
         b"in annotation type StochasticConstraintBodyAnnotation "
         b"must be of types Constraint or Block. Invalid type: "
         b"<class 'pyomo.core.base.objective.SimpleObjective'>")
     shutil.rmtree(output_dir, ignore_errors=True)
     os.remove(outfile)
Example #13
0
    def _get_version(self):
        """
        Returns a tuple describing the solver executable version.
        """
        solver_exec = self.executable()

        if solver_exec is None:
            return _extract_version('')
        else:
            fnames = self._get_dummy_input_files(check_license=False)
            try:
                results = run([solver_exec, fnames[0]])
                return _extract_version(results[1])
            finally:
                self._remove_dummy_input_files(fnames)
Example #14
0
 def test_too_many_declarations(self):
     cmd, output_dir = self._get_cmd(
         join(thisdir, "model_too_many_declarations.py"))
     outfile = output_dir + ".out"
     rc = run(cmd, outfile=outfile)
     self.assertNotEqual(rc[0], 0)
     self._assert_contains(
         outfile, b"RuntimeError: Component b.c was "
         b"assigned multiple declarations in annotation type "
         b"StochasticConstraintBodyAnnotation. To correct this "
         b"issue, ensure that multiple container components under "
         b"which the component might be stored \(such as a Block "
         b"and an indexed Constraint\) are not simultaneously set in "
         b"this annotation.")
     shutil.rmtree(output_dir, ignore_errors=True)
     os.remove(outfile)
Example #15
0
 def _get_os_version(cls):
     _os = cls.get_sysinfo()[0]
     if _os == 'linux':
         if distro_available:
             dist, ver = cls._get_distver_from_distro()
         elif os.path.exists('/etc/redhat-release'):
             dist, ver = cls._get_distver_from_redhat_release()
         elif run(['lsb_release'])[0] == 0:
             dist, ver = cls._get_distver_from_lsb_release()
         elif os.path.exists('/etc/os-release'):
             # Note that (at least on centos), os_release is an
             # imprecise version string
             dist, ver = cls._get_distver_from_os_release()
         else:
             dist, ver = '', ''
         return dist, ver
     elif _os == 'darwin':
         return 'macos', platform.mac_ver()[0]
     elif _os == 'windows':
         return 'win', platform.win32_ver()[0]
     else:
         return '', ''
Example #16
0
    def _get_version(self):
        """
        Returns a tuple describing the solver executable version.
        """
        solver_exec = self.executable()
        if solver_exec is None:
            return _extract_version('')
        f = StringIO()
        results = run([solver_exec],
                      stdin=('from gurobipy import *; '
                             'print(gurobi.version()); exit()'),
                      ostream=f)
        tmp = None
        try:
            tmp = tuple(eval(f.getvalue().strip()))
            while (len(tmp) < 4):
                tmp += (0, )
        except SyntaxError:
            tmp = None
        if tmp is None:
            return _extract_version('')

        return tmp[:4]
Example #17
0
    def test_get_os_version(self):
        f = FileDownloader()
        _os, _ver = f.get_os_version(normalize=False)
        _norm = f.get_os_version(normalize=True)
        #print(_os,_ver,_norm)
        _sys = f.get_sysinfo()[0]
        if _sys == 'linux':
            dist, dist_ver = re.match('^([^0-9]+)(.*)', _norm).groups()
            self.assertNotIn('.', dist_ver)
            self.assertGreater(int(dist_ver), 0)
            if dist == 'ubuntu':
                self.assertEqual(dist_ver, ''.join(_ver.split('.')[:2]))
            else:
                self.assertEqual(dist_ver, _ver.split('.')[0])

            if distro_available:
                d, v = f._get_distver_from_distro()
                #print(d,v)
                self.assertEqual(_os, d)
                self.assertEqual(_ver, v)
                self.assertTrue(v.replace('.','').startswith(dist_ver))

            if os.path.exists('/etc/redhat-release'):
                d, v = f._get_distver_from_redhat_release()
                #print(d,v)
                self.assertEqual(_os, d)
                self.assertEqual(_ver, v)
                self.assertTrue(v.replace('.','').startswith(dist_ver))

            if run(['lsb_release'])[0] == 0:
                d, v = f._get_distver_from_lsb_release()
                #print(d,v)
                self.assertEqual(_os, d)
                self.assertEqual(_ver, v)
                self.assertTrue(v.replace('.','').startswith(dist_ver))

            if os.path.exists('/etc/os-release'):
                d, v = f._get_distver_from_os_release()
                #print(d,v)
                self.assertEqual(_os, d)
                # Note that (at least on centos), os_release is an
                # imprecise version string
                self.assertTrue(_ver.startswith(v))
                self.assertTrue(v.replace('.','').startswith(dist_ver))

        elif _sys == 'darwin':
            dist, dist_ver = re.match('^([^0-9]+)(.*)', _norm).groups()
            self.assertEqual(_os, 'macos')
            self.assertEqual(dist, 'macos')
            self.assertNotIn('.', dist_ver)
            self.assertGreater(int(dist_ver), 0)
            self.assertEqual(_norm, _os+''.join(_ver.split('.')[:2]))
        elif _sys == 'windows':
            self.assertEqual(_os, 'win')
            self.assertEqual(_norm, _os+''.join(_ver.split('.')[:2]))
        else:
            self.assertEqual(ans, '')

        self.assertEqual((_os, _ver), FileDownloader._os_version)
        # Exercise the fetch from CACHE
        try:
            FileDownloader._os_version, tmp \
                = ("test", '2'), FileDownloader._os_version
            self.assertEqual(f.get_os_version(False), ("test","2"))
            self.assertEqual(f.get_os_version(), "test2")
        finally:
            FileDownloader._os_version = tmp
Example #18
0
 def _get_distver_from_lsb_release(cls):
     rc, dist = run(['lsb_release', '-si'])
     rc, ver = run(['lsb_release', '-sr'])
     return cls._map_dist(dist.lower().strip()), ver.strip()