Class: RBT::Cookbooks::CheckForGnomeUpdates

Inherits:
Object
  • Object
show all
Defined in:
lib/rbt/check_for_updates/check_for_gnome_updates.rb

Overview

RBT::Cookbooks::CheckForGnomeUpdates

Constant Summary collapse

URL_TO_GNOME =
#

URL_TO_GNOME

This is the assembled URL.

#
BASE_URL_TO_GNOME_SOURCES+TRAILING_END_OF_THE_REMOTE_WEBPAGE
SHOW_N_DAYS =
#

SHOW_N_DAYS

#
3
GNOME_LAST_UPDATE =
#

GNOME_LAST_UPDATE

Where to store the last update.

#
"#{RBT.store_into_this_directory?}gnome_last_update.md"
REGEX_TO_FETCH_NUMBER =
#

REGEX_TO_FETCH_NUMBER

Next come the regexes.

#
/\<a href=".+\/">(.+)\/<\/a>/
SECOND_REGEX =
#

SECOND_REGEX

#
/^\<a href="(\b.+\b)"\><img/

Instance Method Summary collapse

Constructor Details

#initialize(commandline_arguments = nil, run_already = true) ⇒ CheckForGnomeUpdates

#

initialize

#


46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Method Details

#array_available_programs?Boolean Also known as: array_available_programs

#

array_available_programs

#

Returns:

  • (Boolean)


292
293
294
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 292

def array_available_programs?
  @array_available_programs
end

#consider_compiling_all_programs_that_were_updatedObject

#

consider_compiling_all_programs_that_were_updated

#


447
448
449
450
451
452
453
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 447

def consider_compiling_all_programs_that_were_updated
  if @do_also_compile
    @array_updated_these_entries.each {|program|
      RBT::Action::Installer.new(program)
    }
  end
end

#consider_reporting_which_entries_were_updatedObject

#

consider_reporting_which_entries_were_updated

#


135
136
137
138
139
140
141
142
143
144
145
# 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_happenedObject

#

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
#


202
203
204
205
206
207
208
209
210
211
# 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

#days?Boolean

#

days?

#

Returns:

  • (Boolean)


189
190
191
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 189

def days?
  @show_n_days.to_s
end

#display(i = '') ⇒ Object

#

display

#


128
129
130
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 128

def display(i = '')
  opne i
end

#display_index_name_and_date(index, name, date) ⇒ Object

#

display_index_name_and_date

#


118
119
120
121
122
123
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 118

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_eachObject

#

display_urls_for_each

#


93
94
95
96
97
98
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 93

def display_urls_for_each
  @matches.each {|entry|
    name = entry.first
    e BASE_URL_TO_GNOME_SOURCES+name+'/'+SORTED
  }
end

#do_also_compileObject

#

do_also_compile

#


440
441
442
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 440

def do_also_compile
  @do_also_compile = true
end

#exit_now?Boolean Also known as: shall_we_exit?

#

exit_now?

#

Returns:

  • (Boolean)


285
286
287
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 285

def exit_now?
  @exit_now
end
#

The regex is anchored to the beginning of the line in question.

After this method, we usually tend to filter away too old programs.

#


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# 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_datasetObject

#

fetch_dataset

We obtain the gnome-url dataset, that is, from ftp.gnome.org/pub/GNOME/sources/?C=M;O=D.

#


153
154
155
# 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_programsObject

#

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
#


331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# 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

#konsole_support?Boolean

#

konsole_support?

#

Returns:

  • (Boolean)


111
112
113
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 111

def konsole_support?
  Object.const_defined? :Colours
end

#last_update?Boolean

#

last_update?

This method shows when a last update happened.

#

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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(url = @matches) ⇒ Object

#

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.

#


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# 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(i = commandline_arguments? ) ⇒ Object

#

process_commandline_arguments

To invoke this method, do something like:

gnomenew --help
#


411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# 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_sigintObject

#

register_sigint

#


458
459
460
461
462
463
464
465
466
467
# 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

#remote_url?Boolean

#

remote_url?

#

Returns:

  • (Boolean)


160
161
162
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 160

def remote_url?
  URL_TO_GNOME
end

#report_all_found_programsObject Also known as: report

#

report_all_found_programs

This method is the powerhorse method.

#


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# 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

#report_then_displayObject

#

report_then_display

#


103
104
105
106
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 103

def report_then_display
  report
  obtain_most_recent_entry_from_this_url
end

#resetObject

#

reset

#


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

#return_remote_url_for(i) ⇒ Object

#

return_remote_url_for

#


86
87
88
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 86

def return_remote_url_for(i)
  BASE_URL_TO_GNOME_SOURCES+i+'/'+SORTED
end

#runObject

#

run

Bundle the main logic together here.

#


485
486
487
488
489
490
491
492
493
494
# 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(shall_we_exit = :then_exit) ⇒ Object

#

show_help (help tag)

Show which help options this class accepts.

#


474
475
476
477
478
# 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

#store_where?Boolean

#

store_where?

We will store in a specific file.

#

Returns:

  • (Boolean)


218
219
220
# File 'lib/rbt/check_for_updates/check_for_gnome_updates.rb', line 218

def store_where?
  GNOME_LAST_UPDATE
end

#update_this_entry(i) ⇒ Object

#

update_this_entry

Use this method only when you wish to update a specific program.

We will delegate towards class Cookbooks::UpdateEntry for this.

#


395
396
397
398
399
400
401
# 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