class ProgramInformation::ProgramInformation
Constants
- APOSTROPH
#¶ ↑
APOSTROPH
¶ ↑#¶ ↑
- BE_VERBOSE
#¶ ↑
BE_VERBOSE
¶ ↑#¶ ↑
- COLOUR_FOR_REVERT
#¶ ↑
COLOUR_FOR_REVERT
¶ ↑#¶ ↑
- DEFAULT_VERSION
- LAST_UPDATE
#¶ ↑
LAST_UPDATE
¶ ↑#¶ ↑
- NAME_OF_CLASS
#¶ ↑
NAME_OF_CLASS
¶ ↑#¶ ↑
- REMOVE_PLUS_AND_MINUS
#¶ ↑
REMOVE_PLUS_AND_MINUS
¶ ↑If true then we will remove plus ‘+’ and minus ‘-’ from the program name.
#¶ ↑
- REMOVE_PLUS_TOKEN
#¶ ↑
REMOVE_PLUS_TOKEN
¶ ↑If this constant is true then we will eliminate ‘+’ in the input.
This overrules the replace-constant called
REPLACE_PLUS_WITH_LONG_NAME
.Also note that if the constant
REMOVE_PLUS_AND_MINUS
is set to true then this setting here will always be set to false.#¶ ↑
- REPLACE_PLUS_WITH_LONG_NAME
#¶ ↑
REPLACE_PLUS_WITH_LONG_NAME
¶ ↑If this constant is true then we will replace all ‘+’ with the string ‘plus’ as part of the issued name. Note that remove-actions are stronger, so if the constant
REMOVE_PLUS_AND_MINUS
was also set to true, then the constant here will not be honoured at all.#¶ ↑
- SHALL_WE_DOWNCASE
#¶ ↑
SHALL_WE_DOWNCASE
¶ ↑So that input such as “Libgnome” can become “libgnome”.
#¶ ↑
- VERSION
#¶ ↑
VERSION
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
initialize¶ ↑
Specific Usage Example:
ProgramInformation::ProgramInformation.new('util-linux-ng-2.15')
#¶ ↑
# File lib/program_information/initialize.rb, line 15 def initialize( i = ARGV, run_already = true, optional_be_verbose = false ) reset set_input(i) if optional_be_verbose set_be_verbose(optional_be_verbose) end case run_already when :do_run_already run_already = true when :dont_run_already, :do_not_run_already run_already = false end run if run_already end
Public Instance Methods
#¶ ↑
determine_the_three_main_variables
¶ ↑
This method will determine the three main instance variables:
- @short_name - @real_short_name - @program_version
We must be careful, as the input may not include any ‘-’ or ‘_’ token, and then the regex would fail. Hence, the behaviour of this method will depend on whether the input includes a ‘-’ or whether it does not.
Note that presently, set_input
() will modify the given input and ensure that there will be a ‘-’ token, even in input that does not have any ‘-’ or ‘_’ such as in ‘sendmail.8.15.2’.
#¶ ↑
# File lib/program_information/program_information.rb, line 307 def determine_the_three_main_variables(i = @input) if i if i.include? '|' splitted = i.split('|') short_name = splitted.first.to_s.dup program_version = splitted.last.to_s.dup set_short_name(short_name) set_program_version(program_version) else opn; e "The input `#{i}` does not include a | character." end end end
#¶ ↑
do_not_replace_plus
¶ ↑
#¶ ↑
# File lib/program_information/program_information.rb, line 71 def do_not_replace_plus @replace_plus_with_long_name = false end
#¶ ↑
remove_plus_and_minus
?¶ ↑
#¶ ↑
# File lib/program_information/program_information.rb, line 146 def remove_plus_and_minus? @remove_plus_and_minus end
#¶ ↑
remove_source_like_strings
¶ ↑
#¶ ↑
# File lib/program_information/program_information.rb, line 377 def remove_source_like_strings(i) # ======================================================================= # # i.gsub!(/--/,'-') if i.include? '--' # Replace '--' with '-'. # ^^^ May not be good, depending on the input. # ======================================================================= # i.sub!(/-src/,'') if i.include? '-src' # Get rid of -src entries. i.sub!(/\.source/,'') if i.include? '.source' i.sub!(/-source$/,'') if i.end_with? '-source' return i end
#¶ ↑
report (debug tag, report tag)¶ ↑
#¶ ↑
# File lib/program_information/report.rb, line 12 def report( be_verbose = be_verbose? ) case be_verbose when :do_report, :be_verbose be_verbose = true end lpad = 90 rpad = 49 if be_verbose inner_ljust = 28 # ===================================================================== # # === real short name # ===================================================================== # e (NAME_OF_CLASS+(' Program real short name ').ljust(inner_ljust)+'('+ yellow?+'@real_short_name'+rev+') is: ').ljust(lpad)+ (apostroph?+real_short_name?+apostroph?). ljust(rpad) # ===================================================================== # # === short name # ===================================================================== # e ( NAME_OF_CLASS+(' Program name').ljust(inner_ljust)+'('+ yellow?+'@short_name'+rev+' ) is: ' ).ljust(lpad)+(apostroph?+short_name?+apostroph?). ljust(rpad) # ===================================================================== # # === program version # ===================================================================== # e (NAME_OF_CLASS+(' Program version ').ljust(inner_ljust)+'('+ yellow?+'@program_version'+rev+') is: ').ljust(lpad)+ (apostroph?+version?+apostroph?). ljust(rpad) end end
#¶ ↑
reset¶ ↑
#¶ ↑
# File lib/program_information/reset.rb, line 14 def reset # ======================================================================= # # === @short_name # ======================================================================= # @short_name = nil # ======================================================================= # # === @real_short_name # ======================================================================= # @real_short_name = nil # ======================================================================= # # === @program_version # ======================================================================= # @program_version = nil set_shall_we_downcase # Should become before set_input(). set_be_verbose # ======================================================================= # # === @remove_plus_and_minus # ======================================================================= # @remove_plus_and_minus = REMOVE_PLUS_AND_MINUS # ======================================================================= # # === @replace_plus_with_long_name # ======================================================================= # @replace_plus_with_long_name = REPLACE_PLUS_WITH_LONG_NAME @remove_plus_token = REMOVE_PLUS_TOKEN if @remove_plus_and_minus # Overrule always in this case. @replace_plus_with_long_name = false end if @remove_plus_and_minus @remove_plus_token = false end end
#¶ ↑
return_array
¶ ↑
This method will return a composite, 3-member Array that will hold the short_name
, the real_short_name
, and the program_version.
#¶ ↑
# File lib/program_information/program_information.rb, line 139 def return_array [ short_name?, real_short_name?, program_version? ] end
#¶ ↑
return_name_and_version
¶ ↑
This method will return an array with two elements:
(1) The first element is the name of the program in question. (2) The second element is the version of the program in question.
The input can be rather tricky and complicated.
Consider the following examples:
'libunwind-1.0-rc1' 'htop-1.0.0' 'WebKit-r93670' 'xfce4-dev-tools-4.8.0' 'e_dbus-1.7.10.tar.gz' 'boost_1_54_0.tar.bz2' 'short-mime-1.0.0-rc1'
This can all be valid input.
Which are the key-assumptions that we can do here?
I think the best assumption we can do is to find the first NUMBER in the given string, and then group the rest of the data according to this NUMBER. This will not work in the case of something such as WebKit-r93, as there is a ‘r’ character before that number. So we will have to make a second assumption: if the character preceding the first found integer is NOT a ‘-’ character, we will instead split on that character instead. This will however lead to a problem for something like “xfce4-dev-tools-4.8.0”. So, in case there is a ‘-’ character immediately after the pos variable, we assume we have such a special case.
On Sep 2011, I came up with a far simpler idea. We simply have to find the most important ‘-’ and then split accordingly. As long as we can identify the most important ‘-’ correctly, so long will everything else “just work”. This also did not work out, so I switched to a regex instead. Since then it has worked very fine.
Take input such as:
"font-bitstream-75dpi-1.0.0"
Note though that this approach does not work with input like:
"CrownCutlass-Alpha1.4"
unless we assume that alpha is a part of the version, which is possibly correct.
#¶ ↑
# File lib/program_information/program_information.rb, line 243 def return_name_and_version(i = nil) if i determine_the_three_main_variables(i) end # ======================================================================= # # We must consider what we wish to do about the '_' characters. We could # remove them, but certain programs such as e_dbus may require it as # part of their name. Also, Version entries such as '1_2_3' will become # '123' rather than '1.2.3'. Thus, on Jan 2014 I have disabled the # following line. # ======================================================================= # # i.delete!('_') if i.include? '_' return [ @short_name, @program_version ] end
#¶ ↑
set_input
¶ ↑
This method will, after performing some sanitize-operations, assign to the @input variable.
#¶ ↑
# File lib/program_information/program_information.rb, line 454 def set_input(i) if i.is_a? Array menu(i) i = i.join(' ').strip end i = i.to_s.dup if i.frozen? i = File.basename(i) # ======================================================================= # # We .squeeze away '--' tokens. # ======================================================================= # i.squeeze!('-') i.strip! # ======================================================================= # # Next, some simple replacements without any method. # ======================================================================= # i.sub!(/-paco/,'') if i.include? '-paco' i.sub!(/\/Programs\//,'') if i.include? '/Programs' # ======================================================================= # # The following must not conflict with i.e. "pkgconfig", hence why we # also include a '/' character. # ======================================================================= # i.gsub!(/\/pkg/,'') if i.include? '/pkg' # ======================================================================= # # i.gsub!(/\//,'-') # Controversial. Disabled for now as of Jan 2014. # ======================================================================= # # We don't want either { or }. # ======================================================================= # i.delete!('{') if i.include? '{' i.delete!('}') if i.include? '}' # ======================================================================= # # As of July 2016, we get rid of all tokens that follow a '#' character. # We also will get rid of the '#' as well. Must come before we call # remove_archive_type(). # ======================================================================= # if i.include? '#' i = i[0...i.index('#')] end i = remove_archive_type(i) i = remove_source_like_strings(i) # ======================================================================= # # Next, we correct for input such as "nana%201.5.4". This one has no # '-' token and no '_' token, but a '%' token. This '%' token will be # replaced. # ======================================================================= # if i.include? '%' unless i.include?('_') or i.include?('-') i.tr!('%','-') end end # ======================================================================= # # Leading '-' will be removed. # ======================================================================= # i[0,1] = '' if i.start_with? '-' # ======================================================================= # # === Handle downcasing of the given input # # This will consider downcasing the input. The reason as to why this is # necessary is so that input such as "Libgnome" can become "libgnome". # ======================================================================= # i.downcase! if @shall_we_downcase # ======================================================================= # # === Handle '+' tokens # ======================================================================= # if REMOVE_PLUS_TOKEN i.delete!('+') if i.include? '+' end i = return_proper_pipe_token_to_this_input(i) @input = i end
#¶ ↑
set_program_version
¶ ↑
Use this method whenever you wish to assign to the @program_version variable.
The input to this method may include stuff such as “.tar.xz”, which should be chopped off, as it is not part of the program version.
Keep in mind that we may also return nil.
Until March 2015 we also replaced all ‘_’ with ‘.’ characters, but input such as “ncbi_cxx–12_0_0.tar.gz” would break as a consequence, so this was disabled again.
#¶ ↑
# File lib/program_information/program_information.rb, line 90 def set_program_version(i) i = remove_file_suffix(i) unless i.nil? # Disallow archive-types here. # ======================================================================= # # The following check is not good at all because we may have input # such as "1-0.112.0". Polkitqt has this, for instance. # Thus, the following line was disabled on Feb 2016. # ======================================================================= # # if i.include? '-' # In this case, we use only the last part. # i = i.split('-').last # end # ======================================================================= # # And if result is actually an empty String, but the original input # has included at the least one number, such as in "3.3.6-pl1", # then we will simply use that instead. # ======================================================================= # if i.is_a?(String) and i.empty? and @input =~ /\d+/ # <- If the input has at the least one Number. i = @input.dup end @program_version = i end
#¶ ↑
set_real_short_name
¶ ↑
The instance variable @real_short_name is not allowed to include any ‘-’ characters. It may also not include any ‘_’ characters.
The latter could be found as part of program names such as “SDL_image”. The @short_name variant may include these ‘_’ tokens.
Note that as of September 2017, we also disallow ‘.’ tokens, such as may occur accidentally in input like “sendmail.5.8.2”.
#¶ ↑
# File lib/program_information/program_information.rb, line 333 def set_real_short_name(i = @short_name) i = i.to_s.dup.delete('-_.').rstrip @real_short_name = i end
#¶ ↑
set_short_name
¶ ↑
Use this method whenever you wish to assign to the @short_name variable.
This variable is allowed to keep ‘-’ and ‘_’ entries, but some settings may also modify this.
#¶ ↑
# File lib/program_information/program_information.rb, line 346 def set_short_name(i) i = i.dup # ======================================================================= # # First, consider getting rid of '+' and '-' tokens in the # @short_name variant of the program name. # For this to work, @remove_plus_and_minus has to be set to true. # ======================================================================= # if remove_plus_and_minus? i.delete!('+') if i.include? '+' i.delete!('-') if i.include? '-' end # ======================================================================= # # === Get rid of '+' tokens, if they are included. # ======================================================================= # if remove_plus_token? i.delete!('+') if i.include? '+' # ======================================================================= # # === Alternatively, replace '+' with the string 'plus' should it # exist there. # ======================================================================= # elsif replace_plus_with_long_name? i.gsub!(/\+/, 'plus') if i.include? '+' end @short_name = i set_real_short_name(i) # We will also sync towards @real_short_name. end
#¶ ↑
set_short_name_then_version
¶ ↑
This method will combine two other methods.
# ¶ ↑
# File lib/program_information/program_information.rb, line 179 def set_short_name_then_version(a, b) set_short_name(a) set_program_version(b) end
#¶ ↑
short_name
?¶ ↑
Reader method for the @short_name instance variable.
short_name
is an alias to @short_name.
#¶ ↑
# File lib/program_information/program_information.rb, line 422 def short_name? @short_name end
#¶ ↑
short_name_and_version
?¶ ↑
This method returns an aggregated @short_name+‘-’+@program_version string.
#¶ ↑
# File lib/program_information/program_information.rb, line 170 def short_name_and_version? "#{short_name?}-#{program_version?}" end
#¶ ↑
show_help
(help tag)¶ ↑
To invoke this method, try:
pinfo --show-help
#¶ ↑
# File lib/program_information/program_information.rb, line 532 def show_help e e 'This class is able to split a given input (a string) into' e 'the inferred program-name, and associated program version '\ 'of said string.' e e 'For example, if you have this input string:' e e ' gobject-introspection-1.52.1' e e 'then this class can split this into these constituents:' e e ' @real_short_name: gobjectintrospection' e ' @short_name: gobject-introspection' e ' @program_version: 1.52.1' e e 'This is especially useful for other projects that require this' e 'functionality, such as the rbt gem.' e end