ip_in_range 1.02 → 1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec5922add07e46af01522764899f20e12bf53d593fbf0f75285cb56f69446cde
4
- data.tar.gz: 0adcc4c8363a5f7204dc76821abb6c9d3fe9fa99385080f2c94f24d5406ea621
3
+ metadata.gz: 14fcdba5f28330e02f702f945be58cc57d16fa9aca8f87bb036b3668e083eec0
4
+ data.tar.gz: 6426fb83a768e16cc97848efae637fa14f60e49e7120b63f9ff86ca3ee924853
5
5
  SHA512:
6
- metadata.gz: 6d74db379a62121a15dc2b492829aec897d2f6e57095eb33009fee2719523bc16eaa383be8828912a681769857476ed67494d485cac9ee27bab3eb8296c7788a
7
- data.tar.gz: f65083f6148ee1c6bed6cec741bcb90c2fd163e07cbcd300174cd0052b15e58292b4beb98b126afbab666256749878a516c14e8ce81c8f08670174bc86137326
6
+ metadata.gz: 2c2d55f1c00f4f791b9dd51828e5d9119e3cf92e03c36169f5a0529d8c0e613b63a156bc5c10e4c79395f61aeacc5d493e5d8a70a2e3e0d128a3a3466ff5e5c8
7
+ data.tar.gz: 8165148076f882ce6405fff236cce3aa77613bc9e147bf8f68138f174dcfe1cd2ad21baa980c8ddcdc26679962b973880a50489ac351b2319256e9c03443f860
data/bin/ip_in_range CHANGED
@@ -15,10 +15,10 @@
15
15
  ***************************************************************************/
16
16
  =end
17
17
 
18
+ require_relative '../lib/color_output'
18
19
  require_relative '../lib/ip_range'
19
20
  require_relative '../lib/email'
20
21
  require_relative '../lib/basic_logging'
21
- require_relative '../lib/file_checking'
22
22
 
23
23
  # log on class level
24
24
  extend BasicLogging
@@ -48,7 +48,7 @@ elsif ARGV.length == 2
48
48
  if(!STDIN.tty?)
49
49
  mail = FMail.new(ARGF.read)
50
50
  else
51
- error('No data to compare from STDIN – aborting!')
51
+ error red('No data to compare from STDIN – aborting!')
52
52
  exit false
53
53
  end
54
54
  ips = mail.received.collect{|r| r.scan(IPR)}
@@ -67,12 +67,17 @@ elsif ARGV.length == 1
67
67
  if(!STDIN.tty?)
68
68
  mail = FMail.new(ARGF.read)
69
69
  else
70
- error('No data waiting on STDIN – aborting!')
70
+ error red('No data waiting on STDIN – aborting!')
71
71
  exit false
72
72
  end
73
- msg = File_Checking::file_check(list_file, [:file, :exist, :readable])
74
- if msg
75
- error( "File #{list_file} is unuseable: #{msg}. Aborting!" )
73
+ msg = ''
74
+ [:file?, :exist?, :readable?].each do |cond|
75
+ if !File.send(cond, list_file)
76
+ msg << "\n\tnot " << cond.to_s.split('?')[0]
77
+ end
78
+ end
79
+ if ! msg.strip.empty?
80
+ error red( "File #{list_file} is unuseable: #{msg}. Aborting!" )
76
81
  exit false
77
82
  end
78
83
  ranges = File.readlines(list_file)
@@ -82,13 +87,13 @@ elsif ARGV.length == 1
82
87
  begin
83
88
  debug 'trying to extract an IP-range'
84
89
  range = l.scan(IPR).flatten
85
- debug('testing range ' << range.to_s)
90
+ debug('have extracted range ' << range.to_s)
86
91
  rescue ArgumentError => msg
87
- er = 'Cannot interpret IP-range ' << l
92
+ er = 'Cannot interpret IP-range in ' << l
88
93
  er << ': ' << msg
89
- error(er)
94
+ error red(er)
90
95
  rescue Exception => msg
91
- error(msg)
96
+ error red(msg)
92
97
  else
93
98
  debug( "range is " << range.to_s )
94
99
  ip_range = IPRange.new(range)
@@ -100,7 +105,7 @@ elsif ARGV.length == 1
100
105
  isso = ip_range.in_range?(ip)
101
106
  msg = 'ip ' << ip << (isso ? ' is ' : ' is not ' ) << 'in range: ' << l
102
107
  if isso
103
- info("\nidentified IP: " + msg)
108
+ info cyan("\nidentified IP: " + msg)
104
109
  else
105
110
  debug(msg)
106
111
  end
@@ -111,10 +116,10 @@ elsif ARGV.length == 1
111
116
  end
112
117
  exit false
113
118
  else
114
- puts usage
119
+ puts green(usage)
115
120
  end
116
121
  else
117
- warn( "ERROR! Arguments missing" )
122
+ warn yellow("ERROR! Arguments missing" )
118
123
  info( usage)
119
124
  exit false
120
125
  end
data/ip_in_range.gemspec CHANGED
@@ -9,9 +9,14 @@ Gem::Specification.new do |s|
9
9
  s.description = "Verify that an IP is in a range"
10
10
  s.authors = AUTHORS.join ','
11
11
  s.email = AUTHORS_MAIL.join ','
12
- s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~ip_range.rb version.rb file_checking.rb basic_logging.rb email.rb~.collect{|f| 'lib/' << f} + ['doc/license.txt','ip_in_range.gemspec'] + ["README.md"]
12
+ s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~ip_range.rb color_output.rb version.rb basic_logging.rb email.rb~.collect{|f| 'lib/' << f} + ['doc/license.txt','ip_in_range.gemspec'] + ["README.md"]
13
13
  s.homepage = ''
14
14
  s.executables = 'ip_in_range'
15
- s.license = 'Nonstandard'
16
- s.required_ruby_version = '>= 3.0'
15
+ s.license = 'WTFPL'
16
+ s.required_ruby_version = '>= 4.0'
17
+ s.requirements = 'mail'
18
+ s.add_runtime_dependency 'mail', '~> 2.8', '>= 2.8.1'
19
+ s.metadata = {
20
+ "homepage_uri" => 'https://www.uplawski.eu/software',
21
+ }
17
22
  end
data/lib/basic_logging.rb CHANGED
@@ -2,8 +2,7 @@
2
2
  #encoding: UTF-8
3
3
  =begin
