summaryrefslogtreecommitdiff
path: root/lib/license/finder/ext/pip.rb
diff options
context:
space:
mode:
authorCan Eldem <celdem@gitlab.com>2020-03-27 16:47:50 +0000
committerCan Eldem <celdem@gitlab.com>2020-03-27 16:47:50 +0000
commitd0ff10b6ae1075a13827e00dd0120fac9639fde8 (patch)
tree74a5154ac105b2df4f27e5d2952b04f6547a078c /lib/license/finder/ext/pip.rb
parent4db9ccdf7a07654e7d546b5a6ab7467cf3818c93 (diff)
parentf601e9bfb512ef21f727313959ff6349490abf17 (diff)
Merge branch '199059-setup-py' into 'master'v3.2.0
Read `PIP_INDEX_URL` to install python packages. See merge request gitlab-org/security-products/license-management!125
Diffstat (limited to 'lib/license/finder/ext/pip.rb')
-rw-r--r--lib/license/finder/ext/pip.rb69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/license/finder/ext/pip.rb b/lib/license/finder/ext/pip.rb
new file mode 100644
index 0000000..54b7d40
--- /dev/null
+++ b/lib/license/finder/ext/pip.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+module LicenseFinder
+ class Pip
+ def current_packages
+ detected_dependencies.map do |name, version|
+ PipPackage.new(name, version, pypi.definition_for(name, version))
+ end
+ end
+
+ def possible_package_paths
+ path = project_path || Pathname.pwd
+
+ [
+ path.join(@requirements_path),
+ path.join('setup.py')
+ ]
+ end
+
+ def prepare
+ return install_packages if detected_package_path == @requirements_path
+
+ requirements_path = detected_package_path.dirname.join('requirements.txt')
+ requirements_path.write('.') unless requirements_path.exist?
+ install_packages
+ end
+
+ 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])
+ end
+
+ def execute(command)
+ Dir.chdir(project_path) do
+ ::LicenseFinder::SharedHelpers::Cmd.run(Array(command).join(' '))
+ end
+ end
+
+ def python_executable
+ "python#{@python_version == '2' ? '' : '3'}"
+ end
+
+ def pip_index_url
+ ENV.fetch('PIP_INDEX_URL', 'https://pypi.org/simple/')
+ end
+
+ def pypi
+ @pypi ||= Spandx::Python::PyPI.new(sources: [
+ Spandx::Python::Source.new({
+ 'name' => 'pypi',
+ 'url' => pip_index_url,
+ 'verify_ssl' => true
+ })
+ ])
+ end
+ end
+end