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, pwd: Dir.pwd, **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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fingerprint/scanner.rb', line 47

def initialize(roots, pwd: Dir.pwd, **options)
	@roots = roots.collect{|root| File.expand_path(root, pwd)}

	@excludes = options[:excludes] || []
	@options = options

	@digests = {}

	@progress = nil

	unless @options[:checksums] and @options[:checksums].size > 0
		@options[:checksums] = DEFAULT_CHECKSUMS
	end

	@options[:checksums].each do |name|
		@digests[name] = CHECKSUMS[name].call
	end

	@callback = nil
end

Instance Attribute Details

#digestsObject (readonly)

Returns the value of attribute digests.



69
70
71
# File 'lib/fingerprint/scanner.rb', line 69

def digests
  @digests
end

#recordsetObject (readonly)

Returns the value of attribute recordset.



68
69
70
# File 'lib/fingerprint/scanner.rb', line 68

def recordset
  @recordset
end

Class Method Details

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

A helper function to scan a set of directories.



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/fingerprint/scanner.rb', line 326

def self.scan_paths(paths, options = {})
	if options[:output]
		if options.key? :recordset
			recordset = options[:recordset]
		else
			recordset = RecordSet.new
		end
		
		options[:recordset] = RecordSetPrinter.new(recordset, options[:output])
	end

	scanner = Scanner.new(paths, options)

	scanner.scan(options[:recordset])

	return options[:recordset]
end

Instance Method Details

#excluded?(path) ⇒ Boolean

Returns true if the given path should be excluded.

Returns:

  • (Boolean)


205
206
207
208
209
210
211
212
213
# File 'lib/fingerprint/scanner.rb', line 205

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

	return false
end

#scan(recordset) ⇒ Object

Run the scanning process.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/fingerprint/scanner.rb', line 228

def scan(recordset)
	excluded_count = 0
	processed_count = 0
	processed_size = 0
	directory_count = 0

	total_count = 0
	total_size = 0

	# Estimate the number of files and amount of data to process..
	if @options[:progress]
		@roots.each do |root|
			Find.find(root) do |path|
				# Some special files fail here, and this was the simplest fix.
				Find.prune unless File.exist?(path)
				
				if @options[:progress]
					$stderr.puts "# Scanning: #{path}"
				end
				
				if excluded?(path)
					Find.prune if path.directory?
				elsif path.symlink?
					total_count += 1
				elsif path.file?
					total_count += 1
					total_size += File.size(path)
				end
			end
		end
	end
	
	if @options[:progress]
		@progress = lambda do |read_size|
			$stderr.puts "# Progress: File #{processed_count} / #{total_count}; Byte #{processed_size + read_size} / #{total_size} = #{sprintf('%0.3f%', (processed_size + read_size).to_f / total_size.to_f * 100.0)} (#{read_size}, #{processed_size}, #{total_size})"
		end
	end
	
	@roots.each do |root|
		recordset << header_for(root)
		
		Find.find(root) do |path|
			# Some special files fail here, and this was the simplest fix.
			Find.prune unless File.exist?(path)
			
			if @options[:progress]
				$stderr.puts "# Path: #{path.relative_path}"
			end
			
			if excluded?(path)
				excluded_count += 1
				
				if @options[:verbose]
					recordset << excluded_record_for(path)
				end
				
				Find.prune if path.directory?
			elsif path.directory?
				directory_count += 1
				
				recordset << directory_record_for(path)
			elsif path.symlink?
				recordset << link_record_for(path)
				
				processed_count += 1
			elsif path.file?
				recordset << file_record_for(path)

				processed_count += 1
				processed_size += File.size(path)
			else
				excluded_count += 1
				
				if @options[:verbose]
					recordset << excluded_record_for(path)
				end
			end
			
			# Print out a progress summary if requested
			@progress.call(0) if @progress
		end
	end
	
	summary_message = "#{processed_count} files processed."

	# Output summary
	recordset << Record.new(:summary, summary_message, {
		'summary.directories' => directory_count,
		'summary.files' => processed_count,
		'summary.size' => processed_size,
		'summary.excluded' => excluded_count,
		'summary.time.end' => Time.now
	})
	
	return recordset
end

#scan_path(path) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/fingerprint/scanner.rb', line 215

def scan_path(path)
	return nil if excluded?(path)
	
	@roots.each do |root|
		full_path = Build::Files::Path.join(root, path)
		
		return record_for(full_path)
	end
	
	return nil
end