mds_file_utils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00816f3cb1bc8ad3796ccb573089afa8e17ce915
4
+ data.tar.gz: 0a23e03afd1c15fd3da9b1f1f1707c1610ef699c
5
+ SHA512:
6
+ metadata.gz: 30d7ab027c989f3abeeb9e407fdf828916f989b6cdd15e9141a15445fee3d6d4d444d224da2c812ab5462fad1f42cde76559e13d911e09b9e0933dcd590d3d72
7
+ data.tar.gz: ff0a9c37f6ee2d8015dbd5ded12f3fae27be3b4a7f52134b5bb119348692f6b4c6e62a8508087b25f5fb3ecdd632b2216ba5214617062df0e8fbe24b3b06c40d
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = mds_file_utils
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to mds_file_utils
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Dave. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mds_file_utils"
18
+ gem.homepage = "http://github.com/dj0hnve@gmail.com/mds_file_utils"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A collection of MDS file utilities.}
21
+ gem.description = %Q{A set of useful MDS file utilities.}
22
+ gem.email = "dave.sieh@providigm.com"
23
+ gem.authors = ["Dave"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "mds_file_utils #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_support/core_ext/object'
2
+ require 'unzipMe'
3
+ require 'mds_file_utils/zip_analyzer'
4
+ require 'mds_file_utils/zip_splitter'
@@ -0,0 +1,3 @@
1
+ module MdsFileUtils
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ module MdsFileUtils
2
+
3
+ class ZipAnalyzer
4
+
5
+ EXTENSION_REGEX = /\.zip\z/i
6
+ VALID_COMPOSITE_CONTENTS_REGEX = /(\.zip|\/|\\)\z/i
7
+
8
+ def initialize(path, original_filename=path)
9
+ @path = path
10
+ @original_filename = original_filename
11
+ end
12
+
13
+ def valid_zip?
14
+ return false if @path.blank?
15
+ return false unless File.exists?(@path)
16
+ return false unless has_valid_extension?
17
+ UnzipmeValidator.new(@path).valid_zip?
18
+ end
19
+
20
+ def has_valid_extension?
21
+ return false if @original_filename.blank?
22
+ @original_filename =~ EXTENSION_REGEX
23
+ end
24
+
25
+ def is_composite?
26
+ return false unless valid_zip?
27
+ file_list.count { | file | file =~ EXTENSION_REGEX } > 0
28
+ end
29
+
30
+ def file_list
31
+ @files ||= UnzipmeUnzipper.new(@path).file_list
32
+ end
33
+
34
+ def non_mds_data_in_composite?
35
+ file_list.count { | entry | !( entry =~ VALID_COMPOSITE_CONTENTS_REGEX) } > 0
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,92 @@
1
+ module MdsFileUtils
2
+
3
+ class ZipSplitter
4
+
5
+ def initialize(path, original_filename=path)
6
+ @path = path
7
+ @original_filename = original_filename
8
+ end
9
+
10
+ def split(tmp_storage_path='/tmp')
11
+ splitter_path = File.join(tmp_storage_path, self.class.name)
12
+
13
+ zip_base_name = base_file_name
14
+
15
+ # Create a directory in which to place the contents of the zip file
16
+ extracted_path = File.join(splitter_path, EXTRACTED_DIR, zip_base_name)
17
+ mkdir_p extracted_path
18
+
19
+ # Extract the contents of the zip file to the extracted path
20
+ %x{unzip -d #{extracted_path} '#{@path}'}
21
+ raise "Error extracting ZIP file '#{@path}' to '#{extracted_path}'" if $? != 0
22
+
23
+ entries = directory_entries(extracted_path)
24
+
25
+ if entries.size > FILES_PER_ZIP
26
+ result_zip_file = create_composite_zip_from_entries(zip_base_name, entries, splitter_path, extracted_path)
27
+ else
28
+ # The file is fine as is; just copy the file to the storage
29
+ # location and return the path
30
+ result_zip_file = File.join(splitter_path, File.basename(@path))
31
+ cp @path, composite_zip_file
32
+ end
33
+
34
+ result_zip_file
35
+ end
36
+
37
+ private
38
+
39
+ def create_composite_zip_from_entries(zip_base_name, entries, splitter_path, extracted_path)
40
+ components_path = File.join(splitter_path, COMPONENTS_DIR)
41
+ mkdir_p components_path
42
+ index = 0
43
+
44
+ entries.each_slice(FILES_PER_ZIP) do |files|
45
+ new_partial_name = "#{zip_base_name}_#{index}"
46
+ partial_path = File.join(splitter_path, new_partial_name)
47
+ component_path = File.join(components_path, "#{new_partial_name}.zip")
48
+ mkdir_p partial_path
49
+ # Move all the files in this slice to the new partial path
50
+ files.each { |f| mv File.join(extracted_path, f), partial_path }
51
+ # Now, zip up all the files into it's own component zip file
52
+ %x{cd #{partial_path}; zip #{component_path} *}
53
+ raise "Error zipping up files in '#{partial_path}' into '#{component_path}'" if $? != 0
54
+ # Delete the partial dir
55
+ rm_rf partial_path
56
+ index += 1
57
+ end
58
+
59
+ # Once we get here, all the component files have been built. Package up the
60
+ # components into a new zip
61
+ result_zip_file = File.join(splitter_path, "CMP_#{File.basename(@path)}")
62
+ %x{cd #{components_path}; zip #{result_zip_file} *}
63
+ raise "Error zipping up the components of the composite file: #{result_zip_file}" if $? != 0
64
+
65
+ # Some housekeeping
66
+ rm_rf File.dirname(extracted_path)
67
+ rm_rf components_path
68
+
69
+ result_zip_file
70
+ end
71
+
72
+ #
73
+ # Return the entries in the directory (without the '.' and '..'
74
+ #
75
+ def directory_entries(path)
76
+ Dir.entries(path).tap do |entries|
77
+ # Get rid of the directory entries
78
+ entries.delete('.')
79
+ entries.delete('..')
80
+ end
81
+ end
82
+
83
+ def base_file_name
84
+ basename = File.basename(@original_filename)
85
+ if index = basename.rindex('.')
86
+ basename = basename[0...index]
87
+ end
88
+ basename
89
+ end
90
+ end
91
+
92
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mds_file_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kendra Lawlor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.14
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.14
27
+ - !ruby/object:Gem::Dependency
28
+ name: unzipMe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 2.12.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.12.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jeweler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 1.8.7
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 1.8.7
97
+ description: ''
98
+ email:
99
+ - klawlor@providigm.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - README.rdoc
105
+ - Rakefile
106
+ - lib/mds_file_utils.rb
107
+ - lib/mds_file_utils/version.rb
108
+ - lib/mds_file_utils/zip_analyzer.rb
109
+ - lib/mds_file_utils/zip_splitter.rb
110
+ homepage: http://www.providigm.com
111
+ licenses: []
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.3.0
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Allows MDS Uploads
133
+ test_files: []