Class: Unpack
- Inherits:
-
Object
- Object
- Unpack
- Defined in:
- lib/unpack.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#files ⇒ Object
Returns the value of attribute files.
-
#options ⇒ Object
Returns the value of attribute options.
-
#removeable ⇒ Object
Returns the value of attribute removeable.
Class Method Summary collapse
Instance Method Summary collapse
- #clean! ⇒ Object
- #diff ⇒ Object
- #find_file_type(file_type) ⇒ Object
-
#initialize(args) ⇒ Unpack
constructor
A new instance of Unpack.
- #prepare! ⇒ Object
- #unpack! ⇒ Object
- #unrar(args) ⇒ Object
- #unzip(args) ⇒ Object
-
#wipe! ⇒ Object
Removes the old rar and zip files.
Constructor Details
#initialize(args) ⇒ Unpack
Returns a new instance of Unpack.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/unpack.rb', line 8 def initialize(args) args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] } @options = { :min_files => 5, :depth => 2, :debugger => false, :force_remove => false, :remove => false, :to => false, :absolute_path_to_unrar => "#{File.dirname(__FILE__)}/../bin/unrar" } @removeable = {} @options.merge!(args[:options]) if args[:options] # If the path is relative @directory = File.(@directory) unless @directory.match(/^\//) # Makes shure that every directory structure looks the same @directory = Dir.new(@directory).path rescue nil raise Exception.new("You need to specify a valid path") if @directory.nil? or not system("test -r '#{@directory}'") raise Exception.new("You need unzip to keep going") if %x{whereis unzip}.empty? @files = [] end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
6 7 8 |
# File 'lib/unpack.rb', line 6 def directory @directory end |
#files ⇒ Object
Returns the value of attribute files.
6 7 8 |
# File 'lib/unpack.rb', line 6 def files @files end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/unpack.rb', line 6 def @options end |
#removeable ⇒ Object
Returns the value of attribute removeable.
6 7 8 |
# File 'lib/unpack.rb', line 6 def removeable @removeable end |
Class Method Details
.it!(args) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/unpack.rb', line 37 def self.it!(args) # If no to argument is given, file will be unpacked in the same dir args[:to] = args[:to].nil? ? File.dirname(args[:file]) : args[:to] # Adding the options that is being passed to {it!} directly to {Unpack} this = self.new(:directory => args[:to], :options => {:min_files => 0, :to => true}.merge(args)) # Is the file path absolute ? good, do nothing : get the absolute path file = args[:file].match(/^\//) ? args[:file] : File.(args[:file]) this.files << file this.unpack! this.wipe! if this.[:remove] # Only one files has been unpacked, that is why we select the first object in the list this.diff.first end |
.runner!(directory = '.', options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/unpack.rb', line 55 def self.runner!(directory = '.', = {}) unpack = Unpack.new(:directory => directory, :options => ) rescue nil # If the initializer raised any excetions return [] if unpack.nil? unpack.prepare! unpack.clean! unpack.unpack! unpack.wipe! if [:remove] unpack.diff end |
Instance Method Details
#clean! ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/unpack.rb', line 77 def clean! # Removing all folders that have less then {@options[:lim]} files in them # Those folders are offen subtitle folders folders = @files.map {|file| File.dirname(file)}.uniq.reject {|folder| Dir.entries(folder).count <= (@options[:min_files] + 2)} @files.reject!{|file| not folders.include?(File.dirname(file))} results = [] # Finding one rar file for every folder @files.group_by{|file| File.dirname(file) }.each_pair{|_,file| results << file.sort.first } @files = results end |
#diff ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/unpack.rb', line 144 def diff # The code below this line can only be called once return @removeable if @removeable.first.class == Container # Removing some non welcome data @removeable.reject! do |item,_| @removeable[item][:diff].nil? or @removeable[item][:diff].empty? end @removeable = @removeable.map do |value| Container.new(:files => value.last[:diff], :directory => value.first) end # Never return the hash @removeable.empty? ? [] : @removeable end |
#find_file_type(file_type) ⇒ Object
169 170 171 |
# File 'lib/unpack.rb', line 169 def find_file_type(file_type) %x{cd '#{@directory}' && find '#{@directory}' -type f -maxdepth '#{(@options[:depth])}' -name '*.#{file_type}'}.split(/\n/) end |
#prepare! ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/unpack.rb', line 67 def prepare! @files = [] ['zip', 'rar'].each do |type| @files << find_file_type(type) end @files.flatten!.map! {|file| File.(file, Dir.pwd)} end |
#unpack! ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/unpack.rb', line 89 def unpack! @files.each do |file| type = Mimer.identify(file) # Have the user specified a destenation folder to where the files are going to be unpacked? # If so, use that, if not use the current location of the archive file path = self.[:to] ? @directory : File.dirname(file) before = Dir.new(path).entries if type.zip? @removeable.merge!(path => {:file_type => 'zip'}) self.unzip(:path => path, :file => file) elsif type.rar? @removeable.merge!(path => {:file_type => 'rar'}) self.unrar(:path => path, :file => file) else puts "Something went wrong, the mime type does not match zip or rar" if @options[:debugger] end # What files/folders where unpacked? diff = Dir.new(path).entries - before @removeable[path] ? @removeable[path].merge!(:diff => diff) : @removeable.delete(path) # Some debug info if @options[:debugger] and diff.any? and @removeable[path] puts "#{diff.count} where unpacked" puts "The archive was of type #{@removeable[path][:file_type]}" puts "The name of the file(s) are #{diff.join(', ')}" puts "The path is #{path}" STDOUT.flush end end end |
#unrar(args) ⇒ Object
161 162 163 |
# File 'lib/unpack.rb', line 161 def unrar(args) %x(cd '#{args[:path]}' && '#{@options[:absolute_path_to_unrar]}' e -y -o- '#{args[:file]}') end |
#unzip(args) ⇒ Object
165 166 167 |
# File 'lib/unpack.rb', line 165 def unzip(args) %x(unzip -n '#{args[:file]}' -d '#{args[:path]}') end |
#wipe! ⇒ Object
Removes the old rar and zip files
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/unpack.rb', line 126 def wipe! @removeable.each do |value| path = value.first type = value.last[:file_type] # Does not remove anything if nothing where unpacked next if value.last[:diff].empty? and not @options[:force_remove] puts "Removing files in #{path}" if @options[:debugger] # Finding every file in this directory Dir.glob(path + '/*').each do |file| # Is the found file as the same type as the one that got unpacked? FileUtils.rm(file) if Mimer.identify(file).send(:"#{type}?") end end end |