Sha256: d1775ac1f62c6b5a43006bbe87c9c1070528b7711f820774874fd1e9c9a0ff81

Contents?: true

Size: 1.85 KB

Versions: 21

Compression:

Stored size: 1.85 KB

Contents

require "json"

module Bibliothecary
  module Parsers
    class Conda
      include Bibliothecary::Analyser

      def self.mapping
        {
          match_filename("environment.yml") => {
            parser: :parse_conda,
            kind: "manifest",
          },
          match_filename("environment.yaml") => {
            parser: :parse_conda,
            kind: "manifest",
          },
          match_filename("environment.yml.lock") => {
            parser: :parse_conda_lockfile,
            kind: "lockfile",
          },
          match_filename("environment.yaml.lock") => {
            parser: :parse_conda_lockfile,
            kind: "lockfile",
          },
        }
      end

      def self.parse_conda(info)
        dependencies = call_conda_parser_web(info, "manifest")[:manifest]
        dependencies.map { |dep| dep.merge(type: "runtime") }
      end

      def self.parse_conda_lockfile(info)
        dependencies = call_conda_parser_web(info, "lockfile")[:lockfile]
        dependencies.map { |dep| dep.merge(type: "runtime") }
      end

      private_class_method def self.call_conda_parser_web(file_contents, kind)
        host = Bibliothecary.configuration.conda_parser_host
        response = Typhoeus.post(
          "#{host}/parse",
          headers: {
            ContentType: "multipart/form-data",
          },
          body: {
            file: file_contents,
            # Unfortunately we do not get the filename in the mapping parsers, so hardcoding the file name depending on the kind
            filename: kind == "manifest" ? "environment.yml" : "environment.yml.lock",
          }
        )
        raise Bibliothecary::RemoteParsingError.new("Http Error #{response.response_code} when contacting: #{host}/parse", response.response_code) unless response.success?

        JSON.parse(response.body, symbolize_names: true)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
bibliothecary-6.10.7 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.10.6 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.10.5 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.10.4 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.10.3 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.10.2 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.10.1 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.8 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.7 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.6 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.5 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.4 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.3 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.2 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.1 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.9.0 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.8.10 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.8.9 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.8.8 lib/bibliothecary/parsers/conda.rb
bibliothecary-6.8.7 lib/bibliothecary/parsers/conda.rb