Class: RBT::ReplaceSymlinks

Inherits:
Base show all
Defined in:
lib/rbt/utility_scripts/replace_symlinks.rb

Overview

Constant Summary collapse

REMOVE_INSTEAD_OF_SYMLINKING =
#

REMOVE_INSTEAD_OF_SYMLINKING

Set this to true if you want to remove rather than symlink.

It may be better to just symlink, rather than remove anyway, hence why this constant is usually kept at false.

#
false
ARRAY_ALLOWED_DIRS =
#

ARRAY_ALLOWED_DIRS

ARRAY_ALLOWED_DIRS specifies from which directories to run this script.

We only allow it from specific directories such as /usr/bin and others.

#
%w(
  /bin 
  /usr/bin
  /sbin
  /usr/sbin
)

Constants inherited from LeanPrototype

LeanPrototype::ALL_COLOUR_METHODS, LeanPrototype::ARRAY_KDE_KONSOLE_COLOURS_IN_USE, LeanPrototype::DAY_NAMES, LeanPrototype::NAMESPACE, LeanPrototype::SILENT_REDIRECTION

Instance Method Summary collapse

Methods inherited from Base

#cat, #cd_back_to_the_current_working_directory, #consider_reporting_how_many_programs_are_registered, #cookbook_files?, #copy_recursively, #delete_code_of_conduct?, #determine_appdir_prefix_from_this_input, #directory_validation?, #do_not_show_names, #fast_return_file_size_of_this_program, #file_last_symlinked_program?, #file_programs_version_url, #gem_version, #is_a_64bit_system?, #is_ccache_available?, #is_porg_available?, #is_this_binary_registered?, #is_this_program_included?, #make_command?, #make_install_command?, #new_cookbook_instance_for, #program_exists?, #publish_list_of_all_programs_version, #rcp, registered_binaries?, #remove_symlink, #rename_kde_konsole_tab, #repackage, #report_n_programs, #report_working_directory, #return_compile_time_statistics, #return_n_random_characters, #return_version_of_this_program, #run_make_then_make_install, #sanitize_url1_entry, #set_source_directory, #show_overview_over_available_utility_scripts, #symlink_all_files_from_this_directory_to_that_directory, #system_tags?, #upload_list_of_all_programs_version, #use_yaml_database_for_tags?, #warn_and_exit, #wget

Methods inherited from LeanPrototype

