rbsync 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,44 +1,81 @@
1
1
  = rbsync
2
+ Synchronize files src to dest .
3
+ this class can sync files and recuresively
4
+ options are
5
+ +sync update file only
6
+ +no overwrite when dist files are newer than src
7
+ +sync by file digest hash , not useing filename
2
8
 
3
- rbsync is sync files utility
4
- == usage
5
- === mirror files
6
- 同期元と同期先を同じにする
7
- require 'rbsync'
8
- rsync =RbSync.new
9
- rsync.sync( "src", "dest" )
10
- === mirror updated only files
11
- 同期先に、同期元と同名のファイルがあったら、更新日時を調べる。新しいモノだけをコピーする.
12
- require 'rbsync'
13
- rsync =RbSync.new
14
- rsync.sync( "src", "dest",{:update=>true} )
15
- === using exclude pattern
16
- 同期先と同期元を同じにする,但し、*.rb / *.log の拡張子は除外する.
17
- require 'rbsync'
18
- rsync =RbSync.new
19
- rsync.sync( "src", "dest",{:excludes=>["*.log","*.bak"]} )
20
- ==special usage , sync by file cotetets
21
- if directory has a same file with different file name. insted of filename , sync file by file hash
22
- ディレクトリ内のファイル名をうっかり変えてしまったときに使う.ファイル名でなく、ファイルの中身を比較して同期する.
23
- require 'rbsync'
24
- rsync =RbSync.new
25
- rsync.sync( "src", "dest",{:check_hash=>true} )
26
- === directory has very large file ,such as mpeg video
27
- checking only head of 1024*1024 bytes to distinguish src / dest files.this is for speed up.
28
- FileUtils::cmp is reading whole file. large file will take time.With :hash_limit_size Rbsync read only head of files for comparing.
29
- 巨大なファイルだと,全部読み込むのに時間が掛かるので、先頭1024*1024 バイトを比較してOKとする.写真とかはコレで十分
30
- require 'rbsync'
31
- rsync =RbSync.new
32
- rsync.sync( "src", "dest",{:check_hash=>true,:hash_limit_size=1024*1024} )
9
+ == usage
10
+ === mirror files
11
+ 同期元と同期先を同じにする
12
+ require 'rbsync'
13
+ rsync =RbSync.new
14
+ rsync.sync( "src", "dest" )
15
+ === mirror updated only files
16
+ 同期先に、同期元と同名のファイルがあったら、更新日時を調べる。新しいモノだけをコピーする.
17
+ require 'rbsync'
18
+ rsync =RbSync.new
19
+ rsync.sync( "src", "dest",{:update=>true} )
20
+ === using exclude pattern
21
+ 同期先と同期元を同じにする,但し、*.rb / *.log の拡張子は除外する.
22
+ require 'rbsync'
23
+ rsync =RbSync.new
24
+ rsync.sync( "src", "dest",{:excludes=>["*.log","*.bak"]} )
25
+ == sync by another name if file name confilicts
26
+ send src file with anothername. when file name confilicts
27
+ 名前が衝突した場合で、ファイルを書換える時は,転送元のファイルを別名で転送する
28
+ windows のファイルコピーっぽい動作
29
+ before sync
30
+ |src | test.txt | 2011-06-14
31
+ |dest | test.txt | 2011-06-12
32
+ after sync
33
+ |src | test.txt | 2011-06-14
34
+ |dest | test(1).txt | 2011-06-14 # same to src
35
+ |dest | test.txt | 2011-06-12
36
+ == sync with backup
37
+ 名前が衝突した場合で、ファイルを書換える場合転送先のファイルを別名で保存してから転送する
38
+ before sync
39
+ |src | test.txt | 2011-06-14
40
+ |dest | test.txt | 2011-06-12
41
+ after sync
42
+ |src | test.txt | 2011-06-14
43
+ |dest | test.txt | 2011-06-14 # same to src
44
+ |dest | test_20110614022255.txt | 2011-06-12 # moved
33
45
 
