rbsync 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/lib/rbsync.rb +119 -18
  3. data/rbsync.gemspec +2 -2
  4. data/test/test_rbsync.rb +15 -0
  5. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/lib/rbsync.rb CHANGED
@@ -56,6 +56,7 @@ class RbSync
56
56
  @conf ={}
57
57
  @conf[:update] = false
58
58
  @conf[:excludes] = []
59
+ @conf[:preserve] = true
59
60
  end
60
61
  # collect file paths. paths are relatetive path.
61
62
  def find_as_relative(dir_name,excludes=[])
@@ -91,14 +92,19 @@ class RbSync
91
92
  (File.mtime(File.expand_path(e,src)) > File.mtime( File.expand_path(e,dest)))
92
93
  }
93
94
  end
95
+ if options[:overwrite] == false then
96
+ same_name_files= same_name_files.reject{|e|
97
+ (File.exists?(File.expand_path(e,src)) && File.exists?( File.expand_path(e,dest)))
98
+ }
99
+ end
94
100
  files_not_in_dest = (src_files - dest_files)
95
101
  #files
96
102
  files =[]
97
103
  files = (files_not_in_dest + same_name_files ).flatten
98
104
  end
99
105
  def sync_by_hash(src,dest,options={})
100
- src_files = collet_hash(find_as_relative(src, options), src, options)
101
- dest_files = collet_hash(find_as_relative(dest,options),dest, options)
106
+ src_files = collet_hash(find_as_relative(src, options[:excludes]), src, options)
107
+ dest_files = collet_hash(find_as_relative(dest,options[:excludes]),dest, options)
102
108
  target = src_files.keys - dest_files.keys
103
109
  target = target.reject{|key|
104
110
  e = src_files[key].first
@@ -106,15 +112,31 @@ class RbSync
106
112
  File.exists?( File.expand_path(e,dest)) &&
107
113
  (File.mtime(File.expand_path(e,src)) < File.mtime( File.expand_path(e,dest)))
108
114
  }
115
+ if options[:overwrite] == false then
116
+ target = target .reject{|key|
117
+ e = src_files[key].first
118
+ (File.exists?(File.expand_path(e,src)) && File.exists?( File.expand_path(e,dest)))
119
+ }
120
+ end
109
121
  puts "同期対象ファイル" if self.debug?
110
122
  puts target.each{|key|puts src_files[key].first} if self.debug?
111
123
  puts "同期対象はありません" if self.debug? and target.size==0
