diff options
| author | mo khan <mo.khan@gmail.com> | 2020-11-14 22:03:47 -0700 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-11-14 22:03:47 -0700 |
| commit | 786b5402260a007df6785ca1a4083015f6f212cc (patch) | |
| tree | d1fcbe51fe0fd25088860bb332c9d3708d89baa2 /lib | |
| parent | f0adffb43fdc5be5298767373f716b216ff2f4e9 (diff) | |
feat: parse dpkg catalogue
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/spandx/os/parsers/dpkg.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/spandx/os/parsers/dpkg.rb b/lib/spandx/os/parsers/dpkg.rb new file mode 100644 index 0000000..c2c4637 --- /dev/null +++ b/lib/spandx/os/parsers/dpkg.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +module Spandx + module Os + module Parsers + class Dpkg < ::Spandx::Core::Parser + def match?(path) + path.basename.fnmatch?('status') + end + + def parse(lockfile) + path = lockfile.to_s + + [].tap do |items| + lockfile.open(mode: 'r') do |io| + each_package(io) do |data| + items.push(map_from(data, path)) + end + end + end + end + + private + + def each_package(io) + package = {} + + until io.eof? + line = io.readline.chomp + next if line.start_with?(" ") + + if line.empty? + yield package + + package = {} + else + line.split(':').tap { |(key, *value)| package[key] = value&.join(':')&.strip } + end + end + end + + def map_from(data, path) + ::Spandx::Core::Dependency.new( + path: path, + name: data['Package'], + version: data['Version'], + meta: data + ) + end + end + end + end +end |
