dircat 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # DIRCAT
2
+
3
+ Dircat build, starting from a directory, a catalog with files meta information (path, timestamp, md5. ...), so it
4
+ is possible to compare this catalog with another directory to detect duplicate, file change, and so.
5
+ This utilities could be utilized as help to backup a directory or to find duplicates
6
+
7
+ ### dircat build
8
+
9
+ Build a catalog from a directory
10
+
11
+ Ex.: dircat build -o <catalog_name> dir1
12
+
13
+ builds catalog from directory dir1.
14
+
15
+ ### dircat cfr
16
+
17
+ Compare two catalogs or directories
18
+
19
+ Ex. dircat diff spec/fixtures/dir1 spec/fixtures/dir2
20
+
21
+ ### dircat query
22
+
23
+ query the contents of catalog
24
+
25
+ Ex.: ruby bin/dircat query cat_dir1.yaml
26
+ Ex.: ruby bin/dircat query cat_dir1.yaml duplicates
27
+
28
+ ### INSTALL:
29
+
30
+ sudo gem install dircat
31
+
32
+ or
33
+
34
+ sudo gem install gf-dircat -s gems.github.com
35
+
36
+ ### Copyright
37
+
38
+ Copyright (c) 2009-2010 tokiro.oyama@gmail.com See LICENSE for details.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 5
4
+ :patch: 6
5
5
  :build:
data/dircat.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dircat}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tokiro"]
12
- s.date = %q{2011-01-16}
12
+ s.date = %q{2011-01-22}
13
13
  s.default_executable = %q{dircat}
