source: coopr.pyomo/trunk/scripts/pyomo @ 2647

Last change on this file since 2647 was 2647, checked in by khunter, 11 years ago

Begin Pyomo Logging implementation

Define a base line default for how to interact with our Logger.
Note the import necessary at the beginning of the module, and then
the module Logger set up with

Logger = logging.getLogger('Pyomo')

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1#! /usr/bin/env python
2#
3# A script that runs a solver to optimize a pyomo model:
4#
5#   pyomo <model.py> <model.dat>
6#
7#  _________________________________________________________________________
8#
9#  Coopr: A COmmon Optimization Python Repository
10#  Copyright (c) 2008 Sandia Corporation.
11#  This software is distributed under the BSD License.
12#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
13#  the U.S. Government retains certain rights in this software.
14#  For more information, see the FAST README.txt file.
15#  _________________________________________________________________________
16
17import sys
18
19if sys.version_info[0:3] < (2,5,1):
20   print ""
21   print "ERROR: Pyomo requires Python 2.5.1 or newer"
22   sys.exit(1)
23
24from os.path import abspath, dirname
25import logging
26import os
27
28Logger = logging.getLogger('Pyomo')
29
30
31sys.path.insert(0, dirname(dirname(abspath(__file__))))
32sys.path.append(".")
33currdir = dirname(abspath(__file__))
34coopr_path = dirname(currdir)
35pyutilib_path = dirname(coopr_path)+os.sep+"pyutilib"
36sys.path.append(coopr_path)
37sys.path.append(pyutilib_path)
38sys.path.append(abspath(os.getcwd()))
39#
40# Adjust the user path if it looks like this script is being used
41# from an Acro build (adds the Acro bin directory, if it exists).
42#
43# Recurse up the current path, looking for a subdirectory that
44# contains 'python' and 'bin'
45#
46curr = os.path.abspath(os.getcwd())
47while os.sep in curr:
48    if os.path.exists(curr+os.sep+"python") and os.path.exists(curr+os.sep+"bin"):
49        os.environ["PATH"] = curr+os.sep+"bin:"+os.environ["PATH"]
50        break
51    if os.path.basename(curr) == "":
52        break
53    curr = os.path.dirname(curr)
54
55import coopr.pyomo.scripting.pyomo
56
57class PyomoLogHandler ( logging.Handler ):
58    def __init__ ( self, base, *args, **kwargs ):
59        import logging  # why?  Shouldn't the import above be enough?
60        logging.Handler.__init__(self, *args, **kwargs )
61
62        self.basepath = base
63
64    def emit(self, record):
65        import sys   # why?  Isn't this imported above?
66                     # Doesn't work w/o it though ... ?
67
68        level    = record.levelname
69        filename = record.pathname  # file path
70        lineno   = record.lineno
71        function = record.funcName
72        msg      = record.getMessage()
73
74        filename = filename.replace( self.basepath, '[coopr base]' )
75
76        print >> sys.stderr, '%(level)s: "%(fpath)s", %(lineno)d, %(caller)s\n\t%(msg)s\n' % \
77            {
78              'level'  : level,
79              'fpath'  : filename,
80              'lineno' : lineno,
81              'caller' : function,
82              'msg'    : msg,
83            }
84
85
86try:
87    Logger.addHandler( PyomoLogHandler(coopr_path) )
88    Logger.setLevel( 1 )  # magic number to be updated via conf file soon.
89    # 1 = lowest possible output.  i.e. everything
90
91    coopr.pyomo.scripting.pyomo.run(args=sys.argv[1:])
92except SystemExit:
93    pass
Note: See TracBrowser for help on using the repository browser.