34
- === sync both updated files
35
- 双方向に同期させたい場合は2回起動する.
36
- require 'rbsync'
37
- rsync =RbSync.new
38
- rsync.updated_file_only = true
39
- rsync.sync( "src", "dest" )
40
- rsync.sync( "dest", "src" )
41
46
 
47
+ ==special usage , sync by file cotetets
48
+ if directory has a same file with different file name. insted of filename , sync file by file hash
49
+ when files are theses,
50
+ |src| test.txt | "47bce5c74f589f4867dbd57e9ca9f808" |
51
+ |dst| test.bak | "47bce5c74f589f4867dbd57e9ca9f808" |
52
+ :check_hash results no effect.
53
+ ディレクトリ内のファイル名をうっかり変えてしまったときに使う.ファイル名でなく、ファイルの中身を比較して同期する.
54
+ |src| test.txt | "47bce5c74f589f4867dbd57e9ca9f808" |
55
+ |dst| test.bak | "47bce5c74f589f4867dbd57e9ca9f808" |
56
+ の場合何もおきません
57
+ require 'rbsync'
58
+ rsync =RbSync.new
59
+ rsync.sync( "src", "dest",{:check_hash=>true} )
60
+ === directory has very large file ,such as mpeg video
61
+ using with :check_hash=>true
62
+ checking only head of 1024*1024 bytes to distinguish src / dest files.this is for speed up.
63
+ FileUtils::cmp is reading whole file. large file will take time.With :hash_limit_size Rbsync read only head of files for comparing.
64
+ 巨大なファイルだと,全部読み込むのに時間が掛かるので、先頭1024*1024 バイトを比較してOKとする.写真とかはコレで十分
65
+ ファイル名を書換えてしまってコンテンツ内容の比較だけで使う。
66
+ :check_hash=>true とペアで使います
67
+ require 'rbsync'
68
+ rsync =RbSync.new
69
+ rsync.sync( "src", "dest",{:check_hash=>true,:hash_limit_size=1024*1024} )
70
+
71
+ === sync both updated files
72
+ To sync both, call sync methods twice
73
+ 双方向に同期させたい場合は2回起動する.
74
+ require 'rbsync'
75
+ rsync =RbSync.new
76
+ rsync.updated_file_only = true
77
+ rsync.sync( "src", "dest" )
78
+ rsync.sync( "dest", "src" )# swap src to dest , dest to src
42
79
  == Contributing to rbsync
43
80
 
44
81
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
data/lib/rbsync.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
- #rbsyncの改良版。
4
- # ファイル名でなくハッシュ値を計算してファイルを同期する。
5
- #ファイルの同期
6
3
 
7
4
  require 'fileutils'
8
5
 
@@ -29,27 +26,60 @@ require 'fileutils'
29
26
  # require 'rbsync'
30
27
  # rsync =RbSync.new
31
28
  # rsync.sync( "src", "dest",{:excludes=>["*.log","*.bak"]} )
29
+ # == sync by another name if file name confilicts
30
+ # 名前が衝突した場合で、ファイルを書換える時は,転送元のファイルを別名で転送する
31
+ # windows のファイルコピーっぽい動作
32
+ # send src file with anothername.
33
+ # before sync
34
+ # |src | test.txt | 2011-06-14
35
+ # |dest | test.txt | 2011-06-12
36
+ # after sync
37
+ # |src | test.txt | 2011-06-14
38
+ # |dest | test(1).txt | 2011-06-14 # same to src
39
+ # |dest | test.txt | 2011-06-12
40
+ # == sync with backup
41
+ # 名前が衝突した場合で、ファイルを書換える場合転送先のファイルを別名で保存してから転送する
42
+ # before sync
43
+ # |src | test.txt | 2011-06-14
44
+ # |dest | test.txt | 2011-06-12
45
+ # after sync
46
+ # |src | test.txt | 2011-06-14
47
+ # |dest | test.txt | 2011-06-14 # same to src
48
+ # |dest | test_20110614022255.txt | 2011-06-12 # moved
49
+ #
50
+ #
32
51
  # ==special usage , sync by file cotetets
