class Pione::Package::PackageArchiver

PackageArchiver makes PPG file as PIONE archive.

Attributes

location[R]

Public Class Methods

new(location) click to toggle source
# File lib/pione/package/package-archiver.rb, line 7
def initialize(location)
  unless location.scheme == "local"
    raise Location::NotLocal.new(location)
  end
  @location = location
  @package_info = PackageInfo.read((location + "pione-package.json").read)
end

Public Instance Methods

archive(output_directory_location, show_digest) click to toggle source

Create a package archive file.

@param output_directory_location [DataLocation]

the location of output directory

@param show_directory [Boolean]

flag for appending digest to filename
# File lib/pione/package/package-archiver.rb, line 21
def archive(output_directory_location, show_digest)
  path = Temppath.create
  info = PackageInfo.read((@location + "pione-package.json").read)

  # make digest
  digest = Util::PackageDigest.generate(@location)

  # archive
  Zip::File.open(path.to_s, Zip::File::CREATE) do |zip|
    archive_package_info(zip)
    archive_documents(zip, info)
    archive_scenarios(zip)
    archive_bins(zip, info)
    archive_etcs(zip, info)
    archive_digest(zip, digest)
  end

  # make output location
  output_location = output_directory_location + filename(show_digest ? digest : nil)

  # copy the archive file to output location
  Location[path].copy(output_location)

  return output_location
end

Private Instance Methods

add_file_with_time(zip, path, orig_location) click to toggle source
# File lib/pione/package/package-archiver.rb, line 152
def add_file_with_time(zip, path, orig_location)
  entry = zip.add(path, orig_location.path.to_s)
  entry.time = Zip::DOSTime.at(orig_location.mtime)
  entry.extra.delete("UniversalTime")
end
archive_bins(zip, info) click to toggle source
# File lib/pione/package/package-archiver.rb, line 127
def archive_bins(zip, info)
  info.bins.each do |bin|
    add_file_with_time(zip, bin, @location + bin)
  end
end
archive_digest(zip, digest) click to toggle source
# File lib/pione/package/package-archiver.rb, line 139
def archive_digest(zip, digest)
  digest_location = Location[Temppath.create]
  digest_location.write(digest)
  add_file_with_time(zip, ".digest", digest_location)
end
archive_documents(zip, info) click to toggle source

Archive documents based on package info file.

# File lib/pione/package/package-archiver.rb, line 65
def archive_documents(zip, info)
  info.documents.each do |document|
    add_file_with_time(zip, document, @location + document)
  end
end
archive_etcs(zip, info) click to toggle source
# File lib/pione/package/package-archiver.rb, line 133
def archive_etcs(zip, info)
  info.etcs.each do |file|
    add_file_with_time(zip, file, @location + file)
  end
end
archive_package_info(zip) click to toggle source

Archive package info file.

# File lib/pione/package/package-archiver.rb, line 60
def archive_package_info(zip)
  add_file_with_time(zip, "pione-package.json", @location + "pione-package.json")
end
archive_scenario_document(zip, scenario) click to toggle source

Archive scenario document file.

# File lib/pione/package/package-archiver.rb, line 89
def archive_scenario_document(zip, scenario)
  document_location = @location + scenario + "Scenario.pione"
  add_file_with_time(zip, File.join(scenario, "Scenario.pione"), document_location)
end
archive_scenario_info(zip, scenario) click to toggle source

Archive scenario info file.

# File lib/pione/package/package-archiver.rb, line 95
def archive_scenario_info(zip, scenario)
  info_location = @location + scenario + "pione-scenario.json"
  add_file_with_time(zip, File.join(scenario, "pione-scenario.json"), info_location)
end
archive_scenario_inputs(zip, scenario, info) click to toggle source

Archive input data of the scenario.

# File lib/pione/package/package-archiver.rb, line 101
def archive_scenario_inputs(zip, scenario, info)
  if not(info.inputs.empty?)
    # make scenario input directory
    mkdir_with_time(zip, File.join(scenario, "input"), (@location + scenario + "input").mtime)

    # archive input data
    info.inputs.each do |input|
      input_location = @location + scenario + input
      add_file_with_time(zip, File.join(scenario, input), input_location)
    end
  end
end
archive_scenario_outputs(zip, scenario, info) click to toggle source

Archive output data of the scenario.

# File lib/pione/package/package-archiver.rb, line 115
def archive_scenario_outputs(zip, scenario, info)
  if not(info.outputs.empty?)
    # make scenario output directory
    mkdir_with_time(zip, File.join(scenario, "output"), (@location + scenario + "output").mtime)

    info.outputs.each do |output|
      output_location = @location + scenario + output
      add_file_with_time(zip, File.join(scenario, output), output_location)
    end
  end
end
archive_scenarios(zip) click to toggle source

Archive scenarios. This method adds scenario directory, scenario info file, inputs, and outputs to the archive based on scenario info file.

# File lib/pione/package/package-archiver.rb, line 73
def archive_scenarios(zip)
  @package_info.scenarios.each do |scenario|
    info = ScenarioInfo.read((@location + scenario + "pione-scenario.json").read)

    # make the scenario directory
    mkdir_with_time(zip, scenario, (@location + scenario).mtime)

    # archive scenario contents
    archive_scenario_document(zip, scenario)
    archive_scenario_info(zip, scenario)
    archive_scenario_inputs(zip, scenario, info)
    archive_scenario_outputs(zip, scenario, info)
  end
end
filename(digest) click to toggle source

Return PPG filename.

# File lib/pione/package/package-archiver.rb, line 50
def filename(digest)
  PackageFilename.new(
    package_name: @package_info.name,
    editor: @package_info.editor,
    tag: @package_info.tag,
    digest: digest
  ).to_s
end
mkdir_with_time(zip, path, time) click to toggle source
# File lib/pione/package/package-archiver.rb, line 145
def mkdir_with_time(zip, path, time)
  zip.mkdir(path)
  entry = zip.get_entry(path)
  entry.time = Zip::DOSTime.at(time)
  entry.extra.delete("UniversalTime")
end