Class: Fingerprint::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/fingerprint/scanner.rb

Overview

The scanner class can scan a set of directories and produce an index.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roots, options = {}) ⇒ Scanner

Initialize the scanner to scan a given set of directories in order.

options[:excludes]

An array of regular expressions of files to avoid indexing.

options[:output]

An IO where the results will be written.



35
36
37
38
39
40
41
42
# File 'lib/fingerprint/scanner.rb', line 35

def initialize(roots, options = {})
	@roots = roots

	@excludes = options[:excludes] || DEFAULT_EXCLUDES
	@output = options[:output] || StringIO.new
	
	@options = options
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



44
45
46
# File 'lib/fingerprint/scanner.rb', line 44

def output
  @output
end

Class Method Details

.scan_paths(paths, options = {}) ⇒ Object

A helper function to scan a set of directories.



130
131
132
133
134
135
136
# File 'lib/fingerprint/scanner.rb', line 130

def self.scan_paths(paths, options = {})
	scanner = Scanner.new(paths, options)
	
	scanner.scan
	
	return scanner
end

Instance Method Details

#excluded?(path) ⇒ Boolean

Returns true if the given path should be excluded.

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
# File 'lib/fingerprint/scanner.rb', line 83

def excluded?(path)
	@excludes.each do |exclusion|
		if exclusion.match(path)
			return true
		end
	end

	return false
end

#scanObject

Run the scanning process.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fingerprint/scanner.rb', line 94

def scan
	excluded_count = 0
	checksummed_count = 0
	directory_count = 0
	
	@roots.each do |root|
		Dir.chdir(root) do
			output_header(root)
			Find.find("./") do |path|
				if File.directory?(path)
					if excluded?(path)
						excluded_count += 1
						output_excluded(path)
						Find.prune # Ignore this directory
					else
						directory_count += 1
						output_dir(path)
					end
				else
					unless excluded?(path)
						checksummed_count += 1
						output_file(path)
					else
						excluded_count += 1
						output_excluded(path)
					end
				end
			end
		end
	end
	
	# Output summary
	@output.puts "\# Directories: #{directory_count} Files: #{checksummed_count} Excluded: #{excluded_count}"
end