0

I am using this bash script for copying log files from one system to another system

#!/bin/bash s="/var/log"
d="/root/Directory" BACKUPFILE=scripts.backup.`date +%F`.tar.gz
scp -r root@$1:$s $2 rsync -chavzP --stats root@ipaddr


filename=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
tar -czvf $2/$BACKUPFILE $s
tar --create --gzip --file=$d$filename $s
rm -rf /root/aaa/log

Also I have done progressbar code like this

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject

class ProgressBarWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="ProgressBar Demo")
        self.set_border_width(10)

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(vbox)

        self.progressbar = Gtk.ProgressBar()
        vbox.pack_start(self.progressbar, True, True, 0)

        button = Gtk.CheckButton("Show text")
        button.connect("toggled", self.on_show_text_toggled)
        vbox.pack_start(button, True, True, 0)

        button = Gtk.CheckButton("Activity mode")
        button.connect("toggled", self.on_activity_mode_toggled)
        vbox.pack_start(button, True, True, 0)

        button = Gtk.CheckButton("Right to Left")
        button.connect("toggled", self.on_right_to_left_toggled)
        vbox.pack_start(button, True, True, 0)

        self.timeout_id = GObject.timeout_add(50, self.on_timeout, None)
        self.activity_mode = False

    def on_show_text_toggled(self, button):
        show_text = button.get_active()
        if show_text:
            text = "some text"
        else:
            text = None
        self.progressbar.set_text(text)
        self.progressbar.set_show_text(show_text)

    def on_activity_mode_toggled(self, button):
        self.activity_mode = button.get_active()
        if self.activity_mode:
            self.progressbar.pulse()
        else:
            self.progressbar.set_fraction(0.0)

    def on_right_to_left_toggled(self, button):
        value = button.get_active()
        self.progressbar.set_inverted(value)

    def on_timeout(self, user_data):
        """
        Update value on the progress bar
        """
        if self.activity_mode:
            self.progressbar.pulse()
        else:
            new_value = self.progressbar.get_fraction() + 0.01

            if new_value > 1:
                new_value = 0

            self.progressbar.set_fraction(new_value)

        # As this is a timeout function, return True so that it
        # continues to get called
        return True

win = ProgressBarWindow() win.connect("delete-event", Gtk.main_quit)
win.show_all() Gtk.main()

But I don't know how to embed my script with this progress bar code.

liberforce
  • 11,189
  • 37
  • 48

1 Answers1

1

There are some approaches to this problem but main concern that you must care is that GUI and time consuming tasks (long tasks) are not good friends. Most GUI frameworks use their mainloop to take care of user input handling and draw the UI. This being said, you must separate those long tasks from the main UI and there are some ways to do it, be it threads, async methods, etc, it will all resume to how your language of choice deals with these problems.

Then there is the question of how to invoke the OS functions that you want to track. Probably the best way to do it would be by implementing them in your code, programmatically, but that would be a time consuming effort. So, using shell scripts will be your choice and doing so leads to the question: how to get the output of those commands? Well, there's popen and since you will be using python then you must spawn the shell scripts with popen calls. Best way to do it seems to be subprocess. An additional improvement would be a better tailored bash script that can get some previous analysis of command results (success or failure) and conditioned/formatted output.

Hope you can see where i'm going... You'll have to parse the data that normally goes to the console, interpret it and update the UI accordingly.

I give you a simple example of a Gtk Window with a progress bar that gets pulsed for every line of a shell command output (tree /):

import time
import threading
import subprocess
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk, GObject


def app_main():
    win = Gtk.Window(default_height=50, default_width=300)
    win.connect("delete-event", Gtk.main_quit)
    win.connect("destroy", Gtk.main_quit)

    progress = Gtk.ProgressBar(show_text=True)
    win.add(progress)

    def update_progress(i):
        progress.pulse ()
        #progress.set_fraction (i/100.0) # use this for percentage
        #progress.set_text(str(i))
        return False

    def example_target():
        i = 0 # can be used to pass percentage
        proc = subprocess.Popen(['tree', '/'],stdout=subprocess.PIPE)
        while True:
            line = proc.stdout.readline()
            if line != '':
                time.sleep(0.1) # output is too quick, slow down a little :)
                GLib.idle_add(update_progress, i)
            else:
                break

    win.show_all()

    thread = threading.Thread(target=example_target)
    thread.daemon = True
    thread.start()


if __name__ == "__main__":
    app_main()
    Gtk.main()

On the example above, we use threads. The shell command, that you can try on the console, dumps a tree of the folder structure on you disk. Can be anything but the goal was to have a long task. Since we can't track progress, the bar will be in activity mode and we pulse it for every line but if you can track progress you can use the set_fraction method instead.

Hope this will lead you on the right direction. GL

José Fonte
  • 4,016
  • 2
  • 16
  • 28
  • The code is working file but still i didnot get that how can merge this code with my bash script? @JoseFonte – Pratik Deshmukh Apr 28 '17 at 11:21
  • That's in the Popen call. Here José used the "tree /" command as an example. – liberforce Apr 28 '17 at 11:41
  • indeed. You can start with activity mode to indicate task pending and then go to the next step, parsing output and indicate actual progress. Since your script uses arguments, you must pass them to python ([check this](http://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments-in-python)). Then use those arguments on your shell script, ([check this](https://docs.python.org/3.5/library/subprocess.html)). – José Fonte Apr 28 '17 at 11:56