class Repackage::Repackage
Constants
- COMMAND_TO_CREATE_A_TAR_BZ2_ARCHIVE
#¶ ↑
COMMAND_TO_CREATE_A_TAR_BZ2_ARCHIVE
¶ ↑Command to create a .tar.bz2 archive.
#¶ ↑
- COMMAND_TO_CREATE_A_TAR_GZ_ARCHIVE
#¶ ↑
COMMAND_TO_CREATE_A_TAR_GZ_ARCHIVE
¶ ↑Command to create a .tar.gz archive.
#¶ ↑
- COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE
#¶ ↑
COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE
¶ ↑This is the command that is used when a .tar.xz file is to be created.
#¶ ↑
- COMMAND_TO_CREATE_A_ZIP_ARCHIVE
#¶ ↑
COMMAND_TO_CREATE_A_ZIP_ARCHIVE
¶ ↑Command to create a .zip archive.
#¶ ↑
- DEFAULT_EXTRACT_TO_THIS_DIRECTORY
- DEFAULT_TARGET_FORMAT_TYPE
#¶ ↑
DEFAULT_TARGET_FORMAT_TYPE
¶ ↑The target format comes here, thus allowing the user to change the default.
#¶ ↑
- DELETE_CODE_OF_CONDUCT_FILE_IF_IT_EXISTS
#¶ ↑
DELETE_CODE_OF_CONDUCT_FILE_IF_IT_EXISTS
¶ ↑The following variable is set to false by default. It has been added in September 2022 because another project (rbt) requires this setting - that is to toggle it on or off.
#¶ ↑
- LAST_DOWNLOADED_FILE
- NAMESPACE
#¶ ↑
NAMESPACE
¶ ↑#¶ ↑
- SHALL_WE_DELETE_THE_OLD_ARCHIVE
#¶ ↑
SHALL_WE_DELETE_THE_OLD_ARCHIVE
¶ ↑Remove the old source if this constant is true. Since as of June 2021 this defaults to false, except in certain conditions met on my home setup where the variable that keeps track of it is turned to true actually. Most other users will probably never need to change this, though, so the default is to false.
#¶ ↑
Public Class Methods
#¶ ↑
initialize¶ ↑
The first argument given to this class should be the name, or the path, to a locally existing file, such as “foobar-1.0.tar.gz”.
#¶ ↑
# File lib/repackage/class/misc.rb, line 74 def initialize( commandline_arguments = nil, run_already = true ) register_sigint reset set_commandline_arguments( commandline_arguments ) # ======================================================================= # # === Handle given blocks next # ======================================================================= # if block_given? yielded = yield case yielded # ===================================================================== # # === :do_not_delete_the_old_source # ===================================================================== # when :do_not_delete_the_old_source set_shall_we_delete_old_source(false) # ===================================================================== # # === :run_already # ===================================================================== # when :run_already run_already = true else if yielded.is_a? Hash # ================================================================= # # === :run # ================================================================= # if yielded.has_key? :run run_already = yielded[:run] end # ================================================================= # # === :delete_code_of_conduct # ================================================================= # if yielded.has_key? :delete_code_of_conduct set_delete_code_of_conduct( yielded[:delete_code_of_conduct] ) end end end end run if run_already end
Public Instance Methods
#¶ ↑
cd (cd tag)¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 253 def cd( where_to = extract_to? ) case where_to # ======================================================================= # # === :to_the_start_dir # ======================================================================= # when :to_the_start_dir, :start_dir where_to = start_dir? # ======================================================================= # # === :to_the_original_dir # ======================================================================= # when :to_the_original_dir where_to = the_original_dir? end Dir.chdir(where_to) if File.directory?(where_to) end
#¶ ↑
consider_copying_the_new_archive_into_the_current_working_directory
¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 435 def consider_copying_the_new_archive_into_the_current_working_directory _ = away_with_the_archive_type( extracted_to?+ # This part is like "/home/Temp/repackage/". repackage_which_file? )+ repackage_to_which_format? if File.exist?(_) cd(:to_the_original_dir) new_target = return_pwd+File.basename(_) opn; e 'Now copying '+sfile(_)+' to the current directory.' opn; e 'It will then reside at '+sfile(new_target)+'.' copy_file(_, new_target) if File.exist? new_target opn; e 'Done! The file can be found '\ 'at '+sfile(new_target)+' now.' set_the_final_location_is_at(File.absolute_path(new_target)) if shall_we_delete_the_old_archive? remove_the_old_archive end else opn; e 'No file at '+sfile(new_target)+' exists, '\ 'thus we can not move anything.' end end end
#¶ ↑
create_archive_from_this_directory
(create tag)¶ ↑
# ¶ ↑
# File lib/repackage/class/misc.rb, line 487 def create_archive_from_this_directory( this_directory, repackage_to_this_format = repackage_to_which_format? ) esystem "#{COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE} #{this_directory}#{repackage_to_this_format} #{File.basename(this_directory)}" end
#¶ ↑
dir_where_the_archive_resides?¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 650 def dir_where_the_archive_resides? @internal_hash[:original_directory_where_the_archive_was_kept] end
#¶ ↑
do_repackage_the_assigned_files
¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 773 def do_repackage_the_assigned_files( i = @work_on_these_files ) # ======================================================================= # # The input may look like this: # # ["Python-3.10.7.tar.gz"] # # ======================================================================= # i.flatten.compact.each {|this_file| # ===================================================================== # # Clean up :the_new_file_is_at first: # ===================================================================== # @internal_hash[:the_new_file_is_at] = nil set_the_final_location_is_at(nil) # This is also cleanup. cliner set_repackage_this_file(this_file) determine_file_size if File.exist? this_file if File.exist? this_file # =================================================================== # # === Consider refusing the repackage-action # # Check whether the file is already in the correct target archive # format. # =================================================================== # if is_it_already_in_the_correct_archive_format?(this_file) opn; e rev+'The file at '+sfile(this_file)+' is already in '\ 'the desired target format ('+target_format?+').' opn; e "Repackaging into the same target format makes no sense, "\ "thus aborting now." else opn; e 'We will try to repackage this archive into the `'+ simp(target_format_type?)+'`' opn; e 'format type.' opn; e 'It will be extracted into the directory '+ sdir(extract_to?)+'.' # ================================================================= # # Here we know that the file exists and that it can be repackaged # into another target format. Thus, we can repackage it. We # will delegate towards a method for this. # ================================================================= # repackage_this_particular_file(this_file) end else # =================================================================== # # Ok - in this case the file does not exist. We will first try # to check if we have a number as input; if so we then we may # try a glob-action. And if not, we will raise an error. # # This can be invoked like so: # # repackagerb 1 # # =================================================================== # if this_file and (this_file =~ /^\d$/) # If input is a (positional) number like "3" _ = Dir['*'].select {|entry| File.file?(entry) }. sort[this_file.to_i - 1] do_repackage_these_files([_]) else # ================================================================= # # Else we can still try a glob, before giving up. # ================================================================= # _ = try_glob(this_file) if this_file if _.empty? warn_about_missing_file_then_exit else first = _.first if first and File.file?(first) do_repackage_these_files([first]) end end end end cliner } end
#¶ ↑
extract (extract tag)¶ ↑
Extract it here. Before we do so, though, we must check if the target does not exist yet.
#¶ ↑
# File lib/repackage/class/misc.rb, line 758 def extract( what = filename?, extract_to = extract_to? ) what = what.dup if what.frozen? unless what.include? '/' what = dir_where_the_archive_resides?+File.basename(what) end remove_extracted_data Extracter.extract_what_to(what, extract_to) end
#¶ ↑
extract_to
?¶ ↑
Defaults to /home/Temp/ on my home system.
#¶ ↑
# File lib/repackage/class/misc.rb, line 412 def extract_to? @internal_hash[:extract_to_this_directory] end
#¶ ↑
extract_to_this_directory?¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 308 def extract_to_this_directory? @internal_hash[:extract_to_this_directory] end
#¶ ↑
first_argument?¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 221 def first_argument? @commandline_arguments.first end
#¶ ↑
internal_hash?¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 228 def internal_hash? @internal_hash end
#¶ ↑
remove¶ ↑
Remove a file or a directory with this method.
#¶ ↑
# File lib/repackage/class/misc.rb, line 30 def remove(i) if File.directory?(i) FileUtils.rm_rf(i) unless i.strip == '/' # A tiny safeguard. elsif File.file?(i) delete_file(i) end end
#¶ ↑
remove_old_extracted_directory_should_it_exist
¶ ↑
This method will remove any extracted archive that exits at the target location. This will avoid “unclean” directories.
#¶ ↑
# File lib/repackage/class/misc.rb, line 369 def remove_old_extracted_directory_should_it_exist( i = rds("#{extract_to?}/#{remove_extension(filename?)}") ) if File.directory?(i) # We remove a possibly-existing, extracted directory first. e 'Removing the existing directory '\ 'at `'+sdir(i)+'` next.' remove(i) end end
#¶ ↑
remove_the_old_archive
¶ ↑
Only call this when we are sure to remove the old source. I recommend to delete the old source. Of course we must make sure to delete the right package.
#¶ ↑
# File lib/repackage/class/misc.rb, line 742 def remove_the_old_archive( i = dir_where_the_archive_resides?+filename? ) opn; e "Removing the old archive at #{sfile(i)}" opn; e "next, as requested via the "\ "#{::Repackage.steelblue('SHALL_WE_DELETE_THE_OLD_ARCHIVE')} "\ "constant." remove(i) end
#¶ ↑
repackage_this_particular_file
¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 869 def repackage_this_particular_file(this_file) opn; e rev+'The file '+sfile(this_file)+' will be repackaged '\ 'into the '+orange(repackage_to_which_format?)+' format.' copy_this_file_to_the_working_directory(this_file) working_directory = working_directory? cd(working_directory) # Go to the working directory. set_start_dir(working_directory) opn; e 'Working from the directory `'+sdir(working_directory)+'` next.' absolute_path = File.absolute_path(this_file) opn; e 'The actual file that will be used for the '\ 'repackaging' opn; e 'step now resides at: ' e e " #{sdir(absolute_path)}" e # ======================================================================= # # Ok, now we have to extract it. # ======================================================================= # if File.exist? absolute_path extract(absolute_path) if DELETE_CODE_OF_CONDUCT_FILE_IF_IT_EXISTS target = return_pwd+ remove_archive_from( File.basename(repackage_which_file?) ) all_files = Dir[target+'/*'] if all_files.any? {|file_path| file_path.include?('CODE_OF_CONDUCT') } # In this case we found at the least one code-of-conduct files. # Delete it now. selection = all_files.select {|file_path| file_path.include?('CODE_OF_CONDUCT') and File.file?(file_path) } selection.each {|this_file| opn; e 'Now deleting the file '+sfile(this_file) opn; e 'as requested by the user.' delete_file(this_file) } end end # ===================================================================== # # If all went well then we can now create the new desired target # format. So if that format is '.tar.xz' then we will have to # run the proper command that will create such a tarball for # us next. # ===================================================================== # run_the_proper_to_archive_command( remove_file_extension_from( absolute_path # This here is e. g. "/home/Temp/repackage/htop-3.1.2.tar.gz". ) ) consider_copying_the_new_archive_into_the_current_working_directory end end
#¶ ↑
repackage_to_this_format?¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 548 def repackage_to_this_format? @internal_hash[:repackage_to_this_format] end
#¶ ↑
repackage_which_file?¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 358 def repackage_which_file? @internal_hash[:repackage_this_file] end
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 124 def reset # ======================================================================= # # === @namespace # ======================================================================= # @namespace = NAMESPACE # ======================================================================= # # === @internal_hash # ======================================================================= # @internal_hash = {} # ======================================================================= # # === :delete_code_of_conduct_file_if_it_exists # ======================================================================= # @internal_hash[:delete_code_of_conduct_file_if_it_exists] = DELETE_CODE_OF_CONDUCT_FILE_IF_IT_EXISTS # ======================================================================= # # === :file_size # ======================================================================= # @internal_hash[:file_size] = 0 set_extract_to_this_directory(:default) set_start_dir # ======================================================================= # # === :repackage_into_this_format # # .tar.xz is the default target format. # ======================================================================= # set_repackage_to_this_format(:to_the_default_format) # ======================================================================= # # === :the_new_file_is_at # # This entry will keep track at which new location the file can be # found. # ======================================================================= # @internal_hash[:the_new_file_is_at] = nil set_shall_we_delete_the_old_archive(:default) end
#¶ ↑
run_the_proper_to_archive_command
¶ ↑
The argument to this method should be a String representing a directory.
#¶ ↑
# File lib/repackage/class/misc.rb, line 696 def run_the_proper_to_archive_command( on_this_directory ) on_this_directory = on_this_directory.dup if on_this_directory.frozen? target_format = repackage_to_which_format? case target_format # case tag # ======================================================================= # # === .tar.xz # ======================================================================= # when /\.tar\.xz/i esystem "#{COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE} "\ "#{on_this_directory}#{repackage_to_which_format?} "\ "#{File.basename(on_this_directory)}" # ======================================================================= # # === .tar.gz # ======================================================================= # when /\.tar\.gz/i esystem "#{COMMAND_TO_CREATE_A_TAR_GZ_ARCHIVE} "\ "#{on_this_directory}#{repackage_to_which_format?} "\ "#{File.basename(on_this_directory)}" # ======================================================================= # # === .tar.bz2 # ======================================================================= # when /\.tar\.bz2/i esystem "#{COMMAND_TO_CREATE_A_TAR_BZ2_ARCHIVE} "\ "#{on_this_directory}#{repackage_to_which_format?} "\ "#{File.basename(on_this_directory)}" # ======================================================================= # # === .zip # ======================================================================= # when /\.zip/i esystem "#{COMMAND_TO_CREATE_A_ZIP_ARCHIVE} "\ "#{on_this_directory}#{repackage_to_which_format?} "\ "#{File.basename(on_this_directory)}" else e 'Unknown target format: '+format.to_s end end
#¶ ↑
set_extract_to
¶ ↑
Set the @extract_to variable here.
#¶ ↑
# File lib/repackage/class/misc.rb, line 384 def set_extract_to( i = DEFAULT_EXTRACT_TO_THIS_DIRECTORY # Must be the initial constant as default. ) case i # ======================================================================= # # === :default # ======================================================================= # when :default, nil i = DEFAULT_EXTRACT_TO_THIS_DIRECTORY end i = i.to_s.dup # ======================================================================= # # The next clause is an override, in particular for my home system. # ======================================================================= # if File.directory? '/home/Temp/' i = '/home/Temp/repackage/' end i = '/tmp/' if i.empty? # Hardcoded rescue-step in this case. i << '/' unless i.end_with? '/' # A directory has a trailing /. @internal_hash[:extract_to_this_directory] = i end
#¶ ↑
set_repackage_this_file
¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 568 def set_repackage_this_file(this_file) this_file = this_file.first if this_file.is_a? Array case this_file # ======================================================================= # # === :default # ======================================================================= # when :default, nil # ===================================================================== # # Since as of May 2013 we try to fetch a random file from a list. # ===================================================================== # _ = Dir['*'].reject {|entry| File.directory? entry} this_file = _.first if _.size == 1 # if we only have one entry, continue here. # ======================================================================= # # === --last # ======================================================================= # when /^-?-?last(-|_)?downloaded$/i, /^-?-?last$/i, '-l' this_file = File.readlines(LAST_DOWNLOADED_FILE).first # ===================================================================== # # The format of the file has changed a bit. We have to check # whether it includes a '#' character. If so then we discard # all that comes after said '#' token. # ===================================================================== # if this_file.include? '#' this_file = this_file[0 .. (this_file.index('#')-1)].strip end end this_file = this_file.to_s.dup # Keep a copy. if File.directory?(this_file) and !this_file.end_with?('/') this_file << '/' end if this_file.nil? or this_file.empty? raise 'Please provide a valid archive to repackage.' end @internal_hash[:repackage_this_file] = this_file if this_file and File.exist?(this_file) # ===================================================================== # # === :original_directory_where_the_archive_was_kept # # This entry point will always refer to the directory where the # archive was originally situated in. # ===================================================================== # @internal_hash[:original_directory_where_the_archive_was_kept] = rds( File.absolute_path( File.dirname(this_file) )+'/' ) end end
#¶ ↑
set_repackage_to_this_format
¶ ↑
We will repackage to this format here.
#¶ ↑
# File lib/repackage/class/misc.rb, line 499 def set_repackage_to_this_format( i = :default_format ) i = i.downcase if i and i.is_a?(String) # Only want it downcased. case i # case tag # ======================================================================= # # === nil # ======================================================================= # when nil, # Assume a default here. :default, :default_format, :to_the_default_format i = DEFAULT_TARGET_FORMAT_TYPE # ======================================================================= # # === .tar.xz # ======================================================================= # when 'xz', 'tar.xz' i = '.tar.xz' # ======================================================================= # # === .tar.bz2 # ======================================================================= # when 'bz2', '.tar.bz2' i = '.tar.bz2' # ======================================================================= # # === .tar.gz # ======================================================================= # when 'targz', 'gz', 'tar.gz' i = '.tar.gz' else # else tag # warn 'Did not find registered format type.' end @internal_hash[:repackage_to_this_format] = i end
#¶ ↑
set_shall_we_delete_the_old_archive
¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 162 def set_shall_we_delete_the_old_archive( i = :default ) case i # ======================================================================= # # === :default # ======================================================================= # when :default, nil i = SHALL_WE_DELETE_THE_OLD_ARCHIVE if is_on_roebe? i = true end end @internal_hash[:shall_we_delete_the_old_archive] = i end
#¶ ↑
set_the_new_file_is_at
¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 642 def set_the_new_file_is_at(i = nil) @internal_hash[:the_new_file_is_at] = i end
#¶ ↑
the_final_location_is_at?¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 634 def the_final_location_is_at? @internal_hash[:the_new_file_is_at] end
#¶ ↑
warn_about_missing_file_then_exit
¶ ↑
#¶ ↑
# File lib/repackage/class/misc.rb, line 680 def warn_about_missing_file_then_exit( i = target_file? ) opn;e swarn('The file `')+sfile(i)+ swarn('` does not exist. Must provide a valid path here.') raise "The argument given must be the path to an existing "\ "(local) file.\nAs the path given does not appear "\ "to exist, this class can not continue." end