filescan 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +14 -0
- data/README.rdoc +43 -0
- data/lib/filescan.rb +174 -0
- metadata +47 -0
data/History.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
=== 2.3.1 2011-11-09
|
2
|
+
|
3
|
+
* 1 minor enhancement:
|
4
|
+
* Use .gemspec to create gem.
|
5
|
+
|
6
|
+
=== 2.3.0 2010-01-19
|
7
|
+
|
8
|
+
* 1 major enhancement:
|
9
|
+
* Allow user to decide which exceptions to ignore.
|
10
|
+
|
11
|
+
=== 2.2.1 2010-01-18
|
12
|
+
|
13
|
+
* 1 major enhancement:
|
14
|
+
* First gem release (identical to previous 2.2 release)
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= filescan
|
2
|
+
|
3
|
+
* http://rubygems.org/gems/filescan
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Class to recursively scan through files from a given directory.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Enumerable
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
Filescan.new("/path/to/files") do |fs|
|
16
|
+
fs.each_pathname { |path| puts path }
|
17
|
+
end
|
18
|
+
|
19
|
+
== REQUIREMENTS:
|
20
|
+
|
21
|
+
* Ruby :)
|
22
|
+
|
23
|
+
== INSTALL:
|
24
|
+
|
25
|
+
* gem install filescan
|
26
|
+
|
27
|
+
== LICENSE:
|
28
|
+
|
29
|
+
This software is provided ‘as-is’, without any express or implied warranty. In
|
30
|
+
no event will the authors be held liable for any damages arising from the use of
|
31
|
+
this software.
|
32
|
+
|
33
|
+
Permission is granted to anyone to use this software for any purpose, including
|
34
|
+
commercial applications, and to alter it and redistribute it freely, subject to
|
35
|
+
the following restrictions:
|
36
|
+
|
37
|
+
1. The origin of this software must not be misrepresented; you must not claim
|
38
|
+
that you wrote the original software. If you use this software in a
|
39
|
+
product, an acknowledgment in the product documentation would be
|
40
|
+
appreciated but is not required.
|
41
|
+
2. Altered source versions must be plainly marked as such, and must not be
|
42
|
+
misrepresented as being the original software.
|
43
|
+
3. This notice may not be removed or altered from any source distribution.
|
data/lib/filescan.rb
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# == Synopsis
|
2
|
+
#
|
3
|
+
# Class to recursively scan through files from a given directory.
|
4
|
+
#
|
5
|
+
# == Copyright
|
6
|
+
#
|
7
|
+
# Copyright (C) 2003, 2008, 2009 by Alan Davies.
|
8
|
+
#
|
9
|
+
# == License
|
10
|
+
#
|
11
|
+
# This software is provided 'as-is', without any express or implied
|
12
|
+
# warranty. In no event will the authors be held liable for any damages
|
13
|
+
# arising from the use of this software.
|
14
|
+
#
|
15
|
+
# Permission is granted to anyone to use this software for any purpose,
|
16
|
+
# including commercial applications, and to alter it and redistribute it
|
17
|
+
# freely, subject to the following restrictions:
|
18
|
+
#
|
19
|
+
# 1. The origin of this software must not be misrepresented; you must not
|
20
|
+
# claim that you wrote the original software. If you use this software
|
21
|
+
# in a product, an acknowledgment in the product documentation would be
|
22
|
+
# appreciated but is not required.
|
23
|
+
# 2. Altered source versions must be plainly marked as such, and must not be
|
24
|
+
# misrepresented as being the original software.
|
25
|
+
# 3. This notice may not be removed or altered from any source distribution.
|
26
|
+
|
27
|
+
# Class to recursively scan through files from a given directory.
|
28
|
+
class Filescan
|
29
|
+
include Enumerable
|
30
|
+
|
31
|
+
VERSION = "2.3.1"
|
32
|
+
|
33
|
+
# Default root directory for calls to each.
|
34
|
+
attr_accessor :startDir
|
35
|
+
# Whether to recurse into subdirectories (boolean).
|
36
|
+
attr_accessor :recursive
|
37
|
+
# Whether to output directory names (boolean).
|
38
|
+
attr_accessor :verbose
|
39
|
+
# Array of exceptions to allow (defaults to [Errno::EACCES])
|
40
|
+
# * If one of these exceptions is thrown then an error is shown on stderr, but processing continues.
|
41
|
+
# * If any other exception is thrown then processing stops and the exception is passed back to the user.
|
42
|
+
attr_accessor :allowedExceptions
|
43
|
+
|
44
|
+
# Create a new Filescan Object.
|
45
|
+
#
|
46
|
+
# :call-seq:
|
47
|
+
# new(dir, recusive, verbose) -> Filescan
|
48
|
+
# new(dir, recusive, verbose) { |filescan| ... } -> nil
|
49
|
+
#
|
50
|
+
def initialize(dir='.', recursive=true, verbose=false, allowedExceptions=[Errno::EACCES]) # :yields: filescan
|
51
|
+
@startDir = dir
|
52
|
+
@recursive = recursive
|
53
|
+
@verbose = verbose
|
54
|
+
@allowedExceptions = allowedExceptions
|
55
|
+
|
56
|
+
yield self if block_given?
|
57
|
+
end
|
58
|
+
|
59
|
+
# Execute a block for every entry in a directory.
|
60
|
+
def each(startDir=@startDir, &block) # :yields: fullPath
|
61
|
+
puts startDir if @verbose
|
62
|
+
begin
|
63
|
+
Dir.open(startDir) do |dir|
|
64
|
+
dir.each do |entry|
|
65
|
+
next if entry == '.' or entry == '..'
|
66
|
+
yield(fullPath = File.expand_path(entry, startDir))
|
67
|
+
self.each(fullPath, &block) if @recursive and File.directory?(fullPath)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
rescue Exception => e
|
71
|
+
if allowedExceptions.any? { |allowed| e.is_a?(allowed) }
|
72
|
+
$stderr.puts e
|
73
|
+
return
|
74
|
+
else
|
75
|
+
raise
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end # def each
|
79
|
+
|
80
|
+
# Executes a block for each directory name or file name, where any part of the path matches a pattern.
|
81
|
+
# If you do not specify a pattern, then it is equivilent to calling 'each'.
|
82
|
+
# If no block is given, then it returns an array of matching results.
|
83
|
+
#
|
84
|
+
# :call-seq:
|
85
|
+
# each_pathname(pattern=//) -> Array
|
86
|
+
# each_pathname(pattern=//) { |pathName| ... } -> nil
|
87
|
+
#
|
88
|
+
def each_pathname(pattern=//) # :yields: pathName
|
89
|
+
if block_given?
|
90
|
+
self.each { |pathName| yield pathName if pathName.match(pattern) }
|
91
|
+
else
|
92
|
+
a = []
|
93
|
+
self.each_pathname(pattern) { |pathName| a << pathName }
|
94
|
+
return a
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Executes a block for each directory name.
|
99
|
+
# If no block is given, then it returns an array of matching results.
|
100
|
+
#
|
101
|
+
# :call-seq:
|
102
|
+
# each_dirname(pattern=//) -> Array
|
103
|
+
# each_dirname(pattern=//) { |dirName| ... } -> nil
|
104
|
+
#
|
105
|
+
def each_dirname(pattern=//) # :yields: dirName
|
106
|
+
if block_given?
|
107
|
+
self.each { |dirName| yield dirName if File.directory?(dirName) and File.basename(dirName).match(pattern) }
|
108
|
+
else
|
109
|
+
a = []
|
110
|
+
self.each_dirname(pattern) { |dirName| a << dirName }
|
111
|
+
return a
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Executes a block for each directory.
|
116
|
+
def each_dir(pattern=//)
|
117
|
+
each_dirname(pattern) do |dirName|
|
118
|
+
Dir.open(dirName) { |dirHandle| yield(dirHandle, dirName) }
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Execute a block for each filename.
|
123
|
+
# If no block is given, then it returns an array of matching results.
|
124
|
+
#
|
125
|
+
# :call-seq:
|
126
|
+
# each_filename(pattern=//) -> array
|
127
|
+
# each_filename(pattern=//) { |fileName| ... } -> nil
|
128
|
+
#
|
129
|
+
def each_filename(pattern=//) # :yields: fileName
|
130
|
+
if block_given?
|
131
|
+
self.each { |fileName| yield fileName if File.file?(fileName) and File.basename(fileName).match(pattern) }
|
132
|
+
else
|
133
|
+
a = []
|
134
|
+
self.each_filename(pattern) { |fileName| a << fileName }
|
135
|
+
return a
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# Execute a block for each file.
|
140
|
+
def each_file(mode='r', pattern=//)
|
141
|
+
self.each_filename(pattern) do |filename|
|
142
|
+
File.open(filename, mode) { |file| yield(file, filename) }
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# Executes a block for each line of each file.
|
147
|
+
# If the files are opened in writable mode, then the block must return the number of changes that were made to the line.
|
148
|
+
# Files are not written back to the disk if no lines are altered.
|
149
|
+
#
|
150
|
+
# :call-seq:
|
151
|
+
# each_line(writable=false, pattern=//) -> array
|
152
|
+
# each_line(writable=false, pattern=//) { |file, filename| ... } -> nil
|
153
|
+
#
|
154
|
+
def each_line(writable=false, pattern=//)
|
155
|
+
self.each_file(writable ? 'r+' : 'r', pattern) do |file, filename|
|
156
|
+
fileChanges = 0
|
157
|
+
|
158
|
+
# Yield the block for each line in the file
|
159
|
+
file.each_line do |line|
|
160
|
+
lineChanges = yield(line, filename)
|
161
|
+
fileChanges += lineChanges if writable
|
162
|
+
end
|
163
|
+
|
164
|
+
# Write the file back to disk
|
165
|
+
if writable and (fileChanges > 0)
|
166
|
+
puts "#{filename} - #{fileChanges} changes made" if @verbose
|
167
|
+
file.rewind
|
168
|
+
file.write(text)
|
169
|
+
file.truncate(file.pos)
|
170
|
+
end
|
171
|
+
end # self.eachfile
|
172
|
+
end # def each_line
|
173
|
+
|
174
|
+
end # class Filescan
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filescan
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.3.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alan Davies
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-09 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Class to recursively scan through files from a given directory.
|
15
|
+
email: alan n davies at gmail com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/filescan.rb
|
21
|
+
- History.txt
|
22
|
+
- README.rdoc
|
23
|
+
homepage: http://rubygems.org/gems/filescan
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.11
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Class to recursively scan through files from a given directory.
|
47
|
+
test_files: []
|