def test_deprecated_tempdir(self): TempfileManager.push() try: tmpdir = TempfileManager.create_tempdir() _orig = pyutilib_mngr.tempdir pyutilib_mngr.tempdir = tmpdir TempfileManager.tempdir = None log = StringIO() with LoggingIntercept(log, 'pyomo'): fname = TempfileManager.create_tempfile() self.assertIn( "The use of the PyUtilib TempfileManager.tempdir " "to specify the default location for Pyomo " "temporary files", log.getvalue().replace("\n", " ")) log = StringIO() with LoggingIntercept(log, 'pyomo'): dname = TempfileManager.create_tempdir() self.assertIn( "The use of the PyUtilib TempfileManager.tempdir " "to specify the default location for Pyomo " "temporary directories", log.getvalue().replace("\n", " ")) finally: TempfileManager.pop() pyutilib_mngr.tempdir = _orig
def test_create4_dir(self): """Test create logic - no options""" TempfileManager.sequential_files(2) fname = TempfileManager.create_tempdir() self.assertEqual(len(list(glob.glob(tempdir + '*'))), 1) fname = os.path.basename(fname) self.assertEqual(fname, 'tmp2') # TempfileManager.unique_files() fname = TempfileManager.create_tempdir() self.assertEqual(len(list(glob.glob(tempdir + '*'))), 2) fname = os.path.basename(fname) self.assertNotEqual(fname, 'tmp3') self.assertTrue(fname.startswith('tmp'))
def test_scip_solve_from_instance_options(self): # Creating a dummy scip.set file in the cwd # will cover the code that prints a warning _cwd = os.getcwd() tmpdir = TempfileManager.create_tempdir() try: os.chdir(tmpdir) open(join(tmpdir, 'scip.set'), "w").close() # Test scip solve from a pyomo instance and load the solution with LoggingIntercept() as LOG: results = self.scip.solve(self.model, suffixes=['.*'], options={"limits/softtime": 100}) self.assertRegex( LOG.getvalue().replace("\n", " "), r"A file named (.*) exists in the current working " r"directory, but SCIP options are being " r"set using a separate options file. The " r"options file \1 will be ignored.") finally: os.chdir(_cwd) # We don't want the test to care about which Scip version we are using self.model.solutions.store_to(results) results.Solution(0).Message = "Scip" results.Solver.Message = "Scip" results.Solver.Time = 0 _out = TempfileManager.create_tempfile(".txt") results.write(filename=_out, times=False, format='json') self.compare_json( _out, join(currdir, "test_scip_solve_from_instance.baseline"))
def __enter__(self): self._cwd = os.getcwd() # Add a new context TempfileManager.push() # Create a new tempdir in this context self._tempdir = TempfileManager.create_tempdir( suffix=self._suffix, prefix=self._prefix, dir=self._dir, ) os.chdir(self._tempdir)
def test_ipopt_solve_from_instance_OF_options(self): with self.assertRaises(ValueError): # using OF_ options AND option_file_name # is not allowed self.ipopt.solve(self.sisser_instance, suffixes=['.*'], options={ "OF_mu_init": 0.1, "option_file_name": "junk.opt" }) # Creating a dummy ipopt.opt file in the cwd # will cover the code that prints a warning _cwd = os.getcwd() tmpdir = TempfileManager.create_tempdir() try: os.chdir(tmpdir) # create an empty ipopt.opt file open(join(tmpdir, 'ipopt.opt'), "w").close() # Test ipopt solve from a pyomo instance and load the solution with LoggingIntercept() as LOG: results = self.ipopt.solve(self.sisser_instance, suffixes=['.*'], options={"OF_mu_init": 0.1}) self.assertRegex( LOG.getvalue().replace("\n", " "), r"A file named (.*) exists in the current working " r"directory, but Ipopt options file options \(i.e., " r"options that start with 'OF_'\) were provided. The " r"options file \1 will be ignored.") finally: os.chdir(_cwd) # We don't want the test to care about which Ipopt version we are using self.sisser_instance.solutions.store_to(results) results.Solution(0).Message = "Ipopt" results.Solver.Message = "Ipopt" _out = TempfileManager.create_tempfile(".test_ipopt.txt") results.write(filename=_out, times=False, format='json') self.compare_json(_out, join(currdir, "test_solve_from_instance.baseline"))
def test_create3_dir(self): """Test create logic - no options""" fname = TempfileManager.create_tempdir(suffix='bar') self.assertEqual(len(list(glob.glob(tempdir + '*'))), 1) fname = os.path.basename(fname) self.assertTrue(fname.endswith('bar'))
def test_create2_dir(self): """Test create logic - no options""" fname = TempfileManager.create_tempdir(prefix='foo') self.assertEqual(len(list(glob.glob(tempdir + '*'))), 1) fname = os.path.basename(fname) self.assertTrue(fname.startswith('foo'))
def _create_tempdir(self, label, *args, **kwds): dirname = TempfileManager.create_tempdir(*args, **kwds) self._files[label] = dirname return dirname