33
52
  # if directory has a same file with different file name. insted of filename , sync file by file hash
53
+ # when files are theses,
54
+ # |src| test.txt | "47bce5c74f589f4867dbd57e9ca9f808" |
55
+ # |dst| test.bak | "47bce5c74f589f4867dbd57e9ca9f808" |
56
+ # :check_hash results no effect.
34
57
  # ディレクトリ内のファイル名をうっかり変えてしまったときに使う.ファイル名でなく、ファイルの中身を比較して同期する.
58
+ # |src| test.txt | "47bce5c74f589f4867dbd57e9ca9f808" |
59
+ # |dst| test.bak | "47bce5c74f589f4867dbd57e9ca9f808" |
60
+ # の場合何もおきません
35
61
  # require 'rbsync'
36
62
  # rsync =RbSync.new
37
63
  # rsync.sync( "src", "dest",{:check_hash=>true} )
38
64
  # === directory has very large file ,such as mpeg video
65
+ # using with :check_hash=>true
39
66
  # checking only head of 1024*1024 bytes to distinguish src / dest files.this is for speed up.
40
67
  # FileUtils::cmp is reading whole file. large file will take time.With :hash_limit_size Rbsync read only head of files for comparing.
41
68
  # 巨大なファイルだと,全部読み込むのに時間が掛かるので、先頭1024*1024 バイトを比較してOKとする.写真とかはコレで十分
69
+ # ファイル名を書換えてしまってコンテンツ内容の比較だけで使う。
70
+ # :check_hash=>true とペアで使います
42
71
  # require 'rbsync'
43
72
  # rsync =RbSync.new
44
73
  # rsync.sync( "src", "dest",{:check_hash=>true,:hash_limit_size=1024*1024} )
45
74
  #
46
75
  # === sync both updated files
76
+ # To sync both, call sync methods twice
47
77
  # 双方向に同期させたい場合は2回起動する.
48
78
  # require 'rbsync'
49
79
  # rsync =RbSync.new
50
80
  # rsync.updated_file_only = true
51
81
  # rsync.sync( "src", "dest" )
52
- # rsync.sync( "dest", "src" )
82
+ # rsync.sync( "dest", "src" )# swap src to dest , dest to src
53
83
  class RbSync
54
84
  attr_accessor :conf
55
85
  def initialize()
@@ -165,10 +195,10 @@ class RbSync
165
195
  end
166
196
  # compute digest md5
167
197
  # ==limitsize
168
- # If file size is very large.
169
- # And a few byte head of file is enough to compare.
170
- # for speedup, setting limit size enable to skipp reading file.
171
- # もしファイルがとても巨大で、かつ、先頭の数キロバイトで十分であれば、limitsize 以降をスキップする
198
+ # If file size is very large,
199
+ # and a few bytes at head of file is enough to compare.
200
+ # for speed-up, Set limit size to enable to avoid reading a whole of file.
201
+ # もしファイルがとても巨大で、かつ、先頭の数キロバイトが比較に十分であれば、limitsize 以降をスキップする
172
202
  def compute_digest_file(filename, limitsize=nil)
173
203
  require 'digest/md5'
