File: openipmigui.py

package info (click to toggle)
openipmi 2.0.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,100 kB
  • sloc: ansic: 140,279; python: 8,801; sh: 5,723; perl: 5,394; makefile: 490
file content (327 lines) | stat: -rwxr-xr-x 9,691 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python

# openipmigui.py
#
# The openipmi GUI startup file
#
# Author: MontaVista Software, Inc.
#         Corey Minyard <minyard@mvista.com>
#         source@mvista.com
#
# Copyright 2005 MontaVista Software Inc.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License
#  as published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#
#
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this program; if not, write to the Free
#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# This is a GUI interface to the OpenIPMI library.
#
# Naming Conventions:
#
#  The following names are used universally throughout this program.
#  Note that you have two versions of each object, the Python object
#  (class is defined in this program) and the OpenIPMI version of the
#  object (defined by the OpenIPMI library).
#
#  domain - OpenIPMI.ipmi_domain_t
#  domain_id - OpenIPMI.ipmi_domain_id_t
#  entity - OpenIPMI.ipmi_entity_t
#  entity_id - OpenIPMI.ipmi_entity_id_t
#  mc - OpenIPMI.ipmi_mc_t
#  mc_id - OpenIPMI.ipmi_mc_id_t
#  sensor - OpenIPMI.ipmi_sensor_t
#  sensor_id - OpenIPMI.ipmi_sensor_id_t
#  control - OpenIPMI.ipmi_control_t
#  control_id - OpenIPMI.ipmi_control_id_t
#
#  d - Domain
#  e - Entity
#  m - MC
#  s - Sensor
#  c - Control

import os
try:
    import Tix
except:
    import tkinter
    from tkinter import tix as Tix

import sys
import OpenIPMI
from openipmigui import _domain
from openipmigui import gui
from openipmigui import _saveprefs
from openipmigui import gui_cmdwin

# Used to enable internal debug output
verbosity = 0

#
# Nasty thread support stuff.
#
# If python is threaded but TCL is not, we have ugliness to deal with.
# In this case, the _tkinter code has a tcl_lock that is claimed
# whenever it enters TCL code, including the event loop.  If we run
# OpenIPMI events inside the TCL event loop, it will call directly
# into the OpenIPMI calls and bypass _tkinter, leaving the lock held.
# Inside OpenIPMI, it might call back to the python code which can
# then do a TCL call, which will try to claim tcl_lock and deadlock.
#
# We fix this in this case by having a separate thread
# (openipmi_driver() below) run OpenIPMI events, and not in the TCL
# event loop.
#
# If both python and TCL are threaded, the tcl_lock is not used and
# everything is fine with a single thread.  If python is not threaded,
# then threads don't matter.  In those cases we use the TCL event loop
# for everything.
#
# See the _tkinter.c file in the python distribution for more details.
#
shutdown_thread = False
def openipmi_driver():
    while (not shutdown_thread):
        OpenIPMI.wait_io(1000)
        pass
    return

class TopHandler(Tix.Tk):
    def __init__(self, preffile, log_file, histfile):

        Tix.Tk.__init__(self)
        
        self.init_history = [ ]
        self.defaultDomains = [ ]
        self.pref_taghash = { }
        self.domains = { }
        self.preffile = preffile
        self.log_file = log_file
        self.histfile = histfile
        return

    def domain_change_cb(self, op, domain):
        if (op == "added"):
            self.domains[domain.get_name()].connected(domain)
        elif (op == "removed"):
            self.domains[domain.get_name()].remove()
            pass
        return

    def SetUI(self, ui):
        self.ui = ui;
        return

    def savePrefs(self):
        objs = list(self.domains.values())
        objs.append(self.ui)
        _saveprefs.save(objs, self.preffile)
        return

    def log(self, level, log):
        if (self.log_file != None):
            self.log_file.write(level + ": " + log + "\n")
        self.ui.new_log(level + ": " + log)
        return

    def quit(self):
        global shutdown_thread
        shutdown_thread = True
        gui_cmdwin._HistorySave(self.ui.mainhandler, self.histfile)

        OpenIPMI.set_log_handler(DummyLogHandler())
        OpenIPMI.shutdown_everything()
        if (self.debug_mem):
            print("OpenIPMI is shutdown, memory problems (SEGVs) after this")
            print(" are likely due to OpenIPMI data not being freed until")
            print(" after this point due to the python garbage collector")
            pass
        sys.exit()
        return

