Created
November 23, 2012 16:59
-
-
Save glurp/4136475 to your computer and use it in GitHub Desktop.
uisync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
########################################################### | |
# uiFind.rb : synvronise deux reperoire, a la rsync | |
########################################################### | |
require_relative 'Ruiby/lib/ruiby.rb' | |
require 'fileutils' | |
class Application < Ruiby_gtk | |
def initialize() | |
super("UiFind",100,0) | |
threader(20) | |
end | |
def component | |
@origine=Ruiby.stock_get("origine") | |
@destination=Ruiby.stock_get("destination") | |
@afilters=Ruiby.stock_get("afilters") | |
@dfilters=Ruiby.stock_get("dfilters") | |
stack { | |
flowi { | |
htoolbar( | |
"Apply/test files to be transfered" => proc { transfert(false) }, | |
"Execute/start transfert" => proc { transfert(true) }, | |
"Edit/show source" => proc { edit(__FILE__) }, | |
"right-Stop/Sortie" => proc { exit!(0) if ask("exit ?") } | |
) | |
} | |
flowi { space; label("Directroryes Synchronisation") ; space } | |
separator | |
flowi { | |
button("Origine",width:100) { | |
@origine=cask_dir_to_read(@origine)||@origine | |
update_stat() | |
} | |
space ; label("⇒",font:"Sans 28"); space | |
button("Target",width:100) { | |
@destination=cask_dir_to_read(@destination)||@destination | |
update_stat() | |
} | |
} | |
flowi { | |
space ; | |
@we_origine=entry("",40) | |
space ; | |
@we_destination=entry("",40) | |
space ; | |
} | |
sloti(table(0,0) { | |
row { | |
cell(label("Allowed :")) | |
cell(@waf=entry(@afilters)) | |
cell(label(" *.png;*.gif ...")) | |
} | |
row { | |
cell(label("Disallowed :")) | |
cell(@wdf=entry(@dfilters)) | |
} | |
}) | |
buttoni("Go") { transfert(true) } | |
@list=list("Files to be transfered",0,200); | |
flowi { label("Status :"); @ws=entry(""); space ; space} | |
buttoni("Reload",size:[200,20]) { load(__FILE__) } | |
} | |
update_stat() | |
end | |
def cask_dir_to_read(d) | |
r=ask_dir_to_read(d) | |
r=="" ? nil : r | |
end | |
def update_stat() | |
@we_origine.text=@origine | |
@we_destination.text=@destination | |
@afilters=@waf.text | |
@dfilters=@wdf.text | |
@ws.text=if File.exists?(@destination) && File.exists?(@origine) | |
Ruiby.stock_put("origine",@origine) | |
Ruiby.stock_put("destination",@destination) | |
Ruiby.stock_put("afilters",@afilters) | |
Ruiby.stock_put("dfilters",@dfilters) | |
if File.basename(@destination) == File.basename(@origine) | |
if @destination==@origine | |
"Same!" | |
else | |
"Ready" | |
end | |
else | |
"not same names!" | |
end | |
else | |
"unready" | |
end | |
end | |
def transfert(real) | |
update_stat() | |
return unless @ws.text=="Ready" | |
@list.set_data([]) | |
Ruiby.update | |
@origine=@origine.gsub(/\/$/,"") | |
@destination=@destination.gsub(/\/$/,"")+"" | |
lth=[] | |
nb=0 | |
afilters=@afilters.split(/\s*[;,:]\s*/) | |
afilters=['*.*'] if afilters.size==0 | |
dfilters=@dfilters.split(/\s*[;,:]\s*/) | |
dfilters=[''] if dfilters.size==0 | |
my_find(@origine,afilters,dfilters) do |entry| | |
dest=entry.gsub(/^#{@origine}/,@destination) | |
name=entry.gsub(/^#{@origine}/,"") | |
dst_dir=File.dirname(dest) | |
FileUtils.mkdir_p(dst_dir) unless File.directory?(dst_dir) | |
if (! File.exists?(dest)) || File.mtime(dest) < File.mtime(entry) | |
if real | |
if File.size(entry) > 50*1000*1000 | |
lth << Thread.new(entry,dest) { |entry1,dest1| FileUtils.copy(entry1,dest1); gui_invoke { alert("end copying #{entry1}") } } | |
else | |
FileUtils.copy(entry,dest) | |
end | |
end | |
@list.add_item name | |
nb+=1 | |
@ws.text=nb.to_s | |
Ruiby.update | |
end | |
end | |
lth.each {|th| | |
a=false; | |
until th.join(0.1) | |
Ruiby.update | |
end | |
} | |
alert(nb>0 ? "End transfering #{nb} files ! " : "nothing to transferd !" ) | |
end | |
end | |
def my_find(root,filters,dfilters,&blk) | |
ldir=Dir.glob("#{root}/*") | |
ldir.each do |en| | |
bn=File.basename(en) | |
unless File.directory?(en) | |
afilter = filters.size==0 || filters.select { |fi| File.fnmatch( fi, bn) }.size>0 | |
dfilter = dfilters.size>0 && dfilters.select { |fi| File.fnmatch( fi, bn) }.size>0 | |
if afilter && (! dfilter) | |
blk.call(en) rescue puts "#{en} : #{$!.to_s}" | |
end | |
end | |
end | |
ldir.each do |en| | |
bn=File.basename(en) | |
next if bn =~ /^\.\.?$/ | |
next if bn =~ /^\.\.?$/ | |
if File.directory?(en) | |
my_find(en,filters,dfilters,&blk) | |
end | |
end | |
end | |
Ruiby.start { Application.new } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment