class Extracter::Extracter
Constants
- ARRAY_REGISTERED_ARCHIVES
#¶ ↑
ARRAY_REGISTERED_ARCHIVES
¶ ↑Archives that can be extracted, have to be registered in this Array.
Sort alphabetically.
The libreoffice format .odt is just like .zip.
#¶ ↑
- GEM_UNPACK_COMMAND
#¶ ↑
GEM_UNPACK_COMMAND
¶ ↑The command to use to unpack ruby .gems. We can pass the —target=DIR syntax to extract to a specific location.
#¶ ↑
- LAST_UPDATED
#¶ ↑
LAST_UPDATED
¶ ↑When this class was last updated/releasted. This is not a hugely important constat, so do not worry too much if this may be heavily outdated eventually.
#¶ ↑
- N
#¶ ↑
N
¶ ↑#¶ ↑
- NAMESPACE
#¶ ↑
NAMESPACE
¶ ↑#¶ ↑
- SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
#¶ ↑
SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
¶ ↑If this constant is set to true then we will only show the shortened name of the archive in question by default.
#¶ ↑
- TEMP_DIR
#¶ ↑
If this environment variable is unavailable then use a conservative default value.
#¶ ↑
- VERSION
#¶ ↑
VERSION
¶ ↑Which specific version to use for class
Extracter
.#¶ ↑
Attributes
Public Class Methods
#¶ ↑
Extracter::Extracter.extract_what_to
¶ ↑
This method provides a convenient API to extract something to a specified directory, as a class method. It defaults to the current working directory, as that is by far the most convenient way to extract a source tarball/archive.
Useage example goes like this:
Extracter.extract_what_to('foo-1.0.tar.xz', '/tmp')
#¶ ↑
# File lib/extracter/class_methods.rb, line 41 def self.extract_what_to( what, to = Dir.pwd, # <- This can also be a Hash. optional_be_silent = false ) _ = Extracter.new(what, to, :do_not_run_yet) if to.is_a? Hash # ===================================================================== # # === :prepend_this_namespace # ===================================================================== # if to.has_key? :prepend_this_namespace prefix_with_this = to.delete(:prepend_this_namespace) # Get rid of it from the Hash as well. _.prefix_namespace_with( prefix_with_this ) end # ===================================================================== # # === :to # ===================================================================== # if to.has_key? :to to = to.delete(:to) end # ===================================================================== # # === :verbosity # # Handle how verbose we shall be. # ===================================================================== # if to.has_key? :verbosity _.set_verbosity( to.delete(:verbosity) ) end # ===================================================================== # # === :use_colours # ===================================================================== # if to.has_key? :use_colours _.set_use_colours(to[:use_colours]) end # ===================================================================== # # === :pad_opn_with_n_tokens # ===================================================================== # if to.has_key? :pad_opn_with_n_tokens _.set_pad_opn_with_n_tokens(to[:pad_opn_with_n_tokens]) end # ===================================================================== # # === :use_opn # ===================================================================== # if to.has_key? :use_opn _.set_use_opn(to[:use_opn]) end end if optional_be_silent _.be_silent else _.be_verbose end # ======================================================================= # # === Handle blocks next # ======================================================================= # if block_given? yielded = yield case yielded when :show_the_full_name_of_the_archive _.do_show_the_full_name_of_the_archive end end _.run end
#¶ ↑
Extracter::Extracter.is_this_a_valid_archive?
¶ ↑
Query whether the input is a valid archive.
The registered formats are stored in the constant ARRAY_REGISTERED_ARCHIVES
.
#¶ ↑
# File lib/extracter/class_methods.rb, line 17 def self.is_this_a_valid_archive?( i, array_registered_archives = ARRAY_REGISTERED_ARCHIVES ) return_value = false array_registered_archives.each {|entry| return_value = true if i =~ /#{entry}$/ } return return_value end
#¶ ↑
initialize¶ ↑
The first argument to this method is the archive that should be extracted. This must be a (locally) existing archive, such as foobar.tar.xz or something similar.
The second argument to this method, called `to`, specifies the target location, where this class will extract the archive into, if available. Some keywords and shortcuts exist for this option - for instance, TEMP means $MY_TEMP, which can be set by the user.
Specific usage example in pure Ruby:
x = Extracter.what_to('pry-0.9.9.4.gem', '/Depot/Temp')
#¶ ↑
# File lib/extracter/initialize.rb, line 28 def initialize( what = nil, where_to = nil, # Where to extract into. run_already = true, &block ) register_sigint reset if where_to.is_a? Symbol if where_to.to_s.include? 'dont' # This also covers :dont_run_yet where_to = nil # This is ok because we expect the user to provide the target location. run_already = false end end if debug? # Some debug-information. e "The first argument what is: `#{what}`" e "The second argument to is: `#{to}`" end set_source_location(what) set_extract_to(where_to) # ======================================================================= # # === Handle blocks next # ======================================================================= # if block_given? yielded = yield # ===================================================================== # # === Handle Hash input next: # ===================================================================== # if yielded.is_a? Hash if yielded.has_key? :run_already run_already = yielded.delete(:run_already) end # =================================================================== # # === :use_colours # =================================================================== # if yielded.has_key? :use_colours set_use_colours yielded.delete(:use_colours) end # =================================================================== # # === :extract_to # =================================================================== # if yielded.has_key? :extract_to set_extract_to yielded.delete(:extract_to) end # =================================================================== # # === :run_simulation # =================================================================== # if yielded.has_key? :run_simulation set_run_simulation(yielded.delete(:run_simulation)) end # =================================================================== # # === :use_opn # =================================================================== # if yielded.has_key? :use_opn set_use_opn(yielded.delete(:use_opn)) end else case yielded when :dont_run_yet, :do_not_run_yet run_already = false end end end case run_already when :dont_run_yet, :do_not_run_yet run_already = false end run if run_already end
Public Instance Methods
#¶ ↑
check_whether_rar_is_available
¶ ↑
We try to find out whether unrar is available.
#¶ ↑
# File lib/extracter/misc.rb, line 76 def check_whether_rar_is_available is_available = false ENV['PATH'].split(':').each {|entry| is_available = true if File.exist? entry+'/unrar' } unless is_available copn; e 'Sorry, unrar is not available. Please install it first.' end end
#¶ ↑
create_directory
¶ ↑
Use this to create directories.
#¶ ↑
# File lib/extracter/misc.rb, line 354 def create_directory(i) FileUtils.mkdir_p(i) end
#¶ ↑
custom_opn
¶ ↑
This is like opn(), except that we also check whether we should show the name or not.
#¶ ↑
# File lib/extracter/opn.rb, line 19 def custom_opn opnn unless @do_not_show_name end
#¶ ↑
determine_default_opn_hash
¶ ↑
#¶ ↑
# File lib/extracter/misc.rb, line 329 def determine_default_opn_hash @use_this_opn_hash = { namespace: namespace?, use_colours: use_colours? } end
#¶ ↑
did_we_extract_already
?¶ ↑
Whether we already did extract or whether we did not.
#¶ ↑
# File lib/extracter/misc.rb, line 184 def did_we_extract_already? @did_we_extract_already end
#¶ ↑
do_extract_what_to
¶ ↑
This method should only be called from the method work_on_the_given_input
().
It will attempt to extract the specified archive.
#¶ ↑
# File lib/extracter/do_extract_what_to.rb, line 19 def do_extract_what_to( what, # ← The archive or file that we wish to extract. to = extract_to? ) name_of_the_archive = what.dup @did_we_extract_already = false to = extract_to? if to.nil? set_source_location(what) set_extract_to_this_location(to) # Keep it in sync. # ======================================================================= # # Create the directory if it does not yet exist. # ======================================================================= # create_directory(to) if !File.directory?(to) if be_verbose? if @show_only_the_short_name_of_the_archive name_of_the_archive = File.basename(name_of_the_archive) copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next." else copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next." end end _ = ''.dup # Default. extname = File.extname(what) unless ARRAY_REGISTERED_ARCHIVES.include? extname.delete('.') opn; e 'We did not register the following extension: '+extname.delete('.') fail_message_not_registered(extname) @skip_extracting = true end case extname # Case tag. Those listed on top are more important. # ======================================================================= # # === .tar # ======================================================================= # when '.tar', '.tar.bz2', '.tbz' _ << 'tar -xvf' # ======================================================================= # # === pdf # # For pdf-files we will tap into the pdf_paradise project. # ======================================================================= # when /\.?pdf$/ begin require 'pdf_paradise/convert_pdf_to_text.rb' _ = PdfParadise::ConvertPdfToText.new(what) _.output_file? rescue LoadError; end return # Must return early in this case. # ======================================================================= # # === img # # Note that .img in this context refers to squafhs .img files. # ======================================================================= # when '.img', '.squashfs' opnn; e 'Handling a squashfs .img file format next:' name_without_extension = what.sub(/#{File.extname(what)}$/,'') mkdir(name_without_extension) unless File.directory? name_without_extension esystem 'mount -o loop -t squashfs '+what+' '+name_without_extension e 'The content of the extracted (or rather, mounted) archive is:' pp Dir["#{name_without_extension}*"] return # Must return early in this case. # ======================================================================= # # === iso # ======================================================================= # when '.iso' opnn; e 'Extracting an .iso file is a bit more complicated '\ 'than a .tar.gz tarball release.' opnn; e 'We will first create a directory; and then mount '\ 'the .iso there.' name_without_extension = what.sub(/#{File.extname(what)}$/,'') mkdir(name_without_extension) unless File.directory? name_without_extension esystem 'mount -o loop '+what+' '+name_without_extension e 'The content of the extracted (or rather, mounted) archive is:' pp Dir[name_without_extension+'*'] return # ======================================================================= # # === sxz # ======================================================================= # when '.sxz' _ = 'unsquashfs '.dup # This requires squashfs with xz-support. # ======================================================================= # # === lz # ======================================================================= # when '.lz','.tar.lz' _ = 'tar --lzip -xvf '.dup # This requires lzip to be installed. # ======================================================================= # # === tar.Z # ======================================================================= # when '.tar.Z','.taz' _ << 'tar -xvzf' # ======================================================================= # # === .jar # ======================================================================= # when /\.?jar$/i _ << 'jar xvf' # ======================================================================= # # === .zst # # This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst". # ======================================================================= # when '.zst','.tar.zst' _ << 'tar -I zstd -xvf ' # ======================================================================= # # === txz # ======================================================================= # when '.txz' _ << 'tar Jxvf' # Since Jan 2012. # ======================================================================= # # === gz # ======================================================================= # when '.gz' if what.include? '.tar' _ << 'tar -zxvf' else _ << 'gunzip' end # ======================================================================= # # === xz # ======================================================================= # when '.xz' if _.include? '.tar' end _ << 'tar -xvf' # tar -Jxv #{what} would be an alternative. # ======================================================================= # # === rpm # ======================================================================= # when '.rpm' _ << 'bsdtar xfv' # ======================================================================= # # === bin # ======================================================================= # when '.bin' # handle .bin files here. # _ = 'tar -jxf '+package _ << "./#{what}" # ======================================================================= # # === zip # ======================================================================= # when '.zip','.xpi','.docx','.odt', # .docx and .odt format types can be unpacked with zip too. '.apkg' # _ << 'ar -jxf' # unzip #{what} <-- this should work as well. _ << 'unzip ' when /\.bz2/,'.tbz2' if what.include? '.tar' # Treat it as a .tar file. _ << 'tar -vjxf ' else _ << 'bunzip2 ' end # ======================================================================= # # === lzma # ======================================================================= # when '.lzma' _ << 'unlzma ' # ======================================================================= # # === 7z # ======================================================================= # when '.7z' # 7z does not accept the -C commandline. # _ << '7za e' # <- Deprecated as of 05.06.2020. _ << '7z x' # ======================================================================= # # === gem # ======================================================================= # when '.gem' _ << GEM_UNPACK_COMMAND # ======================================================================= # # === rar # ======================================================================= # when '.rar' check_whether_rar_is_available _ << 'unrar e' # ======================================================================= # # === deb # ======================================================================= # when '.deb' #_ = 'dpkg-deb -x' # {to} _ << 'ar -x' # ar -x #{what} This could work too. # ======================================================================= # # === tgz # ======================================================================= # when '.tgz' _ << 'tar -xvzf' # ======================================================================= # # === ps # ======================================================================= # when '.ps' _ << 'ps2ascii' # ======================================================================= # # === mp4 # ======================================================================= # when '.mp4' # If it is a .mp4 file, we delegate to ExtractAudio instead. if Object.const_defined? :MultimediaParadise MultimediaParadise.extract_audio(@source_package_location) end exit else # else tag. We did not find the extension type. @skip_extracting = true copn; ewarn "We did not find: `#{sfile(what)}`. " copn; ewarn 'The file-type (extension) was: `'+simp(extname)+'`.' # Try to rescue though. result = run_this_system_command("file #{what}") copn; e result if result.include? 'bzip2 compressed' copn; e 'We assume it is a .bz2 file though.' _ = 'tar -vjxf '.dup @skip_extracting = false end end unless @skip_extracting unless File.exist? what @skip_extracting = true copn; e "The file `#{sfile(what)}"\ "` does not exist - can not extract." end # ======================================================================= # # Handle the situation when the given input includes a ')' character. # We will pad such an input with '"' characters. # ======================================================================= # if what.include? ')' what = pad(what, '"') #sanitize_input(what) end # ======================================================================= # # Next, pad it if it includes a ' ' character. # ======================================================================= # what = pad(what) if what.include?(' ') _ << " #{what}" unless _.empty? if _.include? GEM_UNPACK_COMMAND # Gem command needs a --target=DIR option. _ << " --target=#{to}" elsif _.include?('ar -x') and ! _.include?('.tar') # Do nothing in this case. elsif _.end_with? '.sxz' # .sxz does not require the -C option. elsif _.end_with? '.zip','.xpi','.7z','.jar','.apkg' # Do not use -C option for 7z, as it hates this option. # .jar files also do not support the -C option. elsif _.include? 'bunzip' # Do not use -C option for bunzip, as it hates this option. else # ===================================================================== # # Next, we need to determine the location where we extract our # archive to. # ===================================================================== # _ << ' ' # ===================================================================== # # Add -C option except for .deb packages and gunzip-based archives. # ===================================================================== # unless _ =~ /deb$/ or _.include?('gunzip') _ << '-C ' _ << to end end if run_simulation? copn; e 'As we are running in simulation mode, the following command ' copn; e 'is the one that we would have been used if we were to not run ' copn; e 'in simulation mode:' e _ else # Ok, here we are not in a simulation, hence we can run the command. unless @skip_extracting if File.writable? to # ================================================================= # # Next, run the sys-command, with some padding. # ================================================================= # begin run_this_system_command( " #{_}", :also_show_what_we_will_do ) # ================================================================= # # We have to rescue because unrar might not be available and so on. # ================================================================= # rescue Exception => error e 'An error has happened upon attempting to run this system command:' e e " #{_}" e pp error e '-----------' pp error.class e '-----------' end @did_we_extract_already = true else copn; ewarn 'You do not have sufficient permissions to' copn; ewarn "modify #{sdir(to)}." end end end end
#¶ ↑
extract_to
?¶ ↑
Simply output the result of @extract_to_this_location variable.
#¶ ↑
# File lib/extracter/misc.rb, line 112 def extract_to? @extract_to_this_location.to_s end
#¶ ↑
extract_to_this_location
?¶ ↑
Extract to this location.
#¶ ↑
# File lib/extracter/misc.rb, line 193 def extract_to_this_location? @extract_to_this_location end
#¶ ↑
extracted_to?¶ ↑
This method is different from extract_to
?.
It will keep track of the directory to where we extracted to exactly.
#¶ ↑
# File lib/extracter/misc.rb, line 205 def extracted_to? rds( extract_to?+ File.basename(input?).sub(/\.xz$/,'').sub(/\.gz$/,''). sub(/\.tar$/,'')+'/' ) end
#¶ ↑
pad_opn_with_n_tokens
¶ ↑
#¶ ↑
# File lib/extracter/opn.rb, line 42 def pad_opn_with_n_tokens(n_tokens) determine_default_opn_hash # Update this, just in case. @use_this_opn_hash.update(padding: n_tokens) end
#¶ ↑
report_to_the_user
¶ ↑
This method reports to the user. Usually this is done only via this file here though.
#¶ ↑
# File lib/extracter/misc.rb, line 281 def report_to_the_user if @be_verbose unless @skip_extracting copn; e 'Finished extracting to `'+sdir( extract_to?+remove_file_extension( @source_location.first # This is an Array. )+'/' )+'`.' end end end
#¶ ↑
reset (reset tag)¶ ↑
Reset our main instance variables here.
#¶ ↑
# File lib/extracter/reset.rb, line 14 def reset # ======================================================================= # # === @namespace # # Specify the main namespace to be used. This setting can be modified # at "runtime". # ======================================================================= # @namespace = NAMESPACE # ======================================================================= # # === @debug # ======================================================================= # @debug = false # ======================================================================= # # === Extract to this location # # Next specify where to extract the archive in question, defaulting to # the TEMP_DIR constant. # ======================================================================= # @extract_to_this_location = TEMP_DIR # ======================================================================= # # === @did_we_extract_already # # We will keep track of whether we already extracted or not. # ======================================================================= # @did_we_extract_already = false # ======================================================================= # # === @do_not_show_name # ======================================================================= # @do_not_show_name = false # ======================================================================= # # === @use_opn # ======================================================================= # @use_opn = true # ← Whether to use Opn by default or not. # ======================================================================= # # === @show_only_the_short_name_of_the_archive # ======================================================================= # @show_only_the_short_name_of_the_archive = SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE # ======================================================================= # # === @skip_extracting # ======================================================================= # @skip_extracting = false # ======================================================================= # # === @run_simulation # ======================================================================= # @run_simulation = false # ← Whether to run in simulation, or for "real". set_extract_to_this_location do_show_name # We will show the name usually. enable_colours determine_default_opn_hash set_be_verbose(false) end
#¶ ↑
set_be_verbose
¶ ↑
This sets the verbosity level of the class. Use only this method when you wish to modify the @be_verbose instance variable.
#¶ ↑
# File lib/extracter/misc.rb, line 322 def set_be_verbose(i = false) @be_verbose = i end
#¶ ↑
set_extract_to_this_location
¶ ↑
Use this when setting the variable @extract_to_this_location.
This can be modified from the commandline such as by doing this:
#¶ ↑
# File lib/extracter/misc.rb, line 227 def set_extract_to_this_location( i = TEMP_DIR ) if i.is_a? Hash if i.has_key? :to i = i.delete :to elsif i.has_key? :extract_to i = i.delete :extract_to end end case i # case tag when :default i = Dir.pwd when 'TEMP', 'MY_TEMP', 'MYTEMP' i = TEMP_DIR end i = TEMP_DIR if i.nil? i = i.to_s.dup i << '/' unless i.end_with? '/' i = rds(i) i.gsub!(/--to=/,'') if i.include? '--to=' @extract_to_this_location = i end
#¶ ↑
set_source_location
¶ ↑
Use this method to designate the source location of a given (input) program. In other words - the tarball or archive that must be extracted. It will however be stored as Array.
If we pass a hash to this method, we assume that the user wants to also populate some other values.
#¶ ↑
# File lib/extracter/misc.rb, line 126 def set_source_location(i = nil) if i.nil? copn; e 'You should set a file.' end if i.is_a? Array i = i.flatten else i = [i] end # After this point, we will have an Array. case i.first # case tag when /^-?-?help$/i show_help exit end i.map! {|entry| # Iterate over our Array next. # ======================================================================= # # Handle the case when the user did input a number. # ======================================================================= # begin if entry =~ /^\d$/ entry = Dir['*'][( entry.to_i - 1 )] unless File.exist?(entry) end rescue ArgumentError => error e 'Error for '+sfancy(entry)+':' pp error end entry = rds(entry.to_s) # ======================================================================= # # Next, find the proper working directory. # ======================================================================= # unless entry.include? '/' entry = rds( (Dir.pwd+'/'+entry) ) end # ======================================================================= # # If the user supplied a directory instead, we will randomly grab an # entry from said directory. # ======================================================================= # if File.directory? entry entry = Dir[rds(entry+'/')+'*'].sample end entry } # Sanitize the result, just in case. i = [i] unless i.is_a? Array # Much more convenient to work with an array. @source_location = i end
#¶ ↑
set_use_colours
¶ ↑
#¶ ↑
# File lib/extracter/colours.rb, line 25 def set_use_colours(i) # ======================================================================= # # We must also sync this towards our main Hash, for opn(). The next # line of code achieves precisely that. # ======================================================================= # @use_this_opn_hash.update(use_colours: i) @use_colours = i end
#¶ ↑
show_help
(help tag)¶ ↑
This method will show the available - and documented - help options for class Extracter
.
# ¶ ↑
# File lib/extracter/help.rb, line 15 def show_help e opnn; e 'How to extract archives, without helper scripts?' e opnn; e ' tar -zxvf foobar.tar.gz # for .tar.gz' opnn; e ' tar xvzf foobar.tgz # for .tgz' opnn; e ' tar xvfJ foobar.tar.xz # for .tar.xz' opnn; e ' tar jxf foobar.tar.bz2 # for .tar.bz2' opnn; e ' tar -xf foobar.tar.bz2 # for .tbz' opnn; e ' tar --lzip -xvf zutils-1.5.tar.lz # for .tar.lz' opnn; e ' unsquashfs foobar-1.2.3.sxz # for .sxz' e opnn; e 'Furthermore, there are some commandline options '\ 'that can be used for this class (class Extracter).' e opnn; e ' --to=/home/Temp # extract into the '\ 'directory /home/Temp/' e end
#¶ ↑
source_location?¶ ↑
#¶ ↑
# File lib/extracter/misc.rb, line 175 def source_location? @source_location.first end
#¶ ↑
work_on_the_given_input
¶ ↑
#¶ ↑
# File lib/extracter/misc.rb, line 408 def work_on_the_given_input if @source_location.empty? copn; e 'Can not extract anything as no input has been given.' else @source_location.each {|entry| if Extracter.is_this_a_valid_archive?(entry) do_extract_what_to(entry) report_to_the_user else fail_message_not_registered(entry) end } end end