Skip to content

Commit

Permalink
Process Pipfile.lock file.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlgmokha committed Jan 9, 2020
1 parent f24a563 commit 566fb39
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/license_finder/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def log_to_file(contents)
require 'license_finder/package_managers/npm'
require 'license_finder/package_managers/yarn'
require 'license_finder/package_managers/pip'
require 'license_finder/package_managers/pipenv'
require 'license_finder/package_managers/maven'
require 'license_finder/package_managers/mix'
require 'license_finder/package_managers/cocoa_pods'
Expand Down
41 changes: 41 additions & 0 deletions lib/license_finder/package_managers/pipenv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'json'

module LicenseFinder
class Pipenv < PackageManager
def initialize(options = {})
super
@lockfile = Pathname('Pipfile.lock')
end

def current_packages
content = IO.read(detected_package_path)
dependencies = JSON.parse(content)
dependencies['default'].map do |name, value|
version = value['version'].sub(/^==/, '')
PipPackage.new(name, version, pypi_def(name, version))
end
end

def possible_package_paths
project_path ? [project_path.join(@lockfile)] : [@lockfile]
end

private

def pypi_def(name, version)
response = pypi_request("https://pypi.org/pypi/#{name}/#{version}/json")
response.is_a?(Net::HTTPSuccess) ? JSON.parse(response.body).fetch('info', {}) : {}
end

def pypi_request(location, limit = 10)
uri = URI(location)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.get(uri.request_uri).response

response.is_a?(Net::HTTPRedirection) && limit.positive? ? pypi_request(response['location'], limit - 1) : response
end
end
end
6 changes: 4 additions & 2 deletions lib/license_finder/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

module LicenseFinder
class Scanner
PACKAGE_MANAGERS = [GoModules, GoDep, GoWorkspace, Go15VendorExperiment, Glide, Gvt, Govendor, Trash, Dep, Bundler, NPM, Pip,
Yarn, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget, Carthage, Mix, Conan, Sbt, Cargo, Dotnet, Composer].freeze
PACKAGE_MANAGERS = [
GoModules, GoDep, GoWorkspace, Go15VendorExperiment, Glide, Gvt, Govendor, Trash, Dep, Bundler, NPM, Pip,
Yarn, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget, Carthage, Mix, Conan, Sbt, Cargo, Dotnet, Composer, Pipenv
].freeze

class << self
def remove_subprojects(paths)
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions spec/fixtures/pipenv-with-lockfile/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
six = "*"

[requires]
python_version = "3.8"
29 changes: 29 additions & 0 deletions spec/fixtures/pipenv-with-lockfile/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions spec/lib/license_finder/package_managers/pipenv_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require 'spec_helper'
require 'fakefs/spec_helpers'

module LicenseFinder
describe Pipenv do
let(:root) { fixture_path('pipenv-with-lockfile') }
let(:pipenv) { Pipenv.new(project_path: root) }
it_behaves_like 'a PackageManager'

describe '#current_packages' do
let(:response_body) do
<<~RAW
{
"info": {
"author": "Benjamin Peterson",
"classifiers": [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Topic :: Software Development :: Libraries",
"Topic :: Utilities"
],
"home_page": "https://github.com/benjaminp/six",
"license": "MIT",
"name": "six",
"summary": "Python 2 and 3 compatibility utilities",
"version": "1.13.0"
}
}
RAW
end

before do
stub_request(:get, "https://pypi.org/pypi/six/1.13.0/json")
.to_return(status: 200, body: response_body)
end

it 'fetches data for pipenv' do
results = pipenv.current_packages.map do |package|
[package.name, package.version, package.licenses.map { |x| x.send(:short_name) }]
end
expect(results).to match_array([ ['six', '1.13.0', ['MIT']] ])
end
end
end
end

0 comments on commit 566fb39

Please sign in to comment.