#!/usr/bin/env python
import gtk
import pygtk
import gobject
import os
import sys
import re
import getpass
import gtk.glade
import time
#Get the list of available drives even if they are not mounted
os.popen("sudo file -sL /dev/sd* > /tmp/somefile").read()
drive_list = open("/tmp/somefile").readlines()
#We initialize the lists so that we can link them together later
#in the liststore
filesystem = []
device = []
used = []
total = []
free = []
class createUI:
####################
#This function is linked to the timer. It updates the progress bar based on the interval
#specified as a parameter of the timer variable
def progress_timeout(self):
global percent
#Tail the file and start parsing out the percentage complete
shredding_progress = os.popen("tail -n 1 /tmp/shred.txt").read()
try:
shredding_progress = shredding_progress.split(" ")
drive = shredding_progress[1].rstrip()
shredding_progress = shredding_progress[5].replace("%", "")
percent_text = shredding_progress.rstrip()
shredding_progress = float(shredding_progress)/100
#Percentages are float point integers so anything over than 1
# will reset the bar
if shredding_progress >= 1.0:
self.progress.set_text(" %s has been completely shredded" % (drive,))
self.progress.set_fraction(0.0)
return False
else:
self.progress.set_fraction(shredding_progress)
self.progress.set_text(" %s is %s %s shredded" % (drive, percent_text, "%"))
return True
except:
return True
#######################
def shred_selected(self):
#This function determines which field has been selected
#Then it picks the package name from the second column
self.selection = treeview.get_selection()
self.selection.set_mode(gtk.SELECTION_SINGLE)
tree_model, tree_iter = self.selection.get_selected()
#The line below is the line which determines which column is passed to the installer
selected_program = tree_model.get_value(tree_iter, 0)
os.system("python shred_test.py %s &" % selected_program)
time.sleep(0.3)
########################
#This is the event which is called from the button click
#It launches the timer and starts the shredding process
def launch(self, click):
self.shred_selected()
timer = gobject.timeout_add(300, self.progress_timeout)
##################################
def kill_program(self, click):
#attempt to clean up before quitting
os.system('rm -rf /tmp/shred.txt')
gtk.main_quit()
#################################
def __init__(self):
self.createTable(None)
#################################
#This function gets the disk usage by mounting disks, getting df output and then unmounting the disks
def get_disk_usage(self, disk):
os.popen("sudo mount %s /mnt" % disk).read()
total.append(os.popen("df -H |grep 'mnt' | awk '{print $2}'").read())
used.append(os.popen("df -H |grep 'mnt' |awk '{print $3}'").read())
free.append(os.popen("df -H |grep 'mnt' |awk '{print $4}'").read())
os.system("sudo umount /mnt")
#############################
#This makes sure that all of the list elements line up properly
#by adding blanks for information which cannot be gathered
def append_blank(self):
total.append("")
used.append("")
free.append("")
############################
def get_disk_info(self):
#step through the drive list
for line in drive_list:
disk = line.split(":")
disk = disk[0]
#If we encounter a block device which isnt a partition append it
if re.match("^[A-Za-z/]*$", disk):
device.append(disk)
#We want to visually distinguish the partitions from the block device so add spaces
else:
disk_indent = " ", disk
disk_indent = "".join(disk_indent)
device.append(disk_indent)
#I want to eliminate the block devices such as /dev/sda
if re.match("^[A-Za-z/]*$", disk):
filesystem.append("")
self.append_blank()
#Extended partitions also through off the parsing so handle them seperately
elif "ID=0x5" in line:
filesystem.append("Extended Partition")
self.append_blank()
#Note the swap space
elif "swap" in line:
filesystem.append("Swap space")
self.append_blank()
#Linux puts its filesystem type at a different location then NTFS drives so we need to treat them seperately
elif "Linux" in line:
line = line.split(" ")
#line = " ", line[4]
#line = "".join(line)
filesystem.append(line[4])
self.get_disk_usage(disk)
#If there are Windows partitions on the drive find them
#Their drive information is at different element locations than the linux drives
elif "NTFS" in line:
line = line.split(" ")
line = line[8].replace('"', "")
filesystem.append(line)
self.get_disk_usage(disk)
#This is to handle misc filesystems such as DOS executable (device driver)
else:
filesystem.append("???")
self.append_blank()
counter = 0
#This loop matches all of the lists together
while counter < len(device):
self.treestore.append(
[ device[counter], filesystem[counter], used[counter], free[counter],total[counter], "" ]
)
counter +=1
#####################3
def createTable(self, blank):
#Most of this work was done inside of glade
global treeview
#get the builder object so that we can interact with the glade component
self.builder = gtk.Builder()
self.builder.add_from_file('shred_ui.glade')
self.builder.connect_signals(self)
self.progress = self.builder.get_object('progress')
self.aptitude_results = self.builder.get_object('window')
treeview = self.builder.get_object('treeview')
table_vbox = self.builder.get_object('vbox')
self.treestore = self.builder.get_object('liststore')
cols = ["Device","Filesystem Type","Used","Free", "Total"]
treeview.cell = [None] * len(cols)
treeview_column = [None] * len(cols)
for column_number, col in enumerate(cols):
treeview.cell[column_number] = gtk.CellRendererText()
treeview_column[column_number] = gtk.TreeViewColumn(col, treeview.cell[column_number])
treeview_column[column_number].add_attribute(treeview.cell[column_number], 'text', column_number)
treeview_column[column_number].set_resizable(True)
treeview_column[column_number].set_reorderable(True)
treeview_column[column_number].set_sort_indicator(True)
treeview_column[column_number].set_sort_column_id(column_number)
treeview.append_column(treeview_column[column_number])
self.get_disk_info()
#add the tool bar for 'nice' buttons
toolbar = self.builder.get_object('toolbar')
#quit button
quit_icon = gtk.Image()
quit_icon.set_from_file("/root/.tux_search/icons/quit.png")
quit_button = gtk.ToolButton(label="Quit!", icon_widget=quit_icon)
toolbar.insert(quit_button, 0)
quit_button.connect("clicked", self.kill_program)
#uninstall button
shred_icon = gtk.Image()
shred_icon.set_from_file("/root/.tux_search/icons/uninstall.png")
shred_button = gtk.ToolButton(label="Shred!", icon_widget=shred_icon)
toolbar.insert(shred_button, 0)
shred_button.connect("clicked", self.launch)
toolbar.show()
table_vbox.show()
self.aptitude_results.connect("destroy", lambda w: gtk.main_quit())
self.aptitude_results.set_default_size(600, 500)
self.aptitude_results.set_position(gtk.WIN_POS_CENTER)
self.aptitude_results.show_all()
gtk.main()
if __name__ == "__main__":
createUI()
#cleanup the temp files
os.system("rm -rf /tmp/somefile")
os.system("rm -rf /tmp/shred.txt")