metacrunch 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d28e81a93e97edc9aa9016c18587317304ca0d68
4
- data.tar.gz: 19ec0fccbef5888f0397b6d2abd3c404f7878172
3
+ metadata.gz: 5bcfeeb60f1625e155e0af9a3b97fdc4d580f5d8
4
+ data.tar.gz: 9f157ad5acb41c7fe3fc8bad5585653a5a6ff41a
5
5
  SHA512:
6
- metadata.gz: 7f5636649b8bb785b58ba4995aac662803a1901a0b24910ea3eaa2847f86c90a7382a45bd3ad99562548246f8ddefe97af017161074bf09e57ed6686a65c6d6b
7
- data.tar.gz: 121cb6f44102a87e13520f8dfde8e8b2feef04329649fbc7d9ec7f93fff652d07bc42b9a84e8cde71760edaf690ccf3e5d5954439d47ce13bc96c6bf2121cd29
6
+ metadata.gz: d2a45e6ba7218f8b283c9b018b76129ce98187037c84986a11e27bffdad3cde4ce9f51c0112c5e34d17b40b5c46e101ddba74fe93f0fe48e00f51a4859da96a2
7
+ data.tar.gz: 39ff3a35ab79367db1af87ae9ba7abc92a957eb8b92a1c040eaca3cdc4140cde0e15501fe7204afb451fc971ecf9ecae934f74aac39f68f71f6e0cc0fe95e7a6
data/.gitignore CHANGED
@@ -1,5 +1,24 @@
1
1
  .DS_Store
2
- /Gemfile.lock
3
- /.yardoc
4
2
  /doc
