2017-04-27 22 views
0

我使用这个bash脚本从一个系统复制日志文件到另一个系统如何使用python和glade创建进度条以将日志文件从一个系统复制到另一个系统?

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


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 

而且我已经做了进度条这样的代码

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() 

但我不知道如何嵌入我的带有此进度条码的脚本。

+0

我只做了这个@JoséFonte –

回答

1

有一些解决此问题的方法,但您必须关心的主要问题是GUI和耗时的任务(长期任务)不是好朋友。大多数GUI框架使用他们的主循环来处理用户输入处理并绘制UI。这就是说,你必须从主UI中分离那些长期的任务,并且有一些方法可以做到这一点,无论是线程,异步方法等,它都将恢复到你选择的语言处理这些问题的方式。

然后存在如何调用要跟踪的操作系统功能的问题。可能最好的方法是通过编程在代码中实现它们,但这将是一个耗时的工作。因此,使用shell脚本将是您的选择,这样做会导致一个问题:如何获得这些命令的输出?那么,有popen,因为你将使用python,那么你必须用popen调用产生shell脚本。最好的办法似乎是子流程。另一个改进是更好的定制bash脚本,它可以获得一些先前对命令结果(成功或失败)和条件/格式化输出的分析。

希望你能看到我要去的地方...你必须解析通常进入控制台的数据,解释它并相应地更新UI。

我给你一个GTK窗口的一个进度条被脉冲的shell命令输出(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() 

在上面的例子中,我们使用线程。 shell命令,您可以在控制台上尝试,在您的磁盘上转储文件夹结构树。可以是任何事情,但目标是有一个长期的任务。由于我们无法跟踪进度,因此酒吧将处于活动模式,并且我们会为每一行进行脉冲调整,但如果您可以跟踪进度,则可以使用set_fraction方法。

希望这会引导你走向正确的方向。 GL

+0

该代码是工作文件,但我仍然没有得到如何可以将此代码与我的bash脚本合并? @JoseFonte –

+0

这是在Popen电话。在这里,何塞以“树/”命令为例。确实是 – liberforce

+0

。您可以从活动模式开始指示待处理任务,然后转到下一步,解析输出并指示实际进度。由于您的脚本使用参数,您必须将它们传递给python([检查此](http://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments-in-python))。然后在你的shell脚本中使用这些参数([check this](https://docs.python.org/3.5/library/subprocess.html))。 –

相关问题