ptools 1.2.6-universal-mingw32 → 1.2.7-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,142 +1,142 @@
1
- require 'rake'
2
- require 'rake/clean'
3
- require 'rake/testtask'
4
- require 'rbconfig'
5
- include RbConfig
6
-
7
- CLEAN.include("**/*.gem", "**/*.rbc", "**/*coverage*")
8
-
9
- desc 'Install the ptools package (non-gem)'
10
- task :install do
11
- sitelibdir = CONFIG["sitelibdir"]
12
- file = "lib/ptools.rb"
13
- FileUtils.cp(file, sitelibdir, :verbose => true)
14
- end
15
-
16
- namespace 'gem' do
17
- desc 'Create the ptools gem'
18
- task :create => [:clean] do
19
- Dir["*.gem"].each{ |f| File.delete(f) } # Clean first
20
- spec = eval(IO.read('ptools.gemspec'))
21
- if Gem::VERSION < "2.0.0"
22
- Gem::Builder.new(spec).build
23
- else
24
- require 'rubygems/package'
25
- Gem::Package.build(spec)
26
- end
27
- end
28
-
29
- desc 'Install the ptools gem'
30
- task :install => [:create] do
31
- file = Dir["*.gem"].first
32
- sh "gem install -l #{file}"
33
- end
34
- end
35
-
36
- Rake::TestTask.new do |t|
37
- task :test => :clean
38
- t.verbose = true
39
- t.warning = true
40
- end
41
-
42
- namespace 'test' do
43
- desc "Check test coverage using rcov"
44
- task :coverage => [:clean] do
45
- require 'rcov'
46
- rm_rf 'coverage'
47
- sh "rcov -Ilib test/test*.rb"
48
- end
49
-
50
- Rake::TestTask.new('binary') do |t|
51
- t.libs << 'test'
52
- t.verbose = true
53
- t.warning = true
54
- t.test_files = FileList['test/test_binary.rb']
55
- end
56
-
57
- Rake::TestTask.new('constants') do |t|
58
- t.libs << 'test'
59
- t.verbose = true
60
- t.warning = true
61
- t.test_files = FileList['test/test_constants.rb']
62
- end
63
-
64
- Rake::TestTask.new('head') do |t|
65
- t.libs << 'test'
66
- t.verbose = true
67
- t.warning = true
68
- t.test_files = FileList['test/test_head.rb']
69
- end
70
-
71
- Rake::TestTask.new('image') do |t|
72
- t.libs << 'test'
73
- t.verbose = true
74
- t.warning = true
75
- t.test_files = FileList['test/test_image.rb']
76
- end
77
-
78
- Rake::TestTask.new('middle') do |t|
79
- t.libs << 'test'
80
- t.verbose = true
81
- t.warning = true
82
- t.test_files = FileList['test/test_middle.rb']
83
- end
84
-
85
- Rake::TestTask.new('nlconvert') do |t|
86
- t.libs << 'test'
87
- t.verbose = true
88
- t.warning = true
89
- t.test_files = FileList['test/test_nlconvert.rb']
90
- end
91
-
92
- Rake::TestTask.new('null') do |t|
93
- t.libs << 'test'
94
- t.verbose = true
95
- t.warning = true
96
- t.test_files = FileList['test/test_null.rb']
97
- end
98
-
99
- Rake::TestTask.new('sparse') do |t|
100
- t.libs << 'test'
101
- t.verbose = true
102
- t.warning = true
103
- t.test_files = FileList['test/test_is_sparse.rb']
104
- end
105
-
106
- Rake::TestTask.new('tail') do |t|
107
- t.libs << 'test'
108
- t.verbose = true
109
- t.warning = true
110
- t.test_files = FileList['test/test_tail.rb']
111
- end
112
-
113
- Rake::TestTask.new('touch') do |t|
114
- t.libs << 'test'
115
- t.verbose = true
116
- t.warning = true
117
- t.test_files = FileList['test/test_touch.rb']
118
- end
119
-
120
- Rake::TestTask.new('wc') do |t|
121
- t.libs << 'test'
122
- t.verbose = true
123
- t.warning = true
124
- t.test_files = FileList['test/test_wc.rb']
125
- end
126
-
127
- Rake::TestTask.new('whereis') do |t|
128
- t.libs << 'test'
129
- t.verbose = true
130
- t.warning = true
131
- t.test_files = FileList['test/test_whereis.rb']
132
- end
133
-
134
- Rake::TestTask.new('which') do |t|
135
- t.libs << 'test'
136
- t.verbose = true
137
- t.warning = true
138
- t.test_files = FileList['test/test_which.rb']
139
- end
140
- end
141
-
142
- task :default => :test
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rbconfig'
5
+ include RbConfig
6
+
7
+ CLEAN.include("**/*.gem", "**/*.rbc", "**/*coverage*")
8
+
9
+ desc 'Install the ptools package (non-gem)'
10
+ task :install do
11
+ sitelibdir = CONFIG["sitelibdir"]
12
+ file = "lib/ptools.rb"
13
+ FileUtils.cp(file, sitelibdir, :verbose => true)
14
+ end
15
+
16
+ namespace 'gem' do
17
+ desc 'Create the ptools gem'
18
+ task :create => [:clean] do
19
+ Dir["*.gem"].each{ |f| File.delete(f) } # Clean first
20
+ spec = eval(IO.read('ptools.gemspec'))
21
+ if Gem::VERSION < "2.0.0"
22
+ Gem::Builder.new(spec).build
23
+ else
24
+ require 'rubygems/package'
25
+ Gem::Package.build(spec)
26
+ end
27
+ end
28
+
29
+ desc 'Install the ptools gem'
30
+ task :install => [:create] do
31
+ file = Dir["*.gem"].first
32
+ sh "gem install -l #{file}"
33
+ end
34
+ end
35
+
36
+ Rake::TestTask.new do |t|
37
+ task :test => :clean
38
+ t.verbose = true
39
+ t.warning = true
40
+ end
41
+
42
+ namespace 'test' do
43
+ desc "Check test coverage using rcov"
44
+ task :coverage => [:clean] do
45
+ require 'rcov'
46
+ rm_rf 'coverage'
47
+ sh "rcov -Ilib test/test*.rb"
48
+ end
49
+
50
+ Rake::TestTask.new('binary') do |t|
51
+ t.libs << 'test'
52
+ t.verbose = true
53
+ t.warning = true
54
+ t.test_files = FileList['test/test_binary.rb']
55
+ end
56
+
57
+ Rake::TestTask.new('constants') do |t|
58
+ t.libs << 'test'
59
+ t.verbose = true
60
+ t.warning = true
61
+ t.test_files = FileList['test/test_constants.rb']
62
+ end
63
+
64
+ Rake::TestTask.new('head') do |t|
65
+ t.libs << 'test'
66
+ t.verbose = true
67
+ t.warning = true
68
+ t.test_files = FileList['test/test_head.rb']
69
+ end
70
+
71
+ Rake::TestTask.new('image') do |t|
72
+ t.libs << 'test'
73
+ t.verbose = true
74
+ t.warning = true
75
+ t.test_files = FileList['test/test_image.rb']
76
+ end
77
+
78
+ Rake::TestTask.new('middle') do |t|
79
+ t.libs << 'test'
80
+ t.verbose = true
81
+ t.warning = true
82
+ t.test_files = FileList['test/test_middle.rb']
83
+ end
84
+
85
+ Rake::TestTask.new('nlconvert') do |t|
86
+ t.libs << 'test'
87
+ t.verbose = true
88
+ t.warning = true
89
+ t.test_files = FileList['test/test_nlconvert.rb']
90
+ end
91
+
92
+ Rake::TestTask.new('null') do |t|
93
+ t.libs << 'test'
94
+ t.verbose = true
95
+ t.warning = true
96
+ t.test_files = FileList['test/test_null.rb']
97
+ end
98
+
99
+ Rake::TestTask.new('sparse') do |t|
100
+ t.libs << 'test'
101
+ t.verbose = true
102
+ t.warning = true
103
+ t.test_files = FileList['test/test_is_sparse.rb']
104
+ end
105
+
106
+ Rake::TestTask.new('tail') do |t|
107
+ t.libs << 'test'
108
+ t.verbose = true
109
+ t.warning = true
110
+ t.test_files = FileList['test/test_tail.rb']
111
+ end
112
+
113
+ Rake::TestTask.new('touch') do |t|
114
+ t.libs << 'test'
115
+ t.verbose = true
116
+ t.warning = true
117
+ t.test_files = FileList['test/test_touch.rb']
118
+ end
119
+
120
+ Rake::TestTask.new('wc') do |t|
121
+ t.libs << 'test'
122
+ t.verbose = true
123
+ t.warning = true
124
+ t.test_files = FileList['test/test_wc.rb']
125
+ end
126
+
127
+ Rake::TestTask.new('whereis') do |t|
128
+ t.libs << 'test'
129
+ t.verbose = true
130
+ t.warning = true
131
+ t.test_files = FileList['test/test_whereis.rb']
132
+ end
133
+
134
+ Rake::TestTask.new('which') do |t|
135
+ t.libs << 'test'
136
+ t.verbose = true
137
+ t.warning = true
138
+ t.test_files = FileList['test/test_which.rb']
139
+ end
140
+ end
141
+
142
+ task :default => :test
@@ -1,431 +1,465 @@
1
- require 'rbconfig'
2
- require 'win32/file' if File::ALT_SEPARATOR
3
-
4
- class File
5
- # The version of the ptools library.
6
- PTOOLS_VERSION = '1.2.6'
7
-
8
- # :stopdoc:
9
-
10
- # The WIN32EXTS string is used as part of a Dir[] call in certain methods.
11
- if File::ALT_SEPARATOR
12
- MSWINDOWS = true
13
- if ENV['PATHEXT']
14
- WIN32EXTS = ('.{' + ENV['PATHEXT'].tr(';', ',').tr('.','') + '}').downcase
15
- else
16
- WIN32EXTS = '.{exe,com,bat}'
17
- end
18
- else
19
- MSWINDOWS = false
20
- end
21
-
22
- IMAGE_EXT = %w[.bmp .gif .jpg .jpeg .png]
23
-
24
- # :startdoc:
25
-
26
- # Returns whether or not the file is an image. Only JPEG, PNG, BMP and
27
- # GIF are checked against.
28
- #
29
- # This method does some simple read and extension checks. For a version
30
- # that is more robust, but which depends on a 3rd party C library (and is
31
- # difficult to build on MS Windows), see the 'filemagic' library.
32
- #
33
- # Examples:
34
- #
35
- # File.image?('somefile.jpg') # => true
36
- # File.image?('somefile.txt') # => false
37
- #--
38
- # The approach I used here is based on information found at
39
- # http://en.wikipedia.org/wiki/Magic_number_(programming)
40
- #
41
- def self.image?(file)
42
- bool = IMAGE_EXT.include?(File.extname(file).downcase) # Match ext
43
- bool = bmp?(file) || jpg?(file) || png?(file) || gif?(file) # Check data
44
- bool
45
- end
46
-
47
- # Returns the name of the null device (aka bitbucket) on your platform.
48
- #
49
- # Examples:
50
- #
51
- # # On Linux
52
- # File.null # => '/dev/null'
53
- #
54
- # # On MS Windows
55
- # File.null # => 'NUL'
56
- #--
57
- # The values I used here are based on information from
58
- # http://en.wikipedia.org/wiki//dev/null
59
- #
60
- def self.null
61
- case RbConfig::CONFIG['host_os']
62
- when /mswin|win32|msdos|cygwin|mingw|windows/i
63
- 'NUL'
64
- when /amiga/i
65
- 'NIL:'
66
- when /openvms/i
67
- 'NL:'
68
- else
69
- '/dev/null'
70
- end
71
- end
72
-
73
- class << self
74
- alias null_device null
75
- end
76
-
77
- # Returns whether or not +file+ is a binary non-image file, i.e. executable,
78
- # shared object, ect. Note that this is NOT guaranteed to be 100% accurate.
79
- # It performs a "best guess" based on a simple test of the first
80
- # +File.blksize+ characters.
81
- #
82
- # Example:
83
- #
84
- # File.binary?('somefile.exe') # => true
85
- # File.binary?('somefile.txt') # => false
86
- #--
87
- # Based on code originally provided by Ryan Davis (which, in turn, is
88
- # based on Perl's -B switch).
89
- #
90
- def self.binary?(file)
91
- return false if image?(file)
92
- s = (File.read(file, File.stat(file).blksize) || "")
93
- s = s.encode('US-ASCII', :undef => :replace).split(//)
94
- ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
95
- end
96
-
97
- # Looks for the first occurrence of +program+ within +path+.
98
- #
99
- # On Windows, it looks for executables ending with the suffixes defined
100
- # in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if
101
- # that isn't defined, which you may optionally include in +program+.
102
- #
103
- # Returns nil if not found.
104
- #
105
- # Examples:
106
- #
107
- # File.which('ruby') # => '/usr/local/bin/ruby'
108
- # File.which('foo') # => nil
109
- #
110
- def self.which(program, path=ENV['PATH'])
111
- if path.nil? || path.empty?
112
- raise ArgumentError, "path cannot be empty"
113
- end
114
-
115
- # Bail out early if an absolute path is provided.
116
- if program =~ /^\/|^[a-z]:[\\\/]/i
117
- program += WIN32EXTS if MSWINDOWS && File.extname(program).empty?
118
- found = Dir[program].first
119
- if found && File.executable?(found) && !File.directory?(found)
120
- return found
121
- else
122
- return nil
123
- end
124
- end
125
-
126
- # Iterate over each path glob the dir + program.
127
- path.split(File::PATH_SEPARATOR).each{ |dir|
128
- next unless File.exist?(dir) # In case of bogus second argument
129
- file = File.join(dir, program)
130
-
131
- # Dir[] doesn't handle backslashes properly, so convert them. Also, if
132
- # the program name doesn't have an extension, try them all.
133
- if MSWINDOWS
134
- file = file.tr("\\", "/")
135
- file += WIN32EXTS if File.extname(program).empty?
136
- end
137
-
138
- found = Dir[file].first
139
-
140
- # Convert all forward slashes to backslashes if supported
141
- if found && File.executable?(found) && !File.directory?(found)
142
- found.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR
143
- return found
144
- end
145
- }
146
-
147
- nil
148
- end
149
-
150
- # Returns an array of each +program+ within +path+, or nil if it cannot
151
- # be found.
152
- #
153
- # On Windows, it looks for executables ending with the suffixes defined
154
- # in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if
155
- # that isn't defined, which you may optionally include in +program+.
156
- #
157
- # Examples:
158
- #
159
- # File.whereis('ruby') # => ['/usr/bin/ruby', '/usr/local/bin/ruby']
160
- # File.whereis('foo') # => nil
161
- #
162
- def self.whereis(program, path=ENV['PATH'])
163
- if path.nil? || path.empty?
164
- raise ArgumentError, "path cannot be empty"
165
- end
166
-
167
- paths = []
168
-
169
- # Bail out early if an absolute path is provided.
170
- if program =~ /^\/|^[a-z]:[\\\/]/i
171
- program += WIN32EXTS if MSWINDOWS && File.extname(program).empty?
172
- program = program.tr("\\", '/') if MSWINDOWS
173
- found = Dir[program]
174
- if found[0] && File.executable?(found[0]) && !File.directory?(found[0])
175
- if File::ALT_SEPARATOR
176
- return found.map{ |f| f.tr('/', "\\") }
177
- else
178
- return found
179
- end
180
- else
181
- return nil
182
- end
183
- end
184
-
185
- # Iterate over each path glob the dir + program.
186
- path.split(File::PATH_SEPARATOR).each{ |dir|
187
- next unless File.exist?(dir) # In case of bogus second argument
188
- file = File.join(dir, program)
189
-
190
- # Dir[] doesn't handle backslashes properly, so convert them. Also, if
191
- # the program name doesn't have an extension, try them all.
192
- if MSWINDOWS
193
- file = file.tr("\\", "/")
194
- file += WIN32EXTS if File.extname(program).empty?
195
- end
196
-
197
- found = Dir[file].first
198
-
199
- # Convert all forward slashes to backslashes if supported
200
- if found && File.executable?(found) && !File.directory?(found)
201
- found.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR
202
- paths << found
203
- end
204
- }
205
-
206
- paths.empty? ? nil : paths.uniq
207
- end
208
-
209
- # In block form, yields the first +num_lines+ from +filename+. In non-block
210
- # form, returns an Array of +num_lines+
211
- #
212
- # Examples:
213
- #
214
- # # Return an array
215
- # File.head('somefile.txt') # => ['This is line1', 'This is line2', ...]
216
- #
217
- # # Use a block
218
- # File.head('somefile.txt'){ |line| puts line }
219
- #
220
- def self.head(filename, num_lines=10)
221
- a = []
222
-
223
- IO.foreach(filename){ |line|
224
- break if num_lines <= 0
225
- num_lines -= 1
226
- if block_given?
227
- yield line
228
- else
229
- a << line
230
- end
231
- }
232
-
233
- return a.empty? ? nil : a # Return nil in block form
234
- end
235
-
236
- # In block form, yields line +from+ up to line +to+. In non-block form
237
- # returns an Array of lines from +from+ to +to+.
238
- #
239
- def self.middle(filename, from=10, to=20)
240
- if block_given?
241
- IO.readlines(filename)[from-1..to-1].each{ |line| yield line }
242
- else
243
- IO.readlines(filename)[from-1..to-1]
244
- end
245
- end
246
-
247
- # In block form, yields the last +num_lines+ of file +filename+.
248
- # In non-block form, it returns the lines as an array.
249
- #
250
- # Note that this method slurps the entire file, so I don't recommend it
251
- # for very large files. Also note that 'tail -f' functionality is not
252
- # present. See the 'file-tail' library for that.
253
- #
254
- # Example:
255
- #
256
- # File.tail('somefile.txt') # => ['This is line7', 'This is line8', ...]
257
- #
258
- def self.tail(filename, num_lines=10)
259
- if block_given?
260
- IO.readlines(filename).reverse[0..num_lines-1].reverse.each{ |line|
261
- yield line
262
- }
263
- else
264
- IO.readlines(filename).reverse[0..num_lines-1].reverse
265
- end
266
- end
267
-
268
- # Converts a text file from one OS platform format to another, ala
269
- # 'dos2unix'. The possible values for +platform+ include:
270
- #
271
- # * MS Windows -> dos, windows, win32, mswin
272
- # * Unix/BSD -> unix, linux, bsd
273
- # * Mac -> mac, macintosh, apple, osx
274
- #
275
- # Note that this method is only valid for an ftype of "file". Otherwise a
276
- # TypeError will be raised. If an invalid format value is received, an
277
- # ArgumentError is raised.
278
- #
279
- def self.nl_convert(old_file, new_file = old_file, platform = 'dos')
280
- unless File::Stat.new(old_file).file?
281
- raise ArgumentError, 'Only valid for plain text files'
282
- end
283
-
284
- case platform
285
- when /dos|windows|win32|mswin|cygwin|mingw/i
286
- format = "\cM\cJ"
287
- when /unix|linux|bsd/i
288
- format = "\cJ"
289
- when /mac|apple|macintosh|osx/i
290
- format = "\cM"
291
- else
292
- raise ArgumentError, "Invalid platform string"
293
- end
294
-
295
- orig = $\ # $OUTPUT_RECORD_SEPARATOR
296
- $\ = format
297
-
298
- if old_file == new_file
299
- require 'fileutils'
300
- require 'tempfile'
301
-
302
- begin
303
- temp_name = Time.new.strftime("%Y%m%d%H%M%S")
304
- tf = Tempfile.new('ruby_temp_' + temp_name)
305
- tf.open
306
-
307
- IO.foreach(old_file){ |line|
308
- line.chomp!
309
- tf.print line
310
- }
311
- ensure
312
- tf.close if tf && !tf.closed?
313
- end
314
-
315
- File.delete(old_file)
316
- FileUtils.cp(tf.path, old_file)
317
- else
318
- begin
319
- nf = File.new(new_file, 'w')
320
- IO.foreach(old_file){ |line|
321
- line.chomp!
322
- nf.print line
323
- }
324
- ensure
325
- nf.close if nf && !nf.closed?
326
- end
327
- end
328
-
329
- $\ = orig
330
- self
331
- end
332
-
333
- # Changes the access and modification time if present, or creates a 0
334
- # byte file +filename+ if it doesn't already exist.
335
- #
336
- def self.touch(filename)
337
- if File.exist?(filename)
338
- time = Time.now
339
- File.utime(time, time, filename)
340
- else
341
- File.open(filename, 'w'){}
342
- end
343
- self
344
- end
345
-
346
- # With no arguments, returns a four element array consisting of the number
347
- # of bytes, characters, words and lines in filename, respectively.
348
- #
349
- # Valid options are 'bytes', 'characters' (or just 'chars'), 'words' and
350
- # 'lines'.
351
- #
352
- def self.wc(filename, option='all')
353
- option.downcase!
354
- valid = %w/all bytes characters chars lines words/
355
-
356
- unless valid.include?(option)
357
- raise ArgumentError, "Invalid option: '#{option}'"
358
- end
359
-
360
- n = 0
361
-
362
- if option == 'lines'
363
- IO.foreach(filename){ n += 1 }
364
- return n
365
- elsif option == 'bytes'
366
- File.open(filename){ |f|
367
- f.each_byte{ n += 1 }
368
- }
369
- return n
370
- elsif option == 'characters' || option == 'chars'
371
- File.open(filename){ |f|
372
- while f.getc
373
- n += 1
374
- end
375
- }
376
- return n
377
- elsif option == 'words'
378
- IO.foreach(filename){ |line|
379
- n += line.split.length
380
- }
381
- return n
382
- else
383
- bytes,chars,lines,words = 0,0,0,0
384
- IO.foreach(filename){ |line|
385
- lines += 1
386
- words += line.split.length
387
- chars += line.split('').length
388
- }
389
- File.open(filename){ |f|
390
- while f.getc
391
- bytes += 1
392
- end
393
- }
394
- return [bytes,chars,words,lines]
395
- end
396
- end
397
-
398
- # Already provided by win32-file on MS Windows
399
- unless respond_to?(:sparse?)
400
- # Returns whether or not +file+ is a sparse file.
401
- #
402
- # A sparse file is a any file where its size is greater than the number
403
- # of 512k blocks it consumes, i.e. its apparent and actual file size is
404
- # not the same.
405
- #
406
- # See http://en.wikipedia.org/wiki/Sparse_file for more information.
407
- #
408
- def self.sparse?(file)
409
- stats = File.stat(file)
410
- stats.size > stats.blocks * 512
411
- end
412
- end
413
-
414
- private
415
-
416
- def self.bmp?(file)
417
- IO.read(file, 3) == "BM6"
418
- end
419
-
420
- def self.jpg?(file)
421
- IO.read(file, 10, nil, :encoding => 'binary') == "\377\330\377\340\000\020JFIF".force_encoding(Encoding::BINARY)
422
- end
423
-
424
- def self.png?(file)
425
- IO.read(file, 4, nil, :encoding => 'binary') == "\211PNG".force_encoding(Encoding::BINARY)
426
- end
427
-
428
- def self.gif?(file)
429
- ['GIF89a', 'GIF97a'].include?(IO.read(file, 6))
430
- end
431
- end
1
+ require 'rbconfig'
2
+ require 'win32/file' if File::ALT_SEPARATOR
3
+
4
+ class File
5
+ # The version of the ptools library.
6
+ PTOOLS_VERSION = '1.2.7'
7
+
8
+ # :stopdoc:
9
+
10
+ # The WIN32EXTS string is used as part of a Dir[] call in certain methods.
11
+ if File::ALT_SEPARATOR
12
+ MSWINDOWS = true
13
+ if ENV['PATHEXT']
14
+ WIN32EXTS = ('.{' + ENV['PATHEXT'].tr(';', ',').tr('.','') + '}').downcase
15
+ else
16
+ WIN32EXTS = '.{exe,com,bat}'
17
+ end
18
+ else
19
+ MSWINDOWS = false
20
+ end
21
+
22
+ IMAGE_EXT = %w[.bmp .gif .jpg .jpeg .png]
23
+
24
+ # :startdoc:
25
+
26
+ # Returns whether or not the file is an image. Only JPEG, PNG, BMP and
27
+ # GIF are checked against.
28
+ #
29
+ # This method does some simple read and extension checks. For a version
30
+ # that is more robust, but which depends on a 3rd party C library (and is
31
+ # difficult to build on MS Windows), see the 'filemagic' library.
32
+ #
33
+ # Examples:
34
+ #
35
+ # File.image?('somefile.jpg') # => true
36
+ # File.image?('somefile.txt') # => false
37
+ #--
38
+ # The approach I used here is based on information found at
39
+ # http://en.wikipedia.org/wiki/Magic_number_(programming)
40
+ #
41
+ def self.image?(file)
42
+ bool = IMAGE_EXT.include?(File.extname(file).downcase) # Match ext
43
+ bool = bmp?(file) || jpg?(file) || png?(file) || gif?(file) || tiff?(file) # Check data
44
+ bool
45
+ end
46
+
47
+ # Returns the name of the null device (aka bitbucket) on your platform.
48
+ #
49
+ # Examples:
50
+ #
51
+ # # On Linux
52
+ # File.null # => '/dev/null'
53
+ #
54
+ # # On MS Windows
55
+ # File.null # => 'NUL'
56
+ #--
57
+ # The values I used here are based on information from
58
+ # http://en.wikipedia.org/wiki//dev/null
59
+ #
60
+ def self.null
61
+ case RbConfig::CONFIG['host_os']
62
+ when /mswin|win32|msdos|mingw|windows/i
63
+ 'NUL'
64
+ when /amiga/i
65
+ 'NIL:'
66
+ when /openvms/i
67
+ 'NL:'
68
+ else
69
+ '/dev/null'
70
+ end
71
+ end
72
+
73
+ class << self
74
+ alias null_device null
75
+ end
76
+
77
+ # Returns whether or not +file+ is a binary non-image file, i.e. executable,
78
+ # shared object, ect. Note that this is NOT guaranteed to be 100% accurate.
79
+ # It performs a "best guess" based on a simple test of the first
80
+ # +File.blksize+ characters, or 4096, whichever is smaller.
81
+ #
82
+ # Example:
83
+ #
84
+ # File.binary?('somefile.exe') # => true
85
+ # File.binary?('somefile.txt') # => false
86
+ #--
87
+ # Based on code originally provided by Ryan Davis (which, in turn, is
88
+ # based on Perl's -B switch).
89
+ #
90
+ def self.binary?(file)
91
+ return false if image?(file)
92
+ bytes = File.stat(file).blksize
93
+ bytes = 4096 if bytes > 4096
94
+ s = (File.read(file, bytes) || "")
95
+ s = s.encode('US-ASCII', :undef => :replace).split(//)
96
+ ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
97
+ end
98
+
99
+ # Looks for the first occurrence of +program+ within +path+.
100
+ #
101
+ # On Windows, it looks for executables ending with the suffixes defined
102
+ # in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if
103
+ # that isn't defined, which you may optionally include in +program+.
104
+ #
105
+ # Returns nil if not found.
106
+ #
107
+ # Examples:
108
+ #
109
+ # File.which('ruby') # => '/usr/local/bin/ruby'
110
+ # File.which('foo') # => nil
111
+ #
112
+ def self.which(program, path=ENV['PATH'])
113
+ if path.nil? || path.empty?
114
+ raise ArgumentError, "path cannot be empty"
115
+ end
116
+
117
+ # Bail out early if an absolute path is provided.
118
+ if program =~ /^\/|^[a-z]:[\\\/]/i
119
+ program += WIN32EXTS if MSWINDOWS && File.extname(program).empty?
120
+ found = Dir[program].first
121
+ if found && File.executable?(found) && !File.directory?(found)
122
+ return found
123
+ else
124
+ return nil
125
+ end
126
+ end
127
+
128
+ # Iterate over each path glob the dir + program.
129
+ path.split(File::PATH_SEPARATOR).each{ |dir|
130
+ dir = File.expand_path(dir)
131
+
132
+ next unless File.exist?(dir) # In case of bogus second argument
133
+ file = File.join(dir, program)
134
+
135
+ # Dir[] doesn't handle backslashes properly, so convert them. Also, if
136
+ # the program name doesn't have an extension, try them all.
137
+ if MSWINDOWS
138
+ file = file.tr("\\", "/")
139
+ file += WIN32EXTS if File.extname(program).empty?
140
+ end
141
+
142
+ found = Dir[file].first
143
+
144
+ # Convert all forward slashes to backslashes if supported
145
+ if found && File.executable?(found) && !File.directory?(found)
146
+ found.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR
147
+ return found
148
+ end
149
+ }
150
+
151
+ nil
152
+ end
153
+
154
+ # Returns an array of each +program+ within +path+, or nil if it cannot
155
+ # be found.
156
+ #
157
+ # On Windows, it looks for executables ending with the suffixes defined
158
+ # in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if
159
+ # that isn't defined, which you may optionally include in +program+.
160
+ #
161
+ # Examples:
162
+ #
163
+ # File.whereis('ruby') # => ['/usr/bin/ruby', '/usr/local/bin/ruby']
164
+ # File.whereis('foo') # => nil
165
+ #
166
+ def self.whereis(program, path=ENV['PATH'])
167
+ if path.nil? || path.empty?
168
+ raise ArgumentError, "path cannot be empty"
169
+ end
170
+
171
+ paths = []
172
+
173
+ # Bail out early if an absolute path is provided.
174
+ if program =~ /^\/|^[a-z]:[\\\/]/i
175
+ program += WIN32EXTS if MSWINDOWS && File.extname(program).empty?
176
+ program = program.tr("\\", '/') if MSWINDOWS
177
+ found = Dir[program]
178
+ if found[0] && File.executable?(found[0]) && !File.directory?(found[0])
179
+ if File::ALT_SEPARATOR
180
+ return found.map{ |f| f.tr('/', "\\") }
181
+ else
182
+ return found
183
+ end
184
+ else
185
+ return nil
186
+ end
187
+ end
188
+
189
+ # Iterate over each path glob the dir + program.
190
+ path.split(File::PATH_SEPARATOR).each{ |dir|
191
+ next unless File.exist?(dir) # In case of bogus second argument
192
+ file = File.join(dir, program)
193
+
194
+ # Dir[] doesn't handle backslashes properly, so convert them. Also, if
195
+ # the program name doesn't have an extension, try them all.
196
+ if MSWINDOWS
197
+ file = file.tr("\\", "/")
198
+ file += WIN32EXTS if File.extname(program).empty?
199
+ end
200
+
201
+ found = Dir[file].first
202
+
203
+ # Convert all forward slashes to backslashes if supported
204
+ if found && File.executable?(found) && !File.directory?(found)
205
+ found.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR
206
+ paths << found
207
+ end
208
+ }
209
+
210
+ paths.empty? ? nil : paths.uniq
211
+ end
212
+
213
+ # In block form, yields the first +num_lines+ from +filename+. In non-block
214
+ # form, returns an Array of +num_lines+
215
+ #
216
+ # Examples:
217
+ #
218
+ # # Return an array
219
+ # File.head('somefile.txt') # => ['This is line1', 'This is line2', ...]
220
+ #
221
+ # # Use a block
222
+ # File.head('somefile.txt'){ |line| puts line }
223
+ #
224
+ def self.head(filename, num_lines=10)
225
+ a = []
226
+
227
+ IO.foreach(filename){ |line|
228
+ break if num_lines <= 0
229
+ num_lines -= 1
230
+ if block_given?
231
+ yield line
232
+ else
233
+ a << line
234
+ end
235
+ }
236
+
237
+ return a.empty? ? nil : a # Return nil in block form
238
+ end
239
+
240
+ # In block form, yields line +from+ up to line +to+. In non-block form
241
+ # returns an Array of lines from +from+ to +to+.
242
+ #
243
+ def self.middle(filename, from=10, to=20)
244
+ if block_given?
245
+ IO.readlines(filename)[from-1..to-1].each{ |line| yield line }
246
+ else
247
+ IO.readlines(filename)[from-1..to-1]
248
+ end
249
+ end
250
+
251
+ # In block form, yields the last +num_lines+ of file +filename+.
252
+ # In non-block form, it returns the lines as an array.
253
+ #
254
+ # Note that this method slurps the entire file, so I don't recommend it
255
+ # for very large files. Also note that 'tail -f' functionality is not
256
+ # present. See the 'file-tail' library for that.
257
+ #
258
+ # Example:
259
+ #
260
+ # File.tail('somefile.txt') # => ['This is line7', 'This is line8', ...]
261
+ #
262
+ def self.tail(filename, num_lines=10)
263
+ if block_given?
264
+ IO.readlines(filename).reverse[0..num_lines-1].reverse.each{ |line|
265
+ yield line
266
+ }
267
+ else
268
+ IO.readlines(filename).reverse[0..num_lines-1].reverse
269
+ end
270
+ end
271
+
272
+ # Converts a text file from one OS platform format to another, ala
273
+ # 'dos2unix'. The possible values for +platform+ include:
274
+ #
275
+ # * MS Windows -> dos, windows, win32, mswin
276
+ # * Unix/BSD -> unix, linux, bsd, osx, darwin, sunos, solaris
277
+ # * Mac -> mac, macintosh, apple
278
+ #
279
+ # You may also specify 'local', in which case your CONFIG['host_os'] value
280
+ # will be used. This is the default.
281
+ #
282
+ # Note that this method is only valid for an ftype of "file". Otherwise a
283
+ # TypeError will be raised. If an invalid format value is received, an
284
+ # ArgumentError is raised.
285
+ #
286
+ def self.nl_convert(old_file, new_file = old_file, platform = 'local')
287
+ unless File::Stat.new(old_file).file?
288
+ raise ArgumentError, 'Only valid for plain text files'
289
+ end
290
+
291
+ format = nl_for_platform(platform)
292
+
293
+ orig = $\ # $OUTPUT_RECORD_SEPARATOR
294
+ $\ = format
295
+
296
+ if old_file == new_file
297
+ require 'fileutils'
298
+ require 'tempfile'
299
+
300
+ begin
301
+ temp_name = Time.new.strftime("%Y%m%d%H%M%S")
302
+ tf = Tempfile.new('ruby_temp_' + temp_name)
303
+ tf.open
304
+
305
+ IO.foreach(old_file){ |line|
306
+ line.chomp!
307
+ tf.print line
308
+ }
309
+ ensure
310
+ tf.close if tf && !tf.closed?
311
+ end
312
+
313
+ File.delete(old_file)
314
+ FileUtils.mv(tf.path, old_file)
315
+ else
316
+ begin
317
+ nf = File.new(new_file, 'w')
318
+ IO.foreach(old_file){ |line|
319
+ line.chomp!
320
+ nf.print line
321
+ }
322
+ ensure
323
+ nf.close if nf && !nf.closed?
324
+ end
325
+ end
326
+
327
+ $\ = orig
328
+ self
329
+ end
330
+
331
+ # Changes the access and modification time if present, or creates a 0
332
+ # byte file +filename+ if it doesn't already exist.
333
+ #
334
+ def self.touch(filename)
335
+ if File.exist?(filename)
336
+ time = Time.now
337
+ File.utime(time, time, filename)
338
+ else
339
+ File.open(filename, 'w'){}
340
+ end
341
+ self
342
+ end
343
+
344
+ # With no arguments, returns a four element array consisting of the number
345
+ # of bytes, characters, words and lines in filename, respectively.
346
+ #
347
+ # Valid options are 'bytes', 'characters' (or just 'chars'), 'words' and
348
+ # 'lines'.
349
+ #
350
+ def self.wc(filename, option='all')
351
+ option.downcase!
352
+ valid = %w/all bytes characters chars lines words/
353
+
354
+ unless valid.include?(option)
355
+ raise ArgumentError, "Invalid option: '#{option}'"
356
+ end
357
+
358
+ n = 0
359
+
360
+ if option == 'lines'
361
+ IO.foreach(filename){ n += 1 }
362
+ return n
363
+ elsif option == 'bytes'
364
+ File.open(filename){ |f|
365
+ f.each_byte{ n += 1 }
366
+ }
367
+ return n
368
+ elsif option == 'characters' || option == 'chars'
369
+ File.open(filename){ |f|
370
+ while f.getc
371
+ n += 1
372
+ end
373
+ }
374
+ return n
375
+ elsif option == 'words'
376
+ IO.foreach(filename){ |line|
377
+ n += line.split.length
378
+ }
379
+ return n
380
+ else
381
+ bytes,chars,lines,words = 0,0,0,0
382
+ IO.foreach(filename){ |line|
383
+ lines += 1
384
+ words += line.split.length
385
+ chars += line.split('').length
386
+ }
387
+ File.open(filename){ |f|
388
+ while f.getc
389
+ bytes += 1
390
+ end
391
+ }
392
+ return [bytes,chars,words,lines]
393
+ end
394
+ end
395
+
396
+ # Already provided by win32-file on MS Windows
397
+ unless respond_to?(:sparse?)
398
+ # Returns whether or not +file+ is a sparse file.
399
+ #
400
+ # A sparse file is a any file where its size is greater than the number
401
+ # of 512k blocks it consumes, i.e. its apparent and actual file size is
402
+ # not the same.
403
+ #
404
+ # See http://en.wikipedia.org/wiki/Sparse_file for more information.
405
+ #
406
+ def self.sparse?(file)
407
+ stats = File.stat(file)
408
+ stats.size > stats.blocks * 512
409
+ end
410
+ end
411
+
412
+ private
413
+
414
+ def self.nl_for_platform(platform)
415
+ platform = RbConfig::CONFIG["host_os"] if platform == 'local'
416
+
417
+ case platform
418
+ when /dos|windows|win32|mswin|mingw/i
419
+ return "\cM\cJ"
420
+ when /unix|linux|bsd|cygwin|osx|darwin|solaris|sunos/i
421
+ return "\cJ"
422
+ when /mac|apple|macintosh/i
423
+ return "\cM"
424
+ else
425
+ raise ArgumentError, "Invalid platform string"
426
+ end
427
+ end
428
+
429
+ def self.bmp?(file)
430
+ IO.read(file, 3) == "BM6"
431
+ end
432
+
433
+ def self.jpg?(file)
434
+ IO.read(file, 10, nil, :encoding => 'binary') == "\377\330\377\340\000\020JFIF".force_encoding(Encoding::BINARY)
435
+ end
436
+
437
+ def self.png?(file)
438
+ IO.read(file, 4, nil, :encoding => 'binary') == "\211PNG".force_encoding(Encoding::BINARY)
439
+ end
440
+
441
+ def self.gif?(file)
442
+ ['GIF89a', 'GIF97a'].include?(IO.read(file, 6))
443
+ end
444
+
445
+ def self.tiff?(file)
446
+ return false if File.size(file) < 12
447
+
448
+ bytes = IO.read(file, 4)
449
+
450
+ # II is Intel, MM is Motorola
451
+ if bytes[0..1] != 'II'&& bytes[0..1] != 'MM'
452
+ return false
453
+ end
454
+
455
+ if bytes[0..1] == 'II' && bytes[2..3].ord != 42
456
+ return false
457
+ end
458
+
459
+ if bytes[0..1] == 'MM' && bytes[2..3].reverse.ord != 42
460
+ return false
461
+ end
462
+
463
+ true
464
+ end
465
+ end