Class: WatchPaths

Inherits:
Object
  • Object
show all
Defined in:
lib/watch-paths.rb

Overview

A poor man’s configuration management tool.

Constant Summary collapse

VERSION =
'1.2.0'
MANIFEST =

File used to record checksums in each path scanned

'.chksum_manifest.txt'

Instance Method Summary collapse

Instance Method Details

#check(chksum_manifest) ⇒ Object

Check old manifest against the current state.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/watch-paths.rb', line 71

def check( chksum_manifest )
  new_chksum_manifest = create_chksum_manifest
  changed = []
  chksum_manifest.each do |file,chksum|
    changed << file  if chksum != new_chksum_manifest[file]
  end
  if changed.empty?
    chksum_manifest = new_chksum_manifest
  else
    raise ChecksumError, changed
  end
end

#create_chksum_manifestObject

Create a checksum manifest of files in current directory



59
60
61
62
63
64
65
66
# File 'lib/watch-paths.rb', line 59

def create_chksum_manifest
  chksum_manifest = {}
  files = Dir['*'].select{ |f| File.file? f }
  files.each do |file|
    chksum_manifest[file] = `md5sum #{file}`.split.first
  end
  chksum_manifest
end

#dump(chksum_manifest) ⇒ Object

Write YAML file containing checksum manifest



52
53
54
# File 'lib/watch-paths.rb', line 52

def dump( chksum_manifest )
  File.open(MANIFEST,'w') { |f| YAML.dump chksum_manifest, f }
end

#loadObject

Attempts to load checksum manifest YAML file, otherwise returns an empty hash.



45
46
47
# File 'lib/watch-paths.rb', line 45

def load
  YAML.load_file MANIFEST rescue {}
end

#watch(paths) ⇒ Object

compares file checksums for a set of directory paths



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/watch-paths.rb', line 23

def watch( paths )
  chksum_errors = {}
  paths.map!{ |path| Dir["#{path}/**/*"] }.flatten! if $r
  dirs = paths.select{ |path| File.directory? path }
  dirs.each do |dir|
    cd dir do
      begin
        chksum_manifest = load
        chksum_manifest = check chksum_manifest
        dump chksum_manifest
      rescue ChecksumError => error
        chksum_errors[dir] = error.message
      end
    end
  end
  chksum_errors
end