Class: Lono::Md5

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/lono/md5.rb

Class Method Summary collapse

Class Method Details

.name(path) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/lono/md5.rb', line 41

def name(path)
  relative_path = name_without_md5(path)
  relative_path = relative_path.sub(/\.(\w+)$/,'') # strip extension
  ext = File.directory?(path) ? "zip" : $1
  md5 = sum(path)
  "#{relative_path}-#{md5}.#{ext}"
end

.name_without_md5(path) ⇒ Object



49
50
51
52
# File 'lib/lono/md5.rb', line 49

def name_without_md5(path)
  regexp = %r{.*/output/[\w_-]+/files/}
  path.sub(regexp, '') # relative_path w/o md5
end

.sum(path) ⇒ Object

This checksum within the file name is to mainly make sure that Lambda::Function resources “change” and an update is triggered. There’s another checksum in the upload code that makes sure we don’t upload the code again to speed up things.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lono/md5.rb', line 17

def sum(path)
  content = if File.directory?(path)
    sum_directory(path)
  else
    Digest::MD5.file(path).to_s[0..7]
  end

  md5 = Digest::MD5.new
  md5.update(content)
  md5.hexdigest.to_s[0..7]
end

.sum_directory(dir) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/lono/md5.rb', line 30

def sum_directory(dir)
  files = Dir["#{dir}/**/*"]
  files = files.reject { |f| File.directory?(f) }
               .reject { |f| File.symlink?(f) }
  files.sort!

  files.map do |f|
    Digest::MD5.file(f).to_s[0..7]
  end.join
end