thomaspeklak-filefinder 0.0.8

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 (4) hide show
  1. data/README +35 -0
  2. data/bin/filefinder +45 -0
  3. data/lib/FileFinder.rb +71 -0
  4. metadata +56 -0
data/README ADDED
@@ -0,0 +1,35 @@
1
+ finds files in a given path possible filters
2
+ - types
3
+ - includes
4
+ - excludes
5
+ - ignore_subdirectories
6
+ - max_depth
7
+
8
+ Executable:
9
+ filefinder
10
+
11
+ Example:
12
+ FileFinder::find('path',:types => ['.xml'],:exclusions => 't')
13
+
14
+ ___________________________________________________________________________
15
+ The MIT License
16
+
17
+ Copyright (c) 2008 Thomas Peklak
18
+
19
+ Permission is hereby granted, free of charge, to any person obtaining a copy
20
+ of this software and associated documentation files (the "Software"), to deal
21
+ in the Software without restriction, including without limitation the rights
22
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23
+ copies of the Software, and to permit persons to whom the Software is
24
+ furnished to do so, subject to the following conditions:
25
+
26
+ The above copyright notice and this permission notice shall be included in
27
+ all copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35
+ THE SOFTWARE.
data/bin/filefinder ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/local/bin/ruby -w
2
+ #---
3
+ # $Author: tpesl $
4
+ # $Revision: 525 $
5
+ # $LastChangedDate: 2008-02-18 08:23:01 +0100 (Mo, 18 Feb 2008) $
6
+ #+++
7
+
8
+ require 'FileFinder.rb'
9
+ require 'optparse'
10
+
11
+ $path = '.'
12
+ $filters = Hash.new
13
+
14
+ OptionParser.new do |opts|
15
+ $filters[:ignore_subdirectories] = false
16
+ opts.banner = "Usage: FileFinder [options]"
17
+ opts.on('-p', '--path=PATH', String,'Path to search') do |p|
18
+ $path = p
19
+ end
20
+ opts.on('-t','--file-types=TYPE[,TYPE]',Array,'only list files of type TYPE') do |t|
21
+ $filters[:types] = t
22
+ end
23
+ opts.on('-i','--include=INLCUDE[,INCLUDE]',Array,'include only files matching INCLUDE') do |i|
24
+ $filters[:includes] = i
25
+ end
26
+ opts.on('-x','--exclude=EXCLUDE[,EXCLUDE]',Array,'do not include files matching EXCLUDE') do |x|
27
+ $filters[:excludes] = x
28
+ end
29
+ opts.on('-d','--ignore-directories','do not go into listed directories') do |x|
30
+ $filters[:ignore_directories] = x
31
+ end
32
+ opts.on('-s','--ignore-subdirectories','do not recursivle search sub directories') do |x|
33
+ $filters[:ignore_subdirectories] = true
34
+ end
35
+ opts.on('-m','--max-depth=MAX_DEPTH',Integer,'maximum depth of subdirectory') do |m|
36
+ $filters[:max_depth] = m
37
+ end
38
+ opts.separator ""
39
+ opts.on_tail('-h','--help','Show this message') do
40
+ puts opts
41
+ exit
42
+ end
43
+ end.parse!
44
+
45
+ puts FileFinder.find($path, $filters)
data/lib/FileFinder.rb ADDED
@@ -0,0 +1,71 @@
1
+ # FileFinder - finds files in directory
2
+ #--
3
+ # $Author: tpesl $
4
+ # $Revision: 616 $
5
+ # $LastChangedDate: 2008-04-10 10:48:13 +0200 (Thu, 10 Apr 2008) $
6
+ #++
7
+
8
+ require 'Find'
9
+ # finds files in a given path
10
+ # possible filters
11
+ # * types
12
+ # * includes
13
+ # * excludes
14
+ # * ignore_subdirectories
15
+ # * max_depth
16
+ #
17
+ # Example:
18
+ # FileFinder::find('path',:types => ['.xml'],:exclusions => 't')
19
+ class FileFinder
20
+ # Finds files in the given directory.
21
+ # Arguments:
22
+ # [path] directory to search for files
23
+ # [types] file types to include
24
+ # [includes] array of strings a file name must contain
25
+ # [excludes] array of strings a file name must not contain
26
+ # [ignore_subdirectories] boolean, do not recursively search subdirectories
27
+ # [max_depth] integer, maximum depth to descend in subdirectories
28
+ def self.find(path, *config)
29
+ if (config.empty?)
30
+ types = exclusions = false
31
+ else
32
+ types, types_filter = filter_factory(config[0][:types])
33
+ includes, includes_filter = filter_factory(config[0][:includes])
34
+ excludes, excludes_filter = filter_factory(config[0][:excludes])
35
+ ignore_directories, ignore_directories_filter = filter_factory(config[0][:ignore_directories])
36
+ ignore_subdirectories = switch_factory(config[0][:ignore_subdirectories])
37
+ max_depth, max_depth_value = integer_factory(config[0][:max_depth])
38
+ end
39
+ @files = Array.new
40
+ Find.find(path) do |f|
41
+ if ((ignore_subdirectories && FileTest.directory?(f) && f!='.' && f!=path) || (ignore_directories && FileTest.directory?(f) && File.basename(f) =~ /#{ignore_directories_filter}/i))
42
+ Find.prune
43
+ end
44
+ if (max_depth && FileTest.directory?(f) && f.gsub(/^(..\/|.\/)*/,'')=~ /(.*\/){#{max_depth_value}}/)
45
+ Find.prune
46
+ end
47
+ if FileTest.file?(f)
48
+ if ((types && File.basename(f) =~ /#{types_filter}$/i) || !types) && ((includes && File.basename(f) =~ /#{includes_filter}/i) || !includes)
49
+ @files.push(f) unless excludes && File.basename(f) =~ /#{excludes_filter}/i
50
+ end
51
+ end
52
+ end
53
+ @files
54
+ end
55
+
56
+ private
57
+ def self.filter_factory(filter_array)
58
+ filter_string=''
59
+ filter_array.each { |fi| filter_string+="#{fi}|"} unless filter_array.nil?
60
+ [!filter_array.nil? && !filter_array.empty? ,"(#{filter_string[0..-2]})"]
61
+ end
62
+
63
+ def self.switch_factory(switch)
64
+ !switch.nil? && (switch == true || switch == 1)
65
+ end
66
+ def self.integer_factory(int)
67
+ exists = !int.nil? && !int.empty?
68
+ value = (exists)? int+1 : nil
69
+ [exists,value]
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thomaspeklak-filefinder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Peklak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: thomas.peklak@gmail.com
18
+ executables:
19
+ - filefinder
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - bin/filefinder
26
+ - lib/FileFinder.rb
27
+ - README
28
+ has_rdoc: true
29
+ homepage:
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --main
33
+ - README
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: Finds files in directory and filters files through filters
55
+ test_files: []
56
+