#a_or_an?, #abbreviations?, #absolute_path, #action, #add_to_the_commandline_arguments, #all_binaries?, #all_files_from, #all_libraries?, #allowed_cookbook_entries?, #appdir_location_of?, #append_onto_the_internal_hash, #append_what_into, #archive_type_of?, #available_programs?, #be_quiet?, #be_verbose?, #begins_with_a_comment?, #capitalize_first_alphabetical_character, #cd_to_the_log_directory, #cd_to_the_temp_directory, #change_directory, #change_permission, #cheering_person?, #chmod, #chop_off_archive, #clear_commandline_arguments, #cliner, #coloured_and_padded_e, #coloured_and_padded_esystem, #coloured_esystem, #colourize_directory_for_system_results, #colourize_this_error, #colourize_this_file_path, #colourize_this_warning, #commandline_arguments?, #commandline_arguments_without_hyphens?, #comment, #convert_dd_mm_yyyy_to_its_long_variant, #convert_env_variable, #copy_directory, #copy_files, #cpr, #create_directory, #current_hour?, #dd_mmm_yyy, #debug, #debug?, #directory_expanded_cookbooks?, #disable_colours, #display_md5sum?, #do_not_debug, #does_the_cookbook_include_this_program?, #does_this_expanded_cookbook_file_exist_for_this_program?, #does_this_file_exist?, #does_this_file_exist_and_is_it_a_file?, #e, #eblue, #ecomment, #ecrimson, #edir, #editor?, #efancy, #eimp, #enable_colours, #enable_debug, #ensure_main_encoding_for, #eparse, #esystem, #esystem_gold, #etomato, #ewarn, #exit_program, #expanded_cookbooks_directory_exists?, #extract_this_archive, #extract_to?, #file_compiled_programs?, #file_dirname_retaining_trailing_slash, #file_predefined_installation_instructions?, #file_specification_of_registered_cookbook_entries, #find_cookbook_alias_for, #find_this_yaml_file, #first_argument?, #first_non_hyphen_argument?, #get_all_directories_from, #get_all_files_from, #get_all_programs, #get_date, #get_extended_date, #get_files_and_directories_from, #go_to_base_dir, #home_dir?, #host_system?, #infer_the_namespace, #internal_hash?, #internal_hash_set_commandline_arguments, #is_an_archive?, #is_directory?, #is_file?, #is_github_url?, #is_make_available?, #is_meson_installed?, #is_on_gobolinux?, #is_on_windows?, #is_roebe?, #is_superuser?, #is_symlink?, #is_this_a_header?, #is_this_a_library?, #is_this_program_included?, #iso_encoding?, #load_dataset_from_this_expanded_cookbook, #load_yaml, #load_yaml_file_from_the_cookbook_directory_for_this_program, #log_directory?, #main_encoding?, #meson_build_file_exists?, #months?, #move_file, #n_programs_are_available?, #n_programs_available?, #namespace?, #no_directory_exists_at, #no_directory_was_found_at, #no_opn, #no_such_file_exists, #open_in_browser, #open_in_editor, #opncomment, #opne, #opnef, #opnerev, #opnerror, #opnesystem, #opnewarn, #opnfancy, #opnn, #otomato, #packages_directory?, #path_to_this_expanded_cookbooks_dataset, #pkgconfig_directory?, #populate_the_internal_hash_with_default_values, #predefined_installation_instructions?, #prepend_this_commandline_argument, #program_version_of?, #project_base_directory?, #project_yaml_directory?, #query_path?, #rarrow?, #read_file, #read_file_in_default_encoding, #read_file_with_default_encoding, #readlines, #readlines_with_proper_encoding, #register_sigint, #registered_cookbook_entries?, #remove, #remove_archive_from_the_end, #remove_comments_from_each_line, #remove_directory, #remove_double_slashes, #remove_file, #remove_newlines, #remove_parens, #remove_symlink, #remove_the_first_commandline_argument, #remove_this_commandline_argument, #remove_this_entry_from_the_commandline_arguments, #remove_trailing_ANSII_escape_code, #remove_unnecessary_data_from_url, #rename, #report_pwd, #require_the_rbt_aliases, #reset_the_internal_hash, #return_all_archives_from_this_directory, #return_appdir_prefix, #return_commandline_arguments_with_leading_hyphens, #return_current_hour_minutes_second, #return_date, #return_day_of_the_month_based_on_utc, #return_full_time, #return_hours_minutes_seconds_based_on_utc, #return_location_to_this_programs_yaml_file, #return_month_based_on_this_number, #return_month_based_on_utc, #return_opnn, #return_program_name, #return_program_name_for_gobolinux_systems, #return_program_name_via_program_information, #return_pwd, #return_unicode_warning_symbol_or_empty_string, #return_utc, #return_utc_time_in_a_format_similar_to_slackware, #return_weekday_based_on_utc, #return_year_based_on_utc, #rev, #rpn, #run_simulation=, #run_simulation?, #sdir, #sdir_return_pwd, #set_be_silent, #set_be_verbose, #set_first_commandline_argument, #set_namespace, #set_use_colours, #set_use_opn, #set_xorg_buffer, #sfancy, #sfile, #silent_redirection?, #silently_create_this_directory_if_it_does_not_yet_exist, #simp, #source_base_directory?, #ssym, #stderr, #store_into_this_directory?, #string_right_arrow?, #swarn, #symlink, #sysbin_directory?, #sysetc_directory?, #sysinclude_directory?, #syslib_directory?, #sysshare_directory?, #system_directory?, #temp_directory?, #to_bool, #to_camelcase, #to_iso_encoding, #to_unicode, #today?, #touch, #try_to_require_beautiful_url, #try_to_require_the_environment_information_gem, #try_to_require_the_extracter_gem, #try_to_require_the_open_gem, #try_to_require_the_xorg_buffer, #try_to_require_wget, #try_to_return_a_special_compile_component, #unicode_cliner, #unicode_middle_cliner, #use_colours=, #use_colours?, #use_opn=, #use_opn?, #utf_encoding?, #verbose_truth, #word_wrap, #write_what_into, #write_what_into_via_unicode, #yes_or_no

Constructor Details

#initialize(this_dir = return_pwd, remove_instead_of_symlinking = REMOVE_INSTEAD_OF_SYMLINKING, run_already = true) ⇒ ReplaceSymlinks

#

initialize

“this_dir” is the allowed dir, our target.

#


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 48

