swathe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .rvmrc
20
+ .DS_Store
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ swathe
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p374
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'pry'
4
+
5
+ # Specify your gem's dependencies in swathe.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sam Schenkman-Moore
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Swathe
2
+
3
+ An archive extraction tool. Intends to support tar, zip, tgz, etc without much fuss.
4
+
5
+ Presently crude ... but effective? Maybe!
6
+
7
+ ## Installation
8
+
9
+ gem 'swathe'
10
+
11
+ ## Usage
12
+
13
+ # This should work from the root of this checked out project.
14
+ %w(tar tgz zip).each do |archive_type|
15
+ archive = Swathe::Archive.open("samples/sample.#{archive_type}")
16
+ puts "Opening: sample.#{archive_type}"
17
+ puts "First file is #{archive.first.full_name}"
18
+ p "Contents are: #{archive.first.read}"
19
+ end
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ namespace 'test' do
6
+ test_files = FileList['test/**/*_test.rb']
7
+ integration_test_files = FileList['test/**/*_integration_test.rb']
8
+ unit_test_files = test_files - integration_test_files
9
+
10
+ desc "Run unit tests"
11
+ Rake::TestTask.new('unit') do |t|
12
+ t.libs.push "lib"
13
+ t.test_files = unit_test_files
14
+ t.verbose = false
15
+ end
16
+
17
+ desc "Run integration tests"
18
+ Rake::TestTask.new('integration') do |t|
19
+ t.libs.push "lib"
20
+ t.test_files = integration_test_files
21
+ t.verbose = false
22
+ end
23
+ end
24
+
25
+ #Rake::Task['test'].clear
26
+ desc "Run all tests"
27
+ task 'test' => %w[test:unit test:integration]
28
+ task 'default' => 'test'
data/lib/swathe.rb ADDED
@@ -0,0 +1,17 @@
1
+ require_relative "swathe/version"
2
+ require_relative "swathe/archive"
3
+ require_relative "swathe/extractor"
4
+ require_relative "swathe/tar"
5
+ require_relative "swathe/gzip"
6
+ require_relative "swathe/zip"
7
+
8
+
9
+ module Swathe
10
+ def self.root
11
+ File.dirname(__FILE__)
12
+ end
13
+
14
+ def self.project_root
15
+ File.expand_path(File.join(root, '..'))
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ module Swathe
2
+ class Archive
3
+ attr_accessor :file_name
4
+
5
+ def self.open(file_name)
6
+ case File.extname(file_name)
7
+ when '.gz', '.tgz'
8
+ Gzip.open(file_name)
9
+ when '.tar'
10
+ Tar.open(file_name)
11
+ when '.zip'
12
+ Zip.open(file_name)
13
+ end
14
+ end
15
+
16
+ def tar?
17
+ false
18
+ end
19
+
20
+ def gz?
21
+ false
22
+ end
23
+
24
+ def zip?
25
+ false
26
+ end
27
+
28
+ def extractor(default_destination = nil)
29
+ Extractor.new(self, default_destination)
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,38 @@
1
+ require 'fileutils'
2
+
3
+ module Swathe
4
+ class Extractor
5
+ attr_accessor :source, :default_destination
6
+ def initialize(source, default_destination = '.')
7
+ self.source = source
8
+ self.default_destination = default_destination
9
+ end
10
+
11
+ def target_path(destination)
12
+ case destination
13
+ when nil then default_destination
14
+ when %r(\A/) then destination
15
+ else
16
+ "#{default_destination}/#{destination}"
17
+ end
18
+ end
19
+
20
+ def extract(file_path, destination = nil)
21
+ # Need to make this able to ignore the file's path if need be.
22
+ base_destination_directory = destination || default_destination
23
+ entry = source.entry(file_path)
24
+ if entry
25
+ target = "#{base_destination_directory}/#{entry.full_name}"
26
+ ensure_target_directory_exists_for(target)
27
+ File.open target, "wb" do |f|
28
+ entry.read
29
+ end
30
+ end
31
+ end
32
+
33
+ def ensure_target_directory_exists_for(path)
34
+ target_directory = File.dirname(path)
35
+ FileUtils.mkdir_p(target_directory) unless File.directory?(target_directory)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,51 @@
1
+ require 'zlib'
2
+ require 'forwardable'
3
+
4
+ module Swathe
5
+ class Gzip < Archive
6
+ attr_accessor :gzip_reader
7
+
8
+ extend Forwardable
9
+ def_delegators :tar, :entry, :each, :files, :directories
10
+ include Enumerable
11
+
12
+ def initialize(io)
13
+ self.gzip_reader = Zlib::GzipReader.open(io)
14
+ end
15
+
16
+ def self.open(filename)
17
+ new(File.open(filename))
18
+ end
19
+
20
+ def gzip?
21
+ true
22
+ end
23
+
24
+ def tar
25
+ if contains_tar?
26
+ @tar ||= Tar.new(gzip_reader)
27
+ else
28
+ # Need something that acts like an archive of one file.
29
+ end
30
+ end
31
+
32
+ def contains_tar?
33
+ (%r(\.tar\Z) === gzip_reader.orig_name) ||
34
+ (%r(\.tgz\Z) === File.extname(gzip_reader.path))
35
+ end
36
+
37
+ def each(*args, &blk)
38
+ return enum_for(__callee__) unless block_given?
39
+ contains_tar? ? tar.each(*args, &blk) : [gzip_reader].each(*args, &blk)
40
+ end
41
+
42
+ def self.gzip?(file)
43
+ begin
44
+ Zlib::GzipReader.open(file)
45
+ rescue Zlib::GzipFile::Error
46
+ false
47
+ end
48
+ end
49
+
50
+ end
51
+ end
data/lib/swathe/tar.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems/package'
2
+
3
+ # was helpful http://old.thoughtsincomputation.com/posts/tar-and-a-few-feathers-in-ruby
4
+ module Swathe
5
+ class Tar < Archive
6
+ attr_accessor :tar_reader
7
+ include Enumerable
8
+
9
+ def initialize(io)
10
+ self.tar_reader = Gem::Package::TarReader.new(io)
11
+ end
12
+
13
+ def self.open(file_name)
14
+ new(File.open(file_name))
15
+ end
16
+
17
+ def tar?
18
+ true
19
+ end
20
+
21
+ def each
22
+ return enum_for(__callee__) unless block_given?
23
+ tar_reader.rewind
24
+ tar_reader.each {|e| yield(e) }
25
+ end
26
+
27
+ def subset
28
+ Enumerator.new do |files|
29
+ each do |entry|
30
+ files << entry if yield(entry)
31
+ end
32
+ end
33
+ end
34
+
35
+ def files
36
+ subset {|e| e.file? }
37
+ end
38
+
39
+ def directories
40
+ subset {|e| e.directory? }
41
+ end
42
+
43
+ def entry(filename)
44
+ detect {|f| filename === f.full_name }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ class TarEntry
2
+ attr_accessor :entry
3
+ def initialize(entry)
4
+ self.entry = entry
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Swathe
2
+ VERSION = "0.0.1"
3
+ end
data/lib/swathe/zip.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'zip/zip'
2
+ require 'forwardable'
3
+
4
+ module Swathe
5
+ class Zip < Archive
6
+ attr_accessor :zip_reader
7
+ include Enumerable
8
+
9
+ def initialize(io)
10
+ @zip_reader = ::Zip::ZipFile.open(io)
11
+ end
12
+
13
+ def self.open(file_name)
14
+ new(file_name)
15
+ end
16
+
17
+ def zip?
18
+ true
19
+ end
20
+
21
+ def each(*args, &blk)
22
+ return enum_for(__callee__) unless block_given?
23
+ # zip_reader.each(*args, &blk)
24
+ zip_reader.each(*args) do |entry|
25
+ blk.call(FileInZip.new(entry))
26
+ end
27
+ end
28
+
29
+ def subset
30
+ Enumerator.new do |files|
31
+ each do |entry|
32
+ files << entry if yield(entry)
33
+ end
34
+ end
35
+ end
36
+
37
+ def files
38
+ subset {|e| e.file? }
39
+ end
40
+
41
+ def directories
42
+ subset {|e| e.directory? }
43
+ end
44
+
45
+ def entry(filename)
46
+ detect {|f| filename === f.full_name }
47
+ end
48
+
49
+ end
50
+
51
+ class FileInZip
52
+ attr_reader :entry
53
+
54
+ extend Forwardable
55
+ def_delegators :entry, :file?, :directory?
56
+
57
+ def initialize(entry)
58
+ @entry = entry
59
+ end
60
+
61
+ def full_name
62
+ entry.name
63
+ end
64
+
65
+ def read
66
+ entry.get_input_stream.read
67
+ end
68
+
69
+ end
70
+ end
data/samples/file.txt ADDED
@@ -0,0 +1 @@
1
+ This is a sample file to test extraction.
Binary file
Binary file
Binary file
Binary file
data/swathe.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swathe/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "swathe"
8
+ gem.version = Swathe::VERSION
9
+ gem.authors = ["Sam Schenkman-Moore"]
10
+ gem.email = ["samsm@samsm.com"]
11
+ gem.description = %q{An abstraction for dealing with common archive tasks.}
12
+ gem.summary = %q{Copies files out of archives.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "rubyzip"
21
+
22
+ gem.add_development_dependency "minitest"
23
+ gem.add_development_dependency "rake"
24
+
25
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'minitest_helper'
2
+
3
+ class TestArchive < MiniTest::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ def test_that_kitty_can_eat
8
+ assert_kind_of Class, Swathe::Archive
9
+ end
10
+
11
+ def test_archive_open
12
+ gz = Swathe::Archive.open("#{samples_path}/sample.tgz")
13
+ assert gz.gzip?
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'minitest_helper'
2
+
3
+ class TestExtractorIntegration < MiniTest::Unit::TestCase
4
+ def teardown
5
+ FileUtils.rm_r(Dir.glob("#{cleanup_path}*"))
6
+ end
7
+
8
+ def extractor(path = "#{samples_path}/tar_with_directory.tar")
9
+ @extractor ||= Swathe::Tar.open(path).extractor
10
+ end
11
+
12
+ def cleanup_path
13
+ "#{Swathe.project_root}/tmp/"
14
+ end
15
+
16
+ def test_write_file
17
+ extractor.extract('samples/file.txt', 'tmp/')
18
+ assert File.exists?("tmp/samples/file.txt")
19
+ end
20
+
21
+ end
data/test/gzip_test.rb ADDED
@@ -0,0 +1,21 @@
1
+ require_relative 'minitest_helper'
2
+
3
+ class TestGzipIntegration < MiniTest::Unit::TestCase
4
+
5
+ def gzip(path = "#{samples_path}/sample.tgz")
6
+ @gzip ||= Swathe::Gzip.open(path)
7
+ end
8
+
9
+ def test_is_a_gzip
10
+ assert gzip.gzip?
11
+ end
12
+
13
+ def test_has_a_tar
14
+ assert gzip.tar.tar?
15
+ end
16
+
17
+ def test_read_file
18
+ assert_match %r(This is a sample file to test extraction), gzip.entry('file.txt').read
19
+ end
20
+
21
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ gem 'minitest' # ensures you're using the gem, and not the built in MT
3
+ require 'minitest/autorun'
4
+ require 'minitest/unit'
5
+ require 'minitest/mock'
6
+
7
+ require 'swathe'
8
+
9
+ def samples_path
10
+ "#{Swathe.project_root}/samples"
11
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'minitest_helper'
2
+
3
+ class TestSwathe < MiniTest::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ def test_that_kitty_can_eat
8
+ assert_kind_of Module, Swathe
9
+ end
10
+ end
data/test/tar_test.rb ADDED
@@ -0,0 +1,33 @@
1
+ require_relative 'minitest_helper'
2
+
3
+ class TestTarIntegration < MiniTest::Unit::TestCase
4
+
5
+ def tar(path = "#{samples_path}/tar_with_directory.tar")
6
+ @tar ||= Swathe::Tar.open(path)
7
+ end
8
+
9
+ def test_is_a_tar
10
+ assert tar.tar?
11
+ end
12
+
13
+ def test_file_count
14
+ assert_equal 1, tar.files.count
15
+ end
16
+
17
+ def test_file_list
18
+ assert_equal ["samples/file.txt"], tar.files.collect(&:full_name)
19
+ end
20
+
21
+ def test_directory_list
22
+ assert_equal ["samples/"], tar.directories.collect(&:full_name)
23
+ end
24
+
25
+ def test_read_file
26
+ assert_match %r(This is a sample file to test extraction), tar.entry('samples/file.txt').read
27
+ end
28
+
29
+ def test_extractor
30
+ assert_kind_of Swathe::Extractor, tar.extractor
31
+ end
32
+
33
+ end
data/test/zip_test.rb ADDED
@@ -0,0 +1,31 @@
1
+ require_relative 'minitest_helper'
2
+
3
+ class TestZipIntegration < MiniTest::Unit::TestCase
4
+
5
+ def zip(path = "#{samples_path}/sample.zip")
6
+ @zip ||= Swathe::Zip.open(path)
7
+ end
8
+
9
+ def test_is_a_zip
10
+ assert zip.zip?
11
+ end
12
+
13
+ def test_file_count
14
+ assert_equal 1, zip.files.count
15
+ end
16
+
17
+ def test_file_list
18
+ assert_equal ["samples/file.txt"], zip.files.collect(&:full_name)
19
+ end
20
+
21
+ # TODO: Apparently zip directories don't work this way.
22
+ # Or I screwed up the sample zip. Figure this out.
23
+ # def test_directory_list
24
+ # assert_equal ["samples/"], zip.directories.collect(&:full_name)
25
+ # end
26
+
27
+ def test_read_file
28
+ assert_match %r(This is a sample file to test extraction), zip.entry('samples/file.txt').read
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swathe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Schenkman-Moore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rubyzip
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: An abstraction for dealing with common archive tasks.
63
+ email:
64
+ - samsm@samsm.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .ruby-gemset
71
+ - .ruby-version
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/swathe.rb
77
+ - lib/swathe/archive.rb
78
+ - lib/swathe/extractor.rb
79
+ - lib/swathe/gzip.rb
80
+ - lib/swathe/tar.rb
81
+ - lib/swathe/tar_entry.rb
82
+ - lib/swathe/version.rb
83
+ - lib/swathe/zip.rb
84
+ - samples/file.txt
85
+ - samples/sample.tar
86
+ - samples/sample.tgz
87
+ - samples/sample.zip
88
+ - samples/tar_with_directory.tar
89
+ - swathe.gemspec
90
+ - test/archive_test.rb
91
+ - test/extractor_test.rb
92
+ - test/gzip_test.rb
93
+ - test/minitest_helper.rb
94
+ - test/swathe_test.rb
95
+ - test/tar_test.rb
96
+ - test/zip_test.rb
97
+ homepage: ''
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 3367134578741255156
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: 3367134578741255156
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.25
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Copies files out of archives.
127
+ test_files:
128
+ - test/archive_test.rb
129
+ - test/extractor_test.rb
130
+ - test/gzip_test.rb
131
+ - test/minitest_helper.rb
132
+ - test/swathe_test.rb
133
+ - test/tar_test.rb
134
+ - test/zip_test.rb