diff options
Diffstat (limited to 'lib/license/finder/ext/pip.rb')
| -rw-r--r-- | lib/license/finder/ext/pip.rb | 76 |
1 files changed, 57 insertions, 19 deletions
diff --git a/lib/license/finder/ext/pip.rb b/lib/license/finder/ext/pip.rb index 54b7d40..e83f64c 100644 --- a/lib/license/finder/ext/pip.rb +++ b/lib/license/finder/ext/pip.rb @@ -3,8 +3,19 @@ module LicenseFinder class Pip def current_packages - detected_dependencies.map do |name, version| - PipPackage.new(name, version, pypi.definition_for(name, version)) + return legacy_results unless virtual_env? + + _stdout, _stderr, status = pip_licenses + return legacy_results unless status.success? + + JSON.parse(IO.read('pip-licenses.json')).map do |dependency| + Package.new( + dependency['Name'], + dependency['Version'], + description: dependency['Description'], + homepage: dependency['URL'], + spec_licenses: [dependency['License']] + ) end end @@ -27,35 +38,49 @@ module LicenseFinder private - def detected_dependencies - stdout, _stderr, status = execute([ - python_executable, - LicenseFinder::BIN_PATH.join('license_finder_pip.py'), - detected_package_path - ]) - return [] unless status.success? - - JSON.parse(stdout).map { |package| package.values_at('name', 'version') } - end - def install_packages - execute([prepare_command, "-i", pip_index_url, "-r", @requirements_path]) + within_project_dir do + shell.execute(['virtualenv -p', python_executable, '--activators=bash --seeder=app-data venv']) + shell.sh([". venv/bin/activate", "&&", :pip, :install, '-i', pip_index_url, '-r', @requirements_path]) + end end - def execute(command) - Dir.chdir(project_path) do - ::LicenseFinder::SharedHelpers::Cmd.run(Array(command).join(' ')) - end + def pip_licenses + 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' }) end def python_executable - "python#{@python_version == '2' ? '' : '3'}" + '"$(asdf where python)/bin/python"' end def pip_index_url ENV.fetch('PIP_INDEX_URL', 'https://pypi.org/simple/') end + def virtual_env? + within_project_dir { File.exist?('venv/bin/activate') } + end + + def within_project_dir + Dir.chdir(project_path) { yield } + end + + def shell + @shell ||= ::License::Management::Shell.new + end + def pypi @pypi ||= Spandx::Python::PyPI.new(sources: [ Spandx::Python::Source.new({ @@ -65,5 +90,18 @@ module LicenseFinder }) ]) end + + def legacy_results + pip_output.map do |name, version, children, location| + spec = pypi.definition_for(name, version) + Package.new( + name, + version, + description: spec['description'], + homepage: spec['home_page'], + spec_licenses: PipPackage.license_names_from_spec(spec) + ) + end + end end end |