def initialize(
    this_dir                     = return_pwd, 
    remove_instead_of_symlinking = REMOVE_INSTEAD_OF_SYMLINKING,
    run_already                  = true
  )
  reset
  set_this_dir(this_dir)
  set_remove_symlink(
    remove_instead_of_symlinking
  )
  run if run_already
end

Instance Method Details

#allowed_dirs?Boolean

#

allowed_dirs?

#

Returns:

  • (Boolean)


140
141
142
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 140

def allowed_dirs?
  @array_allowed_dirs
end

#append_this_directory_to_the_allowed_directories(i) ⇒ Object

#

append_this_directory_to_the_allowed_directories

#


133
134
135
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 133

def append_this_directory_to_the_allowed_directories(i)
  @array_allowed_dirs << i
end

#collect_filesObject

#

collect_files

Collects all files from a given dir.

#


166
167
168
169
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 166

def collect_files
  @array_files = Dir["#{@this_dir}*"].select {|entry| File.file? entry }
  ppexit @array_files if @debug
end

#feedback_allowed_directoriesObject

#

feedback_allowed_directories

#


198
199
200
201
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 198

def feedback_allowed_directories
  opne "#{rev}The #{salmon('allowed')} #{rev}directories are:"
  print '  '; pp ARRAY_ALLOWED_DIRS
end

#is_allowed_dir?(d) ⇒ Boolean

#

is_allowed_dir?

Returns true if the dir is allowed, otherwise it returns false.

#

Returns:

  • (Boolean)


124
125
126
127
128
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 124

def is_allowed_dir?(d)
  _ = false
  _ = true if @array_allowed_dirs.include? d
  return _
end

#replace_filesObject

#

replace_files

Normally this is used to symlink, but we can also remove.

#


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 176

def replace_files
  @array_files.each { |file|
    target_file = sysbin_directory?+File.basename(file)
    if File.exist? target_file
      if @remove_symlink
        opne "#{rev}Removing #{sfile(file)}#{rev}."
      else
        opne "#{rev}Replacing '{sfile(file)}"\
             "#{rev} with #{sfile(target_file)}#{rev}." # Wanna remove /bin/unlink.
      end
      if @simulate_only
      else
        remove(file) # Need to remove the old file.
        symlink(file, target_file) if @remove_symlink == false
      end
    end
  }
end

#resetObject

#

reset

#


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 64

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === :debug
  # ======================================================================= #
  disable_debug
  # ======================================================================= #
  # === @simulate_only
  # ======================================================================= #
  @simulate_only = true
  # ======================================================================= #
  # === @array_allowed_dirs
  # ======================================================================= #
  @array_allowed_dirs = ARRAY_ALLOWED_DIRS
end

#runObject

#

run (run tag)

We collect the files, then we replace them.

#


208
209
210
211
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 208

def run
  collect_files
  replace_files
end
#
#


115
116
117
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 115

def set_remove_symlink(i)
  @remove_symlink = i
end

#set_this_dir(i) ⇒ Object

#

set_this_dir

#


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 84

def set_this_dir(i)
  i = i.first if i.is_a? Array
  i = return_pwd if i.nil?
  case i # case tag
  # ======================================================================= #
  # === replace_symlinks --help
  # ======================================================================= #
  when /-?-?help$/i
    show_help
    exit
  # ======================================================================= #
  # === replace_symlinks --directory=/opt
  # ======================================================================= #
  when /--directory=(.+)/ # This is allowed to bypass the allowed directory.
    i = $1.to_s.dup
    append_this_directory_to_the_allowed_directories(i)
  end
  unless is_allowed_dir? i # Verify that the directory is proper. Report otherwise.
    e 'Not allowed directory: '+sdir(i)
    feedback_allowed_directories
    e 'Consider changing or using one of the directories listed above.'
    e 'Hint: You can do so via --directory='
    raise NotAllowedDirectory, 'not allowed directory' 
  end
  i << '/' unless i.end_with? '/'
  @this_dir = i
end

#show_helpObject

#

show_help (help tag)

#


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rbt/utility_scripts/replace_symlinks.rb', line 147

def show_help
  opn(:no_colon)
  e
  e
  e 'This class can either replace or remove symlinks.'
  e 'In order to use this, navigate to the target directory '\
    'in question'
  e 'so that it becomes the current working directory (cwd / pwd).'
  e
  e 'Alternatively, you can use the option --directory= to assign'
  e 'to a specific directory.'
  e
end