5
- /*.gem
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ *.bundle
21
+ *.so
22
+ *.o
23
+ *.a
24
+ mkmf.log
data/Gemfile CHANGED
@@ -3,5 +3,15 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem "rake"
6
- gem "pry-byebug", "~> 3.1.0"
7
- gem "rspec", "~> 3.2.0"
6
+ gem "rspec", "~> 3.2.0"
7
+
8
+ if !ENV["CI"]
9
+ group :development do
10
+ gem "hashdiff"
11
+ gem "pry", "~> 0.9.12.6"
12
+ gem "pry-byebug", "<= 1.3.2"
13
+ gem "pry-rescue", "~> 1.4.1", github: "ConradIrwin/pry-rescue", branch: :master
14
+ gem "pry-stack_explorer", "~> 0.4.9.1"
15
+ gem "pry-syntax-hacks", "~> 0.0.6"
16
+ end
17
+ end
data/lib/metacrunch.rb CHANGED
@@ -15,9 +15,13 @@ module Metacrunch
15
15
  require_relative "./metacrunch/version"
16
16
  require_relative "./metacrunch/cli"
17
17
  require_relative "./metacrunch/command"
18
- require_relative "./metacrunch/utils"
18
+ require_relative "./metacrunch/file_reader"
19
+ require_relative "./metacrunch/file_reader_entry"
20
+ require_relative "./metacrunch/file_writer"
21
+ require_relative "./metacrunch/tar_writer"
19
22
  require_relative "./metacrunch/snr"
20
23
  require_relative "./metacrunch/transformer"
24
+ require_relative "./metacrunch/parallel"
21
25
 
22
26
  def self.load_plugins
23
27
  Gem.find_latest_files("metacrunch_plugin.rb").each do |path|
@@ -14,9 +14,9 @@ module Metacrunch
14
14
  @klass
15
15
  end
16
16
 
17
- def name
18
- name = @klass.to_s.demodulize.underscore
19
- name.gsub!(/_command\Z/, "")
17
+ def name(value = nil)
18
+ @name = value if value.present?
19
+ @name || @klass.to_s.demodulize.underscore.gsub!(/_command\Z/, "")
20
20
  end
21
21
 
22
22
  def usage(value = nil)
@@ -1,7 +1,16 @@
1
1
  module Metacrunch
2
2
  class Cli
3
3
  class Main < Thor
4
- # Define metacrunch root commands here
4
+ desc "console", "Start a console. It uses Pry if installed, IRB otherwise."
5
+ def console
6
+ begin
7
+ require "pry"
8
+ Pry.start
9
+ rescue LoadError
10
+ require "irb"
11
+ IRB.start
12
+ end
13
+ end
5
14
  end
6
15
  end
7
16
  end
@@ -0,0 +1,56 @@
1
+ require "rubygems/package"
2
+ require_relative "./file_reader_entry"
3
+
4
+ module Metacrunch
5
+ class FileReader
6
+
7
+ def initialize(filenames)
8
+ @filenames = [*filenames].map{|f| f.presence}.compact
9
+ end
10
+
11
+ def each(&block)
12
+ return enum_for(__method__) unless block_given?
13
+
14
+ @filenames.each do |_filename|
15
+ if is_archive?(_filename)
16
+ read_archive(_filename, &block)
17
+ else
18
+ read_regular_file(_filename, &block)
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def is_archive?(filename)
26
+ filename.ends_with?(".tar") || filename.ends_with?(".tar.gz")
27
+ end
28
+
29
+ def is_gzip_file?(filename)
30
+ filename.ends_with?(".gz")
31
+ end
32
+
33
+ def read_regular_file(filename, &block)
34
+ if File.file?(filename)
35
+ io = is_gzip_file?(filename) ? Zlib::GzipReader.open(filename) : File.open(filename, "r")
36
+ yield Entry.new(filename: filename, archive_filename: nil, contents: io.read)
37
+ end
38
+ end
39
+
40
+ def read_archive(filename, &block)
41
+ io = is_gzip_file?(filename) ? Zlib::GzipReader.open(filename) : File.open(filename, "r")
42
+ tarReader = Gem::Package::TarReader.new(io)
43
+
44
+ tarReader.each do |_tar_entry|
45
+ if _tar_entry.file?
46
+ yield Entry.new(
47
+ filename: filename,
48
+ archive_filename: _tar_entry.full_name,
49
+ contents: _tar_entry.read
50
+ )
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,21 @@
1
+ require_relative "./file_reader"
2
+
3
+ module Metacrunch
4
+ class FileReader
5
+ class Entry
6
+
7
+ attr_reader :filename, :archive_filename, :contents
8
+
9
+ def initialize(filename:, archive_filename:nil, contents:nil)
10
+ @filename = filename
11
+ @archive_filename = archive_filename.presence
12
+ @contents = contents
13
+ end
14
+
15
+ def from_archive?
16
+ @archive_filename != nil
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ module Metacrunch
2
+ class FileWriter
3
+
4
+ class FileExistError < RuntimeError ; end
5
+
6
+
7
+ def initialize(filename, override: false, compress: nil)
8
+ @path = File.expand_path(filename)
9
+ @compressed = (compress ||= @path.ends_with?(".gz"))
10
+
11
+ if File.exist?(@path) && !override
12
+ raise FileExistError, "File #{@path} already exists. Set override = true to override the existing file."
13
+ end
14
+ end
15
+
16
+ def write(data, options = {})
17
+ if block_given?
18
+ yield(io)
19
+ else
20
+ io.write(data)
21
+ end
22
+ end
23
+
24
+ def flush
25
+ @io.flush if @io
26
+ end
27
+
28
+ def close
29
+ flush
30
+ @io.close if @io
31
+ end
32
+
33
+ private
34
+
35
+ def io
36
+ @io ||= (@compressed == true) ? Zlib::GzipWriter.open(@path) : File.open(@path, "w")
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,69 @@
1
+ module Metacrunch
2
+ class Parallel
3
+
4
+ module DSL
5
+ def parallel(enumerable, options = {}, &block)
6
+ Parallel.each(enumerable, options, &block)
7
+ end
8
+ end
9
+
10
+ def self.each(enumerable, options = {}, &block)
11
+ self.new(enumerable, options, &block).call
12
+ end
13
+
14
+ def initialize(enumerable, options = {}, &block)
15
+ @enumerable = enumerable
16
+ @callable = block
17
+ @no_of_procs = options[:in_processes] || 0
18
+ @on_process_finished = options[:on_process_finished] || -> {}
19
+
20
+ unless block_given?
21
+ raise ArgumentError, "you must provide a block"
22
+ end
23
+
24
+ unless @enumerable.respond_to?(:each)
25
+ raise ArgumentError, "enumerable must respond to each"
26
+ end
27
+
28
+ unless @on_process_finished.respond_to?(:call)
29
+ raise ArgumentError, "on_process_finished must respond to call"
30
+ end
31
+ end
32
+
33
+ def call
34
+ @enumerable.each do |_value|
35
+ if @no_of_procs == 0
36
+ @callable.call(_value)
37
+ @on_process_finished.call
38
+ else
39
+ fork_process do
40
+ @callable.call(_value)
41
+ end
42
+
43
+ if processes_limit_reached?
44
+ wait_for_some_process_to_terminate
45
+ @on_process_finished.call
46
+ end
47
+ end
48
+ end
49
+ ensure
50
+ Process.waitall.each { @on_process_finished.call }
51
+ end
52
+
53
+ private
54
+
55
+ def fork_process(&block)
56
+ (@pids ||= []).push(fork(&block))
57
+ end
58
+
59
+ def processes_limit_reached?
60
+ (@pids || []).length >= @no_of_procs
61
+ end
62
+
63
+ def wait_for_some_process_to_terminate
64
+ pid_of_finished_process = Process.wait
65
+ @pids.delete(pid_of_finished_process)
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,26 @@
1
+ require "rubygems/package"
2
+
3
+ module Metacrunch
4
+ class TarWriter < FileWriter
5
+
6
+ def write(data, options = {})
7
+ raise ArgumentError, "Missing option 'filename'" if options[:filename].blank?
8
+
9
+ io.add_file_simple(options[:filename], 0644, data.bytesize) do |_io|
10
+ if block_given?
11
+ yield(_io)
12
+ else
13
+ _io.write(data)
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def io
21
+ @io ||= super
22
+ @tar_io ||= Gem::Package::TarWriter.new(@io)
23
+ end
24
+
25
+ end
26
+ end
@@ -1,10 +1,12 @@
1
1
  module Metacrunch
2
2
  class Transformer
3
3
  require_relative "./transformer/step"
4
+ require_relative "./transformer/helper"
4
5
 
5
- attr_reader :source, :target, :options
6
+ attr_accessor :source, :target, :options
6
7
 
7
- def initialize(source:, target:, options: {})
8
+
9
+ def initialize(source:nil, target:nil, options: {})
8
10
  @source = source
9
11
  @target = target
10
12
  @options = options
@@ -19,5 +21,14 @@ module Metacrunch
19
21
  end
20
22
  end
21
23
 
24
+ def helper
25
+ @helper ||= Helper.new(self)
26
+ end
27
+
28
+ def register_helper(helper_module)
29
+ raise ArgumentError, "Must be a module" unless helper_module.is_a?(Module)
30
+ helper.class.send(:include, helper_module) # TODO: Benchmark this
31
+ end
32
+
22
33
  end
23
34
  end
@@ -0,0 +1,29 @@
1
+ require_relative "../transformer"
2
+
3
+ module Metacrunch
4
+ class Transformer
5
+ class Helper
6
+
7
+ def initialize(transformer)
8
+ @transformer = transformer
9
+ end
10
+
11
+ def transformer
12
+ @transformer
13
+ end
14
+
15
+ def source
16
+ transformer.source
17
+ end
18
+
19
+ def target
20
+ transformer.target
21
+ end
22
+
23
+ def options
24
+ transformer.options
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -28,6 +28,10 @@ module Metacrunch
28
28
  transformer.options
29
29
  end
30
30
 
31
+ def helper
32
+ transformer.helper
33
+ end
34
+
31
35
  end
32
36
  end
33
37
  end
@@ -1,3 +1,3 @@
1
1
  module Metacrunch
2
- VERSION = "2.1.0"
2
+ VERSION = "2.1.1"
3
3
  end
data/metacrunch.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  require File.expand_path("../lib/metacrunch/version", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.authors = ["René Sprotte", "Michael Sievers", "Marcel Otto"]
4
+ s.authors = ["René Sprotte", "Michael Sievers", "Marcel Otto"]
5
5
  s.email = "r.sprotte@ub.uni-paderborn.de"
6
6
  s.summary = %q{Data processing toolkit for Ruby}
7
7
  s.description = s.summary
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_dependency "activesupport", "~> 4.2", ">= 4.2.0"
23
23
  s.add_dependency "builder", "~> 3.2", ">= 3.2.2"
24
- s.add_dependency "parallel", "~> 1.4", ">= 1.4.1"
24
+ s.add_dependency "parallel", "~> 1.6", ">= 1.6.0"
25
25
  s.add_dependency "thor", "~> 0.19"
26
26
  s.add_dependency "ox", "~> 2.1"
27
27
  end
@@ -0,0 +1,73 @@
1
+ describe Metacrunch::FileReader do
2
+
3
+ let(:regular_file) { File.join(RSpec.root, "assets", "regular_file.txt") }
4
+ let(:compressed_regular_file) { File.join(RSpec.root, "assets", "regular_file.txt.gz") }
5
+ let(:archive_file) { File.join(RSpec.root, "assets", "archive.tar") }
6
+ let(:compressed_archive_file) { File.join(RSpec.root, "assets", "archive.tar.gz") }
7
+
8
+ it "can read a single file" do
9
+ reader = Metacrunch::FileReader.new(regular_file)
10
+ expect(reader.each.count).to be(1)
11
+ end
12
+
13
+ it "can read multiple files" do
14
+ reader = Metacrunch::FileReader.new([regular_file, regular_file])
15
+ expect(reader.each.count).to be(2)
16
+ end
17
+
18
+ it "can read regular files" do
19
+ reader = Metacrunch::FileReader.new(regular_file)
20
+ reader.each do |entry|
21
+ expect(entry).to be_instance_of(Metacrunch::FileReader::Entry)
22
+ expect(entry.filename).to eq(regular_file)
23
+ expect(entry.archive_filename).to be_nil
24
+ expect(entry.from_archive?).to be false
25
+ expect(entry.contents).to eq("THIS IS A TEST\n")
26
+ end
27
+ end
28
+
29
+ it "can read compressed regular files" do
30
+ reader = Metacrunch::FileReader.new(compressed_regular_file)
31
+ reader.each do |entry|
32
+ expect(entry).to be_instance_of(Metacrunch::FileReader::Entry)
33
+ expect(entry.filename).to eq(compressed_regular_file)
34
+ expect(entry.archive_filename).to be_nil
35
+ expect(entry.from_archive?).to be false
36
+ expect(entry.contents).to eq("THIS IS A TEST\n")
37
+ end
38
+ end
39
+
40
+ it "can read TAR archives" do
41
+ reader = Metacrunch::FileReader.new(archive_file)
42
+ reader.each do |entry|
43
+ expect(entry).not_to be_nil
44
+ expect(entry).to be_instance_of(Metacrunch::FileReader::Entry)
45
+ expect(entry.filename).to eq(archive_file)
46
+ expect(entry.archive_filename).to eq("regular_file.txt")
47
+ expect(entry.from_archive?).to be true
48
+ expect(entry.contents).to eq("THIS IS A TEST\n")
49
+ end
50
+ end
51
+
52
+ it "can read compressed TAR archives" do
53
+ reader = Metacrunch::FileReader.new(compressed_archive_file)
54
+ reader.each do |entry|
55
+ expect(entry).not_to be_nil
56
+ expect(entry).to be_instance_of(Metacrunch::FileReader::Entry)
57
+ expect(entry.filename).to eq(compressed_archive_file)
58
+ expect(entry.archive_filename).to eq("regular_file.txt")
59
+ expect(entry.from_archive?).to be true
60
+ expect(entry.contents).to eq("THIS IS A TEST\n")
61
+ end
62
+ end
63
+
64
+ describe ".each()" do
65
+ context "when called without a block" do
66
+ it "returns an enumerator" do
67
+ reader = Metacrunch::FileReader.new(regular_file)
68
+ expect(reader.each).to be_instance_of(Enumerator)
69
+ end
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,46 @@
1
+ describe Metacrunch::FileWriter do
2
+
3
+ let(:regular_file) { "/tmp/metacrunch_spec_regular_file.txt" }
4
+ let(:compressed_file) { "/tmp/metacrunch_spec_regular_file.txt.gz" }
5
+
6
+ before do
7
+ File.unlink(regular_file) if File.exist?(regular_file)
8
+ File.unlink(compressed_file) if File.exist?(compressed_file)
9
+ end
10
+
11
+ it "can write a file" do
12
+ writer = Metacrunch::FileWriter.new(regular_file)
13
+ writer.write("FOO")
14
+ writer.write("BAR")
15
+ writer.close
16
+
17
+ content = File.read(regular_file)
18
+ expect(content).to eq("FOOBAR")
19
+ end
20
+
21
+ it "can write a compressed file" do
22
+ writer = Metacrunch::FileWriter.new(compressed_file, compress: true)
23
+ writer.write("FOO")
24
+ writer.write("BAR")
25
+ writer.close
26
+
27
+ io = Zlib::GzipReader.open(compressed_file)
28
+ content = io.read
29
+ io.close
30
+
31
+ expect(content).to eq("FOOBAR")
32
+ end
33
+
34
+ it "can override existing file" do
35
+ File.write(regular_file, "FOO")
36
+
37
+ expect {
38
+ Metacrunch::FileWriter.new(regular_file)
39
+ }.to raise_error(Metacrunch::FileWriter::FileExistError)
40
+
41
+ expect {
42
+ Metacrunch::FileWriter.new(regular_file, override: true)
43
+ }.not_to raise_error
44
+ end
45
+
46
+ end
@@ -0,0 +1,33 @@
1
+ describe Metacrunch::TarWriter do
2
+
3
+ let(:regular_tar_file) { "/tmp/metacrunch_spec_regular_file.tar" }
4
+ let(:compressed_tar_file) { "/tmp/metacrunch_spec_regular_file.tar.gz" }
5
+
6
+ before do
7
+ File.unlink(regular_tar_file) if File.exist?(regular_tar_file)
8
+ File.unlink(compressed_tar_file) if File.exist?(compressed_tar_file)
9
+ end
10
+
11
+ it "can write a file" do
12
+ writer = Metacrunch::TarWriter.new(regular_tar_file)
13
+ writer.write("FOO", filename: "foo.txt")
14
+ writer.write("BAR", filename: "bar.txt")
15
+ writer.close
16
+
17
+ files = Metacrunch::FileReader.new(regular_tar_file).each
18
+ expect(files.count).to eq(2)
19
+ expect(files.first.contents).to eq("FOO")
20
+ end
21
+
22
+ it "can write a compressed file" do
23
+ writer = Metacrunch::TarWriter.new(compressed_tar_file)
24
+ writer.write("FOO", filename: "foo.txt")
25
+ writer.write("BAR", filename: "bar.txt")
26
+ writer.close
27
+
28
+ files = Metacrunch::FileReader.new(compressed_tar_file).each
29
+ expect(files.count).to eq(2)
30
+ expect(files.first.contents).to eq("FOO")
31
+ end
32
+
33
+ end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metacrunch
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - René Sprotte
7
+ - René Sprotte
8
8
  - Michael Sievers
9
9
  - Marcel Otto
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-05-07 00:00:00.000000000 Z
13
+ date: 2015-06-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -58,20 +58,20 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.4'
61
+ version: '1.6'
62
62
  - - ">="
63
63
  - !ruby/object:Gem::Version
64
- version: 1.4.1
64
+ version: 1.6.0
65
65
  type: :runtime
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
68
68
  requirements:
69
69
  - - "~>"
70
70
  - !ruby/object:Gem::Version
71
- version: '1.4'
71
+ version: '1.6'
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
- version: 1.4.1
74
+ version: 1.6.0
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: thor
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -122,23 +122,28 @@ files:
122
122
  - lib/metacrunch/cli/command_registry.rb
123
123
  - lib/metacrunch/cli/main.rb
124
124
  - lib/metacrunch/command.rb
125
+ - lib/metacrunch/file_reader.rb
126
+ - lib/metacrunch/file_reader_entry.rb
127
+ - lib/metacrunch/file_writer.rb
128
+ - lib/metacrunch/parallel.rb
125
129
  - lib/metacrunch/snr.rb
126
130
  - lib/metacrunch/snr/field.rb
127
131
  - lib/metacrunch/snr/section.rb
132
+ - lib/metacrunch/tar_writer.rb
128
133
  - lib/metacrunch/transformer.rb
134
+ - lib/metacrunch/transformer/helper.rb
129
135
  - lib/metacrunch/transformer/step.rb
130
- - lib/metacrunch/utils.rb
131
- - lib/metacrunch/utils/file_reader.rb
132
- - lib/metacrunch/utils/file_reader_result.rb
133
136
  - lib/metacrunch/version.rb
134
137
  - metacrunch.gemspec
135
138
  - spec/assets/archive.tar
136
139
  - spec/assets/archive.tar.gz
137
140
  - spec/assets/regular_file.txt
138
141
  - spec/assets/regular_file.txt.gz
142
+ - spec/file_reader_spec.rb
143
+ - spec/file_writer_spec.rb
139
144
  - spec/snr_spec.rb
140
145
  - spec/spec_helper.rb
141
- - spec/utils/file_reader_spec.rb
146
+ - spec/tar_writer_spec.rb
142
147
  homepage: http://github.com/ubpb/metacrunch
143
148
  licenses:
144
149
  - MIT
@@ -159,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
164
  version: '0'
160
165
  requirements: []
161
166
  rubyforge_project:
162
- rubygems_version: 2.4.5
167
+ rubygems_version: 2.4.6
163
168
  signing_key:
164
169
  specification_version: 4
165
170
  summary: Data processing toolkit for Ruby
@@ -168,6 +173,8 @@ test_files:
168
173
  - spec/assets/archive.tar.gz
169
174
  - spec/assets/regular_file.txt
170
175
  - spec/assets/regular_file.txt.gz
176
+ - spec/file_reader_spec.rb
177
+ - spec/file_writer_spec.rb
171
178
  - spec/snr_spec.rb
172
179
  - spec/spec_helper.rb
173
- - spec/utils/file_reader_spec.rb
180
+ - spec/tar_writer_spec.rb
@@ -1,6 +0,0 @@
1
- module Metacrunch
2
- module Utils
3
- require_relative "./utils/file_reader"
4
- require_relative "./utils/file_reader_result"
5
- end
6
- end
@@ -1,66 +0,0 @@
1
- module Metacrunch
2
- module Utils
3
- class FileReader
4
-
5
- def initialize
6
- end
7
-
8
- def read_files(files, &block)
9
- [*files].each do |filename|
10
- read_file(filename, &block)
11
- end
12
- end
13
-
14
- def read_file(filename, &block)
15
- if is_tar_file?(filename)
16
- read_tar_file(filename, &block)
17
- else
18
- read_regular_file(filename, &block)
19
- end
20
- end
21
-
22
- private
23
-
24
- def is_tar_file?(filename)
25
- filename.ends_with?(".tar") || filename.ends_with?(".tar.gz")
26
- end
27
-
28
- def is_gzip_file?(filename)
29
- filename.ends_with?(".gz")
30
- end
31
-
32
- def read_tar_file(filename, &block)
33
- io = is_gzip_file?(filename) ? Zlib::GzipReader.open(filename) : File.open(filename, "r")
34
-
35
- tarReader = Gem::Package::TarReader.new(io)
36
- tarReader.each do |entry|
37
- if entry.file?
38
- result = FileReaderResult.new(
39
- filename: entry.full_name,
40
- source_filename: filename,
41
- contents: entry.read,
42
- from_archive: true
43
- )
44
-
45
- yield(result)
46
- end
47
- end
48
- end
49
-
50
- def read_regular_file(filename, &block)
51
- if File.file?(filename)
52
- io = is_gzip_file?(filename) ? Zlib::GzipReader.open(filename) : File.open(filename, "r")
53
-
54
- result = FileReaderResult.new(
55
- filename: filename,
56
- source_filename: nil,
57
- contents: io.read
58
- )
59
-
60
- yield(result)
61
- end
62
- end
63
-
64
- end
65
- end
66
- end
@@ -1,20 +0,0 @@
1
- module Metacrunch
2
- module Utils
3
- class FileReaderResult
4
-
5
- attr_reader :filename, :source_filename, :contents
6
-
7
- def initialize(filename:, source_filename:nil, contents:nil, from_archive:false)
8
- @filename = filename
9
- @source_filename = source_filename
10
- @contents = contents
11
- @from_archive = from_archive
12
- end
13
-
14
- def from_archive?
15
- @from_archive
16
- end
17
-
18
- end
19
- end
20
- end
@@ -1,53 +0,0 @@
1
- describe Metacrunch::Utils::FileReader do
2
-
3
- let(:reader) { Metacrunch::Utils::FileReader.new }
4
- let(:regular_file) { File.join(RSpec.root, "assets", "regular_file.txt") }
5
- let(:compressed_regular_file) { File.join(RSpec.root, "assets", "regular_file.txt.gz") }
6
- let(:archive_file) { File.join(RSpec.root, "assets", "archive.tar") }
7
- let(:compressed_archive_file) { File.join(RSpec.root, "assets", "archive.tar.gz") }
8
-
9
- it "can read regular files" do
10
- reader.read_files(regular_file) do |result|
11
- expect(result).not_to be_nil
12
- expect(result).to be_instance_of(Metacrunch::Utils::FileReaderResult)
13
- expect(result.filename).to eq(regular_file)
14
- expect(result.source_filename).to be_nil
15
- expect(result.from_archive?).to be false
16
- expect(result.contents).to eq("THIS IS A TEST\n")
17
- end
18
- end
19
-
20
- it "can read compressed regular files" do
21
- reader.read_files(compressed_regular_file) do |result|
22
- expect(result).not_to be_nil
23
- expect(result).to be_instance_of(Metacrunch::Utils::FileReaderResult)
24
- expect(result.filename).to eq(compressed_regular_file)
25
- expect(result.source_filename).to be_nil
26
- expect(result.from_archive?).to be false
27
- expect(result.contents).to eq("THIS IS A TEST\n")
28
- end
29
- end
30
-
31
- it "can read TAR archives" do
32
- reader.read_files(archive_file) do |result|
33
- expect(result).not_to be_nil
34
- expect(result).to be_instance_of(Metacrunch::Utils::FileReaderResult)
35
- expect(result.filename).to eq("regular_file.txt")
36
- expect(result.source_filename).to eq(archive_file)
37
- expect(result.from_archive?).to be true
38
- expect(result.contents).to eq("THIS IS A TEST\n")
39
- end
40
- end
41
-
42
- it "can read compressed TAR archives" do
43
- reader.read_files(compressed_archive_file) do |result|
44
- expect(result).not_to be_nil
45
- expect(result).to be_instance_of(Metacrunch::Utils::FileReaderResult)
46
- expect(result.filename).to eq("regular_file.txt")
47
- expect(result.source_filename).to eq(compressed_archive_file)
48
- expect(result.from_archive?).to be true
49
- expect(result.contents).to eq("THIS IS A TEST\n")
50
- end
51
- end
52
-
53
- end