class DummyLogHandler:
    def __init__(self):
        pass

    def log(self, level, log):
        sys.stderr.write(level + ": " + log + "\n")

class CmdlangEventHandler:
    def __init__(self, app):
        self.app = app
        return
    
    def cmdlang_event(self, event):
        if (not self.app.ui.logevents):
            return
        name = [ "" ]
        value = [ "" ]
        vtype = [ "" ]
        level = [ 0 ]
        event.restart()
        estr = "Event:"
        more = event.next_field(level, vtype, name, value)
        while (more != 0):
            estr += "\n  %*s%s: %s" % (level[0], "", name[0], value[0])
            more = event.next_field(level, vtype, name, value)
            pass
        self.app.ui.new_log(estr)
        return
    
    pass

def trace(frame, event, arg):
    print(event + ": " + frame.f_code.co_name +
          "(" + frame.f_code.co_filename + ":" + str(frame.f_lineno) + ")")
    return trace

def run(args):
    global verbosity

    preffile = os.path.join(os.environ['HOME'], '.openipmigui.startup')
    histfile = os.path.join(os.environ['HOME'], '.openipmigui.history')

    debug_msg = False
    debug_rawmsg = False
    debug_mem = False
    do_trace = False
    read_preffile = True
    log_file = None

    # Skip program name.
    carg = 1

    while (carg < len(args)):
        arg = args[carg]
        carg += 1
        if (arg == "--dmsg"):
            debug_msg = True
        elif (arg == "--drawmsg"):
            debug_msg = True
        elif (arg == "--dmem"):
            debug_mem = True
        elif (arg == "--verbose"):
            verbosity += 1
        elif (arg == "--trace"):
            do_trace = True
        elif (arg == "--logstderr"):
            log_file = sys.stderr
        elif (arg == "--logstdout"):
            log_file = sys.stdout
        elif (arg == "-n"):
            read_preffile = False
        elif (arg == '-p'):
            if (len(args) == 0):
                print("No argument given for -p")
                return
            preffile = args[carg]
            carg += 1
        else:
            print("Unknown argument: " + arg)
            return
        pass

    if (debug_mem):
        OpenIPMI.enable_debug_malloc()
        pass

    top = TopHandler(preffile, log_file, histfile)

    # Detect if we need a separate OpenIPMI driver thread.  See the
    # openipmi_driver function above for the reason.
    try:
        import threading
        try:
            top.tk.getvar("tcl_platform", "threaded")
            # Tcl is threaded, no need for another thread.
            need_separate_openipmi_thread = False
        except:
            # Python is threaded, but Tcl is not.  Need to run the
            # OpenIPMI event loop in another thread.
            need_separate_openipmi_thread = True
            pass
        pass
    except:
        # No thread support, can't use another thread.
        need_separate_openipmi_thread = False
        pass

    if (need_separate_openipmi_thread):
        if (verbosity >= 1):
            print("Creating separate OpenIPMI event driver thread")
            pass
        rv = OpenIPMI.init()
        if (rv != 0):
            print("Unable to initialize OpenIPMI")
            return
        thread.start_new_thread(openipmi_driver, ())
        pass
    else:
        if (verbosity >= 1):
            print("Using TCL event loop, no threads")
            pass
        rv = OpenIPMI.init_tcl()
        if (rv != 0):
            print("Unable to initialize OpenIPMI, probably no TCL support")
            return
        pass

    if (debug_rawmsg):
        OpenIPMI.enable_debug_rawmsg()
        pass
    if (debug_msg):
        OpenIPMI.enable_debug_msg()
        pass
    if (do_trace):
        sys.settrace(trace)
        pass

    mainhandler = top
    _domain._DomainRestore(mainhandler)
    gui._GUIRestore(mainhandler)

    if (read_preffile):
        _saveprefs.restore(mainhandler, preffile)
    
    gui_cmdwin._HistoryRestore(mainhandler, histfile)

    OpenIPMI.add_domain_change_handler(_domain.DomainWatcher(mainhandler))

    mainhandler.debug_mem = debug_mem
    top.title('OpenIPMI GUI')

    ui = gui.IPMIGUI(top, mainhandler)
    mainhandler.SetUI(ui)

    OpenIPMI.add_domain_change_handler(mainhandler)
    OpenIPMI.set_log_handler(mainhandler)

    _domain.RestoreDomains(mainhandler)

    OpenIPMI.set_cmdlang_event_handler(CmdlangEventHandler(mainhandler))

    top.mainloop()

    mainhandler.quit()


if __name__ == "__main__":
    run(sys.argv)