checkm 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ checkm (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ rake (0.9.2.2)
11
+ rcov (0.9.11)
12
+ rspec (2.7.0)
13
+ rspec-core (~> 2.7.0)
14
+ rspec-expectations (~> 2.7.0)
15
+ rspec-mocks (~> 2.7.0)
16
+ rspec-core (2.7.1)
17
+ rspec-expectations (2.7.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.7.0)
20
+ yard (0.7.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler
27
+ checkm!
28
+ rake
29
+ rcov
30
+ rspec
31
+ yard
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.1.0
@@ -5,8 +5,7 @@ require File.join(File.dirname(__FILE__), "lib/checkm/version")
5
5
  Gem::Specification.new do |s|
6
6
  s.name = %q{checkm}
7
7
  s.version = Checkm::VERSION
8
- s.summary = "Checkm is a general-purpose text-based file manifest format "
9
- s.description = "Checkm is a general-purpose text-based file manifest format "
8
+ s.summary = "checkm implementation"
10
9
 
11
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
11
  s.authors = ["Chris Beer"]
@@ -1,5 +1,8 @@
1
1
  require 'checkm/manifest'
2
2
  require 'checkm/entry'
3
+ require 'digest/md5'
4
+ require 'digest/sha1'
5
+ require 'digest/sha2'
3
6
 
4
7
  module Checkm
5
8
  # Size (in bytes) to read (in chunks) to compute checksums
@@ -1,44 +1,69 @@
1
1
  require 'time'
2
2
  module Checkm
3
3
  class Entry
4
- BASE_FIELDS = ['sourcefileorurl', 'alg', 'digest', 'length', 'modtime', 'targetfileorurl']
4
+ def self.create file_or_path, args = {}
5
+ return file_or_path if file_or_path.is_a? Entry
6
+
7
+ options = args.delete(:options) || {}
8
+ path = options[:path] || Dir.pwd
9
+
10
+ return Checkm::Entry.new(file_or_path, options) if file_or_path.is_a? Array or file_or_path.is_a? Hash
11
+
12
+ file = file_or_path if file.is_a? File
13
+ file ||= File.open(File.expand_path(file_or_path, path))
14
+
15
+ args[:sourcefileorurl] = File.expand_path(file.path).gsub(path + "/", '') if file.respond_to? :path
16
+ args[:alg] ||= 'md5'
17
+ args[:digest] ||= Checkm.checksum(file, args[:alg])
18
+ args[:length] ||= File.size(file.path)
19
+ args[:modtime] ||= file.mtime.utc.xmlschema
20
+
21
+ Checkm::Entry.new(args, options)
22
+ end
23
+
5
24
  attr_reader :values
6
25
 
7
- def self.create path, args = {}
8
- base = args[:base] || Dir.pwd
9
- alg = args[:alg] || 'md5'
10
- file = File.new File.join(base, path)
11
-
12
- "%s | %s | %s | %s | %s | %s" % [path, alg, Checkm.checksum(file, alg), File.size(file.path), file.mtime.utc.xmlschema, nil]
26
+ def initialize source, options = {}
27
+ @fields = options[:fields] || Manifest::BASE_FIELDS
28
+ @path = options[:path]
29
+ @path ||= Dir.pwd
30
+
31
+ @values = case source
32
+ when Hash
33
+ tmp = {}
34
+ source.each { |k, v| tmp[k.to_s.downcase.to_sym] = v }
35
+ @fields.map { |k| source[k.to_s.downcase.to_sym] }
36
+ when Array
37
+ source
38
+ when String
39
+ source.split("|").map { |x| x.strip }
40
+ end
13
41
  end
14
-
15
- def initialize line, manifest = nil
16
- @line = line.strip
17
- @include = false
18
- @fields = BASE_FIELDS
19
- @fields = manifest.fields if manifest and manifest.fields
20
- @values = line.split('|').map { |s| s.strip }
21
- @manifest = manifest
42
+
43
+ def [] idx
44
+ values[idx] rescue nil
45
+ end
46
+
47
+ def []= idx, value
48
+ values[idx] = value rescue nil
49
+ end
50
+
51
+ def to_s
52
+ values.join(" | ")
22
53
  end
23
54
 
24
55
  def method_missing(sym, *args, &block)
25
- @values[@fields.index(sym.to_s.downcase) || BASE_FIELDS.index(sym.to_s.downcase)] rescue nil
56
+ self[@fields.map { |x| x.downcase }.index(sym.to_s.downcase) || Manifest::BASE_FIELDS.map { |x| x.downcase }.index(sym.to_s.downcase)]
26
57
  end
27
58
 
28
-
29
59
  def valid?
30
- return source_exists? && valid_checksum? && valid_multilevel? # xxx && valid_length? && valid_modtime?
60
+ return File.exists?(source) && valid_checksum? # xxx && valid_length? && valid_modtime?
31
61
  end
32
62
 
33
63
  private
34
64
  def source
35
65
  file = sourcefileorurl
36
- file = file[1..-1] if file =~ /^@/
37
- File.join(@manifest.path, file)
38
- end
39
-
40
- def source_exists?
41
- return File.exists? source
66
+ File.join(@path, file)
42
67
  end
43
68
 
44
69
  def valid_checksum?
@@ -55,10 +80,5 @@ module Checkm
55
80
  def valid_modtime?
56
81
  throw NotImplementedError
57
82
  end
58
-
59
- def valid_multilevel?
60
- return true unless sourcefileorurl =~ /^@/
61
- return Manifest.parse(open(source).read, File.dirname(source))
62
- end
63
83
  end
64
84
  end
@@ -1,48 +1,75 @@
1
1
  module Checkm
2
2
  class Manifest
3
- def self.parse str, args = {}
4
- Manifest.new str, args
5
- end
3
+ BASE_FIELDS = ['sourcefileorurl', 'alg', 'digest', 'length', 'modtime', 'targetfileorurl']
6
4
 
7
- attr_reader :version
8
- attr_reader :entries
5
+ attr_reader :source
6
+ attr_reader :eof
7
+
9
8
  attr_reader :fields
10
9
  attr_reader :path
10
+ attr_accessor :entries
11
11
 
12
- def initialize checkm, args = {}
12
+ def initialize str_io_or_file, args = {}
13
+ @source = str_io_or_file
13
14
  @args = args
14
- @version = nil
15
- @checkm = checkm
16
- @lines = checkm.split "\n"
17
15
  @entries = []
18
- @eof = false
19
- @fields= nil
16
+
17
+ @eof = args[:eof]
18
+ @version = args[:version]
19
+ @fields = args[:ields]
20
+
20
21
  @path = args[:path]
21
22
  @path ||= Dir.pwd
22
23
 
23
- parse_lines
24
- # xxx error on empty entries?
25
- @lines.unshift('#%checkm_0.7') and @version = '0.7' if @version.nil?
26
-
24
+ parse_lines(@source)
27
25
  end
28
-
26
+
27
+ def version
28
+ @version || "0.7"
29
+ end
30
+
31
+ def fields
32
+ @fields || BASE_FIELDS
33
+ end
34
+
29
35
  def valid?
30
36
  return true if @entries.empty?
31
37
  not @entries.map { |e| e.valid? }.any? { |b| b == false }
32
38
  end
33
39
 
34
- def add path, args = {}
35
- line = Checkm::Entry.create path, args
36
-
37
- Checkm::Manifest.new [@lines, line].flatten.join("\n"), @args
40
+ def add path_or_entry, args = {}
41
+ @entries << Checkm::Entry.create(path_or_entry, args)
38
42
  end
39
-
40
- def remove path
41
- Checkm::Manifest.new @lines.reject { |x| x =~ /^@?#{path}/ }.join("\n"), @args
43
+
44
+ alias_method :<<, :add
45
+
46
+ def remove path_or_entry
47
+ path = path_or_entry.path if path_or_entry.respond_to? :path
48
+ path ||= path_or_entry[0] if path_or_entry.is_a? Entry or path_or_entry.is_a? Array
49
+ path ||= path_or_entry
50
+
51
+ @entries.reject! { |x| x[0] =~ /^@?#{path}/ }
42
52
  end
53
+
54
+ alias_method :-, :remove
43
55
 
44
56
  def to_s
45
- @lines.join("\n")
57
+ lines = []
58
+
59
+ lines << "#%checkm_#{version}"
60
+ lines << "#%fields | #{ @fields.join(" | ") }" if @fields
61
+
62
+ lines += entries.map(&:to_s)
63
+
64
+ lines << '#%eof' if eof
65
+
66
+ lines.join("\n")
67
+ end
68
+
69
+ def save
70
+ raise unless @source.is_a? File
71
+
72
+ File.open(@source.path, 'w') { |f| f.write(self.to_s) }
46
73
  end
47
74
 
48
75
  def to_hash
@@ -50,8 +77,24 @@ module Checkm
50
77
  end
51
78
 
52
79
  private
53
- def parse_lines
54
- @lines.each do |line|
80
+ def source_text
81
+ return if @source_text
82
+
83
+ case source
84
+ when IO
85
+ @source_text ||= source.read
86
+ when String
87
+ if source.is_a?(String) and (File.exists?(source) or source =~ /:\/\//)
88
+ @source_text ||= open(source).read
89
+ end
90
+ end
91
+ @source_text ||= source
92
+
93
+ @source_text
94
+ end
95
+
96
+ def parse_lines str
97
+ source_text.split("\n").each do |line|
55
98
  case line
56
99
  when /^#%/
57
100
  parse_header line
@@ -60,9 +103,9 @@ module Checkm
60
103
  when /^$/
61
104
 
62
105
  when /^@/
63
- parse_line line
106
+ parse_manifest line
64
107
  else
65
- parse_line line
108
+ parse_entry line
66
109
  end
67
110
  end
68
111
  end
@@ -71,13 +114,13 @@ module Checkm
71
114
  case line
72
115
  when /^#%checkm/
73
116
  match = /^#%checkm_(\d+)\.(\d+)/.match line
74
- @version = "#{match[1]}.#{match[2]}" if match
117
+ @version ||= "#{match[1]}.#{match[2]}" if match
75
118
  when /^#%eof/
76
- @eof = true
119
+ @eof ||= true
77
120
  when /^#%fields/
78
121
  list = line.split('|')
79
122
  list.shift
80
- @fields = list.map { |v| v.strip.downcase }
123
+ @fields ||= list.map { |v| v.strip }
81
124
  when /^#%prefix/
82
125
 
83
126
  when /^#%profile/
@@ -89,8 +132,16 @@ module Checkm
89
132
 
90
133
  end
91
134
 
92
- def parse_line line
93
- @entries << Entry.new(line, self)
135
+ def parse_entry line
136
+ @entries << Entry.new(line, options_for_entries )
137
+ end
138
+
139
+ def options_for_entries
140
+ { :path => path, :fields => fields, :manifest => self }
141
+ end
142
+
143
+ def parse_manifest line
144
+ @entries << Manifest.new(line[/^@([^|]+)/].strip.gsub(/^@/, ''))
94
145
  end
95
146
  end
96
147
  end
@@ -3,21 +3,21 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe "checkm" do
4
4
  it "should be valid if empty" do
5
5
  checkm = ''
6
- res = Checkm::Manifest.parse(checkm)
6
+ res = Checkm::Manifest.new(checkm)
7
7
  res.entries.should be_empty
8
8
  res.should be_valid
9
9
  end
10
10
 
11
11
  it "should ignore comments" do
12
12
  checkm = '#'
13
- res = Checkm::Manifest.parse(checkm)
13
+ res = Checkm::Manifest.new(checkm)
14
14
  res.entries.should be_empty
15
15
  res.should be_valid
16
16
  end
17
17
 
18
- it "should parse the checkm version" do
18
+ it "should new the checkm version" do
19
19
  checkm = '#%checkm_0.7'
20
- res = Checkm::Manifest.parse(checkm)
20
+ res = Checkm::Manifest.new(checkm)
21
21
  res.entries.should be_empty
22
22
  res.should be_valid
23
23
  res.version.should == '0.7'
@@ -26,15 +26,15 @@ describe "checkm" do
26
26
  describe "simple checkm line" do
27
27
  before(:each) do
28
28
  @checkm = 'book/Chapter9.xml | md5 | 49afbd86a1ca9f34b677a3f09655eae9'
29
- @result = Checkm::Manifest.parse(@checkm)
29
+ @result = Checkm::Manifest.new(@checkm)
30
30
  @line = @result.entries.first
31
31
  end
32
32
 
33
- it "should parse one entry" do
33
+ it "should new one entry" do
34
34
  @result.should have(1).entries
35
35
  end
36
36
 
37
- it "should parse a checkm line" do
37
+ it "should new a checkm line" do
38
38
  @line.values[0].should == 'book/Chapter9.xml'
39
39
  @line.values[1].should == 'md5'
40
40
  @line.values[2].should == '49afbd86a1ca9f34b677a3f09655eae9'
@@ -47,10 +47,10 @@ describe "checkm" do
47
47
  end
48
48
  end
49
49
 
50
- it "should support custom field names" do
50
+ it "should support custom field names", :blah => true do
51
51
  checkm= '#%fields | testa | test b' + "\n" +
52
52
  'book/Chapter9.xml | md5 | 49afbd86a1ca9f34b677a3f09655eae9'
53
- res = Checkm::Manifest.parse(checkm)
53
+ res = Checkm::Manifest.new(checkm)
54
54
 
55
55
  line = res.entries.first
56
56
 
@@ -64,28 +64,28 @@ describe "checkm" do
64
64
  describe "validity check" do
65
65
  it "should be valid if the file exists" do
66
66
  checkm = '1 | md5 | b026324c6904b2a9cb4b88d6d61c81d1'
67
- res = Checkm::Manifest.parse(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures/test_1'))
67
+ res = Checkm::Manifest.new(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures/test_1'))
68
68
  res.should have(1).entries
69
69
  res.should be_valid
70
70
  end
71
71
 
72
72
  it "should be valid if the directory exists" do
73
73
  checkm = 'test_1 | dir'
74
- res = Checkm::Manifest.parse(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures'))
74
+ res = Checkm::Manifest.new(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures'))
75
75
  res.should have(1).entries
76
76
  res.should be_valid
77
77
  end
78
78
 
79
79
  it "should be invalid if a file is missing" do
80
80
  checkm = '2 | md5 | b026324c6904b2a9cb4b88d6d61c81d1'
81
- res = Checkm::Manifest.parse(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures/test_1'))
81
+ res = Checkm::Manifest.new(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures/test_1'))
82
82
  res.should have(1).entries
83
83
  res.should_not be_valid
84
84
  end
85
85
 
86
86
  it "should be invalid if the checksum is different" do
87
87
  checkm = '1 | md5 | zzz'
88
- res = Checkm::Manifest.parse(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures/test_1'))
88
+ res = Checkm::Manifest.new(checkm, :path => File.join(File.dirname(__FILE__), 'fixtures/test_1'))
89
89
  res.should have(1).entries
90
90
  res.should_not be_valid
91
91
  end
@@ -94,21 +94,21 @@ describe "checkm" do
94
94
  describe "manipulate manifest" do
95
95
  it "should support simple create" do
96
96
  res = Checkm::Entry.create('LICENSE.txt')
97
- res.should match( /LICENSE\.txt | md5 | 927368f89ca84dbec878a8d017f06443 | 1054 | \d{4}/)
97
+ res.to_s.should match( /LICENSE\.txt | md5 | 927368f89ca84dbec878a8d017f06443 | 1054 | \d{4}/)
98
98
  end
99
99
 
100
100
  it "should allow files to be added to an existing manifest" do
101
- m = Checkm::Manifest.parse('')
102
- res = m.add('LICENSE.txt')
103
- res.should have(1).entries
104
- res.should be_valid
101
+ m = Checkm::Manifest.new('')
102
+ m.add('LICENSE.txt')
103
+ m.should have(1).entries
104
+ m.should be_valid
105
105
  end
106
106
  end
107
107
 
108
108
  it "should be serializable to a string" do
109
- m = Checkm::Manifest.parse('')
110
- n = m.add('LICENSE.txt')
111
- lines = n.to_s.split "\n"
109
+ m = Checkm::Manifest.new('')
110
+ m.add('LICENSE.txt')
111
+ lines = m.to_s.split "\n"
112
112
  lines[0].should == '#%checkm_0.7'
113
113
  lines[1].should match(/^LICENSE\.txt/)
114
114
  end
metadata CHANGED
@@ -1,105 +1,81 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: checkm
3
- version: !ruby/object:Gem::Version
4
- hash: 19
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 6
10
- version: 0.0.6
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Chris Beer
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-06-09 00:00:00 -05:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-06-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rake
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70168432758200 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: bundler
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70168432758200
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70168432757220 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
47
33
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rspec
51
34
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70168432757220
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70168432756460 !ruby/object:Gem::Requirement
53
39
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
61
44
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: rcov
65
45
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *70168432756460
47
+ - !ruby/object:Gem::Dependency
48
+ name: rcov
49
+ requirement: &70168432755600 !ruby/object:Gem::Requirement
67
50
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
75
55
  type: :development
76
- version_requirements: *id004
77
- - !ruby/object:Gem::Dependency
78
- name: yard
79
56
  prerelease: false
80
- requirement: &id005 !ruby/object:Gem::Requirement
57
+ version_requirements: *70168432755600
58
+ - !ruby/object:Gem::Dependency
59
+ name: yard
60
+ requirement: &70168432754900 !ruby/object:Gem::Requirement
81
61
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
- version: "0"
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
89
66
  type: :development
90
- version_requirements: *id005
91
- description: "Checkm is a general-purpose text-based file manifest format "
67
+ prerelease: false
68
+ version_requirements: *70168432754900
69
+ description:
92
70
  email: chris@cbeer.info
93
71
  executables: []
94
-
95
72
  extensions: []
96
-
97
73
  extra_rdoc_files: []
98
-
99
- files:
74
+ files:
100
75
  - .document
101
76
  - .gitignore
102
77
  - Gemfile
78
+ - Gemfile.lock
103
79
  - LICENSE.txt
104
80
  - README.textile
105
81
  - Rakefile
@@ -107,47 +83,40 @@ files:
107
83
  - checkm.gemspec
108
84
  - lib/checkm.rb
109
85
  - lib/checkm/entry.rb
110
- - lib/checkm/file.rb
111
86
  - lib/checkm/manifest.rb
112
87
  - lib/checkm/version.rb
113
88
  - spec/checkm_spec.rb
114
89
  - spec/fixtures/test_1/1
115
90
  - spec/spec_helper.rb
116
- has_rdoc: true
117
91
  homepage:
118
92
  licenses: []
119
-
120
93
  post_install_message:
121
94
  rdoc_options: []
122
-
123
- require_paths:
95
+ require_paths:
124
96
  - lib
125
- required_ruby_version: !ruby/object:Gem::Requirement
97
+ required_ruby_version: !ruby/object:Gem::Requirement
126
98
  none: false
127
- requirements:
128
- - - ">="
129
- - !ruby/object:Gem::Version
130
- hash: 3
131
- segments:
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
132
104
  - 0
133
- version: "0"
134
- required_rubygems_version: !ruby/object:Gem::Requirement
105
+ hash: -4308377203823409037
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
107
  none: false
136
- requirements:
137
- - - ">="
138
- - !ruby/object:Gem::Version
139
- hash: 3
140
- segments:
141
- - 0
142
- version: "0"
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
143
112
  requirements: []
144
-
145
113
  rubyforge_project:
146
- rubygems_version: 1.5.3
114
+ rubygems_version: 1.8.10
147
115
  signing_key:
148
116
  specification_version: 3
149
- summary: Checkm is a general-purpose text-based file manifest format
150
- test_files:
117
+ summary: checkm implementation
118
+ test_files:
151
119
  - spec/checkm_spec.rb
152
120
  - spec/fixtures/test_1/1
153
121
  - spec/spec_helper.rb
122
+ has_rdoc:
File without changes