class Cookbooks::UpdateEntry
Constants
- ARE_WE_ALLOWED_TO_SAVE
#¶ ↑
ARE_WE_ALLOWED_TO_SAVE¶ ↑
#¶ ↑
- BR
#¶ ↑
BR¶ ↑
#¶ ↑
- CHANGELOG_SEPARATOR_LINE
#¶ ↑
CHANGELOG_SEPARATOR_LINE¶ ↑
This is the same line used by slackware.
#¶ ↑
- DEFAULT_URL
#¶ ↑
DEFAULT_URL¶ ↑
This URL is only for testing purposes. Do not actively set it if you are NOT testing the functionality.
#¶ ↑
- NAMESPACE
#¶ ↑
NAMESPACE¶ ↑
#¶ ↑
- SHALL_WE_ALSO_DOWNLOAD_THE_PACKAGE
#¶ ↑
SHALL_WE_ALSO_DOWNLOAD_THE_PACKAGE¶ ↑
#¶ ↑
- SHALL_WE_ALSO_UPDATE_THE_LAST_UPDATE_ENTRY
#¶ ↑
SHALL_WE_ALSO_UPDATE_THE_LAST_UPDATE_ENTRY¶ ↑
If true then we will add such an entry.
#¶ ↑
- SHALL_WE_TRY_TO_CHANGE_DIRECTORY_INTO_THE_BASE_DIR
#¶ ↑
SHALL_WE_TRY_TO_CHANGE_DIRECTORY_INTO_THE_BASE_DIR¶ ↑
If true we will cd() if we are in the wrong directory.
#¶ ↑
- WGET_IS_AVAILABLE
Public Class Methods
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 95 def initialize( i = nil, run_already = true ) reset set_remote_url(i) if block_given? yielded = yield case yielded when :do_not_ftp_upload @shall_we_upload_the_cookbook_recipe = false end end run if run_already end
Public Instance Methods
#¶ ↑
#backup_old_file¶ ↑
We will copy the old file to a backup-location through this method here.
This will usually be at the following location:
/Depot/Temp/OldCookbooks/
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 305 def backup_old_file unless @old_file_content == @save_this_data # Only do so if we don't have the same data. _ = temp_directory?+'OldCookbooks/' mkdir(_) unless Dir.exist? _ output 'Backing up old file at '+sfile(location?) output 'to `'+sfile(_+File.basename(location?))+'`.' copy_file(location?, _) end end
#¶ ↑
#check_whether_file_exists_or_not¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 201 def check_whether_file_exists_or_not unless File.exist? location? output 'We were unable to find `'+sfile(location?)+'`.' output 'Make sure that it exists, in case that it should exist.' output 'Exiting now.' exit end end
#¶ ↑
#consider_changing_directory ¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 283 def consider_changing_directory if SHALL_WE_TRY_TO_CHANGE_DIRECTORY_INTO_THE_BASE_DIR _ = source_directory?+@_.short_name?.downcase # .upcase # No longer need .upcase() since Jun 2015. _ << '/' unless _.end_with? '/' _.delete!('_') if _.include? '_' # Directories must not contain a '_' character. _.delete!('-') if _.include? '-' unless (Dir.pwd+'/').squeeze('/') == _ # Need the .squeeze to append a '/'. output 'Now changing to the directory `'+sdir(_)+'`.' cd _ end end end
#¶ ↑
#consider_downloading_this_package (download tag, wget tag)¶ ↑
In this method we may download the remote file package, but only if we can not find a file with the same name in the directory.
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 397 def consider_downloading_this_package(i = remote_url?) consider_changing_directory if @shall_we_also_download_the_package if WGET_IS_AVAILABLE # Make use of wget_wrapper project. Wget.new(i) {{ namespace: 'Cookbooks->Wget' }} # Delegate to Wget here. else # else default to a simpler hardcoded, non-ruby wget solution here. _ = 'wget '+i esystem _ end end unless File.exist?(File.basename(i)) end
#¶ ↑
#consider_handling_a_changelog_entry¶ ↑
This method can be used to keep a “changelog” entry of last updates. Whenever we update a program, we will also upload to a remote .html page.
We will keep a format that is very similar to slackware changelogs.
An example for that can be seen here:
www.slackware.com/changelog/current.php?cpu=x86_64
Keep in mind that we will use a SIMPLIFIED version.
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 423 def consider_handling_a_changelog_entry if is_on_roebe? remote_url = URL_TO_REMOTE_CHANGELOG # ===================================================================== # # Check if the remote website exists. If so we download it via # open-uri. # ===================================================================== # #if Wget.does_this_remote_website_exist?(remote_url) # dataset = open(remote_url).read # ===================================================================== # # Else we have to generate the String anew. # ===================================================================== # #else #end string_to_append = CHANGELOG_SEPARATOR_LINE.dup+N+BR.dup # ===================================================================== # # We will use a UTC format, similar to slackware too. # ===================================================================== # string_to_append << return_utc_time_in_a_format_similar_to_slackware+ BR.dup+N+BR.dup+N string_to_append << File.basename(location?).sub(/\.yml$/,'')+ ' version: '+@new_program_version.to_s+N # ===================================================================== # # Put it into a .html file. # ===================================================================== # html_page_content = '<html><title>RBT Changelog</title>'.dup html_page_content << '<body>'+string_to_append+'</body></html>' into = log_dir?+File.basename(remote_url) e swarn('DEBUG - UNFINISHED FIX ME AT SOME LATER POINT - PERHAPS :)') # <- FIXME: todo e string_to_append opnn; e 'Storing into `'+sfile(into)+'`.' write_what_into(html_page_content, into) if @shall_we_upload_the_cookbook_recipe opnn; e 'Next trying to upload the file `'+sfile(into)+'` onto '\ 'the remote website.' begin FtpParadise.upload(into) rescue Net::FTPTempError => error pp error pp error.class opnn; e 'The above error occurred.' end end end end
#¶ ↑
#consider_modifying_the_dataset¶ ↑
This method will work on @save_this_data and do some modifications, if a certain constant was set to true.
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 273 def consider_modifying_the_dataset if @save_this_data.any? {|line| line.include? 'last_update' } # Then remove this line. @save_this_data.reject! {|line| line.include? 'last_update' } end # It is ok to add it as last line. @save_this_data << ' last_update: '+get_date end
#¶ ↑
#consider_updating_the_expanded_cookbooks_dataset¶ ↑
This method will try to update the dataset stored in the expanded cookbooks. This should help us avoid full-scale regeneration of these yaml files.
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 506 def consider_updating_the_expanded_cookbooks_dataset if expanded_cookbooks_directory_exists? # ===================================================================== # # In this case, simply dump the entry. # ===================================================================== # program_name = ProgramInformation.new(remote_url?).program_name?.delete('_-') cookbook_dataset = Cookbooks::Cookbook.new(program_name) hash_to_store = cookbook_dataset.return_hash # ===================================================================== # # Save the Hash next. # ===================================================================== # what = YAML.dump(hash_to_store) into = temp_directory?+'CookbookDirectory/expanded_cookbooks/'+program_name+'.yml' opnn; e 'Also saving the expanded dataset into `'+sfile(into)+'`.' write_what_into(what, into) end end
#¶ ↑
#read_file¶ ↑
This will read in the dataset. We must be careful with File.readlines() as the encoding may be US-ASCII.
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 128 def read_file output 'Now working on the file' e ' '+sfile(location?)+' ('+@_.name?.downcase+')' if File.exist? location? @old_file_content = File.readlines(location?, :encoding => USE_MAIN_ENCODING) @old_file_content.map!(&:chomp) # Remove the newlines next. _ = @old_file_content.dup # Work on a copy here. _.each {|line| line = line.dup # Next we work on only those entries which contain an " url1:" entry if line =~ / url1:/ position = line.index(':') # Find the first : # Next, simply append it after the position character. if remote_url?.include? '/' # Here we assume this to be a full url. line[position..-1] = ': '+remote_url? else # Here we assume it not be a full url yet. full_url = line[(position+2)..-1] splitted = full_url.split('/') url = splitted[0..-2].join('/')+'/' # =============================================================== # # Find out the archive type of a package. # =============================================================== # archive = Cookbooks::ArchiveType.new(splitted[-1]) # bl $RUBY_COOKBOOKS/lib/cookbooks/archive_type.rb assign_line_to_this = ': '+url+remote_url?+archive.to_s line[position..-1] = assign_line_to_this end use_this_as_new_program_version = ProgramInformation.new(line[position..-1]).program_version? set_new_program_version(use_this_as_new_program_version) end @save_this_data << line } end end
#¶ ↑
remote_url?¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 495 def remote_url? @remote_url end
#¶ ↑
reset¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 114 def reset super() @new_program_version = nil @save_this_data = [] # Is an Array. @shall_we_also_download_the_package = SHALL_WE_ALSO_DOWNLOAD_THE_PACKAGE @shall_we_upload_the_cookbook_recipe = true end
#¶ ↑
run (run tag, main logic)¶ ↑
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 481 def run set_location check_whether_file_exists_or_not read_file backup_old_file save_the_file consider_downloading_this_package consider_updating_the_expanded_cookbooks_dataset consider_handling_a_changelog_entry end
#¶ ↑
#save_the_file¶ ↑
Specialized old save functionality.
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 336 def save_the_file store_into_this_file = location? if are_we_allowed_to_save? if @old_file_content == @save_this_data output 'Can not store new file because they hold the same data.' else if SHALL_WE_ALSO_UPDATE_THE_LAST_UPDATE_ENTRY consider_modifying_the_dataset end output 'Storing into '+sfile(store_into_this_file) what = @save_this_data.join(N) write_what_into(what, store_into_this_file) # =================================================================== # # If we are on roebe, thus at home, we also backup the old file # directly. # =================================================================== # if is_on_roebe? _ = RUBY_SRC_DIR_AT_HOME+'cookbooks/lib/cookbooks/yaml/individual_cookbooks/' target = _+File.basename(store_into_this_file) output 'We will also store at `'+sfile(target)+'`.' write_what_into(what, target) # ================================================================= # # On my system, I will also update the programs_version yaml file. # The next few lines of code does precisely that. # ================================================================= # new_hash = YAML.load_file(FILE_PROGRAMS_VERSION) # ================================================================= # # Need to update only THAT particular program and the version. # ================================================================= # new_hash[program_name?.to_s.downcase] = new_program_version?.to_s # Be careful not to use the old program version here. Cookbooks.generate_programs_version_yaml_file( new_hash ) end end else output 'We are not allowed to save into '\ '`'+sfile(store_into_this_file)+'`.' end end
#¶ ↑
#set_location¶ ↑
Use this method to set the @location variable. This will also set the “disposable” @_ variable,
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 192 def set_location @_ = ProgramInformation.new(remote_url?) @location = cookbooks_directory?+@_.real_name?. downcase.delete('_')+'.yml' # Which yaml file we will use. end
#¶ ↑
#set_remote_url (url tag, entry tag)¶ ↑
Use this method whenever you wish to set the default URL entry, captured in the @remote_url variable.
#¶ ↑
# File lib/cookbooks/utility_scripts/update_entry.rb, line 216 def set_remote_url(i = nil) i = i.first if i.is_a? Array # Only take the first parameter if it is an Array. if i.nil? opnn; e 'Please provide a remote URL to class UpdateEntry.' exit end i = i.to_s.dup # ======================================================================= # # As of June 2014, we get rid of '~' characters if they come last. # ======================================================================= # i.chop! if i.end_with? '~' # ======================================================================= # # Same as above for when we end with ':' or ',' # ======================================================================= # i.chop! if i.end_with? ':',',' # ======================================================================= # # We also need to get rid of .tar.bz2 and so forth. Hmm but is this # good? Does not seem to be good, so I disabled it as of Mar 2013. # ======================================================================= # # i = remove_extension(i) # ======================================================================= # # In Jun 2013, I added this logic - if the URL includes the string # "sourceforge", and the last part is "download", we will chop it off # and use it. # ======================================================================= # if i.include?('sourceforge') and i[-'/download'.size,'/download'.size] == '/download' i[-'/download'.size,'/download'.size] = '' # chop it off here. end @remote_url = i end