14
14
  s.description = %q{
15
15
  command line utilites to manage catalogs of directory
@@ -17,12 +17,12 @@ Gem::Specification.new do |s|
17
17
  s.email = %q{tokiro.oyama@gmail.com}
18
18
  s.executables = ["dircat"]
19
19
  s.extra_rdoc_files = [
20
- "LICENSE",
21
- "README.rdoc"
20
+ "LICENSE.txt",
21
+ "README.md"
22
22
  ]
23
23
  s.files = [
24
- "LICENSE",
25
- "README.rdoc",
24
+ "LICENSE.txt",
25
+ "README.md",
26
26
  "Rakefile",
27
27
  "VERSION.yml",
28
28
  "dircat.gemspec",
@@ -36,7 +36,9 @@ Gem::Specification.new do |s|
36
36
  "lib/dircat/entry.rb",
37
37
  "lib/dircat/extension_md5.rb",
38
38
  "lib/dircat/extension_numeric.rb",
39
- "lib/dircat/report.rb"
39
+ "tasks/jeweler.rake",
40
+ "tasks/rspec.rake",
41
+ "tasks/yard.rake"
40
42
  ]
41
43
  s.homepage = %q{http://github.com/tokiro/dircat}
42
44
  s.require_paths = ["lib"]
@@ -68,16 +70,16 @@ Gem::Specification.new do |s|
68
70
  s.specification_version = 3
69
71
 
70
72
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
71
- s.add_runtime_dependency(%q<treevisitor>, [">= 0"])
72
- s.add_runtime_dependency(%q<optparse-command>, ["= 0.1.0"])
73
+ s.add_runtime_dependency(%q<treevisitor>, ["= 0.1.6"])
74
+ s.add_runtime_dependency(%q<optparse-command>, ["= 0.1.1"])
73
75
  s.add_development_dependency(%q<jeweler>, [">= 0"])
74
76
  s.add_development_dependency(%q<rake>, [">= 0"])
75
77
  s.add_development_dependency(%q<yard>, [">= 0"])
76
78
  s.add_development_dependency(%q<bundler>, [">= 0"])
77
79
  s.add_development_dependency(%q<rspec>, [">= 0"])
78
80
  else
79
- s.add_dependency(%q<treevisitor>, [">= 0"])
80
- s.add_dependency(%q<optparse-command>, ["= 0.1.0"])
81
+ s.add_dependency(%q<treevisitor>, ["= 0.1.6"])
82
+ s.add_dependency(%q<optparse-command>, ["= 0.1.1"])
81
83
  s.add_dependency(%q<jeweler>, [">= 0"])
82
84
  s.add_dependency(%q<rake>, [">= 0"])
83
85
  s.add_dependency(%q<yard>, [">= 0"])
@@ -85,8 +87,8 @@ Gem::Specification.new do |s|
85
87
  s.add_dependency(%q<rspec>, [">= 0"])
86
88
  end
87
89
  else
88
- s.add_dependency(%q<treevisitor>, [">= 0"])
89
- s.add_dependency(%q<optparse-command>, ["= 0.1.0"])
90
+ s.add_dependency(%q<treevisitor>, ["= 0.1.6"])
91
+ s.add_dependency(%q<optparse-command>, ["= 0.1.1"])
90
92
  s.add_dependency(%q<jeweler>, [">= 0"])
91
93
  s.add_dependency(%q<rake>, [">= 0"])
92
94
  s.add_dependency(%q<yard>, [">= 0"])
data/lib/dircat/cat.rb CHANGED
@@ -5,51 +5,64 @@ module DirCat
5
5
 
6
6
  attr_reader :dirname
7
7
 
8
- # data di creazione
9
- attr_reader :ctime
10
- attr_writer :ctime
8
+ # creation date
9
+ attr_accessor :ctime
11
10
 
12
- def initialize( options = {} )
13
- @verbose_level = options.delete(:verbose) || 0
14
- @dirname = ""
15
- @ctime = DateTime.now
16
- @entries = Array.new
17
- @md5ToEntries = Hash.new
11
+ def initialize(options = {})
12
+ @verbose_level = options.delete(:verbose) || 0
13
+ @dirname = ""
14
+ @ctime = DateTime.now
15
+ @entries = Array.new
16
+ @md5_to_entries = Hash.new
17
+ end
18
+
19
+ def self.from_dir(dirname, options = {})
20
+ new(options).from_dir(dirname)
18
21
  end
19
22
 
20
- def from_dir( dirname )
21
- if not File.directory?( dirname )
23
+ def self.from_file(filename, options = {})
24
+ new(options).from_file(filename)
25
+ end
26
+
27
+ def self.load(file_or_dir, options = {})
28
+ new(options).load(file_or_dir)
29
+ end
30
+
31
+ def from_dir(dirname)
32
+ if not File.directory?(dirname)
22
33
  raise "'#{dirname}' is not a directory or doesn't exists"
23
34
  end
24
35
  @dirname = File.expand_path dirname
25
36
  @ctime = DateTime.now
26
- _loadfromdir
37
+ _load_from_dir
27
38
  self
28
39
  end
29
40
 
30
- def self.from_dir( dirname, options = {} )
31
- new(options).from_dir(dirname)
32
- end
33
-
34
- def from_file( filename )
35
- if not File.exist?( filename )
41
+ def from_file(filename)
42
+ if not File.exist?(filename)
36
43
  raise DirCatException.new, "'#{filename}' not exists"
37
44
  end
38
- dircat_ser = YAML::load( File.open( filename ) )
39
- @dirname = dircat_ser.dirname
40
- @ctime = dircat_ser.ctime
41
- dircat_ser.entries.each{ |entry_ser|
42
- add_entry( Entry.new.from_ser( entry_ser ) )
45
+ dircat_ser = YAML::load(File.open(filename))
46
+ @dirname = dircat_ser.dirname
47
+ @ctime = dircat_ser.ctime
48
+ dircat_ser.entries.each { |entry_ser|
49
+ add_entry(Entry.new.from_ser(entry_ser))
43
50
  }
44
51
  self
45
52
  end
46
53
 
47
- def self.from_file( filename, options = {} )
48
- new(options).from_file(filename)
54
+ def load(file_or_dir)
55
+ if File.directory?(file_or_dir)
56
+ from_dir(file_or_dir)
57
+ elsif File.exists?(file_or_dir)
58
+ from_file(file_or_dir)
59
+ else
60
+ raise DirCatException.new, "'#{file_or_dir}' not exists"
61
+ end
49
62
  end
50
63
 
51
64
  def to_ser
52
- dircat_ser = DirCatSer.new
65
+ dircat_ser = DirCatSer.new
53
66
  dircat_ser.version = 0.1
54
67
  dircat_ser.dirname = @dirname
55
68
  dircat_ser.ctime = @ctime
@@ -60,31 +73,46 @@ module DirCat
60
73
  dircat_ser
61
74
  end
62
75
 
63
- def _loadfromdir()
76
+ def _load_from_dir
64
77
  old_dirname = Dir.pwd
65
- Dir.chdir( @dirname )
78
+ Dir.chdir(@dirname)
66
79
  Dir["**/*"].each { |f|
67
- next if File.directory?( f )
80
+ next if File.directory?(f)
68
81
 
69
82
  if @verbose_level > 0
70
- cr = "\r"
83
+ cr = "\r"
71
84
  clear = "\e[K"
72
85
  print "#{cr}#{filename}#{clear}"
73
86
  end
74
87
 
75
- add_entry( Entry.new.from_filename( f ) )
88
+ add_entry(Entry.new.from_filename(f))
76
89
  }
77
90
  if @verbose_level > 0
78
91
  print "\n"
79
92
  end
80
- Dir.chdir( old_dirname )
93
+ Dir.chdir(old_dirname)
81
94
  self
82
95
  end
83
96
 
84
- def save_to( file )
97
+ def _load_from_dir_with_tree_visitor
98
+ dtw = DirTreeWalker.new("..")
99
+ dtw.run do
100
+ visit_leaf_node do |filename|
101
+ add_entry(Entry.new.from_filename(f))
102
+ if @verbose_level > 0
103
+ cr = "\r"
104
+ clear = "\e[K"
105
+ print "#{cr}#{filename}#{clear}"
106
+ end
107
+ end
108
+ end
109
+ self
110
+ end
111
+
112
+ def save_to(file)
85
113
  if file.kind_of?(String)
86
114
  begin
87
- File.open( file, "w" ) do |f|
115
+ File.open(file, "w") do |f|
88
116
  f.puts to_ser.to_yaml
89
117
  end
90
118
  rescue Errno::ENOENT
@@ -99,33 +127,36 @@ module DirCat
99
127
  @entries.size
100
128
  end
101
129
 
130
+ def empty?
131
+ @entries.size == 0
132
+ end
133
+
102
134
  def bytes
103
- @entries.inject(0) {|sum, entry| sum + entry.size }
135
+ @entries.inject(0) { |sum, entry| sum + entry.size }
104
136
  end
105
137
 
106
138
  def report
107
139
  dups = duplicates
108
- s = "Base dir: #@dirname\n" +
109
- "Nr. file: #{size}\n" +
110
- "Bytes: #{bytes.with_separator}"
140
+ s = "Base dir: #{@dirname}\n"
141
+ s += "Nr. file: #{size}"
111
142
  if duplicates.size > 0
112
- s+= "\n duplicates #{dups.size}"
143
+ s+= " (duplicates #{dups.size})"
113
144
  end
145
+ s += "\nBytes: #{bytes.with_separator}"
114
146
  s
115
147
  end
116
148
 
117
- def add_entry( e )
118
- @entries.push( e )
119
- if @md5ToEntries.has_key?( e.md5 )
120
- # puts "Entry duplicata!!!"
121
- @md5ToEntries[ e.md5 ].push( e )
149
+ def add_entry(e)
150
+ @entries.push(e)
151
+ if @md5_to_entries.has_key?(e.md5)
152
+ @md5_to_entries[e.md5].push(e)
122
153
  else
123
- @md5ToEntries[ e.md5 ] = [ e ]
154
+ @md5_to_entries[e.md5] = [e]
124
155
  end
125
156
  end
126
157
 
127
- def contains( e )
128
- @md5ToEntries.has_key?( e.md5 )
158
+ def contains(e)
159
+ @md5_to_entries.has_key?(e.md5)
129
160
  end
130
161
 
131
162
  def -(s)
@@ -138,9 +169,9 @@ module DirCat
138
169
 
139
170
  def duplicates
140
171
  list = []
141
- @md5ToEntries.each_value do |ee|
172
+ @md5_to_entries.each_value do |ee|
142
173
  next if ee.size < 2
143
- list.push( ee )
174
+ list.push(ee)
144
175
  end
145
176
  list
146
177
  end
@@ -155,10 +186,10 @@ module DirCat
155
186
  DirCat::report(@entries, :md5, :name, :path)
156
187
  end
157
188
 
158
- def fmt_ruby( dst )
189
+ def fmt_ruby(dst)
159
190
  puts "require 'fileutils'"
160
191
  @entries.each { |entry|
161
- src = File.join( @dirname, entry.path, entry.name );
192
+ src = File.join(@dirname, entry.path, entry.name);
162
193
  puts "FileUtils.cp( \"#{src}\", \"#{dst}\" )"
163
194
  }
164
195
  end
@@ -175,12 +206,12 @@ module DirCat
175
206
  r = "require 'fileutils'\n"
176
207
  duplicates.each { |entries|
177
208
  flg_first = true
178
- r += "\n"
179
- entries.each{ |entry|
180
- src = File.join( @dirname, entry.path, entry.name );
209
+ r += "\n"
210
+ entries.each { |entry|
211
+ src = File.join(@dirname, entry.path, entry.name);
181
212
  if flg_first
182
213
  flg_first = false
183
- r += "# FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
214
+ r += "# FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
184
215
  else
185
216
  r += "FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
186
217
  end
@@ -1,17 +1,17 @@
1
1
  # -*- coding: utf-8 -*-
2
-
3
- class CliDirCat < CliMain
2
+ class CliDirCat < OptParseCommand::CliMain
4
3
 
5
4
  def self.command
6
5
  "dircat"
7
6
  end
8
7
 
9
8
  def self.description
10
- "Simple set of command to build catalogs of files."
9
+ "Simple set of command to build catalogs of files.\n" +
10
+ "Code https://github.com/tokiro/dircat. Feedback to tokiro.oyama@gmail.com"
11
11
  end
12
12
 
13
13
  def self.version
14
- puts DirCat::version
14
+ DirCat::version
15
15
  end
16
16
 
17
17
  def defaults
@@ -1,6 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- class CommandBuild < CliCommand
3
+ #
4
+ # Build a catalogue starting from a directory
5
+ #
6
+ class CommandBuild < OptParseCommand::CliCommand
4
7
 
5
8
  def self.command
6
9
  "build"
@@ -14,10 +17,17 @@ class CommandBuild < CliCommand
14
17
  "Usage: dircat build [options]"
15
18
  end
16
19
 
17
- def opt_parser(options)
18
- opt_parser = super(options)
20
+ def defaults(options)
21
+ options.force = false
22
+ end
23
+
24
+ def option_parser(options)
25
+ parser = super(options)
26
+ parser.on("-f", "--force", "force write on existent file") do |v|
27
+ options.force = true
28
+ end
19
29
 
20
- opt_parser.on("-o [FILE]", "--output [FILE]", String) do |v|
30
+ parser.on("-o [FILE]", "--output [FILE]", String) do |v|
21
31
  if options.output
22
32
  puts "only one file of output can be used"
23
33
  options.exit = true
@@ -25,17 +35,13 @@ class CommandBuild < CliCommand
25
35
  options.output = v
26
36
  end
27
37
 
28
- opt_parser.on("-f", "--force", "force write on existent file") do |v|
29
- options.force
30
- end
31
-
32
- opt_parser
38
+ parser
33
39
  end
34
40
 
35
41
  def exec(options, rest)
36
42
  if rest.length < 1
37
- puts "directory (from which build catalog) is missing"
38
- puts "-h to print help"
43
+ $stderr.puts "directory (from which build catalog) is missing"
44
+ $stderr.puts "-h to print help"
39
45
  return false
40
46
  end
41
47
 
@@ -44,7 +50,7 @@ class CommandBuild < CliCommand
44
50
  cat_opts = {}
45
51
 
46
52
  if not FileTest.directory?(dirname)
47
- puts "'#{dirname}' not exists or is not a directory"
53
+ $stderr.puts "'#{dirname}' not exists or is not a directory"
48
54
  return 0
49
55
  end
50
56
 
@@ -66,17 +72,14 @@ class CommandBuild < CliCommand
66
72
  filename = "cat_" + File.basename(dirname) + "_" + Date.today.strftime("%Y%m%d") + ".yaml"
67
73
  end
68
74
  if File.exist?(filename) and not options.force
69
- puts "catalog '#{filename}' exists use --force or -f to overwrite"
75
+ $stderr.puts "catalog '#{filename}' exists use --force or -f to overwrite"
70
76
  return 0
71
77
  end
72
- output = File.open(filename, "w")
73
78
 
79
+ output = File.open(filename, "w")
74
80
  start_time = Time.now
75
81
  s = Cat.from_dir(dirname)
76
82
  end_time = Time.now
77
-
78
- # s.pr
79
- # f.puts s.to_yaml
80
83
  s.save_to(output)
81
84
 
82
85
  if output != $stdout
@@ -84,118 +87,9 @@ class CommandBuild < CliCommand
84
87
  end
85
88
  $stderr.puts s.report
86
89
  $stderr.puts "elapsed: #{end_time - start_time}"
90
+ $stderr.puts "written to #{filename}" if output != $stdout
87
91
 
88
92
  true
89
93
  end
90
94
 
91
95
  end
92
-
93
-
94
- #module DirCat
95
- # #
96
- # # Build a catalogue starting from a directory
97
- # #
98
- # class DirCatBuild
99
- #
100
- # def self.run
101
- # return self.new.parse_args( ARGV )
102
- # end
103
- #
104
- # def parse_args( argv )
105
- # options = { :verbose => true, :force => false }
106
- # opts = OptionParser.new
107
- # opts.banner << "\n"
108
- # opts.banner << "Build a catalogue starting from a directory\n";
109
- # opts.banner << "\n"
110
- #
111
- # opts.on("-h", "--help", "Print this message") do
112
- # puts opts
113
- # return 0
114
- # end
115
- #
116
- # opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
117
- # options[:verbose] = v
118
- # end
119
- #
120
- # opts.on("-q", "--quiet", "quiet mode as --no-verbose") do |v|
121
- # options[:verbose] = false
122
- # end
123
- #
124
- # opts.on("-f", "--force", "force write on existent file") do |v|
125
- # options[:force] = true
126
- # end
127
- #
128
- # opts.on("-o [FILE]", "--output [FILE]",String) do |v|
129
- # if options[:output]
130
- # puts "only one file of output can be used"
131
- # return 1
132
- # end
133
- # options[:output] = v
134
- # end
135
- #
136
- # rest = opts.parse(argv)
137
- #
138
- # # p options
139
- # # p ARGV
140
- #
141
- # if rest.length < 1
142
- # puts "directory (from which build catalog) is missing"
143
- # puts "-h to print help"
144
- # return 0
145
- # end
146
- #
147
- # dirname = rest[0]
148
- # dirname = File.expand_path( dirname )
149
- # cat_opts = {}
150
- #
151
- # if not FileTest.directory?(dirname)
152
- # puts "'#{dirname}' not exists or is not a directory"
153
- # return 0
154
- # end
155
- #
156
- # #
157
- # # option verbose
158
- # #
159
- #
160
- # if options.has_key?(:verbose)
161
- # if options[:verbose]
162
- # cat_opts[:verbose_level] = 1
163
- # end
164
- # end
165
- #
166
- # #
167
- # # option: output, force
168
- # #
169
- # output = $stdout
170
- # if options.has_key?(:output)
171
- # if options[:output]
172
- # filename = options[:output]
173
- # else
174
- # filename = "cat_" + File.basename( dirname ) + "_" + Date.today.strftime("%Y%m%d") + ".yaml"
175
- # end
176
- # if File.exist?(filename) and not options[:force]
177
- # puts "catalog '#{filename}' exists use --force or -f to overwrite"
178
- # return 0
179
- # end
180
- # output = File.open(filename, "w")
181
- # end
182
- #
183
- # start_time = Time.now
184
- # s = Cat.from_dir(dirname)
185
- # end_time = Time.now
186
- #
187
- # # s.pr
188
- # # f.puts s.to_yaml
189
- # s.save_to( output )
190
- #
191
- # if output != $stdout
192
- # output.close
193
- # end
194
- # $stderr.puts s.report
195
- # $stderr.puts "elapsed: #{end_time - start_time}"
196
- #
197
- # 0
198
- # end
199
- #
200
- # end
201
- #end
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- class CommandDiff < CliCommand
3
+ class CommandDiff < OptParseCommand::CliCommand
4
4
 
5
5
  def self.command
6
6
  "diff"
@@ -15,15 +15,15 @@ class CommandDiff < CliCommand
15
15
  Usage: dircat diff [options] <filedircat1> <filedircat2>
16
16
  diff first catalog from second (<filedircat1> - <filedircat2>)
17
17
  and then print the difference with the format specified on output
18
- EOS
18
+ EOS
19
19
  end
20
-
21
- def opt_parser(options)
22
- opt_parser = super(options)
23
- opt_parser.on("-f FORMAT", "--fmt FORMAT", "formato") do |v|
20
+
21
+ def option_parser(options)
22
+ parser = super(options)
23
+ parser.on("-f FORMAT", "--fmt FORMAT", "format (simple, ruby)") do |v|
24
24
  options.format = v
25
25
  end
26
- opt_parser
26
+ parser
27
27
  end
28
28
 
29
29
  def exec(options, rest)
@@ -36,123 +36,50 @@ and then print the difference with the format specified on output
36
36
  cat_filename1 = rest[0]
37
37
  cat_filename2 = rest[1]
38
38
 
39
- if File.exists?( cat_filename1 )
40
- puts "load catalog #{cat_filename1}"
41
- s1 = Cat.from_file(cat_filename1)
42
- elsif File.directory?(cat_filename1)
39
+ #
40
+ # process first argument
41
+ #
42
+ if File.directory?(cat_filename1)
43
43
  puts "build first set from directory #{cat_filename1}"
44
44
  s1 = Cat.from_dir(cat_filename1)
45
+ elsif File.exists?(cat_filename1)
46
+ puts "load catalog #{cat_filename1}"
47
+ s1 = Cat.from_file(cat_filename1)
45
48
  else
46
49
  puts "#{cat_filename1} is not a catalog file or directory"
47
50
  return 1
48
51
  end
49
52
 
50
- if File.exists?( cat_filename2 )
51
- puts "load catalog #{cat_filename2}"
52
- s2 = Cat.from_file(cat_filename2)
53
- elsif File.directory?(cat_filename2)
53
+ #
54
+ # process second argument
55
+ #
56
+ if File.directory?(cat_filename2)
54
57
  puts "build first set from directory #{cat_filename2}"
55
58
  s2 = Cat.from_dir(cat_filename2)
59
+ elsif File.exists?(cat_filename2)
60
+ puts "load catalog #{cat_filename2}"
61
+ s2 = Cat.from_file(cat_filename2)
56
62
  else
57
63
  puts "#{cat_filename2} is not a catalog file or directory"
58
64
  return 1
59
65
  end
60
66
 
61
- puts "build difference"
62
67
  s3 = s1 - s2
63
68
 
64
- case options.format
65
- when "simple"
66
- s3.fmt_simple
67
- when "ruby"
68
- s3.fmt_ruby( "." )
69
+ if s3.empty?
70
+ puts "no difference (first catalog contains all file of second catalog)"
69
71
  else
70
- s3.fmt_simple
72
+ puts "differences (file in first catalog not contained in the second catalog)"
73
+ case options.format
74
+ when "simple"
75
+ s3.fmt_simple
76
+ when "ruby"
77
+ s3.fmt_ruby(".")
78
+ else
79
+ s3.fmt_simple
80
+ end
71
81
  end
72
82
  true
73
83
  end
74
84
 
75
85
  end
76
-
77
-
78
- #module DirCat
79
- #
80
- # class DirCatDiff
81
- #
82
- # def self.run
83
- # return self.new.parse_args( ARGV )
84
- # end
85
- #
86
- # def parse_args( args )
87
- # options = {}
88
- # opts = OptionParser.new
89
- # opts.banner =
90
- # "Usage: dircat_cfr.rb [options] <filedircat1> <filedircat2>\n\n" +
91
- # "fa la differenza fra il primo catalog e il secondo\n" +
92
- # "<filedircat1> - <filedircat2>\n" +
93
- # "e stampa sull'output con un formato\n"
94
- #
95
- # opts.on("-h", "--help", "Print this message") do
96
- # puts opts
97
- # return 0
98
- # end
99
- #
100
- # opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
101
- # options[:verbose] = v
102
- # end
103
- #
104
- # opts.on("-f FORMAT", "--fmt FORMAT", "formato") do |v|
105
- # options[:fmt] = v
106
- # end
107
- # rest = opts.parse( args )
108
- #
109
- # # p options
110
- # # p ARGV
111
- #
112
- # if rest.length < 2
113
- # puts "you must provide two args (catalogs or directory)"
114
- # puts "dircat_cfr -h to print help"
115
- # exit
116
- # end
117
- #
118
- # cat_filename1 = rest[0]
119
- # cat_filename2 = rest[1]
120
- #
121
- # if File.exists?( cat_filename1 )
122
- # puts "load catalog #{cat_filename1}"
123
- # s1 = Cat.from_file(cat_filename1)
124
- # elsif File.directory?(cat_filename1)
125
- # puts "build first set from directory #{cat_filename1}"
126
- # s1 = Cat.from_dir(cat_filename1)
127
- # else
128
- # puts "#{cat_filename1} is not a catalog file or directory"
129
- # return 1
130
- # end
131
- #
132
- # if File.exists?( cat_filename2 )
133
- # puts "load catalog #{cat_filename2}"
134
- # s2 = Cat.from_file(cat_filename2)
135
- # elsif File.directory?(cat_filename2)
136
- # puts "build first set from directory #{cat_filename2}"
137
- # s2 = Cat.from_dir(cat_filename2)
138
- # else
139
- # puts "#{cat_filename2} is not a catalog file or directory"
140
- # return 1
141
- # end
142
- #
143
- # puts "build difference"
144
- # s3 = s1 - s2
145
- #
146
- # case options[:fmt]
147
- # when "simple"
148
- # s3.fmt_simple
149
- # when "ruby"
150
- # s3.fmt_ruby( "." )
151
- # else
152
- # s3.fmt_simple
153
- # end
154
- # 0
155
- # end
156
- # end
157
- #end
158
- #
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- class CommandQuery < CliCommand
3
+ class CommandQuery < OptParseCommand::CliCommand
4
4
 
5
5
  def self.command
6
6
  "query"
@@ -11,25 +11,7 @@ class CommandQuery < CliCommand
11
11
  end
12
12
 
13
13
  def self.usage
14
- "Usage: dircat-query [options] <filedircat> [<command>]"
15
- end
16
-
17
- def opt_parser(options)
18
- opt_parser = super(options)
19
-
20
- opt_parser.on("-o [FILE]", "--output [FILE]", String) do |v|
21
- if options[:output]
22
- puts "only one file of output can be used"
23
- return 1
24
- end
25
- options.output = v
26
- end
27
-
28
- opt_parser.on("-f", "--force", "force write on existent file") do |v|
29
- options[:force] = true
30
- end
31
-
32
- opt_parser
14
+ "Usage: query [options] <catalog> [<method>]"
33
15
  end
34
16
 
35
17
  def exec(options, rest)
@@ -41,7 +23,7 @@ class CommandQuery < CliCommand
41
23
 
42
24
  cat_opts = {}
43
25
  cat_filename = rest[0]
44
- unless File.exists?(cat_filename)
26
+ if !File.exists?(cat_filename) or File.directory?(cat_filename)
45
27
  puts "first args must be a catalogue"
46
28
  return 1
47
29
  end
@@ -66,73 +48,3 @@ class CommandQuery < CliCommand
66
48
  end
67
49
 
68
50
  end
69
-
70
-
71
- #module DirCat
72
- #
73
- # class DirCatQuery
74
- #
75
- # def self.run
76
- # return self.new.parse_args( ARGV)
77
- # end
78
- #
79
- # def parse_args( args )
80
- # options = {}
81
- # opts = OptionParser.new
82
- # opts.banner =
83
- # "show info on dircat catalogs\n"
84
- #
85
- # opts.on("--version", "show the dircat version") do
86
- # puts "dircat version #{DirCat::version}"
87
- # return 0
88
- # end
89
- #
90
- # opts.on("-h", "--help", "Print this message") do
91
- # puts opts
92
- # return 0
93
- # end
94
- #
95
- # opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
96
- # options[:verbose] = v
97
- # end
98
- #
99
- # rest = opts.parse( args )
100
- #
101
- # # p options
102
- # # p ARGV
103
- #
104
- # if rest.length < 1
105
- # puts "missing catalog!"
106
- # puts "-h to print help"
107
- # return 0
108
- # end
109
- #
110
- # cat_opts = {}
111
- # cat_filename = rest[0]
112
- # unless File.exists?(cat_filename)
113
- # puts "first args must be a catalogue"
114
- # return 1
115
- # end
116
- #
117
- # if rest.length > 1
118
- # command = rest[1]
119
- # else
120
- # command = "report"
121
- # end
122
- #
123
- # #
124
- # # option verbose
125
- # #
126
- # if options.has_key?(:verbose)
127
- # if options[:verbose]
128
- # cat_opts[:verbose_level] = 1
129
- # end
130
- # end
131
- #
132
- # s = Cat.from_file( cat_filename, cat_opts )
133
- #
134
- # puts s.send( command.to_sym )
135
- # 0
136
- # end
137
- # end
138
- #end
data/lib/dircat/entry.rb CHANGED
@@ -58,4 +58,4 @@ module DirCat
58
58
  class DirCatException < RuntimeError
59
59
  end
60
60
 
61
- end
61
+ end
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  #
3
- # calculate md5 of big files, found on usenet
3
+ # calculate md5 of big files, snipped found on usenet
4
4
  #
5
5
 
6
6
  if RUBY_VERSION =~ /1\.8/
@@ -1,12 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  class Numeric
3
3
  #
4
- # ritorna una stringa con le migliaia serparate da <separator>
4
+ # returns a string separated by the thousands <separator>
5
5
  # es.: 100000 -> 1.000.000
6
6
  #
7
- #
8
- # copiata da http://wiki.rubygarden.org/Ruby/page/show/FixNumFormat
9
- #
10
7
  def with_separator( separator = ',', length = 3 )
11
8
  splitter = Regexp.compile "(\\d{#{length}})"
12
9
  before, after = self.to_s.split('.')
data/lib/dircat.rb CHANGED
@@ -7,19 +7,27 @@ require 'tmpdir'
7
7
  require 'yaml'
8
8
  require 'ostruct'
9
9
  require 'optparse'
10
+ require 'pp'
10
11
 
11
12
  #
12
13
  # rubygems
13
14
  #
15
+ gem "optparse-command", "0.1.1"
14
16
  require 'optparse-command'
15
17
 
18
+ gem "treevisitor", "0.1.6"
19
+ require 'treevisitor'
20
+
21
+ #
22
+ # dircat
23
+ #
16
24
  module DirCat
17
25
  def self.version
18
- cwd = File.dirname( __FILE__)
19
- yaml = YAML.load_file(cwd + '/../VERSION.yml')
20
- major = (yaml['major'] || yaml[:major]).to_i
21
- minor = (yaml['minor'] || yaml[:minor]).to_i
22
- patch = (yaml['patch'] || yaml[:patch]).to_i
26
+ filename = File.join(File.dirname(__FILE__), %w{.. VERSION.yml})
27
+ yaml = YAML.load_file(filename)
28
+ major = (yaml['major'] || yaml[:major]).to_i
29
+ minor = (yaml['minor'] || yaml[:minor]).to_i
30
+ patch = (yaml['patch'] || yaml[:patch]).to_i
23
31
  "#{major}.#{minor}.#{patch}"
24
32
  end
25
33
  end
@@ -29,7 +37,6 @@ require 'dircat/extension_numeric'
29
37
 
30
38
  require 'dircat/cat'
31
39
  require 'dircat/entry'
32
- require 'dircat/report'
33
40
 
34
41
  require 'dircat/cli/cli_dircat'
35
42
  require 'dircat/cli/command_build'
@@ -1,3 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+
1
3
  require File.expand_path( File.join(File.dirname(__FILE__), "..", "spec_helper") )
2
4
 
3
5
  #
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
3
 
3
4
  describe CliDirCat do
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
3
 
3
4
  describe CommandBuild do
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.expand_path( File.join(File.dirname(__FILE__), "..", "..", "spec_helper") )
2
3
 
3
4
  describe CommandDiff do
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.expand_path( File.join(File.dirname(__FILE__), "..", "..", "spec_helper") )
2
3
 
3
4
  describe CommandQuery do
@@ -1,12 +1,8 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require File.expand_path( File.join(File.dirname(__FILE__), "..", "spec_helper") )
2
-
3
3
  describe "MD5" do
4
-
5
- TEST_FILE = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib", "dircat", "extension_md5.rb" ) )
6
-
7
4
  it "test_simple_md5" do
8
- file_name = File.join( TEST_FILE )
9
- MD5.file( file_name ).to_s.should == "eb29e47aba9693862895688422171221"
5
+ file_name = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib", "dircat", "extension_md5.rb" ) )
6
+ MD5.file( file_name ).to_s.should == "ab4bc9cf679dede00672f2246e204b06"
10
7
  end
11
-
12
8
  end
@@ -1,3 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+
1
3
  require File.expand_path( File.join(File.dirname(__FILE__), "..", "spec_helper") )
2
4
 
3
5
  describe "TCNumeric" do
data/spec/spec_helper.rb CHANGED
@@ -1,17 +1,14 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $:.unshift(File.dirname(__FILE__))
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "stringio"
4
+ require 'ostruct'
3
5
 
6
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
7
  require 'dircat'
5
8
  include DirCat
6
9
 
7
10
  TEST_DIR = File.expand_path( File.join( File.dirname(__FILE__), "fixtures" ) )
8
11
 
9
- # Spec::Runner.configure do |config|
10
- # end
11
-
12
- # require 'test/unit'
13
- require "stringio"
14
-
15
12
  def with_stdout_captured
16
13
  old_stdout = $stdout
17
14
  out = StringIO.new
@@ -0,0 +1,44 @@
1
+ # -*- coding: utf-8 -*-
2
+ begin
3
+ require 'jeweler'
4
+ Jeweler::Tasks.new do |gem|
5
+
6
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
7
+ gem.name = "dircat"
8
+ gem.summary = "command line utilites to manage catalogs of directory"
9
+ gem.description = %Q{
10
+ command line utilites to manage catalogs of directory
11
+ }
12
+ gem.authors = ["Tokiro"]
13
+ gem.email = "tokiro.oyama@gmail.com"
14
+ gem.homepage = "http://github.com/tokiro/dircat"
15
+
16
+ #
17
+ # dependencies, automatically loaded from Gemfile
18
+ #
19
+
20
+ #
21
+ # bin
22
+ #
23
+ gem.executables = %w{ dircat }
24
+
25
+ #
26
+ # files
27
+ #
28
+ gem.files = %w{LICENSE.txt README.md Rakefile VERSION.yml dircat.gemspec}
29
+ gem.files.concat Dir['lib/**/*.rb']
30
+ gem.files.concat Dir['examples/*.rb']
31
+ gem.files.concat Dir['tasks/*.rake']
32
+
33
+ #
34
+ # test files
35
+ #
36
+ gem.test_files = Dir['spec/**/*.rb']
37
+ gem.test_files.concat Dir['spec/fixtures/**/*']
38
+
39
+ end
40
+
41
+ Jeweler::GemcutterTasks.new
42
+ rescue LoadError
43
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
44
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,14 @@
1
+ # -*- coding: utf-8 -*-
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = ["--color", "--format", "spec", '--backtrace']
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ end
10
+ rescue LoadError
11
+ puts "rspec (or a dependency) not available. Install it with: sudo gem install jeweler"
12
+ end
13
+
14
+
data/tasks/yard.rake ADDED
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ begin
3
+ require 'yard'
4
+ YARD::Rake::YardocTask.new do |t|
5
+ t.files = ['lib/**/*.rb']
6
+ t.options = [
7
+ '--readme', 'README.md',
8
+ '--any',
9
+ '--extra',
10
+ '--opts'
11
+ ]
12
+ end
13
+ rescue LoadError
14
+ puts "Yard (or a dependency) not available. Install it with: sudo gem install jeweler"
15
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tokiro
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-16 00:00:00 +01:00
17
+ date: 2011-01-22 00:00:00 +01:00
18
18
  default_executable: dircat
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -22,11 +22,13 @@ dependencies:
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
23
23
  none: false
24
24
  requirements:
25
- - - ">="
25
+ - - "="
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
28
  - 0
29
- version: "0"
29
+ - 1
30
+ - 6
31
+ version: 0.1.6
30
32
  type: :runtime
31
33
  prerelease: false
32
34
  version_requirements: *id001
@@ -40,8 +42,8 @@ dependencies:
40
42
  segments:
41
43
  - 0
42
44
  - 1
43
- - 0
44
- version: 0.1.0
45
+ - 1
46
+ version: 0.1.1
45
47
  type: :runtime
46
48
  prerelease: false
47
49
  version_requirements: *id002
@@ -117,11 +119,11 @@ executables:
117
119
  extensions: []
118
120
 
119
121
  extra_rdoc_files:
120
- - LICENSE
121
- - README.rdoc
122
+ - LICENSE.txt
123
+ - README.md
122
124
  files:
123
- - LICENSE
124
- - README.rdoc
125
+ - LICENSE.txt
126
+ - README.md
125
127
  - Rakefile
126
128
  - VERSION.yml
127
129
  - dircat.gemspec
@@ -135,7 +137,9 @@ files:
135
137
  - lib/dircat/entry.rb
136
138
  - lib/dircat/extension_md5.rb
137
139
  - lib/dircat/extension_numeric.rb
138
- - lib/dircat/report.rb
140
+ - tasks/jeweler.rake
141
+ - tasks/rspec.rake
142
+ - tasks/yard.rake
139
143
  - spec/dircat/cat_spec.rb
140
144
  - spec/dircat/cli/cli_dircat_spec.rb
141
145
  - spec/dircat/cli/command_build_spec.rb
data/README.rdoc DELETED
@@ -1,36 +0,0 @@
1
- = DIRCAT
2
-
3
- Utilities for manage catalogs of files and directory.
4
- This utilities can be utilized as help to backup a directory.
5
-
6
- === dircat-build
7
-
8
- Build a catalog from a directory
9
-
10
- Ex.: dircat-build -o <catalog_name> dir1
11
-
12
- builds catalog from directory dir1.
13
-
14
- === dircat-cfr
15
-
16
- Compare two catalogs
17
-
18
- === dircat-query
19
-
20
- show the contents of catalog
21
-
22
- == REQUIREMENTS:
23
-
24
- * utilizza la libreria abstract di kwartz
25
-
26
- == INSTALL:
27
-
28
- sudo gem install dircat
29
-
30
- or
31
-
32
- sudo gem install gf-dircat -s gems.github.com
33
-
34
- == Copyright
35
-
36
- Copyright (c) 2009-2010 tokiro. See LICENSE for details.
data/lib/dircat/report.rb DELETED
@@ -1,62 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module DirCat
3
-
4
- # From http://gist.github.com/72234
5
- #
6
- # mysql-style output for an array of ActiveRecord objects
7
- #
8
- # Usage:
9
- # report(records) # displays report with all fields
10
- # report(records, :field1, :field2, ...) # displays report with given fields
11
- #
12
- # Example:
13
- # >> report(records, :id, :amount, :created_at)
14
- # +------+-----------+--------------------------------+
15
- # | id | amount | created_at |
16
- # +------+-----------+--------------------------------+
17
- # | 8301 | $12.40 | Sat Feb 28 09:20:47 -0800 2009 |
18
- # | 6060 | $39.62 | Sun Feb 15 14:45:38 -0800 2009 |
19
- # | 6061 | $167.52 | Sun Feb 15 14:45:38 -0800 2009 |
20
- # | 6067 | $12.00 | Sun Feb 15 14:45:40 -0800 2009 |
21
- # | 6059 | $1,000.00 | Sun Feb 15 14:45:38 -0800 2009 |
22
- # +------+-----------+--------------------------------+
23
- # 5 rows in set
24
- #
25
- def self.report(items, *fields)
26
- # find max length for each field; start with the field names themselves
27
- max_len = Hash[*fields.map {|f| [f, f.to_s.length]}.flatten]
28
-
29
- items.each do |item|
30
- fields.each do |field|
31
- len = item.send(field).to_s.length
32
- max_len[field] = len if len > max_len[field]
33
- end
34
- end
35
-
36
- border = '+-' + fields.map {|f| '-' * max_len[f] }.join('-+-') + '-+'
37
- title_row = '| ' + fields.map {|f| sprintf("%-#{max_len[f]}s", f.to_s) }.join(' | ') + ' |'
38
-
39
- puts border
40
- puts title_row
41
- puts border
42
-
43
- items.each do |item|
44
- row = '| ' + fields.map {|f| sprintf("%-#{max_len[f]}s", item.send(f)) }.join(' | ') + ' |'
45
- puts row
46
- end
47
-
48
- puts border
49
- puts "#{items.length} rows in set\n"
50
- end
51
- end
52
-
53
- if __FILE__ == $0
54
- require 'ostruct'
55
- o1 = OpenStruct.new
56
- o1.a = 1
57
- o1.b = 2
58
-
59
- o2 = OpenStruct.new :a => 2, :b => 4
60
-
61
- DirCat::report( [o1, o2], :a, :b )
62
- end