Skip to content
Mats Wichmann edited this page Apr 9, 2022 · 3 revisions

This is a quick program that parses the output of a scons --debug=tree run and displays it in a more friendly format, as a tree widget. It uses wxPython, so you'll need to have that installed. It's my first wxPython program, so feel free to point out anything I've done wrong.

from wxPython.wx import *
import os
import sys

ID_OPEN=102

class SconsTreeView(wxApp):
  def OnInit(self):
    frame = wxFrame(NULL, -1, 'Scons tree view')
    self.frame=frame
    frame.Show(true)
    self.SetTopWindow(frame)

    filemenu = wxMenu()
    filemenu.Append(ID_OPEN, '&Open')
    menuBar = wxMenuBar()
    menuBar.Append(filemenu, '&File')
    frame.SetMenuBar(menuBar)
    EVT_MENU(self, ID_OPEN, self.OnOpen)

    self.tree = wxTreeCtrl(frame)
    sizer = wxBoxSizer(wxHORIZONTAL)
    sizer.Add(self.tree, 1, wxEXPAND)
    frame.SetSizer(sizer)
    frame.SetAutoLayout(1)
    sizer.Fit(frame)
    if (len(sys.argv) > 1):
      f = open(sys.argv[1], 'r')
      print f
      self.Parse(f)
    return true

  def OnOpen(self, evt):
    dlg = wxFileDialog(self.frame, "Choose a file", "", "", "*.*", wxOPEN)
    if dlg.ShowModal() == wxID_OK:
      filename = dlg.GetFilename()
      dirname = dlg.GetDirectory()
      f = open(os.path.join(dirname, filename), 'r')
      self.Parse(f)
      f.close()
    dlg.Destroy()

  def Parse(self, f):
    t = self.tree
    t.DeleteAllItems()
    for line in f:
      if line.startswith('+') and line.find('-') != -1:
        lastdepth = line.index('-')/2
        nodes = [ t.AddRoot('.') ]
        break

    lastnode = nodes[-1]
    for line in f:
      if line.find('-') == -1:
        break
      depth=line.index('-')/2
      content = line[line.index('-')+1:]
      if depth > lastdepth:
        nodes.append(lastnode)
        lastnode = t.AppendItem(nodes[-1], content)
      elif depth < lastdepth:
        for i in range(depth, lastdepth):
          nodes.pop()
        lastnode = t.AppendItem(nodes[-1], content)
      else:
        lastnode = t.AppendItem(nodes[-1], content)
      lastdepth = depth

app=SconsTreeView(0)
app.MainLoop()

I like this script, but in newer version of wxpython the names of some classes/modules have changed. I have attached a patched version of this script SconsTreeView-wx2.8.8.0.py . (John Femiani)


CategoryHomepage

Clone this wiki locally