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
|
"""
This file is part of the Ventoo program.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software. If not, see <http://www.gnu.org/licenses/>.
Copyright 2010 Christopher Harvey
"""
import sys
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import os.path as osp
import augeas_utils
import subprocess
import getpass
import os
import re
import pdb
class RcUpdateWindow(gtk.Window):
def __init__(self):
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
self.set_default_size(300, 500)
self.connect("delete_event", self.close)
self.runlevels = ["boot", "default", "nonetwork"]
# name boot default nonetwork
self.tv_store = gtk.ListStore(str, 'gboolean', 'gboolean', 'gboolean')
self.tv_store.set_sort_column_id(0, gtk.SORT_ASCENDING)
self.tv_view = gtk.TreeView(self.tv_store)
#renderers and columns
nameRenderer = gtk.CellRendererText()
nameColumn = gtk.TreeViewColumn("Script Name")
nameColumn.pack_start(nameRenderer, False)
nameColumn.add_attribute(nameRenderer, 'text', 0)
self.tv_view.append_column(nameColumn)
i = 1
for level in self.runlevels:
renderer = gtk.CellRendererToggle()
renderer.set_property("activatable", True)
renderer.connect("toggled", self.entry_toggled, i)
column = gtk.TreeViewColumn(level)
column.pack_start(renderer, False)
column.add_attribute(renderer, 'active', i)
self.tv_view.append_column(column)
i += 1
scrollWindow = gtk.ScrolledWindow()
scrollWindow.add(self.tv_view)
scrollWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
self.add(scrollWindow)
#fill model with data
reportedData = []
username = getpass.getuser()
if username == 'root':
process = subprocess.Popen("rc-update -s", stdout=subprocess.PIPE, shell=True)
os.waitpid(process.pid, 0)
output = process.stdout.read().strip()
output = output.split('\n')
for line in output:
datas = line.split(' ')
datas = filter(lambda a: a != '', datas) #remove all ''
datas = filter(lambda a: a != '|', datas) #remove the '|'
scriptName = datas[0]
reportedData.append(scriptName)
datas = datas[1:]
rowData = [scriptName]
for level in self.runlevels:
if level in datas:
rowData.append(True)
else:
rowData.append(False)
self.tv_store.append(rowData)
#fill in the rest of the data that rc-update -s hasn't reported.
allNames = os.listdir("/etc/init.d")
for name in allNames:
if not name in reportedData: #this name wasn't reported by rc-update -s
rowData = [False]*len(self.runlevels)
rowData.insert(0, name)
self.tv_store.append(rowData)
#TODO: sort the view.
else:
pass #say something about having to be a root user to rc-update
def entry_toggled(self, cell, path, column):
add = not self.tv_store[path][column]
scriptName = self.tv_store[path][0]
call = ["rc-update"]
if add:
call.append("add")
else:
call.append("del")
call.append(scriptName)
level = self.runlevels[column-1]
call.append(level)
retcode = subprocess.call(call)
if retcode == 0:
self.tv_store[path][column] = not self.tv_store[path][column]
else:
print("Call to rc-update failed.") #TODO: print nice error.
#self.emit("entry-toggled", path)
pass
def close(self, widget, event, data=None):
pass
|