summaryrefslogtreecommitdiff
path: root/lib/license/management/python.rb
blob: ccf53e983312cfadbeae5aec4c8b520765cb4acc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

module License
  module Management
    class Python
      attr_reader :shell

      def initialize(shell: ::License::Management.shell)
        @shell = shell
      end

      def major_version
        version.split('.')[0]
      end

      def version
        ENV.fetch('ASDF_PYTHON_VERSION') do
          _stdout, stderr, status = shell.execute([:python, '--version'])
          status.success? ? stderr.split(' ')[-1] : 3
        end
      end

      def pip_index_url
        ENV.fetch('PIP_INDEX_URL', 'https://pypi.org/simple/')
      end

      def pip_licenses(venv: '.venv', detection_path:)
        _stdout, _stderr, status = shell.sh([
          ". #{venv}/bin/activate &&",
          :pip, :install,
          '--no-index',
          '--find-links $HOME/.config/virtualenv/app-data', 'pip-licenses', '&&',
          'pip-licenses',
          '--ignore-packages prettytable',
          '--with-description',
          '--with-urls',
          '--from=meta',
          '--format=json',
          '--output-file pip-licenses.json'
        ], env: { 'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' })
        return [] unless status.success?

        JSON.parse(IO.read('pip-licenses.json')).map do |dependency|
          ::LicenseFinder::Dependency.new(
            'Pip',
            dependency['Name'],
            dependency['Version'],
            description: dependency['Description'],
            detection_path: detection_path,
            homepage: dependency['URL'],
            spec_licenses: [dependency['License']]
          )
        end
      end

      def default_env
        return {} unless shell.custom_certificate_installed?

        { 'PIP_CERT' => ENV.fetch('PIP_CERT', shell.custom_certificate_path.to_s) }
      end
    end
  end
end