Exemplo n.º 1
0
    def test_clear(self):
        """Test clear logic"""
        TempfileManager.push()
        OUTPUT = open(tempdir + 'pushpop2', 'w')
        OUTPUT.write('tempfile\n')
        OUTPUT.close()
        os.mkdir(tempdir + 'pushpopdir2')
        TempfileManager.add_tempfile(tempdir + 'pushpop2')
        TempfileManager.add_tempfile(tempdir + 'pushpopdir2')

        TempfileManager.push()
        OUTPUT = open(tempdir + 'pushpop2a', 'w')
        OUTPUT.write('tempfile\n')
        OUTPUT.close()
        os.mkdir(tempdir + 'pushpopdir2a')
        TempfileManager.add_tempfile(tempdir + 'pushpop2a')
        TempfileManager.add_tempfile(tempdir + 'pushpopdir2a')

        TempfileManager.clear_tempfiles()

        if os.path.exists(tempdir + 'pushpop2a'):
            self.fail("clear_tempfiles() failed to clean out files")
        if os.path.exists(tempdir + 'pushpopdir2a'):
            self.fail("clear_tempfiles() failed to clean out directories")
        if os.path.exists(tempdir + 'pushpop2'):
            self.fail("clear_tempfiles() failed to clean out files")
        if os.path.exists(tempdir + 'pushpopdir2'):
            self.fail("clear_tempfiles() failed to clean out directories")
Exemplo n.º 2
0
 def tearDown(self):
     if os.path.exists("unknown.lp"):
         os.unlink("unknown.lp")
     TempfileManager.clear_tempfiles()
     if os.path.exists(os.path.join(currdir,'result.yml')):
         os.remove(os.path.join(currdir,'result.yml'))
     self.model = None
Exemplo n.º 3
0
 def test_capture_output_logfile_string(self):
     logfile = TempfileManager.create_tempfile()
     self.assertTrue(isinstance(logfile, str))
     try: 
         with tee.capture_output(logfile):
             print('HELLO WORLD')
         with open(logfile, 'r') as f:
             result = f.read()
         self.assertEqual('HELLO WORLD\n', result)
     finally:
         TempfileManager.clear_tempfiles()
