class RBT::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.

#
SHALL_WE_UPLOAD_THE_COOKBOOK_RECIPE
#

SHALL_WE_UPLOAD_THE_COOKBOOK_RECIPE

If the following constant is set to true, then we will also attempt to upload this program to a remote FTP site. As of August 2018, I have changed the default here to false, since the real benefit of automatically uploading is too small compared to the disadvantages (in particular that this class would take longer before it completes its job).

#
TRY_TO_REPACKAGE_INTO_TAR_XZ_FORMAT
#

TRY_TO_REPACKAGE_INTO_TAR_XZ_FORMAT

If the following constant is true then incremented programs will be repackaged into .tar.xz, unless they are already in this format.

This constant is thus mostly a convenience setting, if you wish to keep archives in .tar.xz, which I do. However had, by default, this class will not repackage, since other users may not want to have this. Currently only class RBT::IncrementProgramVersion will enable this repackaging action.

If we are on a roebe-system then this will be switched to true, aka on.

#
WGET_IS_AVAILABLE

Public Class Methods

new( i = nil, run_already = true ) { || ... } click to toggle source
#

initialize

#
# File lib/rbt/utility_scripts/update_entry.rb, line 122
def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_remote_url(i)
  # ======================================================================= #
  # === Handle blocks next:
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    when :do_not_ftp_upload
      @shall_we_upload_the_cookbook_recipe = false
    else
      # =================================================================== #
      # === Handle Hashes
      # =================================================================== #
      if yielded.is_a? Hash
        if yielded.has_key? :repackage_into_tar_xz_format
          if yielded[:repackage_into_tar_xz_format] == true
            do_try_to_repackage_into_tar_xz_format
          end
        end
      end
    end
  end
  run if run_already
end

Public Instance Methods

are_we_allowed_to_save?() click to toggle source
#

are_we_allowed_to_save?

#
# File lib/rbt/utility_scripts/update_entry.rb, line 285
def are_we_allowed_to_save?
  ARE_WE_ALLOWED_TO_SAVE
end
assign_url( i = nil )
Alias for: set_remote_url
backup_old_file() click to toggle source
#

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/rbt/utility_scripts/update_entry.rb, line 327
def backup_old_file
  unless @old_file_content == @save_this_data # Only do so if we don't have the same data.
    _ = rbt_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() click to toggle source
#

check_whether_file_exists_or_not

#
# File lib/rbt/utility_scripts/update_entry.rb, line 233
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() click to toggle source
#

consider_changing_directory

#
# File lib/rbt/utility_scripts/update_entry.rb, line 305
def consider_changing_directory
  if SHALL_WE_TRY_TO_CHANGE_DIRECTORY_INTO_THE_BASE_DIR
    _ = source_directory?+@program_information.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 _, :ensure_that_the_directory_exists
    end
  end
end
consider_downloading_this_package( i = remote_url? ) click to toggle source
#

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.

Once the download has finished, we may also repackage the archive into the .tar.xz format.

#
# File lib/rbt/utility_scripts/update_entry.rb, line 562
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: 'RBT->Wget' }} # Delegate to Wget here.
    else # else default to a simpler hardcoded, non-ruby wget solution here.
      _ = 'wget '+i
      esystem _
    end
    if @try_to_repackage_into_tar_xz_format
      # =================================================================== #
      # Presently the formats .tar.gz, .tar.bz2, .tgz and .gzip will
      # be repackaged, if the class is instructed to do so.
      # =================================================================== #
      if i.end_with?('.tar.gz')  or
         i.end_with?('.tar.bz2') or
         i.end_with?('.tgz') or
         i.end_with?('.zip')
        repackage(
          File.basename(i)
        )
      end
    end
  end unless File.exist?(File.basename(i))
end
consider_handling_a_changelog_entry() click to toggle source
#

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/rbt/utility_scripts/update_entry.rb, line 431
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)
    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() click to toggle source
#

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/rbt/utility_scripts/update_entry.rb, line 295
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() click to toggle source
#

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/rbt/utility_scripts/update_entry.rb, line 489
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 = RBT::Cookbooks::Cookbook.new(program_name)
    hash_to_store = cookbook_dataset.return_hash
    # ===================================================================== #
    # Save the Hash next.
    # ===================================================================== #
    what = YAML.dump(hash_to_store)
    into = "#{RBT.expanded_cookbooks?}#{program_name}.yml"
    opnn; e 'Also saving the expanded dataset into `'+sfile(into)+'`.'
    write_what_into(what, into)
  end
end
do_try_to_repackage_into_tar_xz_format() click to toggle source
#

do_try_to_repackage_into_tar_xz_format

#
# File lib/rbt/utility_scripts/update_entry.rb, line 171
def do_try_to_repackage_into_tar_xz_format
  @try_to_repackage_into_tar_xz_format = true
end
entry?()
Alias for: remote_url?
keep_track_of_all_programs_that_were_updated() click to toggle source
#

keep_track_of_all_programs_that_were_updated

