Class: LicenseAcceptance::Strategy::File

Inherits:
Base
  • Object
show all
Includes:
Logger
Defined in:
lib/license_acceptance/strategy/file.rb

Overview

Read and write marker files that show acceptance.

Constant Summary collapse

INVOCATION_TIME =
DateTime.now.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

initialize, logger, #logger

Constructor Details

#initialize(config) ⇒ File

Returns a new instance of File.


17
18
19
# File 'lib/license_acceptance/strategy/file.rb', line 17

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


15
16
17
# File 'lib/license_acceptance/strategy/file.rb', line 15

def config
  @config
end

Instance Method Details

#accepted?(product_relationship) ⇒ Boolean

For all the given products in the product set, search all possible locations for the license acceptance files.

Returns:

  • (Boolean)

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/license_acceptance/strategy/file.rb', line 25

def accepted?(product_relationship)
  searching = [product_relationship.parent] + product_relationship.children
  missing_licenses = searching.clone
  logger.debug("Searching for the following licenses: #{missing_licenses.map(&:id)}")

  searching.each do |product|
    found = false
    config.license_locations.each do |loc|
      f = ::File.join(loc, product.filename)
      if ::File.exist?(f)
        found = true
        logger.debug("Found license #{product.filename} at #{f}")
        missing_licenses.delete(product)
        break
      end
    end
    break if missing_licenses.empty?
  end
  logger.debug("Missing licenses remaining: #{missing_licenses.map(&:id)}")
  missing_licenses
end

#persist(product_relationship, missing_licenses) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/license_acceptance/strategy/file.rb', line 47

def persist(product_relationship, missing_licenses)
  parent = product_relationship.parent
  parent_version = product_relationship.parent_version
  root_dir = config.persist_location

  if !Dir.exist?(root_dir)
    begin
      FileUtils.mkdir_p(root_dir)
    rescue StandardError => e
      msg = "Could not create license directory #{root_dir}"
      logger.info "#{msg}\n\t#{e.message}\n\t#{e.backtrace.join("\n\t")}"
      return [e]
    end
  end

  errs = []
  if missing_licenses.include?(parent)
    err = persist_license(root_dir, parent, parent, parent_version)
    errs << err unless err.nil?
  end
  product_relationship.children.each do |child|
    if missing_licenses.include?(child)
      err = persist_license(root_dir, child, parent, parent_version)
      errs << err unless err.nil?
    end
  end
  return errs
end