ptools 1.0.0 → 1.1.0
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.
- data/CHANGES +9 -0
- data/MANIFEST +1 -0
- data/README +6 -1
- data/lib/ptools.rb +54 -26
- data/test/tc_constants.rb +9 -1
- data/test/tc_nlconvert.rb +5 -3
- data/test/tc_null.rb +40 -0
- data/test/tc_whereis.rb +4 -2
- data/test/ts_all.rb +1 -0
- metadata +37 -29
data/CHANGES
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
== 1.1.0 - 23-Aug-2006
|
2
|
+
* Added the File.null method which returns the bit bucket on your platform.
|
3
|
+
* The suffixes on MS Windows are now based on the PATHEXT environment variable,
|
4
|
+
and defaults to '.com', '.bat' and '.exe' only if it's not defined.
|
5
|
+
* The File.which and File.whereis methods were tweaked a bit for Windows so
|
6
|
+
that they ignore the case of suffixes.
|
7
|
+
* Modified the platform checking and path separator handling.
|
8
|
+
* Added and tweaked some tests.
|
9
|
+
|
1
10
|
== 1.0.0 - 2-Jun-2005
|
2
11
|
* Modified the File.middle method to accept an optional block.
|
3
12
|
* File.whereis is now limited to unique values so that redundant PATH entries
|
data/MANIFEST
CHANGED
data/README
CHANGED
@@ -6,6 +6,10 @@
|
|
6
6
|
Ruby 1.8.0 or later is recommended but not required.
|
7
7
|
|
8
8
|
== Installation
|
9
|
+
=== Gem
|
10
|
+
ruby ptools.gemspec
|
11
|
+
gem install ptools-x.y.z.gem
|
12
|
+
=== Manual
|
9
13
|
ruby test/ts_all.rb (optional)
|
10
14
|
ruby install.rb
|
11
15
|
|
@@ -21,6 +25,7 @@
|
|
21
25
|
File.wc("myfile",'words') # Returns the number of words in 'myfile'
|
22
26
|
|
23
27
|
File.touch("newfile") # "newfile" now exists
|
28
|
+
File.null # '/dev/null' on Unix, 'NUL' on Windows
|
24
29
|
|
25
30
|
# Creates a copy of 'myfile' called 'newfile', in DOS format
|
26
31
|
File.nl_convert("myfile", "newfile", "dos")
|
@@ -48,7 +53,7 @@
|
|
48
53
|
Ruby's
|
49
54
|
|
50
55
|
== Copyright
|
51
|
-
(C) 2003-
|
56
|
+
(C) 2003-2006 Daniel J. Berger
|
52
57
|
All Rights Reserved.
|
53
58
|
|
54
59
|
== Warranty
|
data/lib/ptools.rb
CHANGED
@@ -1,21 +1,41 @@
|
|
1
1
|
class File
|
2
|
+
PTOOLS_VERSION = '1.1.0'
|
3
|
+
IS_WINDOWS = RUBY_PLATFORM.match('mswin') ? true : false
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
+
WIN32EXTS = ENV['PATHEXT'].split(';').map{ |e| e.downcase } rescue %w/.exe .com .bat/
|
6
|
+
|
7
|
+
# Returns the null device (aka bitbucket) on your platform. On most
|
8
|
+
# Unix-like systems this is '/dev/null', on Windows it's 'NUL', etc.
|
9
|
+
#--
|
10
|
+
# Based on information from http://en.wikipedia.org/wiki//dev/null
|
11
|
+
#
|
12
|
+
def self.null
|
13
|
+
case RUBY_PLATFORM
|
14
|
+
when /mswin/i
|
15
|
+
'NUL'
|
16
|
+
when /amiga/i
|
17
|
+
'NIL:'
|
18
|
+
when /openvms/i
|
19
|
+
'NL:'
|
20
|
+
else
|
21
|
+
'/dev/null'
|
22
|
+
end
|
23
|
+
end
|
5
24
|
|
6
25
|
# Looks for the first occurrence of +program+ within +path+.
|
7
26
|
#
|
8
|
-
# On
|
9
|
-
#
|
27
|
+
# On Windows, it looks for executables ending with the suffixes defined
|
28
|
+
# in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if
|
29
|
+
# that isn't defined, which you may optionally include in +program+.
|
30
|
+
#
|
31
|
+
# Returns nil if not found.
|
10
32
|
#
|
11
|
-
# Returns nil if not found.
|
12
33
|
def self.which(program, path=ENV['PATH'])
|
13
34
|
programs = program.to_a
|
14
35
|
|
15
|
-
# If no file extension is provided on Windows, try
|
16
|
-
|
17
|
-
|
18
|
-
unless WIN32EXTS.include?(File.extname(program))
|
36
|
+
# If no file extension is provided on Windows, try the WIN32EXT's in turn
|
37
|
+
if IS_WINDOWS && File.extname(program).empty?
|
38
|
+
unless WIN32EXTS.include?(File.extname(program).downcase)
|
19
39
|
WIN32EXTS.each{ |ext|
|
20
40
|
programs.push(program + ext)
|
21
41
|
}
|
@@ -29,7 +49,7 @@ class File
|
|
29
49
|
f = File.join(dir, prog)
|
30
50
|
if File.executable?(f) && !File.directory?(f)
|
31
51
|
location = File.join(dir, prog)
|
32
|
-
location.tr!('/',File::ALT_SEPARATOR) if File::ALT_SEPARATOR
|
52
|
+
location.tr!('/', File::ALT_SEPARATOR) if File::ALT_SEPARATOR
|
33
53
|
throw(:done, location)
|
34
54
|
end
|
35
55
|
}
|
@@ -42,17 +62,20 @@ class File
|
|
42
62
|
|
43
63
|
# In block form, yields each +program+ within +path+. In non-block
|
44
64
|
# form, returns an array of each +program+ within +path+.
|
45
|
-
#
|
46
|
-
# On
|
47
|
-
#
|
65
|
+
#
|
66
|
+
# On Windows, it looks for executables ending with the suffixes defined
|
67
|
+
# in your PATHEXT environment variable, or '.exe', '.bat' and '.com' if
|
68
|
+
# that isn't defined, which you may optionally include in +program+.
|
69
|
+
#
|
70
|
+
# Returns nil if not found.
|
71
|
+
#
|
48
72
|
def self.whereis(program, path=ENV['PATH'])
|
49
73
|
dirs = []
|
50
74
|
programs = program.to_a
|
51
75
|
|
52
|
-
# If no file extension is provided on Windows, try
|
53
|
-
|
54
|
-
|
55
|
-
unless WIN32EXTS.include?(File.extname(program))
|
76
|
+
# If no file extension is provided on Windows, try the WIN32EXT's in turn
|
77
|
+
if IS_WINDOWS && File.extname(program).empty?
|
78
|
+
unless WIN32EXTS.include?(File.extname(program).downcase)
|
56
79
|
WIN32EXTS.each{ |ext|
|
57
80
|
programs.push(program + ext)
|
58
81
|
}
|
@@ -62,7 +85,7 @@ class File
|
|
62
85
|
path.split(File::PATH_SEPARATOR).each{ |dir|
|
63
86
|
programs.each{ |prog|
|
64
87
|
file = File.join(dir,prog)
|
65
|
-
file.tr!('/',File::ALT_SEPARATOR) if File::ALT_SEPARATOR
|
88
|
+
file.tr!('/', File::ALT_SEPARATOR) if File::ALT_SEPARATOR
|
66
89
|
if File.executable?(file) && !File.directory?(file)
|
67
90
|
if block_given?
|
68
91
|
yield file
|
@@ -77,6 +100,7 @@ class File
|
|
77
100
|
|
78
101
|
# In block form, yields the first +num_lines+ from +filename+. In non-block
|
79
102
|
# form, returns an Array of +num_lines+
|
103
|
+
#
|
80
104
|
def self.head(filename, num_lines=10)
|
81
105
|
a = []
|
82
106
|
IO.foreach(filename){ |line|
|
@@ -93,6 +117,7 @@ class File
|
|
93
117
|
|
94
118
|
# In block form, yields line +from+ up to line +to+. In non-block form
|
95
119
|
# returns an Array of lines from +from+ to +to+.
|
120
|
+
#
|
96
121
|
def self.middle(filename, from=10, to=20)
|
97
122
|
if block_given?
|
98
123
|
IO.readlines(filename)[from-1..to-1].each{ |line| yield line }
|
@@ -107,6 +132,7 @@ class File
|
|
107
132
|
# Note that this method slurps the entire file, so I don't recommend it
|
108
133
|
# for very large files. Also note that 'tail -f' functionality is not
|
109
134
|
# present.
|
135
|
+
#
|
110
136
|
def self.tail(filename, num_lines=10)
|
111
137
|
if block_given?
|
112
138
|
IO.readlines(filename).reverse[0..num_lines-1].reverse.each{ |line|
|
@@ -120,21 +146,21 @@ class File
|
|
120
146
|
# Converts a text file from one OS platform format to another, ala
|
121
147
|
# 'dos2unix'. Valid values for 'format', which are case insensitve,
|
122
148
|
# include:
|
123
|
-
|
124
|
-
# * MS Windows -> dos, windows, win32
|
149
|
+
#
|
150
|
+
# * MS Windows -> dos, windows, win32, mswin
|
125
151
|
# * Unix/BSD -> unix, linux, bsd
|
126
152
|
# * Mac -> mac, macintosh, apple, osx
|
127
|
-
|
153
|
+
#
|
128
154
|
# Note that this method is only valid for an ftype of "file". Otherwise a
|
129
155
|
# TypeError will be raised. If an invalid format value is received, an
|
130
156
|
# ArgumentError is raised.
|
157
|
+
#
|
131
158
|
def self.nl_convert(filename, newfilename=filename, platform="dos")
|
132
|
-
|
133
159
|
unless File.ftype(filename) == "file"
|
134
160
|
raise TypeError, "Only valid for plain text files"
|
135
161
|
end
|
136
162
|
|
137
|
-
if platform =~ /dos|windows|win32/i
|
163
|
+
if platform =~ /dos|windows|win32|mswin/i
|
138
164
|
format = "\cM\cJ"
|
139
165
|
elsif platform =~ /unix|linux|bsd/i
|
140
166
|
format = "\cJ"
|
@@ -173,17 +199,19 @@ class File
|
|
173
199
|
end
|
174
200
|
|
175
201
|
# Creates the 0 byte file +filename+.
|
202
|
+
#
|
176
203
|
def self.touch(filename)
|
177
|
-
File.open(filename,
|
204
|
+
File.open(filename, 'w'){}
|
178
205
|
self
|
179
206
|
end
|
180
207
|
|
181
208
|
# With no arguments, returns a four element array consisting of the number
|
182
209
|
# of bytes, characters, words and lines in filename, respectively.
|
183
|
-
|
210
|
+
#
|
184
211
|
# Valid options are 'bytes', 'characters' (or just 'chars'), 'words' and
|
185
212
|
# 'lines'.
|
186
|
-
|
213
|
+
#
|
214
|
+
def self.wc(filename, option='all')
|
187
215
|
option.downcase!
|
188
216
|
valid = %w/all bytes characters chars lines words/
|
189
217
|
|
data/test/tc_constants.rb
CHANGED
@@ -17,6 +17,14 @@ require "ptools"
|
|
17
17
|
|
18
18
|
class TC_Constants < Test::Unit::TestCase
|
19
19
|
def test_version
|
20
|
-
assert_equal("1.
|
20
|
+
assert_equal("1.1.0", File::PTOOLS_VERSION, "Bad VERSION")
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_windows
|
24
|
+
assert_not_nil(File::IS_WINDOWS)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_win32exts
|
28
|
+
assert_not_nil(File::WIN32EXTS)
|
21
29
|
end
|
22
30
|
end
|
data/test/tc_nlconvert.rb
CHANGED
@@ -42,8 +42,8 @@ class TC_FileNLConvert < Test::Unit::TestCase
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_nl_convert_to_mac
|
45
|
-
if
|
46
|
-
msg = "test may fail on
|
45
|
+
if PLATFORM.match("mswin")
|
46
|
+
msg = "test may fail on MS Windows"
|
47
47
|
else
|
48
48
|
msg = "mac file should be the same size (or larger), but isn't"
|
49
49
|
end
|
@@ -68,7 +68,9 @@ class TC_FileNLConvert < Test::Unit::TestCase
|
|
68
68
|
|
69
69
|
def test_nl_convert_expected_errors
|
70
70
|
assert_raises(ArgumentError){ File.nl_convert }
|
71
|
-
assert_raises(ArgumentError){
|
71
|
+
assert_raises(ArgumentError){
|
72
|
+
File.nl_convert(@test_file1, "bogus.txt", "blah")
|
73
|
+
}
|
72
74
|
end
|
73
75
|
|
74
76
|
def teardown
|
data/test/tc_null.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
##########################################
|
2
|
+
# tc_null.rb
|
3
|
+
#
|
4
|
+
# Test case for the File.null method.
|
5
|
+
##########################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" or base =~ /ptools/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") if Dir.pwd != "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "ptools"
|
17
|
+
|
18
|
+
class TC_Null < Test::Unit::TestCase
|
19
|
+
def setup
|
20
|
+
@nulls = ['/dev/null', 'NUL', 'NIL:', 'NL:']
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_null_basic
|
24
|
+
assert_respond_to(File, :null)
|
25
|
+
assert_nothing_raised{ File.null }
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_null_expected_results
|
29
|
+
assert_kind_of(String, File.null)
|
30
|
+
assert(@nulls.include?(File.null))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_null_expected_errors
|
34
|
+
assert_raises(ArgumentError){ File.null(1) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def teardown
|
38
|
+
@nulls = nil
|
39
|
+
end
|
40
|
+
end
|
data/test/tc_whereis.rb
CHANGED
@@ -36,8 +36,10 @@ class TC_FileWhereis < Test::Unit::TestCase
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_whereis_expected_return_values
|
39
|
-
|
40
|
-
|
39
|
+
msg = "You may need to adjust the setup method if this test failed"
|
40
|
+
locs = File.whereis('ruby').map{ |e| e.downcase }
|
41
|
+
assert_kind_of(Array, locs)
|
42
|
+
assert(@expected_locs.include?(locs.first), msg)
|
41
43
|
assert_equal(nil, File.whereis("blahblah"))
|
42
44
|
end
|
43
45
|
|
data/test/ts_all.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.
|
2
|
+
rubygems_version: 0.9.0
|
3
3
|
specification_version: 1
|
4
4
|
name: ptools
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.
|
7
|
-
date:
|
6
|
+
version: 1.1.0
|
7
|
+
date: 2006-08-23 00:00:00 -06:00
|
8
8
|
summary: Extra methods for the File class
|
9
9
|
require_paths:
|
10
|
-
|
10
|
+
- lib
|
11
11
|
email: djberg96@gmail.com
|
12
12
|
homepage: http://www.rubyforge.org/projects/shards
|
13
13
|
rubyforge_project:
|
@@ -18,38 +18,46 @@ bindir: bin
|
|
18
18
|
has_rdoc: true
|
19
19
|
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
20
|
requirements:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
version: 0.0.0
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
25
24
|
version:
|
26
25
|
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
27
29
|
authors:
|
28
|
-
|
30
|
+
- Daniel J. Berger
|
29
31
|
files:
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
32
|
+
- lib/ptools.rb
|
33
|
+
- CHANGES
|
34
|
+
- MANIFEST
|
35
|
+
- README
|
36
|
+
- test/tc_constants.rb
|
37
|
+
- test/tc_head.rb
|
38
|
+
- test/tc_middle.rb
|
39
|
+
- test/tc_nlconvert.rb
|
40
|
+
- test/tc_null.rb
|
41
|
+
- test/tc_tail.rb
|
42
|
+
- test/tc_touch.rb
|
43
|
+
- test/tc_wc.rb
|
44
|
+
- test/tc_whereis.rb
|
45
|
+
- test/tc_which.rb
|
46
|
+
- test/test_file1.txt
|
47
|
+
- test/test_file2.txt
|
48
|
+
- test/ts_all.rb
|
46
49
|
test_files:
|
47
|
-
|
50
|
+
- test/ts_all.rb
|
48
51
|
rdoc_options: []
|
52
|
+
|
49
53
|
extra_rdoc_files:
|
50
|
-
|
51
|
-
|
54
|
+
- README
|
55
|
+
- CHANGES
|
52
56
|
executables: []
|
57
|
+
|
53
58
|
extensions: []
|
59
|
+
|
54
60
|
requirements: []
|
55
|
-
|
61
|
+
|
62
|
+
dependencies: []
|
63
|
+
|