Page 1 of 1

Crude Python3/GTK3 frontend

Posted: Sat Nov 12, 2011 3:28 pm
by RockDoctor
Here's a simple GUI for fsarchiver that meets my needs (and hasn't blown up on me yet, although I've not tested archive restoration in this version).

Code: Select all

#!/usr/bin/env python3
# -*- coding: iso-8859-1 -*-
"""
A GUI for fsarchiver using Python3 and Gtk3
Author:  RockDoctor
Version: 0.9-20111112
"""

from gi.repository import Gtk, Gdk
import os, time, threading

class ArchiverGUI(object):
  def __init__(self):

    window = Gtk.Window()
    window.connect("destroy",Gtk.main_quit)
    vbox = Gtk.VBox(homogeneous=False, spacing=3, expand=False)

    g = Gtk.Grid()
    g.set_border_width(10)
    g.set_row_spacing(5)
    g.set_column_spacing(5)
    vbox.add(g)

    offset = 0
    frame = self.new_frame(g,offset,7)

    label = Gtk.Label()
    label.set_markup('<span font="12" weight="bold">Create a Filesystem Archive</span>')
    g.attach(label,0,0,2,1)

    labels = ["<b>   Filesystem(s) to archive:</b>",
              "<b> Path to archive file:</b>",
              "<b> Archive filename:</b>",
              "<b> Compression level:</b>",
              "<b> Number of threads</b>"]
    for i in range(5):
      label = Gtk.Label()
      label.set_markup(labels[i])
      g.attach(label,0,1+i,1,1)

    self.dev_list = self.get_device_labels()

    self.fs_to_archive = Gtk.ComboBoxText.new_with_entry()
    for d in self.dev_list:
      self.fs_to_archive.append_text(d)
    self.fs_to_archive.connect("changed",self.set_default_archive_filename)
    g.attach(self.fs_to_archive,1,1,1,1)

    self.archive_file_path = Gtk.FileChooserButton()
    self.archive_file_path.set_title("Directory in which to save archive")
    self.archive_file_path.set_current_folder("/")
    self.archive_file_path.set_action(Gtk.FileChooserAction.SELECT_FOLDER)
    g.attach(self.archive_file_path,1,2,1,1)

    self.create_archive_filename = Gtk.Entry()
    g.attach(self.create_archive_filename,1,3,1,1)

    self.compression_level = Gtk.ComboBoxText()
    for i in range(1,10):
      self.compression_level.append_text(str(i))
    g.attach(self.compression_level,1,4,1,1)

    self.number_of_threads = Gtk.ComboBoxText()
    for i in range(1,3):
      self.number_of_threads.append_text(str(i))
    g.attach(self.number_of_threads,1,5,1,1)

    #============================

    offset = 7
    frame = self.new_frame(g,offset,5)

    label = Gtk.Label()
    label.set_markup('<span font="12" weight="bold">Restore an Archived Filesystem</span>')

    g.attach(label,0,offset,2,1)

    labels = ["<b>Archive filename:</b>",
              "<b>Position in archive file:</b>",
              "<b>Destination filesystem:</b>"]
    for i in range(3):
      label = Gtk.Label()
      label.set_markup(labels[i])
      g.attach(label,0,offset+1+i,1,1)

    self.restore_archive_filename = Gtk.FileChooserButton()
    self.restore_archive_filename.set_title("File containing archive:")
    self.restore_archive_filename.set_current_folder("/")
    self.restore_archive_filename.connect("file-set",self.show_info_window)
    g.attach(self.restore_archive_filename,1,offset+1,1,1)

    self.archive_position = Gtk.ComboBoxText.new_with_entry()
    g.attach(self.archive_position,1,offset+2,1,1)

    self.destination_filesystem = Gtk.ComboBoxText.new_with_entry()
    for d in self.dev_list:
      self.destination_filesystem.append_text(d)
    g.attach(self.destination_filesystem,1,offset+3,1,1)

    #============================

    offset = 12
    frame = self.new_frame(g,offset,3)

    btn_box = Gtk.HButtonBox()
    frame.add(btn_box)

    btn_box.set_layout(Gtk.ButtonBoxStyle.SPREAD)
    btn = Gtk.Button.new_from_stock(Gtk.STOCK_QUIT)
    btn.connect("clicked", Gtk.main_quit)
    btn_box.add(btn)
    btn = Gtk.Button.new_from_stock(Gtk.STOCK_EXECUTE)
    btn.connect("clicked", self.gather_the_info)
    btn_box.add(btn)

    #============================

    offset = 15
    #frame = self.new_frame(g,offset,3)

    self.status_label = Gtk.Label("This is the status_label")
    #frame.add(self.status_label)
    g.attach(self.status_label,0,15,4,1)

    #============================

    window.add(vbox)
    window.show_all()

    self.info_window = Gtk.Dialog("Archive Information")
    c = self.info_window.get_content_area()
    self.archive_info = Gtk.TextView()
    c.add(self.archive_info)
    a = self.info_window.get_action_area()
    btn = Gtk.Button.new_from_stock(Gtk.STOCK_CLOSE)
    btn.connect("clicked", lambda x: self.info_window.hide())
    a.add(btn)

  # ---------------------------------

  def new_frame(self,grid, top, height):
    frame = Gtk.Frame()
    frame.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
    grid.attach(frame, 0,top,3,height)
    return frame

  # ---------------------------------

  def gather_the_info(self, widget):
    starttime = time.strftime('%H:%M:%S',time.localtime())

    target = self.fs_to_archive.get_active_text()
    target = target.strip()
    try:
      archive_name = self.restore_archive_filename.get_filename()
    except:
      archive_name = ""
    if target:
      path = self.archive_file_path.get_filename()
      filename = self.create_archive_filename.get_text()+"-"+time.strftime("%Y%m%d%H%M")+".fsa"
      compression_level = self.compression_level.get_active()+1
      thread_count = self.number_of_threads.get_active()+1
      my_cmd = "sudo fsarchiver -z " + str(compression_level) + \
        " -j " + str(thread_count) + " savefs " + path + \
        "/" + filename + " " + target
      #print ("my_cmd = ",my_cmd)
    elif archive_name:
      position = self.archive_position.get_active_text()
      destination = self.destination_filesystem.get_active_text()
      my_cmd = "sudo fsarchiver restfs " + archive_name + \
          " id=" + position + ",dest=" + destination
      #print ("my_cmd = ",my_cmd)
    self.my_cmd = my_cmd
    self.archiver_busy_flag = 1
    t = threading.Thread(target=self.do_my_cmd)
    t.start()
    start_time = time.strftime('%H:%M:%S')
    num_sec = 0
    while self.archiver_busy_flag:
      while Gtk.events_pending():
        Gtk.main_iteration_do(False)
      self.status_label.set_text("Start: "+start_time+"     Run time: "+str(num_sec)+" sec")
      time.sleep(1)
      num_sec += 1

  # ---------------------------------

  def do_my_cmd(self):
    os.system(self.my_cmd)
    self.archiver_busy_flag = 0

  # ---------------------------------

  def show_info_window(self,widget):
    target = self.restore_archive_filename.get_filename()
    print ("target = ",target)
    my_cmd = 'fsarchiver archinfo ' + target + ' 2>&1'
    archinfo_msg = ''
    archinfo = os.popen(my_cmd,'r')
    line = archinfo.readline()
    knt = 1
    while line:
      archinfo_msg = archinfo_msg+line
      line = archinfo.readline()
      knt += 1
      if knt==3:
        words = line.split(' ')
        try:
          archive_filecount = int(words[2])
          for i in range(archive_filecount):
            self.archive_position.append_text(str(i+100))
        except:
          continue
    b = self.archive_info.get_buffer()
    b.set_text(archinfo_msg)
    self.info_window.show_all()

  # ---------------------------------

  def get_device_labels(self):
    blkid = os.popen('sudo blkid -c /dev/null','r')
    dev_list = {}
    line = blkid.readline()
    while (line):
      mine={}
      line = line.replace('/dev','DEVICE=/dev')
      line = line.replace(':',' ')
      line = line.split(' ')
      for l in line:
        if l.strip():
          # 20100126 Can't currently have LABELs containing spaces
          m = l.split('=')
          m[1] = m[1].replace('"',' ')
          mine[m[0]] = m[1].strip()
      try:
        x = mine['LABEL']
      except:
        x = 'none'
      dev_list[mine['DEVICE']] = x
      line = blkid.readline()
    return dev_list

  # ---------------------------------

  def set_default_archive_filename(self, widget):
    f = self.fs_to_archive.get_active_text()
    self.create_archive_filename.set_text(self.dev_list[f])

# ===============================

if __name__ == "__main__":
  app = ArchiverGUI()
  Gtk.main()




Re: Crude Python3/GTK3 frontend

Posted: Sat Dec 14, 2013 8:50 pm
by ulefr01
when i execute this python program i see :
Image
As you can see , i don't see the labels in the first frame :
  • Create a Filesystem Archive
    Filesystem(s) to archive:
    Path to archive file:
    Archive filename:
    Compression level:
    Number of threads
I don't see the labels in the second frame..
i see the buttons in the thirth frame and the status label in the fourth frame

Do you know what is wrong with python ?
I am using LinuxMint Mate 16 Petra .(python3 and GTK3 are installed).