#
# File lib/rbt/utility_scripts/update_entry.rb, line 526
def keep_track_of_all_programs_that_were_updated
  what = " - #{name_of_the_program_that_was_updated?}#{N}"
  into = "#{rbt_log_dir?}these_programs_were_updated.yml"
  opnn;e 'Appending `'+sfancy(what.strip)+'` into `'+sfile(into)+'`.'
  append_what_into(what, into)
end
location?() click to toggle source
#

location?

This feedbacks the local yaml file in question, such as 'htop.yml' and similar.

#
# File lib/rbt/utility_scripts/update_entry.rb, line 539
def location?
  @location
end
name_of_the_program_that_was_updated?() click to toggle source
#

name_of_the_program_that_was_updated?

This method will return the name of the remote program, with '-' and '_' removed.

#
# File lib/rbt/utility_scripts/update_entry.rb, line 549
def name_of_the_program_that_was_updated?
  File.basename(location?).sub(/\.yml$/,'').delete('-_')
end
new_program_version?() click to toggle source
#

new_program_version?

#
# File lib/rbt/utility_scripts/update_entry.rb, line 226
def new_program_version?
  @new_program_version
end
output(i) click to toggle source
#

output

Wrap opn() and e() into this single method.

#
# File lib/rbt/utility_scripts/update_entry.rb, line 342
def output(i)
  opnn; e i
end
program_name?() click to toggle source
#

program_name?

#
# File lib/rbt/utility_scripts/update_entry.rb, line 413
def program_name?
  @program_information.program_name?
end
program_version?() click to toggle source
#

program_version?

#
# File lib/rbt/utility_scripts/update_entry.rb, line 406
def program_version?
  @program_information.program_version?
end
read_file() click to toggle source
#

read_file

This will read in the dataset. We must be careful with File.readlines() as the encoding may be US-ASCII.

#
# File lib/rbt/utility_scripts/update_entry.rb, line 181
def read_file
  output 'Now working on the file'
  e "  #{sfile(location?)}"\
    " (#{royalblue(@program_information.name?.downcase)})"
  if File.exist? location?
    @old_file_content = readlines_with_proper_encoding(location?)
    @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 = archive_type_of?(splitted[-1]) # bl $RUBY_RBT/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?() click to toggle source
#

remote_url?

#
# File lib/rbt/utility_scripts/update_entry.rb, line 478
def remote_url?
  @remote_url
end
Also aliased as: entry?
reset() click to toggle source
#

reset

#
Calls superclass method RBT::Base#reset
# File lib/rbt/utility_scripts/update_entry.rb, line 155
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 = SHALL_WE_UPLOAD_THE_COOKBOOK_RECIPE
  @try_to_repackage_into_tar_xz_format = TRY_TO_REPACKAGE_INTO_TAR_XZ_FORMAT
  if is_on_roebe?
    @try_to_repackage_into_tar_xz_format = true
  end
  @namespace = NAMESPACE
end
run() click to toggle source
#

run (run tag, main logic)

#
# File lib/rbt/utility_scripts/update_entry.rb, line 593
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
  keep_track_of_all_programs_that_were_updated
end
save_the_file() click to toggle source
#

save_the_file

Specialized old save functionality.

#
# File lib/rbt/utility_scripts/update_entry.rb, line 351
def save_the_file
  store_into_this_file = location?
  if store_into_this_file.include? RUBY_SRC_DIR_AT_HOME
    # ===================================================================== #
    # This is the wrong path for now, since we will add the correct
    # path on roebe lateron.
    # ===================================================================== #
    store_into_this_file = "#{individual_cookbooks_directory?}#{File.basename(store_into_this_file)}"
  end
  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_RBT_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.
        # ================================================================= #
        if File.exist? FILE_PROGRAMS_VERSION
          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.
          RBT.generate_programs_version_yaml_file(
            new_hash
          )
        else
          output 'No file exists at '+sfile(FILE_PROGRAMS_VERSION)+'.'
        end
      end
    end
  else
    output 'We are not allowed to save into '\
           '`'+sfile(store_into_this_file)+'`.'
  end
end
set_location() click to toggle source
#

set_location

Use this method to set the @location variable. This will also set on the @program_information variable.

#
# File lib/rbt/utility_scripts/update_entry.rb, line 513
def set_location
  @program_information = ProgramInformation.new(remote_url?)
  _ = @program_information.real_name?.
      downcase.delete('_')
  # ======================================================================= #
  # Determine which yaml file is to be used.
  # ======================================================================= #
  @location = return_location_to_this_programs_yaml_file(_)
end
set_new_program_version(i) click to toggle source
#

set_new_program_version

#
# File lib/rbt/utility_scripts/update_entry.rb, line 219
def set_new_program_version(i)
  @new_program_version = i
end
set_remote_url( i = nil ) click to toggle source
#

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/rbt/utility_scripts/update_entry.rb, line 248
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, the following logic section was added - if the URL
  # includes the string "sourceforge", and the last part is "download",
  # we will chop it off that part and use the modified variant instead.
  # ======================================================================= #
  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
Also aliased as: assign_url