4
4
  /***************************************************************************
5
- * ©2013-2023, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
- * *
5
+ * 2023-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
7
6
  * This program is free software; you can redistribute it and/or modify *
8
7
  * it under the terms of the WTFPL 2.0 or later, see *
9
8
  * http://www.wtfpl.net/about/ *
@@ -19,10 +18,6 @@
19
18
  # Simplified logging.
20
19
  # See example code at the bottom of this file.
21
20
  # Execute this file to see the output.
22
- #
23
-
24
- #require 'time'
25
-
26
21
  module BasicLogging
27
22
 
28
23
  DEBUG = 0
@@ -53,14 +48,19 @@ module BasicLogging
53
48
 
54
49
  # set the log level
55
50
  def set_level(lv)
56
- if lv.respond_to?(:to_str)
51
+ if lv.respond_to?(:to_str) && Levels.keys.include?(lv.strip.to_sym)
57
52
  lv = Levels[lv.to_sym]
53
+ elsif lv.respond_to?(:to_sym) && Levels.keys.include?(lv)
54
+ lv = Levels[lv]
58
55
  end
56
+
59
57
  if(!lv || (lv.respond_to?(:to_int) && lv >= DEBUG && lv <= FATAL) )
60
58
  @@log_level = lv
61
59
  else
62
- STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << lv.to_s << "\""
63
- STDERR.puts "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s
60
+ msg = calling_method << ": ERROR : invalid log level \"" << lv.to_s << "\""
61
+ msg << "\n" << "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s
62
+ STDERR.puts msg
63
+ puts msg
64
64
  end
65
65
  end
66
66
 
@@ -70,15 +70,17 @@ module BasicLogging
70
70
  @@target = tg
71
71
  elsif(!File::exist?(tg) || ( File.file?(tg) && File.writable?(tg) ) )
72
72
  @@target = File.open(tg, 'w+')
73
+ elsif !tg || tg.respond_to?(:to_str) && tg.strip.empty?
74
+ @@target = nil
73
75
  else
74
- STDERR.puts __FILE__.dup << ': ERROR : target ' << tg << ' cannot be set'
76
+ STDERR.puts calling_method << ': ERROR : target ' << tg.to_str << ' cannot be set'
75
77
  STDERR.puts "Keeping old target " << @@target.inspect
76
78
  return
77
79
  end
78
80
  end
79
81
 
80
- # Output of log messages, depending on the log level set for the calling class
81
- # and the name of the alias method which is actually called.
82
+ # Output of log messages, depending on the set log level and the name of the
83
+ # alias method which is actually called.
82
84
  def log(message)
83
85
  if !BasicLogging.is_muted?(self)
84
86
  # how has this method been called?
@@ -88,28 +90,105 @@ module BasicLogging
88
90
  # the calling alias.
89
91
  format_log( message, mlevel) if @@log_level && Levels[mlevel] >= @@log_level
90
92
  else
91
- STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
93
+ STDERR.puts calling_method << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
92
94
  end
93
95
  end
94
96
  end
95
97
 
98
+ def target
99
+ @@target.path if @@target
100
+ end
101
+
102
+ def level
103
+ @@level.to_s if @@level
104
+ end
105
+
106
+ # Clear the log (-file)
107
+ def clear_log
108
+ if @@target && @@target.respond_to?(:truncate)
109
+ lock_target{ @@target.truncate(0) }
110
+ end
111
+ end
112
+
113
+ # find the name of the calling object's class or the file name in case of the
114
+ # top level object... or any dumb instance of Object
115
+ def log_name()
116
+ if !@log_name
117
+ clname = self.class.name
118
+ # limit analysis of the call stack to 'Object'.
119
+ if clname == 'Object'
120
+ @log_name = File::basename(caller.last.split(':')[0])
121
+ else
122
+ @log_name = self.class == Class ? self.name.dup << ' [class]' : clname
123
+ end
124
+ end
125
+ @log_name
126
+ end
127
+
96
128
  alias :debug :log
97
129
  alias :info :log
98
130
  alias :warn :log
99
131
  alias :error :log
100
132
  alias :fatal :log
101
133
 
102
- attr_reader :target, :log_level
103
-
104
134
  private
105
135
 
136
+ # Find the method that called, or 'main' in case of a top level Object.
137
+ # This needs a bunch of memory and slows down the process.
138
+ # Use of this method should be limited.
139
+ def calling_method()
140
+ # find instances other than the current module.
141
+ stack_item = caller.detect do |item|
142
+ item.split(':')[0] != __FILE__
143
+ end
144
+ if stack_item
145
+ if stack_item.include?('#')
146
+ m = stack_item.split(':').last.split("#")[1]
147
+ m.gsub!("'", '') if m && m.include?("'")
148
+ # looks cooler with a class name in front.
149
+ return '#' << m << '()'
150
+ else
151
+ # top level Object
152
+ return stack_item.split(':').last
153
+ end
154
+ else
155
+ # This is dumb but won't hurt a lot.
156
+ return 'from main'
157
+ end
158
+ end
159
+
160
+ # could be useful
161
+ def lock_target(&block)
162
+ begin
163
+ if @@target.respond_to?(:flock)
164
+ @@target.flock(File::LOCK_EX)
165
+ block.call
166
+ @@target.flock(File::LOCK_UN)
167
+ elsif @@target.respond_to?(:to_io)
168
+ block.call
169
+ end
170
+ rescue => ex
171
+ STDERR.puts calling_method << ": ERROR : cannot lock target (" << ex.message << ")"
172
+ end
173
+ end
174
+
106
175
  # 1 format_log for all loggers.
107
176
  def format_log(message, mlevel)
108
- # indicate if a registered class or the registered object of a class is calling.
109
- name = self.class == Class ? self.name.dup << ' [class]' : self.class.name
110
- @@target.puts '' << name << ' ' << mlevel.to_s << ' ' << Time.now.strftime("%H:%M:%S:%6N") << ': ' << message
177
+ if @@target
178
+ location = log_name().dup << ' '
179
+ # Limit the memory-hungry analysis of the call stack
180
+ # This also takes about 2.5 times as long.
181
+ if mlevel != :info
182
+ location = log_name().dup << ' ' << calling_method << ' '
183
+ end
184
+ # indicate if a registered class or the registered object of a class is calling.
185
+ lock_target{@@target.puts '' << location << mlevel.to_s << ' ' << Time.now.strftime("%H:%M:%S:%6N") << ': ' << message.gsub("\n", "\n |")}
186
+ end
111
187
  end
112
- end
188
+ end # Module end
189
+
190
+
191
+
113
192
  #---------test: execute file----------
114
193
  if $0 == __FILE__
115
194
  Array.extend(BasicLogging)
@@ -155,14 +234,14 @@ if $0 == __FILE__
155
234
  obj.warn('warn')
156
235
  obj.error('error')
157
236
  obj.fatal('fatal')
158
- puts " ------ Output into file ----"
237
+ # ---- into file ---
159
238
  obj.set_target "/tmp/test_log.log"
160
- puts " ------ INFO -----------"
239
+ puts "------ INFO into #{obj.target}-----------"
161
240
  obj.set_level BasicLogging::INFO
162
241
  obj.info('info output')
163
242
 
164
243
  obj.info('info output 2')
165
- puts "---------- invalid -------"
244
+ puts "---------- invalid (fails) -------"
166
245
  obj.set_target "/dev/sr0"
167
246
  obj.set_level "power"
168
247
  end
@@ -0,0 +1,65 @@
1
+ #encoding: UTF-8
2
+ =begin
3
+ /***************************************************************************
4
+ * 2023-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
+ * This program is free software; you can redistribute it and/or modify *
6
+ * it under the terms of the WTFPL 2.0 or later, see *
7
+ * http://www.wtfpl.net/about/ *
8
+ * *
9
+ * This program is distributed in the hope that it will be useful, *
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
12
+ * *
13
+ ***************************************************************************/
14
+ =end
15
+
16
+ # Functions to apply colors to terminal output.
17
+ # This is stolen from the Internet and I have lost track of my own additions
18
+ # and modifications.
19
+
20
+ COLORS = {:default => 9, :black => 0, :red => 1, :green => 2, :yellow => 3, :blue => 4, :purple => 5, :cyan => 6, :white => 7 }
21
+
22
+ BG = 4
23
+ FG = 3
24
+ REGULAR = 0
25
+ BOLD = 1
26
+ UNDERLINE = 4
27
+ BLINK = 5
28
+ SWAP = 7
29
+ NEUTRAL = 0
30
+
31
+ STYLES = {:regular => REGULAR, :bold => BOLD, :underline => UNDERLINE, :blink => BLINK, :swap => SWAP, :neutral => NEUTRAL}
32
+
33
+ # Colorizes the given text. Color-code is either an escape-sequence or one of
34
+ # the symbols representing color-names in the COLORS hash.
35
+ def colorize(text, color_code)
36
+ if (COLORS.keys.include?(color_code) )
37
+ "\033[3#{COLORS[color_code]}m#{text}\033[0m"
38
+ else
39
+ "#{color_code}#{text}\033[0m"
40
+ end
41
+ end
42
+
43
+ def style(text, style_code)
44
+ "#{style_code}#{text}\033[0m"
45
+ end
46
+ # a function which allows to manipulate every known aspect of the ansi-output.
47
+ def colored_output(output_text, fg_color = :default, bg_color = :default, style = :regular , mode = :neutral )
48
+ "\033[%i;%i;%i%i;%i%im%s\033[0m" %[STYLES[mode.to_sym], STYLES[style.to_sym], FG, COLORS[fg_color.to_sym], BG, COLORS[bg_color.to_sym], output_text]
49
+ end
50
+
51
+ # convenience functions
52
+ def red(text); colorize(text, "\033[31m"); end
53
+ def green(text); colorize(text, "\033[32m"); end
54
+ def yellow(text); colorize(text, "\033[33m"); end
55
+ def purple(text); colorize(text, "\033[35m"); end
56
+ def cyan(text); colorize(text, "\033[36m"); end
57
+ def blue(text); colorize(text, "\033[34m"); end
58
+ def white(text); colorize(text, "\033[37m"); end
59
+
60
+ def black_on_white(text); colorize(colorize(text, "\033[30m"), "\033[47m");end
61
+ def white_on_black(text); colorize(colorize(text, "\033[37m"), "\033[40m");end
62
+
63
+ def bold(text); style(text, "\033[01m");end
64
+ def underline(text); style(text, "\033[04m");end
65
+
data/lib/ip_range.rb CHANGED
@@ -16,7 +16,9 @@
16
16
  =end
