Module: Bibliothecary::MultiParsers::Spdx

Includes:
Analyser, Analyser::TryCache
Defined in:
lib/bibliothecary/multi_parsers/spdx.rb

Constant Summary collapse

WELLFORMED_LINE_REGEXP =

e.g. ‘SomeText:’ (allowing for leading whitespace)

/^\s*[a-zA-Z]+:/
PACKAGE_NAME_REGEXP =

e.g. ‘PackageName: (allowing for excessive whitespace)

/^\s*PackageName:\s*(.*)/
PACKAGE_VERSION_REGEXP =

e.g. ‘PackageVersion:’ (allowing for excessive whitespace)

/^\s*PackageVersion:\s*(.*)/
PURL_REGEXP =

e.g. “ExternalRef: PACKAGE-MANAGER purl (allowing for excessive whitespace)

/^\s*ExternalRef:\s*PACKAGE[-|_]MANAGER\s*purl\s*(.*)/
NoEntries =
Class.new(StandardError)
MalformedFile =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Analyser::TryCache

#try_cache

Methods included from Analyser

create_analysis, create_error_analysis, included

Class Method Details

.mappingObject



29
30
31
32
33
34
35
36
37
# File 'lib/bibliothecary/multi_parsers/spdx.rb', line 29

def self.mapping
  {
    match_extension(".spdx") => {
      kind: "lockfile",
      parser: :parse_spdx_tag_value,
      ungroupable: true,
    },
  }
end

Instance Method Details

#get_platform(purl_string) ⇒ Object



49
50
51
52
53
# File 'lib/bibliothecary/multi_parsers/spdx.rb', line 49

def get_platform(purl_string)
  platform = PackageURL.parse(purl_string).type

  Bibliothecary::PURL_TYPE_MAPPING[platform]
end

#parse_spdx_tag_value(file_contents, options: {}) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/bibliothecary/multi_parsers/spdx.rb', line 39

def parse_spdx_tag_value(file_contents, options: {})
  entries = try_cache(options, options[:filename]) do
    parse_spdx_tag_value_file_contents(file_contents)
  end

  raise NoEntries if entries.empty?

  entries[platform_name.to_sym]
end

#parse_spdx_tag_value_file_contents(file_contents) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bibliothecary/multi_parsers/spdx.rb', line 55

def parse_spdx_tag_value_file_contents(file_contents)
  entries = {}

  package_name = nil
  package_version = nil
  platform = nil

  file_contents.split("\n").each do |line|
    stripped_line = line.strip

    next if skip_line?(stripped_line)

    raise MalformedFile unless stripped_line.match(WELLFORMED_LINE_REGEXP)

    if (match = stripped_line.match(PACKAGE_NAME_REGEXP))
      package_name = match[1]
    elsif (match = stripped_line.match(PACKAGE_VERSION_REGEXP))
      package_version = match[1]
    elsif (match = stripped_line.match(PURL_REGEXP))
      platform ||= get_platform(match[1])
    end

    unless package_name.nil? || package_version.nil? || platform.nil?
      entries[platform.to_sym] ||= []
      entries[platform.to_sym] << {
        name: package_name,
        requirement: package_version,
        type: "lockfile",
      }

      package_name = package_version = platform = nil
    end
  end

  entries
end

#skip_line?(stripped_line) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/bibliothecary/multi_parsers/spdx.rb', line 92

def skip_line?(stripped_line)
  # Ignore blank lines and comments
  stripped_line == "" || stripped_line[0] == "#"
end