class RBT::Cookbooks::FindDuplicateBinaries

Constants

DELETE_DUPLICATE_FILES
#

DELETE_DUPLICATE_FILES

If this constant is true then we will also delete the duplicate binaries. This can be toggled by the user on the commandline.

By default, we will NOT delete the duplicate entries and this will also not be changed. The user specifically has to enable it on his/her own.

#
NAMESPACE
#

NAMESPACE

#

Public Class Methods

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

initialize

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 46
def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_input(i)
  run if run_already
end

Public Instance Methods

comment(i) click to toggle source
#

comment

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 112
def comment(i)
  Colours.comment(i) # Delegate to class Colours for this.
end
delete_duplicates?() click to toggle source
#

delete_duplicates?

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 58
def delete_duplicates?
  @delete_duplicate_files
end
detect_duplicate_files() click to toggle source
#

detect_duplicate_files

This will detect all duplicate files.

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 195
def detect_duplicate_files
  @duplicate_files = [] # Empty at the beginning.
  ubin_array   = @usr_bin.map {|entry| File.basename(entry) }.sort
  sysbin_array = @system_executables.map {|entry| File.basename(entry) }
  counter = 0
  # ======================================================================= #
  # Next, compare the ubin-array with the sysbin-array:
  # ======================================================================= #
  ubin_array.each {|entry|
    if sysbin_array.include? entry
      counter += 1
      _ = ('('+simp(counter.to_s)+') ').rjust(20)
      e _+'Yes, '+slateblue(entry)+' is included. It is thus a duplicate.'
      @duplicate_files << entry
    end
  }
  @duplicate_files.sort! # And keep them sorted alphabetically.
end
do_delete_duplicate_files() click to toggle source
#

do_delete_duplicate_files

This will delete only files in the /usr/bin/ hierarchy.

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 167
def do_delete_duplicate_files
  if delete_duplicates? and !@duplicate_files.empty?
    opnn; e 'Now deleting duplicate files from the '+sdir(ubin?)+
            ' directory.'
    @duplicate_files.each {|entry|
      _ = sysbin?+File.basename(entry)
      if File.exist? _ # Extra safeguard here.
        e 'The file path at '+sfile(ubin?+entry)+' is a duplicate '\
          'of `'+sfile(File.readlink(_))+'`.'
        e 'Thus we can delete `'+sfile(ubin?+entry)+'`.'
        delete(ubin?+entry) # Delete it finally.
      end
    }
  end
end
found_duplicates?() click to toggle source
#

found_duplicates?

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 105
def found_duplicates?
  !@duplicate_files.empty?
end
grab_files_from_system_index_bin() click to toggle source
#

grab_files_from_system_index_bin

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 186
def grab_files_from_system_index_bin
  @system_executables = Dir["#{sysdir}*"]
end
grab_files_from_usr_bin() click to toggle source
#

grab_files_from_usr_bin

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 90
def grab_files_from_usr_bin
  @usr_bin = Dir["#{ubin_dir}*"]
end
input?() click to toggle source
#

input?

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 83
def input?
  @input
end
main_dir?()
Alias for: sysdir?
menu( i = @input ) click to toggle source
#

menu

#
report_duplicate_files() click to toggle source
#

report_duplicate_files

This method will report whether we have duplicate files.

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 228
def report_duplicate_files
  if found_duplicates?
    opnn; e 'We have found these '+simp(@duplicate_files.size.to_s)+' '\
            'duplicates.'
    opnn; e "They originate from the directory `#{sdir(ubin_dir?)}` and"
    opnn; e 'are compared to the directory `'+sdir(sysbin_dir?)+'.'
    long_string = word_wrap(
      @duplicate_files.join(' ').strip, 62
    )
    long_string.chop! if long_string.end_with? ',' # Get rid of traling ','.
    e "  #{long_string.strip}"
  else
    opnn; e 'We did not find any duplicate entries. Thus, we will '\
            'do no further action.'
  end
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method RBT::Base#reset
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 65
def reset
  super()
  @namespace = NAMESPACE
  @delete_duplicate_files = DELETE_DUPLICATE_FILES
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 248
def run
  menu # Check for the commandline-arguments first.
  report_to_the_user_what_we_will_do_next
  grab_files_from_system_index_bin
  grab_files_from_usr_bin
  detect_duplicate_files
  report_duplicate_files
  do_delete_duplicate_files if delete_duplicates?
end
set_input(i = '') click to toggle source
#

set_input

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 74
def set_input(i = '')
  i = i.first if i.is_a? Array
  i = i.to_s.dup if i
  @input = i
end
show_help_options() click to toggle source
#

show_help_options (help tag)

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 141
def show_help_options
  e 'Currently we support only one option:'
  e
  Colours.ecomment '   --delete # delete duplicate files found in '\
    'the '+sdir(ubin?)+comment(' hierarchy') # finddup --help
  e
end
sysbin?()
Alias for: sysdir?
sysbin_dir?()
Alias for: sysdir?
sysdir()
Alias for: sysdir?
sysdir?() click to toggle source
#

sysdir (sysdir tag)

This method will refer to “/System/Index/bin” normally - at the least on a GoboLinux-like system.

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 155
def sysdir?
  sysbin_directory?
end
Also aliased as: sysbin?, sysdir, main_dir?, sysbin_dir?
ubin?()
Alias for: ubin_dir
ubin_dir() click to toggle source
#

ubin_dir

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 97
def ubin_dir
  '/usr/bin/'
end
Also aliased as: ubin?, ubin_dir?
ubin_dir?()
Alias for: ubin_dir

Private Instance Methods

report_to_the_user_what_we_will_do_next() click to toggle source
#

report_to_the_user_what_we_will_do_next

#
# File lib/rbt/utility_scripts/find_duplicate_binaries.rb, line 217
def report_to_the_user_what_we_will_do_next
  opnn; e 'We will compare all entries from '+sdir(sysdir)+' with '\
          'entries from `'+sdir(ubin_dir)+'`.'
  opnn; e 'Duplicate entries will be reported.'
end