file-find 0.1.1 → 0.2.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.
Files changed (5) hide show
  1. data/CHANGES +4 -0
  2. data/README +30 -2
  3. data/lib/file/find.rb +36 -3
  4. data/test/tc_find.rb +39 -1
  5. metadata +2 -2
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.0 - 26-Apr-2007
2
+ * Fixed a bug where it was not traversing subdirectories.
3
+ * Added support for the perm and prune options.
4
+
1
5
  == 0.1.1 - 25-Apr-2007
2
6
  * The default for name is now '*', i.e. everything.
3
7
  * Fixed a bug where directories were not matched. Thanks go to Leslie Viljoen
data/README CHANGED
@@ -35,6 +35,8 @@ few options, hasn't been updated in over six years and isn't packaged properly.
35
35
  * group
36
36
  * name
37
37
  * path
38
+ * perm
39
+ * prune
38
40
  * size
39
41
  * user
40
42
 
@@ -47,13 +49,39 @@ http://www.rubyforge.org/projects/shards.
47
49
 
48
50
  Some specific things I plan on adding:
49
51
 
50
- * Support for limiting depth.
51
- * Greater flexibility with the size option (and perhaps others).
52
+ * exec
53
+ * links
54
+
55
+ = Options that I won't support
56
+ Generally speaking, anything that would require mucking around with C code
57
+ or is just too difficult to implement in a cross platform manner will not be
58
+ supported. These include the following options:
59
+
60
+ * acl/xattr - Way to difficult to implement in a cross platform manner, and a
61
+ rarely used option to boot.
62
+
63
+ * cpio/ncpio - You can shell out on your own if you want, but I'm not going to
64
+ do it for you. The same goes for any similar options that your particular
65
+ platform may support.
66
+
67
+ * ls/print - You can print file names as you see fit on your own. This isn't
68
+ a shell command replacement.
69
+
70
+ * ok - This is not interactive software.
71
+
72
+ = Options I may or may not support
73
+
74
+ * local/mount/xdev - This will probably not be added until I'm satisified with
75
+ the sys-filesystem package.
52
76
 
53
77
  = Bugs
54
78
  None that I'm aware of. Please log any bug reports on the project page at
55
79
  http://www.rubyforge.org/projects/shards.
56
80
 
81
+ = Acknowledgements
82
+ * Richard Clamp's File::Find::Rule Perl module for additional ideas and
83
+ inspiration.
84
+
57
85
  = License
58
86
  Ruby's
59
87
 
data/lib/file/find.rb CHANGED
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  class File::Find
4
4
  # The version of this package
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.0'
6
6
 
7
7
  # :stopdoc:
8
8
  VALID_OPTIONS = %w/
@@ -14,6 +14,8 @@ class File::Find
14
14
  group
15
15
  name
16
16
  path
17
+ perm
18
+ prune
17
19
  size
18
20
  user
19
21
  /
@@ -66,6 +68,17 @@ class File::Find
66
68
  #
67
69
  attr_accessor :name
68
70
 
71
+ # Limits searches to files which have permissions that match the octal
72
+ # value that you provide. For purposes of this comparison, only the user,
73
+ # group, and world settings are used. Do not use a leading 0 in the values
74
+ # that you supply, e.g. use 755 not 0755.
75
+ #
76
+ attr_accessor :perm
77
+
78
+ # Skips files or directories that match the string provided as an argument.
79
+ #
80
+ attr_accessor :prune
81
+
69
82
  # If the value passed is an integer, this option limits searches to files
70
83
  # that match the size, in bytes, exactly. If a string is passed, you can
71
84
  # use the standard comparable operators to match files, e.g. ">= 200" would
@@ -92,6 +105,8 @@ class File::Find
92
105
  @group = nil
93
106
  @follow = true
94
107
  @inum = nil
108
+ @perm = nil
109
+ @prune = nil
95
110
  @size = nil
96
111
  @user = nil
97
112
 
@@ -107,7 +122,13 @@ class File::Find
107
122
  #
108
123
  def find
109
124
  results = [] unless block_given?
110
- paths = [@path]
125
+ paths = @path.to_a
126
+
127
+ if @prune
128
+ prune_regex = Regexp.new(@prune)
129
+ else
130
+ prune_regex = nil
131
+ end
111
132
 
112
133
  catch(:loop) do