174
204
  s = %{
@@ -190,11 +220,7 @@ class RbSync
190
220
  Digest::MD5.open(filename,limitsize).hexdigest
191
221
  end
192
222
 
193
- # ロジックが長すぎるので短くするか別に分ける.
194
- # ロジックのパターン毎に共通化する
195
- # ・ディレクトリ内のファイル一覧を作る
196
- # ・ファイル一覧を比較する
197
- # ・同期するファイル一覧を作って転送する
223
+ # called from sync
198
224
  def sync_normally(src,dest,options={})
199
225
  files = self.find_files(src,dest,options)
200
226
  puts "同期対象のファイルはありません" if self.debug? && files.size==0
@@ -214,8 +240,8 @@ class RbSync
214
240
  pp files if files.size != 0 && self.debug?
215
241
  return files.size == 0
216
242
  end
217
- # 別名で名前をつけて転送する
218
- def sync_by_anothername(src,dest,options)
243
+ # 同期先に同名ファイルがあったらファイルを別名にバックアップしてから転送します
244
+ def sync_with_backup(src,dest,options)
219
245
  # 上書き付加の場合
220
246
  #
221
247
  #ファイル一覧を取得する
@@ -224,10 +250,44 @@ class RbSync
224
250
  files = files.reject{|e|
225
251
  FileUtils.cmp(File.expand_path(e,src) , File.expand_path(e,dest))
226
252
  }
227
- #更新日が当たらしifモノを排除
253
+ #更新日が当たらしいモノを排除
254
+ if options[:update] then
255
+ #更新日が当たらしいモノを排除
256
+ files = files.reject{|e|
257
+ File.mtime(File.expand_path(e,src)) < File.mtime(File.expand_path(e,dest))
258
+ }
259
+ end
260
+ #別名をつける
261
+ files = files.map{|e|
262
+ extname = File.extname(e)
263
+ basename = File.basename(e).gsub(extname,"")
264
+ # 同名のファイルがあった場合
265
+ # ファイルをリネームする
266
+ if File.exists?(File.expand_path(e,dest)) then
267
+ candidate = File.expand_path("#{basename}_#{Time.now.strftime('%Y%m%d%H%M%S')}#{extname}",dest)
268
+ File.rename( File.expand_path(e,dest),candidate )
269
+ end
270
+ [File.expand_path(e,src) , File.expand_path(e,dest)]
271
+ }
272
+ #コピーする
273
+ self.copy_r(files)
274
+ end
275
+ # 別名で名前をつけて転送する
276
+ def sync_by_anothername(src,dest,options)
277
+ # 上書き付加の場合
278
+ #
279
+ #ファイル一覧を取得する
280
+ files = find_as_relative(src,options[:excludes])
281
+ #中身が同じモノを排除
228
282
  files = files.reject{|e|
229
- File.mtime(File.expand_path(e,src)) < File.mtime(File.expand_path(e,dest))
283
+ FileUtils.cmp(File.expand_path(e,src) , File.expand_path(e,dest))
230
284
  }
285
+ if options[:update] then
286
+ #更新日が当たらしいモノを排除
287
+ files = files.reject{|e|
288
+ File.mtime(File.expand_path(e,src)) < File.mtime(File.expand_path(e,dest))
289
+ }
290
+ end
231
291
  #別名をつける
232
292
  files = files.map{|e|
233
293
  extname = File.extname(e)
@@ -260,6 +320,8 @@ class RbSync
260
320
  options[:overwrite] = false if options[:no_overwrite]
261
321
  if options[:rename]
262
322
  return self.sync_by_anothername(src,dest,options)
323
+ elsif options[:backup]
324
+ return self.sync_with_backup(src,dest,options)
263
325
  elsif options[:check_hash]
264
326
  return self.sync_by_hash(src,dest,options)
265
327
  else
@@ -416,3 +478,26 @@ end
416
478
  #p FileUtils.cmp("old/test.txt","new/test(2).txt") == true
417
479
  #end
418
480
  #end
481
+ #require 'tmpdir'
482
+ #require 'find'
483
+ #require 'pp'
484
+ #Dir.mktmpdir('goo') do |dir|
485
+ #Dir.chdir dir do
486
+ #Dir.mkdir("old")
487
+ #Dir.mkdir("new")
488
+ ## 同名のファイルを作って
489
+ #open("./old/test.txt", "w+"){|f| 10.times{f.puts("test")}}
490
+ #old_content =open("./old/test.txt", "r").read
491
+ ## ミラーして
492
+ #rsync = RbSync.new
493
+ #rsync.sync("old","new")
494
+ #p FileUtils.cmp("old/test.txt","new/test.txt") == true
495
+ #open("./old/test.txt", "w+"){|f| 10.times{f.puts("changed")}}
496
+ ## バックアップ同期する
497
+ #rsync.sync("old","new",{:backup => true})
498
+ #p FileUtils.cmp("old/test.txt","new/test.txt") == true
499
+ ## バックアップしたファイルがどうなっているか見る
500
+ #files = Dir.glob "./new/**/*"
501
+ #p old_content == open((files - ["./new/test.txt"]).first).read
502
+ #end
503
+ #end
data/rbsync.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rbsync}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["takuya"]
12
- s.date = %q{2011-06-13}
12
+ s.date = %q{2011-06-14}
13
13
  s.description = %q{rbsync is sync file utility.this can sync directory even if filename differed.checking content insted.}
14
14
  s.email = %q{takuya.1st@gmail}
15
15
  s.extra_rdoc_files = [
data/test/test_rbsync.rb CHANGED
@@ -125,4 +125,46 @@ class TestRbsync < Test::Unit::TestCase
125
125
  end
126
126
  end
127
127
  end
128
+ def test_sync_with_rename
129
+ Dir.mktmpdir('goo') do |dir|
130
+ Dir.chdir dir do
131
+ Dir.mkdir("old")
132
+ Dir.mkdir("new")
133
+ open("./old/test.txt", "w+"){|f| 10.times{f.puts("test")}}
134
+ rsync = RbSync.new
135
+ rsync.sync("old","new")
136
+ assert FileUtils.cmp("old/test.txt","new/test.txt") == true
137
+ open("./old/test.txt", "w+"){|f| 10.times{f.puts("changed")}}
138
+ rsync.sync("old","new",{:rename => true})
139
+ assert FileUtils.cmp("old/test.txt","new/test.txt") == false
140
+ assert FileUtils.cmp("old/test.txt","new/test(1).txt") == true
141
+ open("./old/test.txt", "w+"){|f| 10.times{f.puts("changed!!!")}}
142
+ rsync.sync("old","new",{:rename => true})
143
+ assert FileUtils.cmp("old/test.txt","new/test(2).txt") == true
144
+ end
145
+ end
146
+ end
147
+ def test_sync_with_backup
148
+ Dir.mktmpdir('goo') do |dir|
149
+ Dir.chdir dir do
150
+ Dir.mkdir("old")
151
+ Dir.mkdir("new")
152
+ # �����̃t�@�C���������
153
+ open("./old/test.txt", "w+"){|f| 10.times{f.puts("test")}}
154
+ old_content =open("./old/test.txt", "r").read
155
+ # �~���[����
156
+ rsync = RbSync.new
157
+ rsync.sync("old","new")
158
+ assert FileUtils.cmp("old/test.txt","new/test.txt") == true
159
+ open("./old/test.txt", "w+"){|f| 10.times{f.puts("changed")}}
160
+ # �o�b�N�A�b�v��������
161
+ rsync.sync("old","new",{:backup => true})
162
+ assert FileUtils.cmp("old/test.txt","new/test.txt") == true
163
+ # �o�b�N�A�b�v�����t�@�C�����ǂ��Ȃ��Ă��邩����
164
+ files = Dir.glob "./new/**/*"
165
+ assert old_content == open((files - ["./new/test.txt"]).first).read
166
+ end
167
+ end
168
+ end
169
+
128
170
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbsync
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - takuya
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-13 00:00:00 +09:00
18
+ date: 2011-06-14 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency