hungrynoodle 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +11 -0
  2. data/hungrynoodle.rb +111 -0
  3. metadata +99 -0
data/README.rdoc ADDED
@@ -0,0 +1,11 @@
1
+ This class provides a simple mechanism to find and cache a list
2
+ of files on your system (much like slocate). It expects only 3 parameters:
3
+ * db_name - The name of the sqlite database to create/use
4
+ * logfile - The name of the logfile to use
5
+ * dir - The directory to be searched/cached
6
+ * regex - The regular expression detailing the files to be cached
7
+ The class uses sqlite3 by default for storage of its information and logs
8
+ debug information to hungrynoodle.txt
9
+ Author:: Philip Mcmahon (mailto:philip@packetnode.com)
10
+ Copyright:: Copyright (c) 2011 Packetnode, LLC
11
+ License:: Distributes under the same terms as Ruby
data/hungrynoodle.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'rubygems'
2
+ require 'find'
3
+ require 'sqlite3'
4
+ require 'fileutils'
5
+ require 'logger'
6
+
7
+ # This class provides a simple mechanism to find and cache a list
8
+ # of files on your system (much like slocate). It expects only 3 parameters:
9
+ # * db_name - The name of the sqlite database to create/use
10
+ # * logfile - The name of the logfile to use
11
+ # * dir - The directory to be searched/cached
12
+ # * regex - The regular expression detailing the files to be cached
13
+ # The class uses sqlite3 by default for storage of its information and logs
14
+ # debug information to hungrynoodle.txt
15
+ # Author:: Philip Mcmahon (mailto:philip@packetnode.com)
16
+ # Copyright:: Copyright (c) 2011 Packetnode, LLC
17
+ # License:: Distributes under the same terms as Ruby
18
+
19
+ class HungryNoodle
20
+ include ObjectSpace
21
+
22
+ # Initialize.
23
+ # * db_name - The name of the sqlite database to create/use
24
+ # * logfile - The name of the logfile to use
25
+ # * dir - The directory to be searched/cached
26
+ # * regex - The regular expression detailing the files to be cached
27
+ def initialize(dir = ".",
28
+ regex = "\.*$",
29
+ db_name = "/tmp/hungrynoodle.db",
30
+ logfile = "/tmp/hungrynoodle.txt"
31
+ )
32
+ @dirs = []
33
+ @log = Logger.new(logfile)
34
+ @db = SQLite3::Database.open(db_name)
35
+ @db.execute("create table if not exists files(
36
+ id integer primary key autoincrement,
37
+ basename varchar,
38
+ atime datetime,
39
+ expand_path varchar,
40
+ ctime datetime,
41
+ dirname varchar,
42
+ extname varchar,
43
+ mtime datetime,
44
+ size integer)")
45
+ @dirs << dir
46
+ @regex = Regexp.compile(regex)
47
+ define_finalizer(self, proc { @db.close; @log.close })
48
+ end
49
+
50
+ # Insert a file into the sqlite cache. Files are tested for their
51
+ # existence in the cache first via the exists? method
52
+ def insert(file)
53
+ if exists?(file)
54
+ @log.debug "#{file} already exists in cache"
55
+ else
56
+ @log.debug "Inserting #{file} into cache"
57
+ @db.execute("insert into files (basename,
58
+ atime,
59
+ expand_path,
60
+ ctime,
61
+ dirname,
62
+ extname,
63
+ mtime,
64
+ size) values
65
+ ('#{File.basename(file).sub("'", "''")}',
66
+ '#{File.atime(file)}',
67
+ '#{File.expand_path(file).sub("'", "''")}',
68
+ '#{File.ctime(file)}',
69
+ '#{File.dirname(file).sub("'", "''")}',
70
+ '#{File.extname(file)}',
71
+ '#{File.mtime(file)}',
72
+ '#{File.size(file)}')")
73
+ end
74
+ end
75
+
76
+ # Does this file exists in the cache ? This method performs a lookup
77
+ # based on filename and size. If a matching file is found, the contents
78
+ # are compared to check if the files are identical.
79
+ def exists?(file)
80
+ @log.debug("Processing file: #{file}")
81
+ @records = @db.execute("select * from files where basename =
82
+ '#{File.basename(file).sub("'", "''")}' and size = '#{File.size(file)}'")
83
+ @records.each do |record|
84
+ return true if FileUtils.compare_file(file, record[3])
85
+ end
86
+ false
87
+ end
88
+
89
+ # Find all files in the configured (see hungrynoodle.xml) directory
90
+ # and regex. All matching files are inserted into the cache unless
91
+ # they already exist.
92
+ def find_and_insert
93
+ excludes = []
94
+ for dir in @dirs
95
+ Find.find(dir) do |path|
96
+ if FileTest.directory?(path)
97
+ if excludes.include?(File.basename(path))
98
+ Find.prune
99
+ else
100
+ next
101
+ end
102
+ else
103
+ if path =~ @regex
104
+ insert(path)
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hungrynoodle
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Philip Mcmahon
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-21 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: sqlite3
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 29
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 3
34
+ version: 1.3.3
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: sqlite3-ruby
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 29
46
+ segments:
47
+ - 1
48
+ - 3
49
+ - 3
50
+ version: 1.3.3
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description:
54
+ email: philip@packetnode.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - hungrynoodle.rb
63
+ - README.rdoc
64
+ has_rdoc: true
65
+ homepage: http://github.com/snaggled/hungrynoodle
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - .
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.7
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Library that provides a simple mechanism to find and cache a list of files on your system (much like slocate).
98
+ test_files: []
99
+