summaryrefslogtreecommitdiff
path: root/lib/license/finder/ext
diff options
context:
space:
mode:
Diffstat (limited to 'lib/license/finder/ext')
-rw-r--r--lib/license/finder/ext/pip.rb69
-rw-r--r--lib/license/finder/ext/shared_helpers.rb4
2 files changed, 72 insertions, 1 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
diff --git a/lib/license/finder/ext/shared_helpers.rb b/lib/license/finder/ext/shared_helpers.rb
index bc37b9c..b6b6fcd 100644
--- a/lib/license/finder/ext/shared_helpers.rb
+++ b/lib/license/finder/ext/shared_helpers.rb
@@ -4,8 +4,10 @@ module LicenseFinder
module SharedHelpers
class Cmd
def self.run(command)
+ ::License::Management.logger.debug(command)
stdout, stderr, status = Open3.capture3(command)
- ::License::Management.logger.debug([command, stdout].join('\n'))
+ ::License::Management.logger.debug(stdout) unless stdout.nil? || stdout.empty?
+ ::License::Management.logger.error(stderr) unless stderr.nil? || stderr.empty?
[stdout, stderr, status]
end
end