Sha256: 62fcb6ea8a2f8d3fddccf9957e438535e6aa9919b862964bc19fa1d11257955d

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Spandx
  module Python
    class Index
      include Enumerable

      attr_reader :directory, :name, :pypi, :source

      def initialize(directory:)
        @directory = directory
        @name = 'pypi'
        @source = 'https://pypi.org'
        @pypi = Pypi.new
        Thread.abort_on_exception = true
      end

      def update!(*)
        queue = Queue.new
        [fetch(queue), save(queue)].each(&:join)
      end

      private

      def files(pattern)
        Dir.glob(pattern, base: directory).sort.each do |file|
          fullpath = File.join(directory, file)
          yield fullpath unless File.directory?(fullpath)
        end
      end

      def sort_index!
        files('**/pypi') do |path|
          IO.write(path, IO.readlines(path).sort.join)
        end
      end

      def fetch(queue)
        Thread.new do
          pypi.each do |dependency|
            queue.enq(dependency)
          end
          queue.enq(:stop)
        end
      end

      def save(queue)
        Thread.new do
          loop do
            item = queue.deq
            break if item == :stop

            insert!(item[:name], item[:version], item[:license])
          end
        end
      end

      def digest_for(components)
        Digest::SHA1.hexdigest(Array(components).join('/'))
      end

      def data_dir_for(name)
        File.join(directory, digest_for(name)[0...2].downcase)
      end

      def data_file_for(name)
        File.join(data_dir_for(name), 'pypi')
      end

      def insert!(name, version, license)
        return if license.nil? || license.empty?

        csv = CSV.generate_line([name, version, license], force_quotes: true)
        IO.write(data_file_for(name), csv, mode: 'a')
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spandx-0.12.3 lib/spandx/python/index.rb
spandx-0.12.2 lib/spandx/python/index.rb
spandx-0.12.1 lib/spandx/python/index.rb
spandx-0.12.0 lib/spandx/python/index.rb