Class: Inspec::Resources::CranPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/cran.rb

Instance Method Summary collapse

Constructor Details

#initialize(package_name) ⇒ CranPackage

Returns a new instance of CranPackage.



20
21
22
23
24
25
26
27
# File 'lib/inspec/resources/cran.rb', line 20

def initialize(package_name)
  @package_name = package_name
  @r_cmd = "Rscript"

  # this resource is not supported on Windows
  return skip_resource "The `cran` resource is not supported on your OS yet." if inspec.os.windows?
  return skip_resource "Rscript not found" unless inspec.command(@r_cmd).exist?
end

Instance Method Details

#infoObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/inspec/resources/cran.rb', line 29

def info
  return @info if defined?(@info)

  @info = {}
  @info[:type] = "cran"
  @info[:name] = @package_name
  cmd = inspec.command("#{@r_cmd} -e 'packageVersion(\"#{@package_name}\")'")
  return @info unless cmd.exit_status == 0

  # Extract package version from Rscript output
  # Output includes unicode punctuation (backticks) characters like so:
  # [1] '0.5.1'
  #
  # So make sure command output is converted to unicode, as it returns ASCII-8BIT by default
  utf8_stdout = cmd.stdout.chomp.force_encoding(Encoding::UTF_8)
  params = /^\[\d+\]\s+(?:['\p{Initial_Punctuation}])(.+)(?:['\p{Final_Punctuation}])$/.match(utf8_stdout)
  @info[:installed] = !params.nil?
  return @info unless @info[:installed]

  @info[:version] = params[1]
  @info
end

#installed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/inspec/resources/cran.rb', line 52

def installed?
  info[:installed] == true
end

#resource_idObject



60
61
62
# File 'lib/inspec/resources/cran.rb', line 60

def resource_id
  @package_name || "cran"
end

#to_sObject



64
65
66
# File 'lib/inspec/resources/cran.rb', line 64

def to_s
  "R Module #{@package_name}"
end

#versionObject



56
57
58
# File 'lib/inspec/resources/cran.rb', line 56

def version
  info[:version]
end