Module: Pione::Util::PackageDigest

Defined in:
lib/pione/util/digest.rb

Class Method Summary collapse

Class Method Details

.generate(location) ⇒ Object

Generate a MD5 digest of the PPG archive.



22
23
24
25
26
27
28
29
30
31
# File 'lib/pione/util/digest.rb', line 22

def generate(location)
  case Package::PackageTypeClassifier.classify(location)
  when :archive
    generate_from_ppg(location)
  when :directory
    generate_from_directory_package(location)
  else
    nil
  end
end

.generate_from_directory_package(location) ⇒ Object

Generate a MD5 digest of the directory package.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pione/util/digest.rb', line 34

def generate_from_directory_package(location)
  files = []

  # package files
  package_info = Package::PackageScanner.scan(location)
  files << "pione-package.json"
  files += package_info.filepaths

  # scenario files
  scenario_infos = package_info.scenarios.map do |scenario|
    files << File.join(scenario, "pione-scenario.json")
    files += Package::ScenarioScanner.scan(location + scenario).filepaths.map do |path|
      File.join(scenario, path)
    end
  end

  # make seed string for digest
  seed = files.sort.each_with_object("") do |filepath, string|
    digest = Digest::MD5.file((location + filepath).path).to_s
    string << "%s %s\n" % [filepath, digest]
  end

  return Digest::MD5.hexdigest(seed)
end

.generate_from_ppg(location) ⇒ Object

Generate a MD5 digest of the PPG archive.



60
61
62
63
64
65
66
67
# File 'lib/pione/util/digest.rb', line 60

def generate_from_ppg(location)
  local_location = location.local
  tmp = Temppath.create
  ::Zip::File.open(local_location.path.to_s) do |zip|
    zip.extract(".digest", tmp)
  end
  Location[tmp].read
end