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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
"""
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 augeas
import os.path as osp
import pygtk
pygtk.require('2.0')
import gtk
import augeas_utils
import AugFileTree
import VentooModule
import AugEditTree
import shutil
import os
import re
import ErrorDialog
import gtkmozembed
import difflib
from pygments import highlight
from pygments.lexers import DiffLexer
from pygments.formatters import HtmlFormatter
import RcUpdateWindow
sandboxDir = '/'
class MainWindow(gtk.Window):
def __init__(self, augeas):
self.a = augeas
#setup the gui
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
self.connect("delete_event", self.close)
self.docBox = gtk.VPaned()
self.rootBox = gtk.VBox()
self.docWindow = gtkmozembed.MozEmbed()
self.docBox.add2(self.docWindow)
self.docWindow.load_url("about:blank")
self.mainToolbar = gtk.Toolbar()
self.diffButton = gtk.ToolButton(None, "Diff!")
self.changeRootButton = gtk.ToolButton(None, "Select Root")
self.diffButton.connect("clicked", self.diffPressed, None)
self.changeRootButton.connect("clicked", self.changeRootPressed, None)
self.showErrorsButton = gtk.ToolButton(None, "Augeas Errors")
self.showRCUpdateButton = gtk.ToolButton(None, "rc-update")
self.showRCUpdateButton.connect("clicked", self.showRCUpdate, None)
self.showErrorsButton.connect("clicked", self.showErrPressed, None)
self.mainToolbar.insert(self.diffButton, -1)
self.mainToolbar.insert(self.showErrorsButton, -1)
self.mainToolbar.insert(self.showRCUpdateButton, -1)
self.mainToolbar.insert(self.changeRootButton, -1)
self.rootBox.pack_start(self.mainToolbar, False, False)
self.mainPaned = gtk.HPaned()
self.rootBox.pack_start(self.docBox)
self.applyDiffButton = gtk.Button("Apply diff")
self.applyDiffButton.connect("clicked", self.applyDiffPressed, None)
self.rootBox.pack_start(self.applyDiffButton, False, False)
self.hideApplyDiffButton()
self.files_tv = AugFileTree.AugFileTree()
self.edit_tv = AugEditTree.AugEditTree()
self.edit_tv.connect("cursor-changed", self.nodeChanged, None)
self.files_tv_scrolled_window = gtk.ScrolledWindow()
self.files_tv_scrolled_window.add(self.files_tv)
self.files_tv_scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
self.edit_tv_scrolled_window = gtk.ScrolledWindow()
self.edit_tv_scrolled_window.add(self.edit_tv)
self.edit_tv_scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
self.mainPaned.add1(self.files_tv_scrolled_window)
self.mainPaned.add2(self.edit_tv_scrolled_window)
self.docBox.add1(self.mainPaned)
self.add(self.rootBox)
self.files_tv.connect('cursor-changed', self.fileSelectionChanged, None)
self.refreshAugeasFileList()
self.currentModule = None
self.set_default_size(800,600)
self.edit_tv.connect("entry-edited", self.__rowEdited, None)
self.edit_tv.connect("entry-toggled", self.__rowToggled, None)
"""
A row was enabled or disabled, update augeas tree, then refresh the view.
"""
def __rowToggled(self, editWidget, path, connectData):
model = editWidget.get_model()
thisIter = model.get_iter_from_string(path)
enabled = model.get_value(thisIter, 0)
aug_root = self.a.get("/augeas/root")
if not aug_root == '/':
augPath = osp.join('files', osp.relpath(self.currentConfigFilePath, aug_root), self.edit_tv.get_label_path(thisIter))
else:
augPath = osp.join('files', augeas_utils.stripBothSlashes(self.currentConfigFilePath), self.edit_tv.get_label_path(thisIter))
if enabled: #this row was just added, update augeas tree.
parentIter = model.iter_parent(thisIter)
if parentIter == None:
siblingIter = model.get_iter_root()
else:
siblingIter = model.iter_children(parentIter)
labelPath = self.edit_tv.get_label_path_str(path)
newLabel = osp.split(labelPath)[1]
while siblingIter != None:
try:
if model.get_string_from_iter(thisIter) != model.get_string_from_iter(siblingIter) and not self.currentModule.comesBefore(model.get_value(siblingIter, 1),
re.match('^.+/(.+?)(?:\\[[0-9]+\\])?$', augPath).group(1)):
break
except ValueError:
pass #Don't know about either the first or second arg, so order shouldn't matter for this one. try next.
siblingIter = model.iter_next(siblingIter)
augBeforePath = osp.split(labelPath)[0]
if siblingIter != None:
augBeforePath = osp.join(augBeforePath, model.get_value(siblingIter, 1))
if not aug_root == '/':
augBeforePath = osp.join('files', osp.relpath(self.currentConfigFilePath, aug_root), augBeforePath)
else:
augBeforePath = osp.join('files', augeas_utils.stripBothSlashes(self.currentConfigFilePath), augBeforePath)
self.a.insert(augBeforePath, re.match('^.+/(.+?)(?:\\[[0-9]+\\])?$', augPath).group(1), True)
print("ins "+re.match('^.+/(.+?)(?:\\[[0-9]+\\])?$', augPath).group(1)+" before "+augBeforePath)
self.a.set(augPath, '')
print("set "+augPath+" ''")
else: #this row was deleted, update augeas tree in the refresh
self.a.remove(augPath)
print('rm ' + augPath)
self.refreshAugeasEditTree()
"""
This is called every time a selection in the AST edit window is changed.
This function can be used to update documentation, code complete, finalize changes to the AST
"""
def nodeChanged(self, treeview, user_param1):
p = self.edit_tv.getSelectedEntryPath()
docPath = None
#if this is a comment node display all surround docs/comments
if osp.split(p)[1].startswith("#comment["):
toDisplay = self.getDocsStartingAt(osp.join(augeas_utils.getAugeasPathFromSystemPath(self.a, self.currentConfigFilePath), p), "<br>")
docPath = "/tmp/commentsDoc.html"
outFile = open("/tmp/commentsDoc.html", 'w')
outFile.write("<html>\n")
outFile.write(toDisplay)
outFile.write("</html>\n")
outFile.close()
else:
#this is a normal node, normal display
p = augeas_utils.removeNumbers(p)
xmlPath = osp.join("/VentooModule/root", p)
docPath = self.currentModule.getDocURLOf(xmlPath)
if docPath == None:
self.docWindow.load_url("about:blank")
else:
self.docWindow.load_url(docPath)
"""
Called when a row value (not label, not enabled/disabled) is changed. update augeas tree.
No need to refresh here.
"""
def __rowEdited(self, editWidget, path, text, connectData):
p = self.edit_tv.getSelectedEntryPath()
p = augeas_utils.removeNumbers(p)
xmlPath = osp.join("/VentooModule/root", p)
model = editWidget.get_model()
thisIter = model.get_iter_from_string(path)
enabled = model.get_value(thisIter, 0)
aug_root = self.a.get("/augeas/root")
#given a iter that was edited, and a current file, build the augeas path to the edited value.
if not aug_root == "/":
augPath = osp.join('files', osp.relpath(self.currentConfigFilePath, aug_root), self.edit_tv.get_label_path(thisIter))
else:
augPath = osp.join('files', augeas_utils.stripBothSlashes(self.currentConfigFilePath), self.edit_tv.get_label_path(thisIter))
enteredValue = model.get_value(thisIter, 2) #get what the user entered.
valScript = self.currentModule.getValidationScript(xmlPath)
if valScript != None:
#There is a script that can validate this input. import and run it.
try:
os.remove("/tmp/thisCheck.py")
except OSError:
pass
try:
os.remove("/tmp/thisCheck.pyc")
except OSError:
pass
shutil.copyfile(valScript, '/tmp/thisCheck.py')
import thisCheck as validate
reload(validate)
valid = validate.validate(text)
if valid:
self.edit_tv.setValid(thisIter)
else:
self.edit_tv.setInvalid(thisIter)
self.a.set(augPath, enteredValue)
print("set " + augPath + " '" + enteredValue + "'")
def changeRootPressed(self, button, data=None):
dialog = gtk.FileChooserDialog("Select a new system root", self, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
dialog.add_button("Select", 1)
dialog.add_button("Cancel", 0)
response = dialog.run()
dialog.hide()
newFolder = dialog.get_current_folder()
if response == 1 and newFolder != None:
global sandboxDir
sandboxDir = newFolder
self.a = augeas.Augeas(sandboxDir, None, augeas.Augeas.SAVE_NEWFILE)
self.refreshAugeasFileList()
self.edit_tv.clear()
self.hideApplyDiffButton()
print("Root switched to " + sandboxDir)
def diffPressed(self, button, data=None):
#show the diff for the current file.
try:
self.a.save()
print("SAVE")
except IOError:
pass
#TODO: check augeas/files/<file path>/<name>/error
#to be sure the save worked.
augeas_utils.makeDiffTree(self.a, augeas_utils.getDiffRoot())
diffFiles = [self.currentConfigFilePath, augeas_utils.getDiffLocation(self.a, self.currentConfigFilePath)]
if not osp.isfile(diffFiles[1]):
print("Could not find a diff file...were changes made?")
else:
origFile = open(diffFiles[0])
origList = file.readlines(origFile)
origFile.close()
newFile = open(diffFiles[1])
newList = file.readlines(newFile)
newFile.close()
#now we have origList and newList that is the text for the diff
d = difflib.Differ()
thediff = list(d.compare(origList, newList))
#TODO: append username, to avoid conflicts
outFile = open("/tmp/ventooDiff.html", 'w')
outFile.write("<html>\n")
theDiff = difflib.unified_diff(origList, newList)
text = ""
for l in theDiff:
text += l
highlight(text, DiffLexer(), HtmlFormatter(full=True, linenos=True, cssclass="source"), outFile)
outFile.write("</html>\n")
outFile.close()
self.docWindow.load_url("file:///tmp/ventooDiff.html")
#TODO: check to make sure diff is correctly displayed (html file found)
self.showApplyDiffButton()
def applyDiffPressed(self, button, data=None):
diffFiles = [self.currentConfigFilePath, augeas_utils.getDiffLocation(self.a, self.currentConfigFilePath)]
#merge diffFiles[0] <- diffFiles[1]
shutil.copyfile(diffFiles[1], diffFiles[0])
def showRCUpdate(self, button, data=None):
win = RcUpdateWindow.RcUpdateWindow()
win.show_all()
def showErrPressed(self, button, data=None):
d = ErrorDialog.ErrorDialog(augeas_utils.getFileErrorList(self.a))
d.show_all()
d.run()
d.destroy()
def close(widget, event, data=None):
gtk.main_quit()
return False
def refreshAugeasFileList(self):
#reload the file selection list from augeas internals.
self.files_tv.clearFiles()
fileList = augeas_utils.accumulateFiles(self.a, "/files", [])
for f in fileList:
ex = VentooModule.moduleExists(augeas_utils.getVentooModuleNameFromSysPath(self.a, osp.join('/', f)))
self.files_tv.addPath(f, ex)
"""
Given an augeas state and a ventoo module update the edit model to show
that augeas state.
The workhorse for this function is __buildEditModel()
"""
def refreshAugeasEditTree(self):
model = self.edit_tv.get_model()
model.clear()
# aRoot = files/etc/foo for the root of the tree to build.
# sketchy file manipulations, TODO: make this more clear.
tmp = augeas_utils.stripTrailingSlash(self.files_tv.getSelectedConfigFilePath())
if sandboxDir != '/':
tmp = osp.relpath(self.files_tv.getSelectedConfigFilePath(), sandboxDir)
aRoot = osp.join('files', augeas_utils.stripBothSlashes(tmp))
# a path into the tree model, coresponds to the aRoot
mRoot = model.get_iter_root()
#path into xml description of the tree
xRoot = '/VentooModule/root'
self.__buildEditModel(model, aRoot, mRoot, xRoot, False)
"""
this is the workhorse behind refreshAugeasEditTree()
This is the core function for displaying the editing widget tree.
It can be considered the core of the whole program actually.
This code has to be rock solid.
"""
# Sometimes we are at the root node
# but don't want to build the children
# so this flag is used.
def __buildEditModel(self, model, augeasFileRoot, modelPathIter, xmlRoot, buildChildren = True):
xElemRoot = self.currentModule.getChildrenOf(osp.join(xmlRoot, '*'))
xChildren = list(self.currentModule.getChildrenOf(xmlRoot))
thisMult = self.currentModule.getMultOf(xmlRoot)
if augeas_utils.isDupLevel(self.a, augeasFileRoot):
#this level is just /1 /2 /3, etc...
#for each match
# created = model.append(modelPathIter, [not addedExtra, str(i), '------'])
# if enabled
# self.__buildEditModel(model, osp.join(augeasFileRoot, str(i)), created, xmlRoot)
matches = self.a.match(osp.join(augeasFileRoot, '*'))
have = 0
maxIndex = 0
#matches <anything>/<number> and stores <number> as group 1
indexProg = re.compile('^.+/([0-9]+)/?$')
commentProg = re.compile('"^#comment\[(.*)]$"')
lastLineWasDoc = False
for match in matches: #add all existing entries
indexResult = indexProg.match(match)
commentResult = commentProg.match(match)
if indexResult != None: #sometimes there are entries on these levels we don't care about.
have += 1
thisIndex = int(indexResult.group(1))
maxIndex = max(maxIndex, thisIndex)
created = self.edit_tv.addElement(modelPathIter, True, indexResult.group(1), '-------')
self.__buildEditModel(model, match, created, xmlRoot)
lastLineWasDoc = False
elif commentProg != None: #got a comment
#only display the first line, the rest can be displayed in the doc frame when requested.
if not lastLineWasDoc:
docText = self.a.get(match)
created = self.edit_tv.addElement(modelPathIter, True, osp.split(match)[1], docText)
lastLineWasDoc = True
#add the missing entries
numNeeded = augeas_utils.matchDiff(thisMult, have)
#add the required ones.
for i in range(numNeeded):
created = model.append(modelPathIter, [True, osp.join(augeasFileRoot, str(maxIndex+i+1)), '-------'])
self.__buildEditModel(model, match, created, xmlRoot)
have += 1
maxIndex += 1
needOption = not augeas_utils.matchExact(thisMult, have)
if needOption:
created = self.edit_tv.addElement(modelPathIter, False, str(maxIndex+1), '-------')
maxIndex += 1
else:
listedNodes = [] #a list of nodes that we already found and know about.
if buildChildren:
for child in xChildren:
#build get a list of either [child.tag] or [child.tag[1], child.tag[n]]
childMult = self.currentModule.getMultOf(osp.join(xmlRoot, child.tag))
matches = self.a.match(osp.join(augeasFileRoot, child.tag))
matches.extend(self.a.match(osp.join(augeasFileRoot, child.tag)+'[*]'))
matches = list(set(matches)) #remove dups from matches
listedNodes.extend(matches)
for match in matches:
userData = self.a.get(match) #add all existing data
if userData == None:
userData = ''
created = self.edit_tv.addElement(modelPathIter, True, osp.split(match)[1], userData)
self.__buildEditModel(model, match, created, osp.join(xmlRoot, child.tag))
#add leaves if we're missing some required ones (in augeas itself)
have = len(matches)
toAddtoHave = 0
numNeeded = augeas_utils.matchDiff(childMult, have)
for i in range(have+1, have+numNeeded+1):
p = osp.join(augeasFileRoot, child.tag)
if have+numNeeded > 1:
p = p + '[' + str(i) + ']'
self.a.set(p, '')
print("set "+p+" ''")
toAddtoHave += 1
have += toAddtoHave
#maybe we need to add more of child to the tree, and maybe even an option for the user.
#this code is different from the rest because it doesn't update the augeas tree, it only
#tells the user that the tree COULD be undated along this 'child' variable/branch.
needed = not augeas_utils.matchExact(childMult, have)
if needed:
created = self.edit_tv.addElement(modelPathIter, False, child.tag + '[' + str(have+1) + ']', '')
#now search for and add nodes that haven't been added yet, and may not be in the VentooModule specifically.
allInAugeas = self.a.match(osp.join(augeasFileRoot, '*'))
lastLineWasDoc = False
for a in allInAugeas:
if not a in listedNodes:
#found that 'a' is not in listedNodes, but is in augeasTree, add it, if it is not supposed to be ignored.
if not osp.split(a)[1].startswith('#'): #always ignore comments (delt with later)
userData = self.a.get(a)
created = self.edit_tv.addElement(modelPathIter, True, osp.split(a)[1], userData)
self.__buildEditModel(model, a, created, osp.join(xmlRoot, 'ventoo_dynamic'))
lastLineWasDoc = False
else:
#Add docs inline here
if not lastLineWasDoc:
docText = self.a.get(a)
created = self.edit_tv.addElement(modelPathIter, True, osp.split(a)[1], docText)
lastLineWasDoc = True
"""
Get doc text starting at the augeas path p, until another non-comment is found
nl stands for new line and is a string to be added after each comment node.
"""
def getDocsStartingAt(self, p, nl = "\n"):
docStr = ""
commentProg = re.compile('^.*#comment\[(.*)\]$')
thisNodes = self.a.match(osp.join(osp.split(p)[0], "*"))
try:
idx = thisNodes.index(p)
while commentProg.match(thisNodes[idx])!=None:
docStr = docStr + nl + self.a.get(thisNodes[idx])
idx += 1
except IndexError:
pass
return docStr
"""
Called when the user picks a new file to view.
"""
def fileSelectionChanged(self, tv, data=None):
#user picked a new file to edit.
self.hideApplyDiffButton()
self.currentConfigFilePath = self.files_tv.getSelectedConfigFilePath()
#update the display...and get new module info.
#thse path manipulations are sketchy, should make this code clearer.
tmp = self.currentConfigFilePath
try:
self.currentModule = VentooModule.VentooModule(augeas_utils.getVentooModuleNameFromSysPath(self.a, osp.join('/', tmp)))
self.refreshAugeasEditTree()
except RuntimeError:
#module doesn't exist. ignore request.
self.edit_tv.clear()
def hideApplyDiffButton(self):
self.applyDiffButton.hide()
def showApplyDiffButton(self):
self.applyDiffButton.show()
if __name__ == '__main__':
if len(sys.argv) > 1:
sandboxDir = sys.argv[1]
if not osp.isdir(sandboxDir):
print(sandboxDir + " is not a directory.")
sys.exit(0)
sys.path.append('/tmp')
print('Starting augeas...')
a = augeas.Augeas(sandboxDir, None, augeas.Augeas.SAVE_NEWFILE)
print('Creating window...')
#Note, it IS possible to create mutiple windows and augeas
#instances to edit multiple "roots" at the same time.
window = MainWindow(a)
window.show_all()
window.hideApplyDiffButton() #TODO: overload show_all to preserve apply button state.
#clear the diff storage place...
shutil.rmtree(augeas_utils.getDiffRoot(), True)
os.makedirs(augeas_utils.getDiffRoot())
gtk.main()
|