17
17
 
18
18
 
19
+ require_relative '../lib/color_output'
19
20
  require_relative 'basic_logging'
21
+
20
22
  class IPRange
21
23
  include BasicLogging
22
24
 
@@ -29,7 +31,7 @@ class IPRange
29
31
  if nibbles.size == 4
30
32
  ( (nibbles[0] * 256 + nibbles[1] ) * 256 + nibbles[2] ) * 256 + nibbles[3]
31
33
  else
32
- error(ip.dup << " is not an IP-address!")
34
+ error red(ip.dup << " is not an IP-address!")
33
35
  return nil
34
36
  end
35
37
  end
@@ -54,8 +56,8 @@ class IPRange
54
56
  #last IP
55
57
  @last = ip_to_number args[1]
56
58
  if @last && @first && ( @last < @first )
57
- error('ERROR! Last IP is smaller than the first (' << @first.to_s << ' - ' << @last.to_s << ')')
58
- error('Aborting!')
59
+ error red('ERROR! Last IP is smaller than the first (' << @first.to_s << ' - ' << @last.to_s << ')')
60
+ error red('Aborting!')
59
61
  exit false
60
62
  end
61
63
  @vrange = args[0,2]
data/lib/version.rb CHANGED
@@ -13,9 +13,9 @@
13
13
  ***************************************************************************/
14
14
  =end
15
15
 
16
- VERSION = 1.02
17
- SUMMARY="better error message when last IP < first IP in a range"
16
+ VERSION = 1.6
17
+ SUMMARY="Verify that an IPv4 address is in a range"
18
18
  AUTHORS=["Michael Uplawski@uplawski.eu"]
19
19
  EMAIL="michael.uplawski@uplawski.eu"
20
20
  AUTHORS_MAIL=["<michael.uplawski@uplawski.eu>"]
21
- YEARS= "2021-2024"
21
+ YEARS= "2021-2026"
metadata CHANGED
@@ -1,52 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip_in_range
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.02'
4
+ version: "1.6"
5
5
  platform: ruby
6
6
  authors:
7
- - Michael Uplawski@uplawski.eu
8
- autorequire:
7
+ - Michael Uplawski@uplawski.eu
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-04-13 00:00:00.000000000 Z
12
- dependencies: []
10
+ date: 2026-08-15 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: mail
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: "2.8"
20
+ -
21
+ - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.8.1
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ -
29
+ - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: "2.8"
32
+ -
33
+ - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.8.1
13
36
  description: Verify that an IP is in a range
14
37
  email: "<michael.uplawski@uplawski.eu>"
15
38
  executables:
16
- - ip_in_range
39
+ - ip_in_range
17
40
  extensions: []
18
41
  extra_rdoc_files: []
19
42
  files:
20
- - README.md
21
- - bin/ip_in_range
22
- - doc/license.txt
23
- - ip_in_range.gemspec
24
- - lib/basic_logging.rb
25
- - lib/email.rb
26
- - lib/file_checking.rb
27
- - lib/ip_range.rb
28
- - lib/version.rb
29
- homepage: ''
43
+ - README.md
44
+ - bin/ip_in_range
45
+ - doc/license.txt
46
+ - ip_in_range.gemspec
47
+ - lib/basic_logging.rb
48
+ - lib/color_output.rb
49
+ - lib/email.rb
50
+ - lib/ip_range.rb
51
+ - lib/version.rb
52
+ homepage: ""
30
53
  licenses:
31
- - Nonstandard
32
- metadata: {}
33
- post_install_message:
54
+ - WTFPL
55
+ metadata:
56
+ homepage_uri: "https://www.uplawski.eu/software"
34
57
  rdoc_options: []
35
58
  require_paths:
36
- - lib
59
+ - lib
37
60
  required_ruby_version: !ruby/object:Gem::Requirement
38
61
  requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '3.0'
62
+ -
63
+ - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "4.0"
42
66
  required_rubygems_version: !ruby/object:Gem::Requirement
43
67
  requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: '0'
47
- requirements: []
48
- rubygems_version: 3.4.20
49
- signing_key:
68
+ -
69
+ - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ requirements:
73
+ - mail
74
+ rubygems_version: 4.1.0.dev
50
75
  specification_version: 4
51
- summary: better error message when last IP < first IP in a range
76
+ summary: Verify that an IPv4 address is in a range
52
77
  test_files: []
data/lib/file_checking.rb DELETED
@@ -1,113 +0,0 @@
1
- #encoding: UTF-8
2
- =begin
3
- /***************************************************************************
4
- * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
- * *
6
- * This program is free software; you can redistribute it and/or modify *
7
- * it under the terms of the WTFPL 2.0 or later, see *
8
- * http://www.wtfpl.net/about/ *
9
- * *
10
- * This program is distributed in the hope that it will be useful, *
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
13
- * *
14
- ***************************************************************************/
15
- =end
16
-
17
- require 'filemagic'
18
-
19
- =begin
20
- A module to facilitate frequently occuring checks on
21
- file-system objects
22
- =end
23
- module File_Checking
24
-
25
- @@text_messages = {
26
- :exist? => "does not exist!",
27
- :exist => "does not exist!",
28
- :readable? => "is not readable!",
29
- :readable => "is not readable!",
30
- :executable? => "is not executable!",
31
- :executable => "is not executable!",
32
- :writable? => "is not writable!",
33
- :writable => "is not writable!",
34
- :directory? => "is not a directory!",
35
- :directory => "is not a directory!",
36
- :file? => "is not a file!",
37
- :file => "is not a file!",
38
- # different thing
39
- :type => "is not of the sought file-type (not '%s', but '%s')!",
40
- :mime => "does not have the sought mime-type (not '%s', but '%s')!",
41
- }
42
-
43
- # Checks if the file with the name from the first
44
- # parameter has the properties, listed in the second.
45
- # The messages parameter is an array of one or several
46
- # of :exist?, :readable?, :writable?, :directory? or
47
- # their string-representations, respectively.
48
- # Returns nil in case of success, otherwise an
49
- # informative message, describing the first negative
50
- # test-result.
51
- def file_check(file, *messages)
52
- File_Checking.file_check(file, *messages)
53
- end
54
-
55
- # Checks if the file with the name from the first
56
- # parameter has the properties, listed in the second.
57
- # The messages parameter is an array of one or all
58
- # of :exist?, :readable?, :writable?, :directory? or
59
- # their string-representations, respectively.
60
- # Returns nil in case of success, otherwise an
61
- # informative message, describing the first negative
62
- # test-result.
63
- def self.file_check(file, *messages)
64
- msg = nil
65
- if(file && messages.respond_to?(:to_ary) && !messages.empty?)
66
- messages.each do |k|
67
- if( k != :mime && k != :type)
68
- if(! k.to_s.end_with?('?'))
69
- k = (k.to_s << '?').to_sym
70
- end
71
- @log.debug ('checking ' << k.to_s) if @log
72
- if(msg == nil && File.respond_to?(k) && ! File.send(k, file.to_s))
73
- msg = "#{file} #{@@text_messages[k.to_sym]}"
74
- end
75
- end
76
- end
77
- end
78
- msg
79
- end
80
-
81
- def self.mime_check(file, mime_type)
82
- fm = FileMagic.mime
83
- fd = fm.fd(File.new(file) ).split(';')[0]
84
- if fd != mime_type.strip
85
- return file.dup << ' ' << (@@text_messages[:mime] %[mime_type, fd])
86
- end
87
- return nil
88
- end
89
-
90
- def self.magic_check(file, magic)
91
- fm = FileMagic.fm
92
- fd = fm.fd(File.new(file) ).split(';')[0]
93
- if fd != magic.strip
94
- return file.dup << ' ' << (@@text_messages[:type] %[magic, fd])
95
- end
96
- return nil
97
- end
98
-
99
- end # module
100
-
101
- =begin
102
- # example
103
-
104
- include File_Checking
105
- msg = file_check('some_file.txt', [:exist?, :readable?, 'writable'])
106
- # same as
107
- # msg = file_check('some_file.txt', [:exist, :readable, 'writable?'])
108
-
109
- msg ||= magic_check('some_file.txt', [:type?], 'OpenDocument Text'
110
- msg ||= mime_check('some_file.txt', [:mime?], 'application/vnd.oasis.opendocument.text'
111
- puts msg if msg
112
- =end
113
- # E O F