1 | #!/usr/bin/python |
---|
2 | # |
---|
3 | # An applet to monitor /etc/nologin |
---|
4 | |
---|
5 | import gio |
---|
6 | import dbus |
---|
7 | import dbus.mainloop.glib |
---|
8 | import gtk |
---|
9 | import gobject |
---|
10 | import sys |
---|
11 | import os |
---|
12 | import subprocess |
---|
13 | import time |
---|
14 | from optparse import OptionParser |
---|
15 | |
---|
16 | SM_DBUS_NAME = "org.gnome.SessionManager" |
---|
17 | SM_DBUS_PATH = "/org/gnome/SessionManager" |
---|
18 | SM_DBUS_INTERFACE = "org.gnome.SessionManager" |
---|
19 | SM_CLIENT_DBUS_INTERFACE = "org.gnome.SessionManager.ClientPrivate" |
---|
20 | APP_ID = "debathena-nologin-monitor" |
---|
21 | |
---|
22 | class GDMSucks: |
---|
23 | def __init__(self, options): |
---|
24 | self.debug = options.debug |
---|
25 | self.guitest = options.guitest |
---|
26 | self.sessionEnding = False |
---|
27 | self.sessionBus = dbus.SessionBus() |
---|
28 | try: |
---|
29 | self.register_with_sm() |
---|
30 | self.init_sm_client() |
---|
31 | except: |
---|
32 | print "Warning: Cannot register with session manager." |
---|
33 | |
---|
34 | self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) |
---|
35 | self.window.set_property('can-focus', False) |
---|
36 | box = gtk.VBox() |
---|
37 | self.label = gtk.Label() |
---|
38 | self.label.set_markup('<span font_desc="Sans Bold 24">Software updates are being applied.\n\nThis workstation is temporarily unavailable.\n\nPlease use another workstation.\n\n</span>(Update started at %s)' % (time.strftime("%Y-%m-%d %H:%M"))) |
---|
39 | self.label.set_property('can-focus', False) |
---|
40 | self.label.set_justify(gtk.JUSTIFY_CENTER) |
---|
41 | box.pack_start(self.label, True, True, 5) |
---|
42 | self.window.add(box) |
---|
43 | self.window.set_size_request(800, 600) |
---|
44 | self.window.set_decorated(False) |
---|
45 | self.window.set_position(gtk.WIN_POS_CENTER) |
---|
46 | self.window.hide() |
---|
47 | if self.guitest: |
---|
48 | self.window.show_all() |
---|
49 | try: |
---|
50 | metapackage = subprocess.Popen(["machtype", "-L"], stdout=subprocess.PIPE).communicate()[0].rstrip() |
---|
51 | except OSError: |
---|
52 | # Assume cluster |
---|
53 | metapackage = 'debathena-cluster' |
---|
54 | if (self.debug or metapackage == 'debathena-cluster') and os.path.isfile("/var/run/athena-nologin"): |
---|
55 | self.window.show_all() |
---|
56 | if self.debug or metapackage == 'debathena-cluster': |
---|
57 | self.gfile = gio.File("/var/run/athena-nologin") |
---|
58 | self.monitor = self.gfile.monitor_file(gio.FILE_MONITOR_NONE, None) |
---|
59 | self.monitor.connect("changed", self.directory_changed) |
---|
60 | |
---|
61 | def directory_changed(self, monitor, file1, file2, evt_type): |
---|
62 | if self.debug: |
---|
63 | print str(evt_type), file1.get_path() |
---|
64 | if evt_type == gio.FILE_MONITOR_EVENT_CREATED: |
---|
65 | self.label.set_markup('<span font_desc="Sans Bold 24">Software updates are being applied.\n\nThis workstation is temporarily unavailable.\n\nPlease use another workstation.\n\n</span>(Update started at %s)' % (time.strftime("%Y-%m-%d %H:%M"))) |
---|
66 | self.window.show_all() |
---|
67 | if evt_type == gio.FILE_MONITOR_EVENT_DELETED: |
---|
68 | self.window.hide() |
---|
69 | |
---|
70 | # Connect to the session manager, and register our client. |
---|
71 | def register_with_sm(self): |
---|
72 | proxy = self.sessionBus.get_object(SM_DBUS_NAME, SM_DBUS_PATH) |
---|
73 | sm = dbus.Interface(proxy, SM_DBUS_INTERFACE) |
---|
74 | autostart_id = os.getenv("DESKTOP_AUTOSTART_ID", default="") |
---|
75 | self.smClientId = sm.RegisterClient(APP_ID, autostart_id) |
---|
76 | |
---|
77 | # Set up to handle signals from the session manager. |
---|
78 | def init_sm_client(self): |
---|
79 | proxy = self.sessionBus.get_object(SM_DBUS_NAME, self.smClientId) |
---|
80 | self.smClient = dbus.Interface(proxy, SM_CLIENT_DBUS_INTERFACE) |
---|
81 | self.smClient.connect_to_signal("QueryEndSession", |
---|
82 | self.sm_on_QueryEndSession) |
---|
83 | self.smClient.connect_to_signal("EndSession", self.sm_on_EndSession) |
---|
84 | self.smClient.connect_to_signal("CancelEndSession", |
---|
85 | self.sm_on_CancelEndSession) |
---|
86 | self.smClient.connect_to_signal("Stop", self.sm_on_Stop) |
---|
87 | |
---|
88 | # Here on a QueryEndSession signal from the session manager. |
---|
89 | def sm_on_QueryEndSession(self, flags): |
---|
90 | self.sessionEnding = True |
---|
91 | # Response args: is_ok, reason. |
---|
92 | self.smClient.EndSessionResponse(True, "") |
---|
93 | |
---|
94 | # Here on an EndSession signal from the session manager. |
---|
95 | def sm_on_EndSession(self, flags): |
---|
96 | self.sessionEnding = True |
---|
97 | # Response args: is_ok, reason. |
---|
98 | self.smClient.EndSessionResponse(True, "") |
---|
99 | |
---|
100 | # Here on a CancelEndSession signal from the session manager. |
---|
101 | def sm_on_CancelEndSession(self): |
---|
102 | self.sessionEnding = False |
---|
103 | |
---|
104 | # Here on a Stop signal from the session manager. |
---|
105 | def sm_on_Stop(self): |
---|
106 | gtk.main_quit() |
---|
107 | |
---|
108 | def main(options): |
---|
109 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
110 | GDMSucks(options) |
---|
111 | gtk.main() |
---|
112 | |
---|
113 | if __name__ == '__main__': |
---|
114 | parser = OptionParser() |
---|
115 | parser.set_defaults(debug=False, guitest=False) |
---|
116 | parser.add_option("--test", action="store_true", dest="debug") |
---|
117 | parser.add_option("--test-gui", action="store_true", dest="guitest") |
---|
118 | (options, args) = parser.parse_args() |
---|
119 | main(options) |
---|