Exemplo n.º 4
0
def launch_command(command,
                   options,
                   cmd_args=None,
                   cmd_kwds=None,
                   error_label="",
                   disable_gc=False,
                   profile_count=0,
                   log_level=logging.INFO,
                   traceback=False):
    # This is not the effective level, but the
    # level on the current logger. We want to
    # return the logger to its original state
    # before this function exits
    prev_log_level = logger.level
    logger.setLevel(log_level)

    if cmd_args is None:
        cmd_args = ()
    if cmd_kwds is None:
        cmd_kwds = {}

    #
    # Control the garbage collector - more critical than I would like
    # at the moment.
    #
    with PauseGC(disable_gc) as pgc:

        #
        # Run command - precise invocation depends on whether we want
        # profiling output, traceback, etc.
        #

        rc = 0

        if profile_count > 0:
            # Defer import of profiling packages until we know that they
            # are needed
            try:
                try:
                    import cProfile as profile
                except ImportError:
                    import profile
                import pstats
            except ImportError:
                configure_loggers(shutdown=True)
                raise ValueError(
                    "Cannot use the 'profile' option: the Python "
                    "'profile' or 'pstats' package cannot be imported!")
            #
            # Call the main routine with profiling.
            #
            try:
                tfile = TempfileManager.create_tempfile(suffix=".profile")
                tmp = profile.runctx('command(options, *cmd_args, **cmd_kwds)',
                                     globals(), locals(), tfile)
                p = pstats.Stats(tfile).strip_dirs()
                p.sort_stats('time', 'cumulative')
                p = p.print_stats(profile_count)
                p.print_callers(profile_count)
                p.print_callees(profile_count)
                p = p.sort_stats('cumulative', 'calls')
                p.print_stats(profile_count)
                p.print_callers(profile_count)
                p.print_callees(profile_count)
                p = p.sort_stats('calls')
                p.print_stats(profile_count)
                p.print_callers(profile_count)
                p.print_callees(profile_count)
                TempfileManager.clear_tempfiles()
                rc = tmp
            finally:
                logger.setLevel(prev_log_level)
        else:

            #
            # Call the main PH routine without profiling.
            #
            if traceback:
                try:
                    rc = command(options, *cmd_args, **cmd_kwds)
                finally:
                    logger.setLevel(prev_log_level)
            else:
                try:
                    try:
                        rc = command(options, *cmd_args, **cmd_kwds)
                    except ValueError:
                        sys.stderr.write(error_label + "VALUE ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except KeyError:
                        sys.stderr.write(error_label + "KEY ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except TypeError:
                        sys.stderr.write(error_label + "TYPE ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except NameError:
                        sys.stderr.write(error_label + "NAME ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except IOError:
                        sys.stderr.write(error_label + "IO ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except ConverterError:
                        sys.stderr.write(error_label + "CONVERTER ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except ApplicationError:
                        sys.stderr.write(error_label + "APPLICATION ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except RuntimeError:
                        sys.stderr.write(error_label + "RUN-TIME ERROR:\n")
                        sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        raise
                    except:
                        sys.stderr.write(error_label +
                                         "Encountered unhandled exception:\n")
                        if len(sys.exc_info()) > 1:
                            sys.stderr.write(str(sys.exc_info()[1]) + "\n")
                        else:
                            traceback.print_exc(file=sys.stderr)
                        raise
                except:
                    sys.stderr.write("\n")
                    sys.stderr.write(
                        "To obtain further information regarding the "
                        "source of the exception, use the "
                        "--traceback option\n")
                    rc = 1

    #
    # TODO: Once we incorporate options registration into
    #       all of the PySP commands we will assume the
    #       options object is always a PySPConfigBlock
    #
    if isinstance(options, PySPConfigBlock):
        options.check_usage(error=False)

    logger.setLevel(prev_log_level)

    return rc
Exemplo n.º 5
0
 def tearDown(self):
     global tmpdir
     TempfileManager.clear_tempfiles()
     TempfileManager.unique_files()
     os.chdir(tmpdir)
Exemplo n.º 6
0
 def tearDown(self):
     TempfileManager.clear_tempfiles()
Exemplo n.º 7
0
 def tearDown(self):
     TempfileManager.clear_tempfiles()
     del self.worker
Exemplo n.º 8
0
 def tearDown(self):
     if os.path.exists("unknown.lp"):
         os.unlink("unknown.lp")
     TempfileManager.clear_tempfiles()
Exemplo n.º 9
0
 def tearDown(self):
     TempfileManager.clear_tempfiles()
     TempfileManager.tempdir = old_tempdir
Exemplo n.º 10
0
 def tearDown(self):
     TempfileManager.clear_tempfiles()
     ReaderFactory.unregister('rtest3')
     ReaderFactory.unregister('stest3')
     ReaderFactory.unregister('wtest3')
Exemplo n.º 11
0
 def tearDown(self):
     TempfileManager.clear_tempfiles()
     TempfileManager.tempdir = old_tempdir
     pyomo.opt.WriterFactory.unregister('wtest')
     pyomo.opt.ReaderFactory.unregister('rtest')
     pyomo.opt.SolverFactory.unregister('stest')
Exemplo n.º 12
0
 def tearDown(self):
     pyomo.opt.SolverManagerFactory.unregister('smtest')
     TempfileManager.clear_tempfiles()
     TempfileManager.tempdir = old_tempdir
Exemplo n.º 13
0
 def tearDownModule():
     TempfileManager.clear_tempfiles()
     TempfileManager.tempdir = None
     TempfileManager.unique_files()
Exemplo n.º 14
0
 def tearDown(self):
     TempfileManager.clear_tempfiles()
     TempfileManager.tempdir = old_tempdir
     if os.path.exists(currdir + "test_sol.txt"):
         os.remove(currdir + "test_sol.txt")
Exemplo n.º 15
0
 def tearDown(self):
     pyomo.opt.SolverFactory.unregister('stest2')
     TempfileManager.clear_tempfiles()