Class: Checksummer

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

Constant Summary collapse

DEFAULT_SLEEP =
0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dst, options = {}) ⇒ Checksummer

Returns a new instance of Checksummer.



10
11
12
13
14
# File 'lib/checksummer.rb', line 10

def initialize(dst, options = {})
  self.dst = dst
  self.sleep_in_seconds = options[:sleep] || DEFAULT_SLEEP
  self.replace = !!options[:replace]
end

Instance Attribute Details

#dstObject

Returns the value of attribute dst.



8
9
10
# File 'lib/checksummer.rb', line 8

def dst
  @dst
end

#replaceObject

Returns the value of attribute replace.



8
9
10
# File 'lib/checksummer.rb', line 8

def replace
  @replace
end

#sleep_in_secondsObject

Returns the value of attribute sleep_in_seconds.



8
9
10
# File 'lib/checksummer.rb', line 8

def sleep_in_seconds
  @sleep_in_seconds
end

Class Method Details

.checksum_many(lines, checksum_to, sleep = DEFAULT_SLEEP) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/checksummer.rb', line 86

def self.checksum_many(lines, checksum_to, sleep = DEFAULT_SLEEP)
  started_at = Time.now
  total = lines.count
  lines.each_with_index do |line, index|
    extra = { :index => index + 1, :total => lines.count, :started_at => started_at.iso8601 }
    puts ChecksummerFile.from_line(line).checksum_to!(checksum_to, :replace => true).merge(extra).to_json
    $stdout.flush
    sleep sleep if !Range.new(0,7).include?(Time.now.hour)
  end
end

.find(directory, options = nil) ⇒ Object

LEGACY STUFF



52
53
54
# File 'lib/checksummer.rb', line 52

def self.find(directory, options = nil)
  Kernel.send(:`, %(find #{directory} #{options ? "#{options} " : ""}-type f -printf "%p\t%T+\t%s\t%Y\t%l\n")).split("\n")
end

.run_for_args(args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/checksummer.rb', line 56

def self.run_for_args(args)
  if args.include?("--version")
    puts File.read(File.expand_path("../VERSION", File.dirname(__FILE__)))
    return
  end
  directory_or_file, checksum_to = args[0,2]
  sleep = sleep_from_args(args)
  if directory_or_file && checksum_to && File.directory?(checksum_to)
    lines = if directory_or_file == "--stdin"
      $stdin.readlines
    elsif File.directory?(directory_or_file)
      find(directory_or_file, args[2..-1].join(" "))
    else
      [directory_or_file]
    end
    checksum_many(lines, checksum_to, sleep)
    return true
  end
  puts usage
end

.sleep_from_args(args) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/checksummer.rb', line 77

def self.sleep_from_args(args)
  if idx = args.index("--sleep")
    args.delete_at(idx)
    args.delete_at(idx).to_i / 1000.0
  else
    DEFAULT_SLEEP
  end
end

.usageObject



97
98
99
100
101
# File 'lib/checksummer.rb', line 97

def self.usage
  out = ["checksummer <directory_to_checksum> <directory_to_store_checksummed_files> [options]"]
  out << ["OPTIONS", "--sleep <sleep in ms>", "<any find option>"]
  out.join("\n")
end

Instance Method Details

#checksum_stream(stream) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/checksummer.rb', line 16

def checksum_stream(stream)
  default = { :started => started, :total => stream.count }
  stream.each_with_index do |line, i|
    log ChecksummerFile.from_line(line).checksum_to!(dst, :replace => replace).merge(default).merge(:index => i + 1, :time => current_time, 
      :sleep => sleep_now? ? sleep_in_seconds : 0
    )
    sleep_if_necessary
  end
end

#current_timeObject



34
35
36
# File 'lib/checksummer.rb', line 34

def current_time
  Time.now.iso8601
end

#log(hash) ⇒ Object



26
27
28
# File 'lib/checksummer.rb', line 26

def log(hash)
  puts hash.to_json
end

#sleep_if_necessaryObject



42
43
44
# File 'lib/checksummer.rb', line 42

def sleep_if_necessary
  sleep sleep_in_seconds if sleep_now?
end

#sleep_now?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/checksummer.rb', line 46

def sleep_now?
  !Range.new(0, 7).include?(Time.now.hour)
end

#startedObject



30
31
32
# File 'lib/checksummer.rb', line 30

def started
  @started ||= Time.now.iso8601
end