Class: RbSync
- Inherits:
-
Object
- Object
- RbSync
- Defined in:
- lib/rbsync.rb
Overview
progress 表示のために fileutils.copy を 自作する
Instance Attribute Summary collapse
-
#conf ⇒ Object
Returns the value of attribute conf.
Instance Method Summary collapse
-
#check_hash=(flag) ⇒ Object
flag true or false.
- #collet_hash(file_names, basedir, options = {}) ⇒ Object
-
#compute_digest_file(filename, limitsize = nil) ⇒ Object
compute digest md5 ==limitsize If file size is very large, and a few bytes at head of file is enough to compare.
- #copy_r(files) ⇒ Object
-
#debug_mode=(flag) ⇒ Object
(also: #debug=)
flag true or false.
-
#debug_mode? ⇒ Boolean
(also: #debug?)
for setting.
- #excludes ⇒ Object
- #excludes=(glob_pattern) ⇒ Object
-
#find_as_relative(dir_name, excludes = []) ⇒ Object
collect file paths.
-
#find_files(src, dest, options) ⇒ Object
compare two directory by name and FileUtis.cmp.
- #hash_limit_size ⇒ Object
- #hash_limit_size=(int_byte_size) ⇒ Object
-
#initialize ⇒ RbSync
constructor
A new instance of RbSync.
- #overwrite=(flag) ⇒ Object
- #overwrite? ⇒ Boolean
- #patch_digest_base ⇒ Object
- #preserve=(flag) ⇒ Object
- #preserve? ⇒ Boolean
- #sync(src, dest, options = {}) ⇒ Object
-
#sync_by_anothername(src, dest, options) ⇒ Object
別名で名前をつけて転送する.
- #sync_by_hash(src, dest, options = {}) ⇒ Object
-
#sync_normally(src, dest, options = {}) ⇒ Object
called from sync.
-
#sync_with_backup(src, dest, options) ⇒ Object
同期先に同名ファイルがあったらファイルを別名にバックアップしてから転送します.
-
#updated_file_only=(flag) ⇒ Object
(also: #update=, #newer=)
flag true or false.
-
#verbose=(flag) ⇒ Object
flag true or false.
- #verbose? ⇒ Boolean
Constructor Details
#initialize ⇒ RbSync
Returns a new instance of RbSync.
101 102 103 104 105 106 107 108 |
# File 'lib/rbsync.rb', line 101 def initialize() @conf ={} @conf[:update] = false @conf[:excludes] = [] @conf[:preserve] = true @conf[:overwrite] = true @conf[:strict] = true end |
Instance Attribute Details
#conf ⇒ Object
Returns the value of attribute conf.
86 87 88 |
# File 'lib/rbsync.rb', line 86 def conf @conf end |
Instance Method Details
#check_hash=(flag) ⇒ Object
flag true or false
468 469 470 |
# File 'lib/rbsync.rb', line 468 def check_hash=(flag) @conf[:use_md5_digest] = flag end |
#collet_hash(file_names, basedir, options = {}) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rbsync.rb', line 204 def collet_hash(file_names,basedir,={}) #prepare require 'thread' self.patch_digest_base threads =[] output = Hash.new{|s,key| s[key]=[] } q = Queue.new limitsize = [:hash_limit_size] # compute digests file_names.each{|e| q.push e } 3.times{ threads.push( Thread.start{ while(!q.empty?) name = q.pop #$stdout.puts "reading #{name}" if options[:verbose] #$stdout.flush if options[:verbose] hash = compute_digest_file(File.(name,basedir),limitsize) output[hash].push name end } ) } if [:verbose] then t = Thread.start{ until(q.empty?) puts( "#{q.size}/#{file_names.size}"); $stdout.flush; sleep 1; end } threads.push(t ) end threads.each{|t|t.join} return output end |
#compute_digest_file(filename, limitsize = nil) ⇒ Object
compute digest md5
limitsize
If file size is very large,
and a few bytes at head of file is enough to compare.
for speed-up, Set limit size to enable to avoid reading a whole of file.
もしファイルがとても巨大で、かつ、先頭の数キロバイトが比較に十分であれば、limitsize 以降をスキップする
265 266 267 |
# File 'lib/rbsync.rb', line 265 def compute_digest_file(filename, limitsize=nil) Digest::MD5.open(filename,limitsize).hexdigest end |
#copy_r(files) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/rbsync.rb', line 354 def copy_r(files) # puts ("copy #{files.size} files") if(@conf[:progress]) $stdout.flush if(@conf[:progress]) files.each_with_index{|e,i| #show puts ("start #{i+1}/#{files.size}")if(@conf[:progress]) $stdout.flush if(@conf[:progress]) #main tmp_name = "#{e[1]}.copy_tmp" FileUtils.rm(tmp_name) if File.exists?(tmp_name) copy_thread = Thread.start{ FileUtils.mkdir_p File.dirname(e[1]) unless File.exists?(File.dirname(e[1])) ## todo copy file as stream for progress begin FileUtils.copy( e[0] , tmp_name ,{:preserve=>self.preserve?,:verbose=>self.verbose? } ) FileUtils.mv(tmp_name,e[1]) rescue Errno::EACCES => err puts e[1];puts err end } #progress of each file puts "#{e[0]}" if self.verbose? || self.debug? progress_thread = nil if(@conf[:progress]) progress_thread = Thread.start{ = ProgressBar.new .size = 30 src_size = File.size(e[0]) dst_size = -1 .start("copying #{e[0]} \r\n to #{e[1]}") cnt = 0 dst_name = tmp_name while(src_size!=dst_size) dst_name = e[1] if File.exists?(e[1]) and not File.exists?(tmp_name) unless File.exists?(dst_name) then cnt = cnt + 1 if cnt > 25 then puts "copying #{e[1]} is terminated.\r\n timeout error" throw Error break end sleep 0.2 next end src_size = File.size(e[0]).to_f dst_size = File.size(dst_name).to_f break if src_size == 0 # preven zero divide # next if dst_size == 0 # preven zero divide percent = dst_size/src_size*100 .progress(percent.to_int) sleep 0.6 end src_size = File.size(e[0]).to_f dst_size = File.size(e[1]).to_f percent = dst_size/src_size*100 .progress(percent.to_int) .end("done") } end progress_thread.join if progress_thread copy_thread.join } end |
#debug_mode=(flag) ⇒ Object Also known as: debug=
flag true or false
453 454 455 |
# File 'lib/rbsync.rb', line 453 def debug_mode=(flag) self.conf[:debug] = flag end |
#debug_mode? ⇒ Boolean Also known as: debug?
for setting
442 443 444 |
# File 'lib/rbsync.rb', line 442 def debug_mode? self.conf[:debug] ==true end |
#excludes ⇒ Object
478 479 480 |
# File 'lib/rbsync.rb', line 478 def excludes @conf[:excludes] end |
#excludes=(glob_pattern) ⇒ Object
475 476 477 |
# File 'lib/rbsync.rb', line 475 def excludes=(glob_pattern) @conf[:excludes].push glob_pattern end |
#find_as_relative(dir_name, excludes = []) ⇒ Object
collect file paths. paths are relatetive path.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rbsync.rb', line 110 def find_as_relative(dir_name,excludes=[]) files =[] excludes = [] unless excludes #todo write this two line . exculude initialize test excludes = excludes.split(",") if excludes.class == String excludes = [excludes] unless excludes.class == Array Dir.chdir(dir_name){ files = Dir.glob "./**/*", File::FNM_DOTMATCH exclude_files =[] exclude_files = excludes.map{|g| Dir.glob "./**/#{g}",File::FNM_DOTMATCH } files = files.reject{|e| File.directory?(e) } files = files - exclude_files.flatten } files = files.reject{|e| [".",".."].any?{|s| s== File::basename(e) }} end |
#find_files(src, dest, options) ⇒ Object
compare two directory by name and FileUtis.cmp
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rbsync.rb', line 127 def find_files(src,dest,) src_files = self.find_as_relative( src, [:excludes] ) dest_files = self.find_as_relative( dest, [:excludes] ) # output target files puts " 元フォルダ:" + src_files.size.to_s + "件" if self.debug? puts "同期先フォルダ:" + dest_files.size.to_s + "件" if self.debug? #pp src_files if self.debug? sleep 1 if self.debug? #両方にあるファイル名で中身が違うもので src の方が古いもの same_name_files = (dest_files & src_files) same_name_files.reject!{|e| #ファイルが同じモノは省く next unless File.exists?( File.(e,dest)) puts "compare file bin. #{e}" if self.debug? || self.verbose? $stdout.flush if self.debug? FileUtils.cmp( File.(e,src) , File.(e,dest) ) } if [:strict] same_name_files.reject!{|e| #ファイルサイズが同じモノを省く(全部比較する代替手段) next unless File.exists?( File.(e,dest)) puts "size/mtime compare #{e}" if self.debug? || self.verbose? File.size(File.(e,src)) == File.size( File.(e,dest)) #&& File.mtime(File.expand_path(e,src)) == File.mtime( File.expand_path(e,dest) ) } unless [:strict] if [:update] then same_name_files= same_name_files.select{|e| puts "mtime is newer #{e}" if self.debug? || self.verbose? (File.mtime(File.(e,src)) > File.mtime( File.(e,dest))) } end if [:overwrite] == false then same_name_files= same_name_files.reject{|e| puts "can over write? #{e}" if self.debug? || self.verbose? (File.exists?(File.(e,src)) && File.exists?( File.(e,dest))) } end $stdout.flush if self.debug? files_not_in_dest = (src_files - dest_files) #files files =[] files = (files_not_in_dest + same_name_files ).flatten files end |
#hash_limit_size ⇒ Object
448 449 450 |
# File 'lib/rbsync.rb', line 448 def hash_limit_size @conf[:hash_limit_size] end |
#hash_limit_size=(int_byte_size) ⇒ Object
471 472 473 |
# File 'lib/rbsync.rb', line 471 def hash_limit_size=(int_byte_size) @conf[:hash_limit_size] = int_byte_size end |
#overwrite=(flag) ⇒ Object
487 488 489 |
# File 'lib/rbsync.rb', line 487 def overwrite=(flag) @conf[:overwrite] = flag end |
#overwrite? ⇒ Boolean
490 491 492 |
# File 'lib/rbsync.rb', line 490 def overwrite? @conf[:overwrite] end |
#patch_digest_base ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/rbsync.rb', line 240 def patch_digest_base() require 'digest/md5' s = %{ class Digest::Base def self.open(path,limitsize=nil) obj = new File.open(path, 'rb') do |f| buf = "" while f.read(256, buf) obj << buf break if f.pos > (limitsize or f.pos+1) end end return obj end end } eval s end |
#preserve=(flag) ⇒ Object
481 482 483 |
# File 'lib/rbsync.rb', line 481 def preserve=(flag) @conf[:preserve] = false end |
#preserve? ⇒ Boolean
484 485 486 |
# File 'lib/rbsync.rb', line 484 def preserve? @conf[:preserve] end |
#sync(src, dest, options = {}) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/rbsync.rb', line 420 def sync(src,dest,={}) [:excludes] = self.excludes.push([:excludes]).flatten.uniq if [:excludes] [:update] = @conf[:update] if [:update] == nil [:strict] = @conf[:strict] if [:strict] == nil [:check_hash] = [:check_hash] or @conf[:check_hash] [:hash_limit_size] = @conf[:hash_limit_size] if [:hash_limit_size] == nil [:overwrite] = @conf[:overwrite] if [:overwrite] == nil [:overwrite] = false if [:no_overwrite] FileUtils.mkdir_p dest unless File.exists? dest if [:rename] return self.sync_by_anothername(src,dest,) elsif [:backup] return self.sync_with_backup(src,dest,) elsif [:check_hash] return self.sync_by_hash(src,dest,) else return self.sync_normally(src,dest,) end end |
#sync_by_anothername(src, dest, options) ⇒ Object
別名で名前をつけて転送する
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/rbsync.rb', line 323 def sync_by_anothername(src,dest,) # 上書き付加の場合 # #ファイル一覧を取得する files = find_as_relative(src,[:excludes]) #中身が同じモノを排除 files = files.reject{|e| FileUtils.cmp(File.(e,src) , File.(e,dest)) } if [:update] then #更新日が当たらしいモノを排除 files = files.reject{|e| File.mtime(File.(e,src)) < File.mtime(File.(e,dest)) } end #別名をつける files = files.map{|e| extname = File.extname(e) basename = File.basename(e).gsub(extname,"") candidate = "" 100.times{|i| candidate = File.("#{basename}(#{i+1})#{extname}",dest) break unless File.exists?(File.(candidate,dest)) raise "upto #{i} files are already exists ." if i >=1000 next FileUtils.cmp(File.(e,src) , File.(candidate,dest)) } [File.(e,src) , File.(candidate,dest)] } #コピーする self.copy_r(files) end |
#sync_by_hash(src, dest, options = {}) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rbsync.rb', line 172 def sync_by_hash(src,dest,={}) src_files = collet_hash(find_as_relative(src, [:excludes]), src, ) dest_files = collet_hash(find_as_relative(dest,[:excludes]),dest, ) target = src_files.keys - dest_files.keys target = target.reject{|key| e = src_files[key].first [:update] && File.exists?( File.(e,dest)) && (File.mtime(File.(e,src)) < File.mtime( File.(e,dest))) } if [:overwrite] == false then target = target .reject{|key| e = src_files[key].first (File.exists?(File.(e,src)) && File.exists?( File.(e,dest))) } end puts "同期対象ファイル" if self.debug? puts target.each{|key|puts src_files[key].first} if self.debug? puts "同期対象はありません" if self.debug? and target.size==0 files= target.map{|key| e = src_files[key].first [File.(e,src) , File.(e,dest)] } self.copy_r(files) ret = files.map{|e| FileTest.exist?(e[1]) } puts "同期が終りました" if ret.select{|e|!e}.size == 0 && self.debug? puts "同期に失敗したみたい" if ret.select{|e|!e}.size != 0 && self.debug? end |
#sync_normally(src, dest, options = {}) ⇒ Object
called from sync
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/rbsync.rb', line 270 def sync_normally(src,dest,={}) Thread.abort_on_exception = true if self.debug? files = self.find_files(src,dest,) puts "同期対象のファイルはありません" if self.debug? && files.size==0 return true if files.size == 0 puts "次のファイルを同期します" if self.debug? pp files if self.debug? #srcファイルをdestに上書き #todo options を取り出す self.copy_r(files.map{|e|[File.(e,src) , File.(e,dest)]}) #checking sync result files = self.find_files(src,dest,) puts "同期が終りました" if files.size == 0 && self.debug? puts "同期に失敗がありました" if files.size != 0 && self.debug? pp files if files.size != 0 && self.debug? return files.size == 0 end |
#sync_with_backup(src, dest, options) ⇒ Object
同期先に同名ファイルがあったらファイルを別名にバックアップしてから転送します
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/rbsync.rb', line 291 def sync_with_backup(src,dest,) # 上書き付加の場合 # #ファイル一覧を取得する files = find_as_relative(src,[:excludes]) #中身が同じモノを排除 files = files.reject{|e| FileUtils.cmp(File.(e,src) , File.(e,dest)) } #更新日が当たらしいモノを排除 if [:update] then #更新日が当たらしいモノを排除 files = files.reject{|e| File.mtime(File.(e,src)) < File.mtime(File.(e,dest)) } end #別名をつける files = files.map{|e| extname = File.extname(e) basename = File.basename(e).gsub(extname,"") # 同名のファイルがあった場合 # ファイルをリネームする if File.exists?(File.(e,dest)) then candidate = File.("#{basename}_#{Time.now.strftime('%Y%m%d%H%M%S')}#{extname}",dest) File.rename( File.(e,dest),candidate ) end [File.(e,src) , File.(e,dest)] } #コピーする self.copy_r(files) end |
#updated_file_only=(flag) ⇒ Object Also known as: update=, newer=
flag true or false
463 464 465 |
# File 'lib/rbsync.rb', line 463 def updated_file_only=(flag) @conf[:update] = flag end |
#verbose=(flag) ⇒ Object
flag true or false
457 458 459 460 |
# File 'lib/rbsync.rb', line 457 def verbose=(flag) self.conf[:verbose] = flag $stdout.sync = flag end |
#verbose? ⇒ Boolean
445 446 447 |
# File 'lib/rbsync.rb', line 445 def verbose? self.conf[:verbose] == true end |