# frozen_string_literal: true module License module Management class Python attr_reader :shell def initialize(shell: Shell.new) @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') _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::Package.new( dependency['Name'], dependency['Version'], description: dependency['Description'], homepage: dependency['URL'], spec_licenses: [dependency['License']] ) end end end end end