112
- ret = target.map{|key|
124
+
125
+ files= target.map{|key|
113
126
  e = src_files[key].first
114
- FileUtils.copy( File.expand_path(e,src) , File.expand_path(e,dest),
115
- {:preserve=>self.preserve?,:verbose=>self.verbose? })
116
- FileTest.exist?(File.expand_path(e,dest))
127
+ [File.expand_path(e,src) , File.expand_path(e,dest)]
128
+ }
129
+ self.copy_r(files)
130
+
131
+ ret = files.map{|e|
132
+ FileTest.exist?(e[1])
117
133
  }
134
+ #ret = target.map{|key|
135
+ #e = src_files[key].first
136
+ #FileUtils.copy( File.expand_path(e,src) , File.expand_path(e,dest),
137
+ #{:preserve=>self.preserve?,:verbose=>self.verbose? })
138
+ #FileTest.exist?(File.expand_path(e,dest))
139
+ #}
118
140
  puts "同期が終りました" if ret.select{|e|!e}.size == 0 && self.debug?
119
141
  puts "同期に失敗したみたい" if ret.select{|e|!e}.size != 0 && self.debug?
120
142
  end
@@ -182,11 +204,7 @@ class RbSync
182
204
 
183
205
  #srcファイルをdestに上書き
184
206
  #todo options を取り出す
185
- files.each{|e|
186
- FileUtils.copy( File.expand_path(e,src) , File.expand_path(e,dest),
187
- {:preserve=>self.preserve?,:verbose=>self.verbose? } )
188
- }
189
- files =[]
207
+ self.copy_r(files.map{|e|[File.expand_path(e,src) , File.expand_path(e,dest)]})
190
208
 
191
209
  #checking sync result
192
210
  files = self.find_files(src,dest,options)
@@ -196,15 +214,56 @@ class RbSync
196
214
  pp files if files.size != 0 && self.debug?
197
215
  return files.size == 0
198
216
  end
217
+ # 別名で名前をつけて転送する
218
+ def sync_by_anothername(src,dest,options)
219
+ # 上書き付加の場合
220
+ #
221
+ #ファイル一覧を取得する
222
+ files = find_as_relative(src,options[:excludes])
223
+ #中身が同じモノを排除
224
+ files = files.reject{|e|
225
+ FileUtils.cmp(File.expand_path(e,src) , File.expand_path(e,dest))
226
+ }
227
+ #更新日が当たらしifモノを排除
228
+ files = files.reject{|e|
229
+ File.mtime(File.expand_path(e,src)) < File.mtime(File.expand_path(e,dest))
230
+ }
231
+ #別名をつける
232
+ files = files.map{|e|
233
+ extname = File.extname(e)
234
+ basename = File.basename(e).gsub(extname,"")
235
+ candidate = ""
236
+ 100.times{|i|
237
+ candidate = File.expand_path("#{basename}(#{i+1})#{extname}",dest)
238
+ break unless File.exists?(File.expand_path(candidate,dest))
239
+ raise "upto #{i} files are already exists ." if i >=1000
240
+ next FileUtils.cmp(File.expand_path(e,src) , File.expand_path(candidate,dest))
241
+ }
242
+ [File.expand_path(e,src) , File.expand_path(candidate,dest)]
243
+ }
244
+ #コピーする
245
+ self.copy_r(files)
246
+ end
247
+ def copy_r(files)
248
+ ##todo 進捗を調べる.
249
+
250
+ files.each{|e|
251
+ FileUtils.copy( e[0] , e[1] ,{:preserve=>self.preserve?,:verbose=>self.verbose? } )
252
+ }
253
+ end
199
254
  def sync(src,dest,options={})
200
- options[:excludes] = (self.excludes + [options[:excludes]]).flatten.uniq if options[:excludes]
201
- options[:update] = @conf[:update] if options[:update] == nil
202
- options[:check_hash] = options[:check_hash] and @conf[:check_hash]
203
- options[:hash_limit_size] = @conf[:hash_limit_size] if options[:hash_limit_size] == nil
204
- if options[:check_hash]
205
- return self.sync_by_hash(src,dest,options)
255
+ options[:excludes] = self.excludes.push(options[:excludes]).flatten.uniq if options[:excludes]
256
+ options[:update] = @conf[:update] if options[:update] == nil
257
+ options[:check_hash] = options[:check_hash] and @conf[:check_hash]
258
+ options[:hash_limit_size] = @conf[:hash_limit_size] if options[:hash_limit_size] == nil
259
+ options[:overwrite] = @conf[:overwrite] if options[:overwrite] == nil
260
+ options[:overwrite] = false if options[:no_overwrite]
261
+ if options[:rename]
262
+ return self.sync_by_anothername(src,dest,options)
263
+ elsif options[:check_hash]
264
+ return self.sync_by_hash(src,dest,options)
206
265
  else
207
- return self.sync_normally(src,dest,options)
266
+ return self.sync_normally(src,dest,options)
208
267
  end
209
268
  end
210
269
 
@@ -255,6 +314,12 @@ class RbSync
255
314
  def preserve?
256
315
  @conf[:preserve]
257
316
  end
317
+ def overwrite=(flag)
318
+ @conf[:overwrite] = flag
319
+ end
320
+ def overwrite?
321
+ @conf[:overwrite]
322
+ end
258
323
 
259
324
  #aliases
260
325
 
@@ -265,3 +330,39 @@ class RbSync
265
330
  end
266
331
 
267
332
 
333
+ #require 'tmpdir'
334
+ #require 'find'
335
+ #require 'pp'
336
+ #Dir.mktmpdir('goo') do |dir|
337
+ #Dir.chdir dir do
338
+ #Dir.mkdir("old")
339
+ #Dir.mkdir("new")
340
+ #open("./old/test.txt", "w+"){|f| 10.times{f.puts("test")}}
341
+ #open("./new/test.txt", "w+"){|f| 10.times{f.puts("different")}}
342
+ #rsync = RbSync.new
343
+ #rsync.sync("old","new",{:overwrite=>false,:check_hash=>true})
344
+ #p FileUtils.cmp("old/test.txt","new/test.txt") == false
345
+ #end
346
+ #end
347
+
348
+
349
+ #require 'tmpdir'
350
+ #require 'find'
351
+ #require 'pp'
352
+ #Dir.mktmpdir('goo') do |dir|
353
+ #Dir.chdir dir do
354
+ #Dir.mkdir("old")
355
+ #Dir.mkdir("new")
356
+ #open("./old/test.txt", "w+"){|f| 10.times{f.puts("test")}}
357
+ #rsync = RbSync.new
358
+ #rsync.sync("old","new")
359
+ #p FileUtils.cmp("old/test.txt","new/test.txt") == true
360
+ #open("./old/test.txt", "w+"){|f| 10.times{f.puts("changed")}}
361
+ #rsync.sync("old","new",{:rename => true})
362
+ #p FileUtils.cmp("old/test.txt","new/test.txt") == false
363
+ #p FileUtils.cmp("old/test.txt","new/test(1).txt") == true
364
+ #open("./old/test.txt", "w+"){|f| 10.times{f.puts("changed!!!")}}
365
+ #rsync.sync("old","new",{:rename => true})
366
+ #p FileUtils.cmp("old/test.txt","new/test(2).txt") == true
367
+ #end
368
+ #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.3"
8
+ s.version = "0.0.4"
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-12}
12
+ s.date = %q{2011-06-13}
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
@@ -110,4 +110,19 @@ class TestRbsync < Test::Unit::TestCase
110
110
  end
111
111
  end
112
112
  end
113
+ def test_sync_no_overwrite
114
+ Dir.mktmpdir('goo') do |dir|
115
+ Dir.chdir dir do
116
+ Dir.mkdir("old")
117
+ Dir.mkdir("new")
118
+ open("./old/test.txt", "w+"){|f| 10.times{f.puts("test")}}
119
+ open("./new/test.txt", "w+"){|f| 10.times{f.puts("different")}}
120
+ rsync = RbSync.new
121
+ rsync.sync("old","new",{:overwrite=>false})
122
+ assert FileUtils.cmp("old/test.txt","new/test.txt") == false
123
+ rsync.sync("old","new",{:overwrite=>false,:check_hash=>true})
124
+ assert FileUtils.cmp("old/test.txt","new/test.txt") == false
125
+ end
126
+ end
127
+ end
113
128
  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: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
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-12 00:00:00 +09:00
18
+ date: 2011-06-13 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency