ip_in_range 1.05 → 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: 353a2d86f7e263a1905e030d0c5e8006c1ee4d5bcbd140a23f74c3e8d02acecb
4
- data.tar.gz: 1b4ef7718d85e4536515e763e7d858adaa4d083945dfe3df2b65a61729f0705d
3
+ metadata.gz: 14fcdba5f28330e02f702f945be58cc57d16fa9aca8f87bb036b3668e083eec0
4
+ data.tar.gz: 6426fb83a768e16cc97848efae637fa14f60e49e7120b63f9ff86ca3ee924853
5
5
  SHA512:
6
- metadata.gz: 21c1e2a1395bd9df2b520beda3a1420666ef7a3d612a887258221262da80b9e0c139f85c80a0e6849909709ca53e1649eaf99c432c7c60d16acf07d260ffc833
7
- data.tar.gz: 4c30b698d81a4dc459d7366f3c46ef33a528fe74163e8baa0ca3f662eb479b48315f76da9f39f9e5d72a185624e9708f45deadd2bb5c1898befd0f194eb78034
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,12 +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
17
  s.requirements = 'mail'
18
18
  s.add_runtime_dependency 'mail', '~> 2.8', '>= 2.8.1'
19
-
19
+ s.metadata = {
20
+ "homepage_uri" => 'https://www.uplawski.eu/software',
21
+ }
20
22
  end
data/lib/basic_logging.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  #encoding: UTF-8
3
3
  =begin
4
4
  /***************************************************************************
5
- * 2023-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
+ * 2023-2026, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
6
  * This program is free software; you can redistribute it and/or modify *
7
7
  * it under the terms of the WTFPL 2.0 or later, see *
8
8
  * http://www.wtfpl.net/about/ *
@@ -57,7 +57,7 @@ module BasicLogging
57
57
  if(!lv || (lv.respond_to?(:to_int) && lv >= DEBUG && lv <= FATAL) )
58
58
  @@log_level = lv
59
59
  else
60
- msg = __FILE__.dup << ": ERROR : invalid log level \"" << lv.to_s << "\""
60
+ msg = calling_method << ": ERROR : invalid log level \"" << lv.to_s << "\""
61
61
  msg << "\n" << "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s
62
62
  STDERR.puts msg
63
63
  puts msg
@@ -73,14 +73,14 @@ module BasicLogging
73
73
  elsif !tg || tg.respond_to?(:to_str) && tg.strip.empty?
74
74
  @@target = nil
75
75
  else
76
- STDERR.puts __FILE__.dup << ': ERROR : target ' << tg << ' cannot be set'
76
+ STDERR.puts calling_method << ': ERROR : target ' << tg.to_str << ' cannot be set'
77
77
  STDERR.puts "Keeping old target " << @@target.inspect
78
78
  return
79
79
  end
80
80
  end
81
81
 
82
- # Output of log messages, depending on the log level set for the calling class
83
- # 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.
84
84
  def log(message)
85
85
  if !BasicLogging.is_muted?(self)
86
86
  # how has this method been called?
@@ -90,7 +90,7 @@ module BasicLogging
90
90
  # the calling alias.
91
91
  format_log( message, mlevel) if @@log_level && Levels[mlevel] >= @@log_level
92
92
  else
93
- STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
93
+ STDERR.puts calling_method << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
94
94
  end
95
95
  end
96
96
  end
@@ -100,7 +100,7 @@ module BasicLogging
100
100
  end
101
101
 
102
102
  def level
103
- @@log_level.to_s if @@log_level
103
+ @@level.to_s if @@level
104
104
  end
105
105
 
106
106
  # Clear the log (-file)
@@ -110,15 +110,54 @@ module BasicLogging
110
110
  end
111
111
  end
112
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
+
113
128
  alias :debug :log
114
129
  alias :info :log
115
130
  alias :warn :log
116
131
  alias :error :log
117
132
  alias :fatal :log
118
133
 
119
-
120
134
  private
121
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
122
161
  def lock_target(&block)
123
162
  begin
124
163
  if @@target.respond_to?(:flock)
@@ -129,20 +168,27 @@ module BasicLogging
129
168
  block.call
130
169
  end
131
170
  rescue => ex
132
- STDERR.puts __FILE__.dup << ": ERROR : cannot lock target (" << ex.message << ")"
171
+ STDERR.puts calling_method << ": ERROR : cannot lock target (" << ex.message << ")"
133
172
  end
134
173
  end
135
174
 
136
175
  # 1 format_log for all loggers.
137
176
  def format_log(message, mlevel)
138
177
  if @@target
139
- clname = self.class.name
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
140
184
  # indicate if a registered class or the registered object of a class is calling.
141
- name = self.class == Class ? self.name.dup << ' [class]' : (clname != 'Object' ? clname : 'Top-Level')
142
- lock_target{@@target.puts '' << name << ' ' << mlevel.to_s << ' ' << Time.now.strftime("%H:%M:%S:%6N") << ': ' << message.gsub("\n", "\n |")}
185
+ lock_target{@@target.puts '' << location << mlevel.to_s << ' ' << Time.now.strftime("%H:%M:%S:%6N") << ': ' << message.gsub("\n", "\n |")}
143
186
  end
144
187
  end
145
- end
188
+ end # Module end
189
+
190
+
191
+
146
192
  #---------test: execute file----------
147
193
  if $0 == __FILE__
148
194
  Array.extend(BasicLogging)
@@ -188,14 +234,14 @@ if $0 == __FILE__
188
234
  obj.warn('warn')
189
235
  obj.error('error')
190
236
  obj.fatal('fatal')
191
- puts " ------ Output into file ----"
237
+ # ---- into file ---
192
238
  obj.set_target "/tmp/test_log.log"
193
- puts " ------ INFO -----------"
239
+ puts "------ INFO into #{obj.target}-----------"
194
240
  obj.set_level BasicLogging::INFO
195
241
  obj.info('info output')
196
242
 
197
243
  obj.info('info output 2')
198
- puts "---------- invalid -------"
244
+ puts "---------- invalid (fails) -------"
199
245
  obj.set_target "/dev/sr0"
200
246
  obj.set_level "power"
201
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.05
17
- SUMMARY="list of dependencies corrected in the gemfile"
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,70 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip_in_range
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.05'
4
+ version: "1.6"
5
5
  platform: ruby
6
6
  authors:
7
- - Michael Uplawski@uplawski.eu
7
+ - Michael Uplawski@uplawski.eu
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-19 00:00:00.000000000 Z
10
+ date: 2026-08-15 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: mail
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '2.8'
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: 2.8.1
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- requirements:
26
- - - "~>"
27
- - !ruby/object:Gem::Version
28
- version: '2.8'
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- version: 2.8.1
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
32
36
  description: Verify that an IP is in a range
33
37
  email: "<michael.uplawski@uplawski.eu>"
34
38
  executables:
35
- - ip_in_range
39
+ - ip_in_range
36
40
  extensions: []
37
41
  extra_rdoc_files: []
38
42
  files:
39
- - README.md
40
- - bin/ip_in_range
41
- - doc/license.txt
42
- - ip_in_range.gemspec
43
- - lib/basic_logging.rb
44
- - lib/email.rb
45
- - lib/file_checking.rb
46
- - lib/ip_range.rb
47
- - lib/version.rb
48
- 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: ""
49
53
  licenses:
50
- - Nonstandard
51
- metadata: {}
54
+ - WTFPL
55
+ metadata:
56
+ homepage_uri: "https://www.uplawski.eu/software"
52
57
  rdoc_options: []
53
58
  require_paths:
54
- - lib
59
+ - lib
55
60
  required_ruby_version: !ruby/object:Gem::Requirement
56
61
  requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: '3.0'
62
+ -
63
+ - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "4.0"
60
66
  required_rubygems_version: !ruby/object:Gem::Requirement
61
67
  requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- version: '0'
68
+ -
69
+ - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
65
72
  requirements:
66
- - mail
67
- rubygems_version: 3.6.3
73
+ - mail
74
+ rubygems_version: 4.1.0.dev
68
75
  specification_version: 4
69
- summary: list of dependencies corrected in the gemfile
76
+ summary: Verify that an IPv4 address is in a range
70
77
  test_files: []
data/lib/file_checking.rb DELETED
@@ -1,112 +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
- =begin
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
- =end
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
- puts msg if msg
111
- =end
112
- # E O F