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

new( commandline_arguments = nil, run_already = true ) click to toggle source
#

initialize

#
Calls superclass method
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 46
def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  super()
  register_sigint
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Public Instance Methods

array_available_programs()
array_available_programs?() click to toggle source
#

array_available_programs

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 286
def array_available_programs?
  @array_available_programs
end
Also aliased as: array_available_programs
consider_compiling_all_programs_that_were_updated() click to toggle source
#

consider_compiling_all_programs_that_were_updated

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 437
def consider_compiling_all_programs_that_were_updated
  if @do_also_compile
    @array_updated_these_entries.each {|program|
      RBT::Compile.new(program)
    }
  end
end
consider_reporting_which_entries_were_updated() click to toggle source
#

consider_reporting_which_entries_were_updated

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 129
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() click to toggle source
#

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 196
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
days?() click to toggle source
#

days?

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 183
def days?
  @show_n_days.to_s
end
display(i = '') click to toggle source
#

display

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 122
def display(i = '')
  opnn; e i
end
display_index_name_and_date(index, name, date) click to toggle source
#

display_index_name_and_date

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 112
def display_index_name_and_date(index, name, date)
  name = slateblue(name.to_s.dup) if konsole_support?
  e
  e index+') '+name.ljust(20)+' -> '+bold_green(date)
  e
end
display_urls_for_each() click to toggle source
#

display_urls_for_each

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 87
def display_urls_for_each
  @matches.each {|entry|
    name = entry.first
    e BASE_URL_TO_GNOME_SOURCES+name+'/'+SORTED
  }
end
do_also_compile() click to toggle source
#

do_also_compile

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 430
def do_also_compile
  @do_also_compile = true
end
exit_now?() click to toggle source
#

exit_now?

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 279
def exit_now?
  @exit_now
end
Also aliased as: shall_we_exit?
fetch_dataset() click to toggle source
#

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 147
def fetch_dataset
  @dataset = open(remote_url?).read
end
filter_away_too_old_programs() click to toggle source
#

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 325
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
konsole_support?() click to toggle source
#

konsole_support?

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 105
def konsole_support?
  Object.const_defined? :Colours
end
last_update?() click to toggle source
#

last_update?

This method shows when a last update happened.

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 163
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(url = @matches) click to toggle source
#

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 351
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 = 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(i = @commandline_arguments) click to toggle source
#

process_commandline_arguments

To invoke this method, do something like:

gnomenew --help
#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 403
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|
    opnn; e 'We will also compile all programs that were downloaded.'
    do_also_compile
  }
  option_parser.parse!(i)
end
register_sigint() click to toggle source
#

register_sigint

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 448
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
remote_url?() click to toggle source
#

remote_url?

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 154
def remote_url?
  URL_TO_GNOME
end
report()
report_all_found_programs() click to toggle source
#

report_all_found_programs

This method is the powerhorse method.

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 221
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?
      opnn; e '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 = RBT::Cookbooks::SanitizeCookbookDataset.new(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
  }
  opnn; e 'We used the remote URL '+sfancy(remote_url?)
end
Also aliased as: report
report_then_display() click to toggle source
#

report_then_display

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 97
def report_then_display
  report
  obtain_most_recent_entry_from_this_url
end
reset() click to toggle source
#

reset

#
Calls superclass method
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 69
def reset
  super()
  @array_available_programs    = []
  @show_n_days = SHOW_N_DAYS # Usually 3 days in advance, on a first run.
  @exit_now = false
  @do_also_compile = false
end
return_remote_url_for(i) click to toggle source
#

return_remote_url_for

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 80
def return_remote_url_for(i)
  BASE_URL_TO_GNOME_SOURCES+i+'/'+SORTED
end
run() click to toggle source
#

run

Bundle the main logic together here.

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 475
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
set_commandline_arguments(i) click to toggle source
#

set_commandline_arguments

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 62
def set_commandline_arguments(i)
  @commandline_arguments = i
end
shall_we_exit?()
Alias for: exit_now?
show_help(shall_we_exit = :then_exit) click to toggle source
#

show_help (help tag)

Show which help options this class accepts.

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 464
def show_help(shall_we_exit = :then_exit)
  shall_we_exit = true if shall_we_exit == :then_exit
  opnn; ecomment '--n_days X # set n days'
  exit if shall_we_exit
end
store_where?() click to toggle source
#

store_where?

We will store in a specific file.

#
# File lib/rbt/check_for_updates/check_for_gnome_updates.rb, line 212
def store_where?
  GNOME_LAST_UPDATE
end
update_this_entry(i) click to toggle source
#

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 389
def update_this_entry(i)
  if i.end_with? '.sign'
    i.gsub!(/\.sign$/,'')
  end
  @array_updated_these_entries << i
  RBT::UpdateEntry.new(i) # Delegate towards class UpdateEntry, which will also download.
end