class RBT::Cookbooks::CheckForGnomeUpdates
Constants
- GNOME_LAST_UPDATE
#¶ ↑
GNOME_LAST_UPDATE
¶ ↑Where to store the last update.
#¶ ↑
- REGEX_TO_FETCH_NUMBER
#¶ ↑
REGEX_TO_FETCH_NUMBER
¶ ↑Next come the regexes.
#¶ ↑
- SECOND_REGEX
#¶ ↑
SECOND_REGEX
¶ ↑#¶ ↑
- SHOW_N_DAYS
#¶ ↑
SHOW_N_DAYS
¶ ↑#¶ ↑
- URL_TO_GNOME
#¶ ↑
URL_TO_GNOME
¶ ↑This is the assembled URL.
#¶ ↑
Public Class Methods
Public Instance Methods
#¶ ↑
array_available_programs
¶ ↑
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 292 def array_available_programs? @array_available_programs end
#¶ ↑
consider_reporting_which_entries_were_updated
¶ ↑
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 135 def consider_reporting_which_entries_were_updated unless @array_updated_these_entries.empty? display 'We updated these entries:' @array_updated_these_entries.each_with_index {|entry, index| index += 1 e ' ('+sfancy(index.to_s.rjust(2))+') - '+entry.to_s } else pp @array_updated_these_entries end end
#¶ ↑
consider_saving_file_when_last_update_happened
¶ ↑
Here we store where to save the last update. We will only store it when we have downloaded at least one program though.
This usually will be at:
cat $HOME/gnome_last_update
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 202 def consider_saving_file_when_last_update_happened if @array_updated_these_entries.size > 0 # In this case we downloaded at least one program. what = Time.now.to_s into = store_where? e display "Writing when we last updated into the file `#{sfile(into)}`." e # Newline makes this easier to read. write_what_into(what, into) end end
#¶ ↑
exit_now?¶ ↑
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 285 def exit_now? @exit_now end
#¶ ↑
fetch_all_href_links
¶ ↑
The regex is anchored to the beginning of the line in question.
After this method, we usually tend to filter away too old programs.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 303 def fetch_all_href_links # ======================================================================= # # use_this_regex = ^<a href=".+">(.+)<\/a> # ======================================================================= # use_this_regex = '<a href=".+">(.+)<\/a>\s+(\d\d\d\d-\d\d-\d\d \d\d:\d\d) -' @matches = @dataset.scan(/#{use_this_regex}/) # ======================================================================= # # Next, sanitize that result. The dataset in question will look like # this: # # ["gnome-initial-setup/", "2016-03-21 21:25"] # ======================================================================= # @matches.map! {|name, date| name = name.delete('/') # The name need not have any '/'. [name, date] # Return both the name and the date again. } end
#¶ ↑
fetch_dataset
¶ ↑
We obtain the gnome-url dataset, that is, from ftp.gnome.org/pub/GNOME/sources/?C=M;O=D.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 153 def fetch_dataset @dataset = URI.open(remote_url?).read end
#¶ ↑
filter_away_too_old_programs
¶ ↑
Programs which are “too old”, won’t be shown.
This is determined to be in n days behind.
The file may be at:
/root/gnome_last_update
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 331 def filter_away_too_old_programs threshold_in_seconds = @show_n_days * ONE_DAY # This is a float. if File.exist? store_where? # Overrule in this case. threshold_in_seconds = Time.parse(File.read(store_where?)).to_f end @matches.reject! {|entry| date = entry[1] threshold = (Time.now - threshold_in_seconds) # This will be a Float. parsed = Time.parse(date) # This is a time object now. if threshold > parsed true else false end } end
#¶ ↑
last_update?¶ ↑
This method shows when a last update happened.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 169 def last_update? if File.exist? store_where? data = File.read(GNOME_LAST_UPDATE) difference = (Time.now - Time.parse(data)) difference = difference.to_s # ===================================================================== # # Next we check if the difference is too long. If it is, we will # only display 10 characters max. # ===================================================================== # else # Else the file does not exist. difference = (days?.to_f * ONE_DAY).to_s end difference = difference[0, 10] if difference.to_s.size > 10 when_did_it_happen = sfancy(difference)+' seconds:' return when_did_it_happen end
#¶ ↑
obtain_most_recent_entry_from_this_url
¶ ↑
This method wants an argument such as:
"http://ftp.gnome.org/pub/GNOME/sources/tracker/?C=M;O=D"
It will open this URL, and attempt to fetch the first program. The first program is assumed to be the most recent program.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 357 def obtain_most_recent_entry_from_this_url(url = @matches) if url.is_a? Array url = url.map {|name| BASE_URL_TO_GNOME_SOURCES+name.first+'/'+SORTED } # url.each {|entry| obtain_most_recent_entry_from_this_url(entry) } url.each {|entry| send __method__, entry } else begin result = URI.open(url).read if result # result might be nil though, so we must safeguard here. _ = result.scan(REGEX_TO_FETCH_NUMBER) if _.empty? # We assume that we need another Regex then. _ = result.scan(SECOND_REGEX) end if _.flatten.any? {|entry| entry.include? '.tar' } # Discard entries that do not include .tar with this filter. _ = _.flatten.reject {|inner_entry| ! inner_entry.include? '.tar' } end if _.is_a? Array _ = _.first # We assume the first entry to be the most recent entry. end return _ end rescue OpenURI::HTTPError => error display 'We encountered an OpenURI::HTTPError error, which we '\ 'will next feedback:' pp error display 'Ultimately, we did not find the url at `'+sfancy(url)+'`.' end end end
#¶ ↑
process_commandline_arguments
¶ ↑
To invoke this method, do something like:
gnomenew --help
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 411 def process_commandline_arguments( i = commandline_arguments? ) option_parser = OptionParser.new # case tag option_parser.on('--help','help',String, 'Show help options') { show_help } # ======================================================================= # # Invoke the next section via doing something like: # gnomenew --n_days 55 # ======================================================================= # option_parser.on('--n X''--n_days X','--ndays','-n','ndays',Integer, 'set X days') {|n| @show_n_days = (n.to_i * ONE_DAY) } # ======================================================================= # # === gnomenew --compile # ======================================================================= # option_parser.on('--compile','compile',String, 'not only download but also compile at once') {|n| opne 'We will also compile all programs that were downloaded.' do_also_compile } option_parser.parse!(i) end
#¶ ↑
register_sigint
¶ ↑
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 458 def register_sigint Signal.trap('SIGINT') { display 'User requested to exit via SIGINT (user interrupt).' display 'We will exit as soon as possible.' consider_compiling_all_programs_that_were_updated @exit_now = true consider_reporting_which_entries_were_updated exit } end
#¶ ↑
report_all_found_programs
¶ ↑
This method is the powerhorse method.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 227 def report_all_found_programs n_programs = @matches.size.to_s display 'We have found these '+simp(n_programs)+' programs '+ 'in the last '+last_update? @matches.each_with_index {|entry, index| if shall_we_exit? opne 'Exiting now as requested by the user.' end name, date = entry[0], entry[1] index += 1 index = bold_blue(index.to_s.rjust(4)) display_index_name_and_date(index, name, date) new_url = return_remote_url_for(name) # Last URL should now be: # Latest URL: http://ftp.gnome.org/pub/GNOME/sources/grilo/?C=M;O=D # display 'Latest URL: '+simp(new_url) _ = obtain_most_recent_entry_from_this_url(new_url).first version_number = _.to_s.dup new_url = BASE_URL_TO_GNOME_SOURCES+ File.basename(name)+ '/'+version_number.to_s+ '/'+TRAILING_END_OF_THE_REMOTE_WEBPAGE # display 'Source tarball in subdirectory: '+bold_red(new_url) _ = obtain_most_recent_entry_from_this_url(new_url) _ = _.first if _.is_a? Array final_url = (BASE_URL_TO_GNOME_SOURCES+File.basename(name)+'/'+version_number+'/'+_.to_s) # .squeeze('/') # Can not use .squeeze here as it changes the URL. display 'Final URL at: '+sfancy(final_url) name.delete!('-') if name.include? '-' name = name.downcase name = ::ProgramInformation.return_name(name) @sanitize_cookbook_dataset = action(:SanitizeCookbookDataset, name) if @sanitize_cookbook_dataset.found_result? display 'To compare - the local version of `'+sfancy(name)+'` is '+ sfancy(@sanitize_cookbook_dataset.program_version?)+'.' new_version = ::ProgramInformation.return_version(final_url) # =================================================================== # # The following check is important: # If it returns true, then it means we won't have to update. # If it returns false, however, then it means that we can updated. # =================================================================== # if new_version == @sanitize_cookbook_dataset.program_version? else display 'The new version would be: '+sfancy(new_version) display 'The local version of '+simp(name)+' is not the same '\ 'as the remote version.' display 'We will try to update the local version next.' update_this_entry(final_url) end else display 'The program '+sfancy(name)+' is not included.' end } opne 'We used the remote URL '+sfancy(remote_url?) end
#¶ ↑
reset¶ ↑
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 62 def reset super() infer_the_namespace # ======================================================================= # # === @array_available_programs # ======================================================================= # @array_available_programs = [] # ======================================================================= # # === @show_n_days # ======================================================================= # @show_n_days = SHOW_N_DAYS # Usually 3 days in advance, on a first run. # ======================================================================= # # === @exit_now # ======================================================================= # @exit_now = false # ======================================================================= # # === @do_also_compile # ======================================================================= # @do_also_compile = false end
#¶ ↑
run¶ ↑
Bundle the main logic together here.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 485 def run process_commandline_arguments fetch_dataset fetch_all_href_links filter_away_too_old_programs report_then_display # For now we will always display. consider_reporting_which_entries_were_updated consider_saving_file_when_last_update_happened # We only update that file if we downloaded at least one program. consider_compiling_all_programs_that_were_updated end
#¶ ↑
show_help
(help tag)¶ ↑
Show which help options this class accepts.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 474 def show_help(shall_we_exit = :then_exit) shall_we_exit = true if shall_we_exit == :then_exit opncomment '--n_days X # set n days' exit if shall_we_exit end
#¶ ↑
update_this_entry
¶ ↑
Use this method only when you wish to update a specific program.
We will delegate towards class Cookbooks::UpdateEntry for this.
#¶ ↑
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 395 def update_this_entry(i) if i.end_with? '.sign' i.gsub!(/\.sign$/,'') end @array_updated_these_entries << i update_entry(i) # Delegate towards class UpdateEntry, which will also download. end