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.

#

Public Class Methods

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

initialize

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

Public Instance Methods

are_the_two_target_directories_the_same_directory?() click to toggle source
#

are_the_two_target_directories_the_same_directory?

#
# File lib/rbt/cookbooks/find_duplicate_binaries.rb, line 264
def are_the_two_target_directories_the_same_directory?
  sysdir == ubin_dir
end
delete_duplicates?() click to toggle source
#

delete_duplicates?

#
# File lib/rbt/cookbooks/find_duplicate_binaries.rb, line 72
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/cookbooks/find_duplicate_binaries.rb, line 200
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) }.sort
  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 '+
        steelblue('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/cookbooks/find_duplicate_binaries.rb, line 168
def do_delete_duplicate_files
  if delete_duplicates? and !@duplicate_files.empty?
    opne 'Now deleting duplicate files from the '+sdir(ubin?)+
         rev+' 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/cookbooks/find_duplicate_binaries.rb, line 110
def found_duplicates?
  !@duplicate_files.empty?
end
grab_files_from_system_index_bin(i = sysbin_directory?) click to toggle source
#

grab_files_from_system_index_bin

This will typically obtain all entries from /System/Index/bin/.

#
# File lib/rbt/cookbooks/find_duplicate_binaries.rb, line 191
def grab_files_from_system_index_bin(i = sysbin_directory?)
  @system_executables = Dir["#{i}*"]
end
grab_files_from_usr_bin() click to toggle source
#

grab_files_from_usr_bin

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

input?

#
# File lib/rbt/cookbooks/find_duplicate_binaries.rb, line 88
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/cookbooks/find_duplicate_binaries.rb, line 236
def report_duplicate_files
  if found_duplicates?
    opne 'We have found these '+simp(@duplicate_files.size.to_s)+' '\
         'duplicates.'
    opne "They originate from the directory `#{sdir(ubin_dir?)}` and"
    opne 'are compared to the directory `'+sdir(sysbin_dir?)+'.'
    # ======================================================================= #
    # I am not sure why the code below exists; I assume I may have
    # wanted to show which entries are duplicates. But it is quite
    # spammy, so it was disabled on 25.05.2020. It may be re-enabled
    # at a later time, but then perhaps via a method call, and in
    # a slightly different formatted output, including colours.
    # But it may also be removed entirely instead - we will see.
    #   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
    opne '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::LeanPrototype#reset
# File lib/rbt/cookbooks/find_duplicate_binaries.rb, line 60
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @delete_duplicate_files
  # ======================================================================= #
  @delete_duplicate_files = DELETE_DUPLICATE_FILES
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/cookbooks/find_duplicate_binaries.rb, line 271
def run
  menu # Check for the commandline-arguments first.
  report_to_the_user_what_we_will_do_next
  if are_the_two_target_directories_the_same_directory?
    opne 'The two directories are the same, so we can not '\
         'compare them -'
    opne 'it would make no sense.'
  else
    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
end
set_input(i = '') click to toggle source
#

set_input

#
# File lib/rbt/cookbooks/find_duplicate_binaries.rb, line 79
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/cookbooks/find_duplicate_binaries.rb, line 141
def show_help_options
  e 'Currently we support only one option:'
  e
  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/cookbooks/find_duplicate_binaries.rb, line 156
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/cookbooks/find_duplicate_binaries.rb, line 102
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/cookbooks/find_duplicate_binaries.rb, line 223
def report_to_the_user_what_we_will_do_next
  opne 'We will compare all entries from '+
       sdir(sysdir)+' with '\
       'entries from `'+
       sdir(ubin_dir)+'`.'
  opne 'Duplicate entries will be reported.'
end