113
134
  paths.each{ |path|
@@ -115,6 +136,11 @@ class File::Find
115
136
  next if file == '.'
116
137
  next if file == '..'
117
138
 
139
+ if prune_regex
140
+ next if prune_regex.match(file)
141
+ end
142
+
143
+ orig = file.dup
118
144
  file = File.join(path, file)
119
145
 
120
146
  stat_method = @follow ? :lstat : :stat
@@ -130,17 +156,20 @@ class File::Find
130
156
  end
131
157
 
132
158
  glob = File.join(File.dirname(file), @name)
133
- next unless Dir[glob].include?(file)
134
159
 
135
160
  # Add directories back onto the list of paths to search unless
136
161
  # they've already been added.
137
162
  #
163
+ # TODO: Add max_depth support here
164
+ #
138
165
  if stat_info.directory?
139
166
  unless paths.include?(file)
140
167
  paths << file
141
168
  end
142
169
  end
143
170
 
171
+ next unless Dir[glob].include?(file)
172
+
144
173
  if @atime
145
174
  date1 = Date.parse(Time.now.to_s)
146
175
  date2 = Date.parse(stat_info.atime.to_s)
@@ -167,6 +196,10 @@ class File::Find
167
196
  end
168
197
  end
169
198
 
199
+ if @perm
200
+ next unless sprintf("%o", stat_info.mode & 07777) == @perm.to_s
201
+ end
202
+
170
203
  # Allow plain numbers, or strings for comparison operators.
171
204
  if @size
172
205
  if @size.is_a?(String)
data/test/tc_find.rb CHANGED
@@ -12,17 +12,27 @@ class TC_File_Find < Test::Unit::TestCase
12
12
  def setup
13
13
  @file1 = 'test1.rb'
14
14
  @file2 = 'test1.txt'
15
+ @file3 = 'foo.txt'
15
16
  @link1 = 'link1'
17
+ @dir1 = 'dir1'
18
+ @dir2 = 'dir2'
16
19
 
17
20
  File.open(@file1, 'w'){}
18
21
  File.open(@file2, 'w'){}
22
+ File.open(@file3, 'w'){}
19
23
  File.symlink(@file1, @link1)
20
24
 
25
+ Dir.mkdir(@dir1) unless File.exists?(@dir1)
26
+ Dir.mkdir(@dir2) unless File.exists?(@dir2)
27
+
28
+ File.open(File.join(@dir1, 'bar.txt'), 'w'){}
29
+ File.open(File.join(@dir2, 'baz.txt'), 'w'){}
30
+
21
31
  @rule1 = File::Find.new(:name => '*.rb')
22
32
  end
23
33
 
24
34
  def test_version
25
- assert_equal('0.1.1', File::Find::VERSION)
35
+ assert_equal('0.2.0', File::Find::VERSION)
26
36
  end
27
37
 
28
38
  def test_path
@@ -103,6 +113,31 @@ class TC_File_Find < Test::Unit::TestCase
103
113
  assert_equal('*.rb', @rule1.name)
104
114
  end
105
115
 
116
+ def test_perm_basic
117
+ assert_respond_to(@rule1, :perm)
118
+ assert_respond_to(@rule1, :perm=)
119
+ assert_nil(@rule1.perm)
120
+ end
121
+
122
+ def test_perm
123
+ File.chmod(0666, @file1)
124
+ File.chmod(0644, @file2)
125
+ results = File::Find.new(:name => "test1*", :perm => 666).find
126
+ assert_equal(true, results.length == 1)
127
+ assert_equal('test1.rb', File.basename(results.first))
128
+ end
129
+
130
+ def test_prune_basic
131
+ assert_respond_to(@rule1, :prune)
132
+ assert_respond_to(@rule1, :prune=)
133
+ assert_nil(@rule1.prune)
134
+ end
135
+
136
+ def test_prune
137
+ rule = File::Find.new(:name => "*.txt", :prune => 'foo')
138
+ assert_equal('test1.txt', File.basename(rule.find.first))
139
+ end
140
+
106
141
  def test_size_basic
107
142
  assert_respond_to(@rule1, :size)
108
143
  assert_respond_to(@rule1, :size=)
@@ -122,6 +157,9 @@ class TC_File_Find < Test::Unit::TestCase
122
157
  def teardown
123
158
  FileUtils.rm_rf(@file1)
124
159
  FileUtils.rm_rf(@file2)
160
+ FileUtils.rm_rf(@file3)
125
161
  FileUtils.rm_rf(@link1)
162
+ FileUtils.rm_rf(@dir1)
163
+ FileUtils.rm_rf(@dir2)
126
164
  end
127
165
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: file-find
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2007-04-25 00:00:00 -06:00
6
+ version: 0.2.0
7
+ date: 2007-04-26 00:00:00 -06:00
8
8
  summary: A better way to find files
9
9
  require_paths:
10
10
  - lib