chillfile 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/chillfile.gemspec +6 -5
- data/lib/chillfile.rb +6 -3
- data/lib/chillfile/helper/sync_helper.rb +53 -0
- data/lib/chillfile/model.rb +1 -1
- data/lib/chillfile/model/asset.rb +24 -0
- data/lib/chillfile/model/base.rb +1 -1
- data/lib/chillfile/sync.rb +70 -118
- data/spec/lib/chillfile/model/asset_spec.rb +22 -0
- data/spec/lib/chillfile_spec.rb +1 -1
- data/version.yml +2 -2
- metadata +7 -6
- data/lib/chillfile/model/sync_file.rb +0 -28
- data/spec/lib/chillfile/model/sync_file_spec.rb +0 -18
data/chillfile.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{chillfile}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["dpree"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-01-09}
|
13
13
|
s.default_executable = %q{chillfile}
|
14
14
|
s.description = %q{Let your files chill on the couch}
|
15
15
|
s.email = %q{whiterabbit.init@gmail.com}
|
@@ -32,9 +32,10 @@ Gem::Specification.new do |s|
|
|
32
32
|
"lib/chillfile/cli.rb",
|
33
33
|
"lib/chillfile/config.rb",
|
34
34
|
"lib/chillfile/database_server.rb",
|
35
|
+
"lib/chillfile/helper/sync_helper.rb",
|
35
36
|
"lib/chillfile/model.rb",
|
37
|
+
"lib/chillfile/model/asset.rb",
|
36
38
|
"lib/chillfile/model/base.rb",
|
37
|
-
"lib/chillfile/model/sync_file.rb",
|
38
39
|
"lib/chillfile/sync.rb",
|
39
40
|
"spec/filesystem_after/bar.1.moved.untouched",
|
40
41
|
"spec/filesystem_after/bar.2.moved.copy",
|
@@ -55,7 +56,7 @@ Gem::Specification.new do |s|
|
|
55
56
|
"spec/filesystem_before/wuu.deleted",
|
56
57
|
"spec/fixtures/filesystem_after.json",
|
57
58
|
"spec/fixtures/filesystem_before.json",
|
58
|
-
"spec/lib/chillfile/model/
|
59
|
+
"spec/lib/chillfile/model/asset_spec.rb",
|
59
60
|
"spec/lib/chillfile_spec.rb",
|
60
61
|
"spec/spec_helper.rb",
|
61
62
|
"spec/support/db_helper.rb",
|
@@ -69,7 +70,7 @@ Gem::Specification.new do |s|
|
|
69
70
|
s.rubygems_version = %q{1.3.7}
|
70
71
|
s.summary = %q{Tiny tool to sync filesystem with couchdb}
|
71
72
|
s.test_files = [
|
72
|
-
"spec/lib/chillfile/model/
|
73
|
+
"spec/lib/chillfile/model/asset_spec.rb",
|
73
74
|
"spec/lib/chillfile_spec.rb",
|
74
75
|
"spec/spec_helper.rb",
|
75
76
|
"spec/support/db_helper.rb",
|
data/lib/chillfile.rb
CHANGED
@@ -14,14 +14,17 @@ require "progressbar"
|
|
14
14
|
|
15
15
|
require File.join(File.dirname(__FILE__), "chillfile/config")
|
16
16
|
require File.join(File.dirname(__FILE__), "chillfile/cli")
|
17
|
+
|
18
|
+
require File.join(File.dirname(__FILE__), "chillfile/helper/sync_helper")
|
19
|
+
|
17
20
|
require File.join(File.dirname(__FILE__), "chillfile/database_server")
|
18
21
|
require File.join(File.dirname(__FILE__), "chillfile/sync")
|
19
22
|
require File.join(File.dirname(__FILE__), "chillfile/model")
|
20
23
|
|
21
24
|
module Chillfile
|
22
25
|
get_version = lambda do
|
23
|
-
v = YAML.
|
24
|
-
"#{v[:major]
|
26
|
+
v = YAML.load_file(File.join(File.dirname(__FILE__), "../version.yml"))
|
27
|
+
"#{v[:major]}.#{v[:minor]}.#{v[:patch]}"
|
25
28
|
end
|
26
29
|
VERSION = get_version.call
|
27
30
|
|
@@ -55,7 +58,7 @@ module Chillfile
|
|
55
58
|
|
56
59
|
# databse
|
57
60
|
def db_list
|
58
|
-
Chillfile::
|
61
|
+
Chillfile::Asset.by_filesystem_raw
|
59
62
|
end
|
60
63
|
end
|
61
64
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Chillfile::SyncHelper
|
2
|
+
# CREATED
|
3
|
+
def create_doc(checksum, path)
|
4
|
+
doc = Chillfile::Asset.new(:checksum => checksum, :path => path)
|
5
|
+
doc.save
|
6
|
+
begin
|
7
|
+
file = File.open(path)
|
8
|
+
doc.create_attachment(:file => file, :name => "master")
|
9
|
+
ensure
|
10
|
+
file.close
|
11
|
+
end
|
12
|
+
doc
|
13
|
+
end
|
14
|
+
|
15
|
+
# DELETED
|
16
|
+
def delete_doc(doc)
|
17
|
+
doc.deleted = true
|
18
|
+
doc.save
|
19
|
+
doc
|
20
|
+
end
|
21
|
+
|
22
|
+
# COPIED
|
23
|
+
def copy_doc(doc, path)
|
24
|
+
doc = create_doc(doc.checksum, path)
|
25
|
+
|
26
|
+
# TODO keep metadata => make a deep clone here...
|
27
|
+
|
28
|
+
doc
|
29
|
+
end
|
30
|
+
|
31
|
+
# MOVED
|
32
|
+
def update_doc_path(doc, new_path)
|
33
|
+
doc.path = new_path
|
34
|
+
doc
|
35
|
+
end
|
36
|
+
|
37
|
+
# MODIFIED
|
38
|
+
def update_doc_attachment(doc, new_checksum)
|
39
|
+
doc.checksum = new_checksum
|
40
|
+
begin
|
41
|
+
file = File.open(doc.path)
|
42
|
+
doc.update_attachment(:file => file, :name => "master")
|
43
|
+
ensure
|
44
|
+
file.close
|
45
|
+
end
|
46
|
+
doc
|
47
|
+
end
|
48
|
+
|
49
|
+
# FINALIZER
|
50
|
+
def doc_save!(doc)
|
51
|
+
doc.save
|
52
|
+
end
|
53
|
+
end
|
data/lib/chillfile/model.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Chillfile::Asset < Chillfile::Base
|
2
|
+
timestamps!
|
3
|
+
property :checksum, String
|
4
|
+
property :path, String
|
5
|
+
|
6
|
+
property :deleted, TrueClass, :default => false
|
7
|
+
|
8
|
+
view_by :checksum
|
9
|
+
view_by :path
|
10
|
+
|
11
|
+
view_by :filesystem, :map => "
|
12
|
+
function(doc) {
|
13
|
+
if (doc['type'] == 'Chillfile::Asset' && !doc['deleted']) {
|
14
|
+
emit(null, [doc['checksum'], doc['path']]);
|
15
|
+
}
|
16
|
+
}
|
17
|
+
"
|
18
|
+
|
19
|
+
def self.by_filesystem_raw
|
20
|
+
self.by_filesystem(:include_docs => false)["rows"].map do |f|
|
21
|
+
f["value"]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/chillfile/model/base.rb
CHANGED
data/lib/chillfile/sync.rb
CHANGED
@@ -1,138 +1,90 @@
|
|
1
|
-
module Chillfile
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
# deleted
|
8
|
-
process_list(comparator.deleted, "Delete") do |checksum, path|
|
9
|
-
for_doc_with(checksum) do |doc|
|
10
|
-
if doc.paths.size > 1
|
11
|
-
update_doc_paths(doc, [path])
|
12
|
-
else
|
13
|
-
delete_doc(doc)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# moved files
|
19
|
-
process_list(comparator.moved, "Move") do |checksum, paths|
|
20
|
-
for_doc_with(checksum) do |doc|
|
21
|
-
update_doc_paths(doc, paths[:old_paths], paths[:new_paths])
|
22
|
-
end
|
23
|
-
end
|
1
|
+
module Chillfile::Sync
|
2
|
+
class << self
|
3
|
+
include Chillfile::SyncHelper
|
4
|
+
|
5
|
+
def process!(comparator, progressbar = nil)
|
6
|
+
@progressbar = progressbar
|
24
7
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
update_doc_attachment(doc, checksums[:new_checksum])
|
41
|
-
end
|
42
|
-
end
|
8
|
+
process_deleted(comparator)
|
9
|
+
process_moved(comparator)
|
10
|
+
process_modified(comparator)
|
11
|
+
process_created(comparator)
|
12
|
+
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# deleted
|
19
|
+
def process_deleted(comparator)
|
20
|
+
process_list(comparator.deleted, "Delete") do |checksum, path|
|
21
|
+
for_doc_with(checksum) do |doc|
|
22
|
+
delete_doc(doc)
|
43
23
|
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# moved files
|
28
|
+
def process_moved(comparator)
|
29
|
+
process_list(comparator.moved, "Move") do |checksum, paths|
|
30
|
+
for_doc_with(checksum) do |doc|
|
31
|
+
if paths[:new_paths].size > 1
|
32
|
+
delete_doc(doc)
|
33
|
+
docs = paths[:new_paths].map do |new_path|
|
34
|
+
copy_doc(doc, new_path)
|
52
35
|
end
|
36
|
+
docs
|
37
|
+
else
|
38
|
+
update_doc_path(doc, paths[:new_paths].first)
|
53
39
|
end
|
54
40
|
end
|
55
41
|
end
|
42
|
+
end
|
56
43
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
doc.save
|
63
|
-
begin
|
64
|
-
file = File.open(path)
|
65
|
-
doc.create_attachment(:file => file, :name => "master")
|
66
|
-
ensure
|
67
|
-
file.close
|
44
|
+
def process_modified(comparator)
|
45
|
+
# modified files
|
46
|
+
process_list(comparator.modified, "Modify") do |path, checksums|
|
47
|
+
for_doc_with(checksums[:old_checksum]) do |doc|
|
48
|
+
update_doc_attachment(doc, checksums[:new_checksum])
|
68
49
|
end
|
69
|
-
doc
|
70
|
-
end
|
71
|
-
|
72
|
-
# COPIED
|
73
|
-
def update_doc_add_path(doc, path)
|
74
|
-
update_doc_paths(doc, [], [path])
|
75
50
|
end
|
51
|
+
end
|
76
52
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
53
|
+
def process_created(comparator)
|
54
|
+
# created files
|
55
|
+
process_list(comparator.created, "Create") do |checksum, path|
|
56
|
+
for_doc_with(checksum) do |doc|
|
57
|
+
create_doc(checksum, path)
|
82
58
|
end
|
83
|
-
|
84
|
-
# add new paths
|
85
|
-
doc.paths = doc.paths + add_paths
|
86
|
-
|
87
|
-
doc
|
88
59
|
end
|
60
|
+
end
|
89
61
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
62
|
+
# wrapper outer
|
63
|
+
def process_list(list, title, &block)
|
64
|
+
return true if list.empty?
|
65
|
+
|
66
|
+
list_iterator = lambda do |notifier|
|
67
|
+
list.each do |args|
|
68
|
+
block.call(*args)
|
69
|
+
notifier.call if notifier
|
98
70
|
end
|
99
|
-
doc
|
100
|
-
end
|
101
|
-
|
102
|
-
# DELETED
|
103
|
-
def delete_doc(doc)
|
104
|
-
doc.deleted = true
|
105
|
-
doc.paths = []
|
106
|
-
doc
|
107
71
|
end
|
108
72
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
list_iterator = lambda do |notifier|
|
114
|
-
list.each do |args|
|
115
|
-
block.call(*args)
|
116
|
-
notifier.call if notifier
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
if @progressbar
|
121
|
-
@progressbar.call({:name => title, :size => list.size}, list_iterator)
|
122
|
-
else
|
123
|
-
list_iterator.call(nil)
|
124
|
-
end
|
73
|
+
if @progressbar
|
74
|
+
@progressbar.call({:name => title, :size => list.size}, list_iterator)
|
75
|
+
else
|
76
|
+
list_iterator.call(nil)
|
125
77
|
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
78
|
+
end
|
79
|
+
|
80
|
+
# wrapper
|
81
|
+
def for_doc_with(checksum, &block)
|
82
|
+
old_doc = Chillfile::Asset.by_checksum(:key => checksum).first
|
83
|
+
new_doc_or_docs = block.call(old_doc)
|
84
|
+
if new_doc_or_docs.is_a? Array
|
85
|
+
new_doc_or_docs.each{|d| doc_save!(d)}
|
86
|
+
else
|
87
|
+
doc_save!(new_doc_or_docs)
|
136
88
|
end
|
137
89
|
end
|
138
90
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Chillfile::Asset do
|
4
|
+
include DbHelper
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
reset_db!
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be possible create a file" do
|
11
|
+
Chillfile::Asset.new(:path => "./foo/bar.baz", :checksum => "13123f6casdd03480be5f781ee12312asdasd232").save.persisted?.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be possible to find using a checksum" do
|
15
|
+
Chillfile::Asset.new(:path => "./foo/bar.baz", :checksum => "13123f6casdd03480be5f781ee12312asdasd232").save
|
16
|
+
Chillfile::Asset.new(:path => "./woo_lands", :checksum => "13123f6casdd03480be5f781ee12312asdasd232").save
|
17
|
+
paths = Chillfile::Asset.by_checksum(:key => "13123f6casdd03480be5f781ee12312asdasd232").map{|a| a.path}
|
18
|
+
paths.size.should == 2
|
19
|
+
paths.include?("./foo/bar.baz").should be_true
|
20
|
+
paths.include?("./woo_lands").should be_true
|
21
|
+
end
|
22
|
+
end
|
data/spec/lib/chillfile_spec.rb
CHANGED
@@ -21,7 +21,7 @@ describe Chillfile do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should be possible to list from db" do
|
24
|
-
Chillfile::
|
24
|
+
Chillfile::Asset.new(:path => "./foo/bar.baz", :checksum => "13123f6casdd03480be5f781ee12312asdasd232").save
|
25
25
|
Chillfile.db_list.should == [["13123f6casdd03480be5f781ee12312asdasd232", "./foo/bar.baz"]]
|
26
26
|
end
|
27
27
|
|
data/version.yml
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.3
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- dpree
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-09 00:00:00 +01:00
|
18
18
|
default_executable: chillfile
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -195,9 +195,10 @@ files:
|
|
195
195
|
- lib/chillfile/cli.rb
|
196
196
|
- lib/chillfile/config.rb
|
197
197
|
- lib/chillfile/database_server.rb
|
198
|
+
- lib/chillfile/helper/sync_helper.rb
|
198
199
|
- lib/chillfile/model.rb
|
200
|
+
- lib/chillfile/model/asset.rb
|
199
201
|
- lib/chillfile/model/base.rb
|
200
|
-
- lib/chillfile/model/sync_file.rb
|
201
202
|
- lib/chillfile/sync.rb
|
202
203
|
- spec/filesystem_after/bar.1.moved.untouched
|
203
204
|
- spec/filesystem_after/bar.2.moved.copy
|
@@ -218,7 +219,7 @@ files:
|
|
218
219
|
- spec/filesystem_before/wuu.deleted
|
219
220
|
- spec/fixtures/filesystem_after.json
|
220
221
|
- spec/fixtures/filesystem_before.json
|
221
|
-
- spec/lib/chillfile/model/
|
222
|
+
- spec/lib/chillfile/model/asset_spec.rb
|
222
223
|
- spec/lib/chillfile_spec.rb
|
223
224
|
- spec/spec_helper.rb
|
224
225
|
- spec/support/db_helper.rb
|
@@ -259,7 +260,7 @@ signing_key:
|
|
259
260
|
specification_version: 3
|
260
261
|
summary: Tiny tool to sync filesystem with couchdb
|
261
262
|
test_files:
|
262
|
-
- spec/lib/chillfile/model/
|
263
|
+
- spec/lib/chillfile/model/asset_spec.rb
|
263
264
|
- spec/lib/chillfile_spec.rb
|
264
265
|
- spec/spec_helper.rb
|
265
266
|
- spec/support/db_helper.rb
|
@@ -1,28 +0,0 @@
|
|
1
|
-
class Chillfile::Model::SyncFile < Chillfile::Model::Base
|
2
|
-
timestamps!
|
3
|
-
property :checksum, String
|
4
|
-
property :paths, [String]
|
5
|
-
|
6
|
-
# alternative to paths.empty?
|
7
|
-
property :deleted, TrueClass, :default => false
|
8
|
-
|
9
|
-
validates_uniqueness_of :checksum
|
10
|
-
|
11
|
-
view_by :checksum
|
12
|
-
|
13
|
-
view_by :filesystem, :map => "
|
14
|
-
function(doc) {
|
15
|
-
if (doc['type'] == 'Chillfile::Model::SyncFile') {
|
16
|
-
for (var i = 0; i < doc['paths'].length; i++) {
|
17
|
-
emit(null, [doc['checksum'], doc['paths'][i]]);
|
18
|
-
}
|
19
|
-
}
|
20
|
-
}
|
21
|
-
"
|
22
|
-
|
23
|
-
def self.by_filesystem_raw
|
24
|
-
self.by_filesystem(:include_docs => false)["rows"].map do |f|
|
25
|
-
f["value"]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Chillfile::Model::SyncFile do
|
4
|
-
include DbHelper
|
5
|
-
|
6
|
-
before(:each) do
|
7
|
-
reset_db!
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should be possible create a file" do
|
11
|
-
Chillfile::Model::SyncFile.new(:paths => ["./foo/bar.baz"], :checksum => "13123f6casdd03480be5f781ee12312asdasd232").save.persisted?.should be_true
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should be possible to find using a checksum" do
|
15
|
-
Chillfile::Model::SyncFile.new(:paths => ["./foo/bar.baz", "./woo_lands"], :checksum => "13123f6casdd03480be5f781ee12312asdasd232").save
|
16
|
-
Chillfile::Model::SyncFile.by_checksum(:key => "13123f6casdd03480be5f781ee12312asdasd232").first[:paths].should == ["./foo/bar.baz", "./woo_lands"]
|
17
|
-
end
|
18
|
-
end
|