license_finder 0.9.4-java → 0.9.5-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -7
  3. data/.travis.yml +1 -3
  4. data/CHANGELOG.rdoc +13 -0
  5. data/db/migrate/201307251004_data_fix_manual_licenses.rb +2 -2
  6. data/db/migrate/201307251107_reassociate_license.rb +18 -18
  7. data/db/migrate/201311192002_add_manually_approved_to_dependencies.rb +7 -0
  8. data/db/migrate/201311192003_reassociate_manual_approval.rb +14 -0
  9. data/db/migrate/201311192010_drop_approvals.rb +5 -0
  10. data/features/cli.feature +1 -1
  11. data/features/html_report.feature +1 -1
  12. data/features/{non_bundler_dependencies.feature → manually_managed_dependencies.feature} +6 -6
  13. data/features/step_definitions/html_report_steps.rb +2 -9
  14. data/features/step_definitions/{non_bundler_steps.rb → manually_managed_steps.rb} +0 -0
  15. data/features/step_definitions/shared_steps.rb +4 -8
  16. data/lib/license_finder.rb +21 -17
  17. data/lib/license_finder/bower.rb +3 -34
  18. data/lib/license_finder/bower_package.rb +63 -0
  19. data/lib/license_finder/bundler.rb +73 -0
  20. data/lib/license_finder/bundler_package.rb +33 -0
  21. data/lib/license_finder/cli.rb +33 -35
  22. data/lib/license_finder/dependency_manager.rb +14 -23
  23. data/lib/license_finder/license/apache2.rb +1 -1
  24. data/lib/license_finder/license/lgpl.rb +1 -0
  25. data/lib/license_finder/npm.rb +22 -39
  26. data/lib/license_finder/npm_package.rb +61 -0
  27. data/lib/license_finder/package.rb +14 -80
  28. data/lib/license_finder/package_saver.rb +13 -75
  29. data/lib/license_finder/pip.rb +21 -33
  30. data/lib/license_finder/pip_package.rb +51 -0
  31. data/lib/license_finder/platform.rb +3 -15
  32. data/lib/license_finder/possible_license_file.rb +0 -4
  33. data/lib/license_finder/possible_license_files.rb +4 -0
  34. data/lib/license_finder/tables.rb +2 -2
  35. data/lib/license_finder/tables/bundler_group.rb +3 -0
  36. data/lib/license_finder/tables/dependency.rb +43 -18
  37. data/lib/license_finder/tables/license_alias.rb +4 -0
  38. data/lib/license_finder/yml_to_sql.rb +22 -30
  39. data/license_finder.gemspec +3 -3
  40. data/readme.md +5 -5
  41. data/spec/lib/license_finder/bower_package_spec.rb +56 -0
  42. data/spec/lib/license_finder/bower_spec.rb +3 -24
  43. data/spec/lib/license_finder/bundler_package_spec.rb +62 -0
  44. data/spec/lib/license_finder/{bundle_spec.rb → bundler_spec.rb} +7 -7
  45. data/spec/lib/license_finder/cli_spec.rb +6 -6
  46. data/spec/lib/license_finder/dependency_manager_spec.rb +14 -15
  47. data/spec/lib/license_finder/html_report_spec.rb +2 -3
  48. data/spec/lib/license_finder/markdown_report_spec.rb +4 -4
  49. data/spec/lib/license_finder/npm_package_spec.rb +51 -0
  50. data/spec/lib/license_finder/npm_spec.rb +25 -25
  51. data/spec/lib/license_finder/package_saver_spec.rb +50 -190
  52. data/spec/lib/license_finder/pip_package_spec.rb +74 -0
  53. data/spec/lib/license_finder/pip_spec.rb +33 -55
  54. data/spec/lib/license_finder/tables/dependency_spec.rb +83 -32
  55. data/spec/lib/license_finder/yml_to_sql_spec.rb +5 -12
  56. data/spec/spec_helper.rb +22 -2
  57. metadata +30 -18
  58. data/lib/license_finder/bundle.rb +0 -74
  59. data/lib/license_finder/tables/approval.rb +0 -4
  60. data/spec/lib/license_finder/package_spec.rb +0 -98
@@ -3,19 +3,13 @@ require 'forwardable'
3
3
  module LicenseFinder
4
4
  class PackageSaver
5
5
  extend Forwardable
6
- def_delegators :spec, :name, :version, :homepage
7
- def_delegators :package, :bundler_dependency, :license, :children, :groups, :summary, :description
6
+ def_delegators :package, :license, :children, :groups, :summary, :description, :version, :homepage
8
7
 
9
8
  attr_reader :dependency, :package
10
9
 
11
- def self.find_or_create_by_name(package)
12
- dependency = Dependency.named(package.spec.name)
13
- new(dependency, package)
14
- end
15
-
16
- def self.save_packages(current_packages)
17
- current_packages.map do |package|
18
- PackageSaver.find_or_create_by_name(package).save
10
+ def self.save_all(packages)
11
+ packages.map do |package|
12
+ find_or_create_by_name(package).save
19
13
  end
20
14
  end
21
15
 
@@ -26,79 +20,23 @@ module LicenseFinder
26
20
 
27
21
  def save
28
22
  DB.transaction do
29
- apply_dependency_definition
30
- sync_bundler_groups
31
- sync_children
32
- apply_better_license
33
- end
34
- dependency
35
- end
36
-
37
- private
38
-
39
- def spec
40
- package.spec
41
- end
42
-
43
- def apply_dependency_definition
44
- if values_have_changed?
45
23
  dependency.version = version.to_s
46
24
  dependency.summary = summary
47
25
  dependency.description = description
48
26
  dependency.homepage = homepage
49
- dependency.save
50
- end
51
- end
52
-
53
- def values_have_changed?
54
- return dependency.version != version.to_s ||
55
- dependency.summary != summary ||
56
- dependency.description != description ||
57
- dependency.homepage != homepage
58
- end
59
-
60
- def sync_bundler_groups
61
- existing_groups = dependency.bundler_groups
62
- new_groups = groups.map(&:to_s)
63
-
64
- existing_groups.reverse.each do |group|
65
- unless new_groups.include?(group.name)
66
- dependency.remove_bundler_group(group)
67
- end
68
- end
69
-
70
- new_groups.each do |group|
71
- unless existing_groups.map(&:name).include? group
72
- dependency.add_bundler_group BundlerGroup.find_or_create(name: group)
73
- end
27
+ dependency.bundler_group_names = groups.map(&:to_s)
28
+ dependency.children_names = children
29
+ dependency.apply_better_license license
30
+ dependency.save_changes
74
31
  end
32
+ dependency
75
33
  end
76
34
 
77
- def sync_children
78
- existing_children = dependency.children
79
- new_children = children
80
-
81
- existing_children.reverse.each do |child|
82
- unless new_children.include?(child.name)
83
- dependency.remove_child(child)
84
- end
85
- end
86
-
87
- new_children.each do |child|
88
- unless existing_children.map(&:name).include?(child)
89
- dependency.add_child Dependency.named(child)
90
- end
91
- end
92
- end
35
+ private
93
36
 
94
- def apply_better_license
95
- if !dependency.license_manual
96
- bundled_license = license
97
- if dependency.license.nil? || bundled_license != dependency.license.name
98
- dependency.license = LicenseAlias.find_or_create(name: bundled_license)
99
- dependency.save
100
- end
101
- end
37
+ def self.find_or_create_by_name(package)
38
+ dependency = Dependency.named(package.name)
39
+ new(dependency, package)
102
40
  end
103
41
  end
104
42
  end
@@ -1,10 +1,9 @@
1
1
  require 'json'
2
2
  require 'httparty'
3
- require 'license_finder/package'
4
3
 
5
4
  module LicenseFinder
6
5
  class Pip
7
- GET_DEPENDENCIES_PY = <<-PYTHON
6
+ GET_DEPENDENCIES_PY = <<-PYTHON.gsub(/\n+/, ";")
8
7
  from pip.util import get_installed_distributions
9
8
 
10
9
  dists = [(x.project_name, x.version, x.location) for x in get_installed_distributions()]
@@ -13,47 +12,36 @@ dists = ["[\\\"{0}\\\", \\\"{1}\\\", \\\"{2}\\\"]".format(*dist) for dist in dis
13
12
  print "[" + ",".join(dists) + "]"
14
13
  PYTHON
15
14
 
16
- def self.current_dists
17
- return @dists if @dists
15
+ def self.current_packages
16
+ output = `python -c '#{GET_DEPENDENCIES_PY}'`
18
17
 
19
- command = GET_DEPENDENCIES_PY.gsub(/\n+/, ";")
20
-
21
- output = `python -c '#{command}'`
22
-
23
- @dists = JSON(output).map do |dist_ary|
24
- PythonPackage.new(OpenStruct.new(
25
- :name => dist_ary[0],
26
- :version => dist_ary[1],
27
- :full_gem_path => File.join(dist_ary[2], dist_ary[0]),
28
- :python? => true
29
- ))
18
+ JSON(output).map do |(name, version, install_dir)|
19
+ PipPackage.new(
20
+ name,
21
+ version,
22
+ File.join(install_dir, name),
23
+ pypi_def(name, version)
24
+ )
30
25
  end
31
26
  end
32
27
 
33
- def self.has_requirements?
28
+ def self.active?
34
29
  File.exists?(requirements_path)
35
30
  end
36
31
 
37
- def self.license_for(package)
38
- info = package.json
39
- license = info.fetch("license", "UNKNOWN")
40
-
41
- if license == "UNKNOWN"
42
- classifiers = info.fetch("classifiers", [])
43
- license = classifiers.map do |c|
44
- if c.start_with?("License")
45
- c.gsub(/^License.*::\s*(.*)$/, '\1')
46
- end
47
- end.compact.first
48
- end
49
-
50
- license || "other"
51
- end
52
-
53
32
  private
54
33
 
55
34
  def self.requirements_path
56
35
  Pathname.new('requirements.txt').expand_path
57
36
  end
37
+
38
+ def self.pypi_def(name, version)
39
+ response = HTTParty.get("https://pypi.python.org/pypi/#{name}/#{version}/json")
40
+ if response.code == 200
41
+ JSON.parse(response.body).fetch("info", {})
42
+ else
43
+ {}
44
+ end
45
+ end
58
46
  end
59
- end
47
+ end
@@ -0,0 +1,51 @@
1
+ module LicenseFinder
2
+ class PipPackage < Package
3
+ def initialize(name, version, install_path, pypi_def)
4
+ @name = name
5
+ @version = version
6
+ @install_path = install_path
7
+ @pypi_def = pypi_def
8
+ end
9
+
10
+ attr_reader :name, :version
11
+
12
+ def summary
13
+ pypi_def.fetch("summary", "")
14
+ end
15
+
16
+ def description
17
+ pypi_def.fetch("description", "")
18
+ end
19
+
20
+ def children
21
+ [] # no way to determine child deps from pip (maybe?)
22
+ end
23
+
24
+ def groups
25
+ [] # no concept of dev/test groups in pip (maybe?)
26
+ end
27
+
28
+ def homepage
29
+ nil # no way to extract homepage from pip (maybe?)
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :install_path, :pypi_def
35
+
36
+ def license_from_spec
37
+ license = pypi_def.fetch("license", "UNKNOWN")
38
+
39
+ if license == "UNKNOWN"
40
+ classifiers = pypi_def.fetch("classifiers", [])
41
+ license = classifiers.map do |c|
42
+ if c.start_with?("License")
43
+ c.gsub(/^License.*::\s*(.*)$/, '\1')
44
+ end
45
+ end.compact.first
46
+ end
47
+
48
+ license
49
+ end
50
+ end
51
+ end
@@ -1,27 +1,15 @@
1
1
  module LicenseFinder
2
2
  module Platform
3
3
  def self.sqlite_adapter
4
- if java?
5
- 'jdbc:sqlite'
6
- else
7
- 'sqlite'
8
- end
4
+ java? ? 'jdbc:sqlite' : 'sqlite'
9
5
  end
10
6
 
11
7
  def self.sqlite_gem
12
- if java?
13
- 'jdbc-sqlite3'
14
- else
15
- 'sqlite3'
16
- end
8
+ java? ? 'jdbc-sqlite3' : 'sqlite3'
17
9
  end
18
10
 
19
11
  def self.sqlite_load_path
20
- if java?
21
- 'jdbc/sqlite3'
22
- else
23
- 'sqlite3'
24
- end
12
+ java? ? 'jdbc/sqlite3' : 'sqlite3'
25
13
  end
26
14
 
27
15
  def self.java?
@@ -9,10 +9,6 @@ module LicenseFinder
9
9
  @file_path.relative_path_from(@install_path).to_s
10
10
  end
11
11
 
12
- def full_file_path
13
- Pathname.new(@file_path).realpath.to_s
14
- end
15
-
16
12
  def file_name
17
13
  @file_path.basename.to_s
18
14
  end
@@ -2,6 +2,10 @@ module LicenseFinder
2
2
  class PossibleLicenseFiles
3
3
  LICENSE_FILE_NAMES = %w(LICENSE License Licence COPYING README Readme ReadMe)
4
4
 
5
+ def self.find(install_path)
6
+ new(install_path).find
7
+ end
8
+
5
9
  def initialize(install_path)
6
10
  @install_path = install_path
7
11
  end
@@ -2,6 +2,6 @@ require 'rubygems'
2
2
  require 'sequel'
3
3
  require LicenseFinder::Platform.sqlite_load_path
4
4
 
5
- DB = Sequel.connect("#{LicenseFinder::Platform.sqlite_adapter}://#{LicenseFinder.config.database_uri}")
5
+ LicenseFinder::DB = Sequel.connect("#{LicenseFinder::Platform.sqlite_adapter}://#{LicenseFinder.config.database_uri}")
6
6
  Sequel.extension :migration, :core_extensions
7
- Sequel::Migrator.run(DB, LicenseFinder::ROOT_PATH.join('../db/migrate'))
7
+ Sequel::Migrator.run(LicenseFinder::DB, LicenseFinder::ROOT_PATH.join('../db/migrate'))
@@ -1,4 +1,7 @@
1
1
  module LicenseFinder
2
2
  class BundlerGroup < Sequel::Model
3
+ def self.named(name)
4
+ find_or_create(name: name)
5
+ end
3
6
  end
4
7
  end
@@ -2,18 +2,17 @@ module LicenseFinder
2
2
  class Dependency < Sequel::Model
3
3
  plugin :boolean_readers
4
4
  many_to_one :license, class: LicenseAlias
5
- many_to_one :approval
6
5
  many_to_many :children, join_table: :ancestries, left_key: :parent_dependency_id, right_key: :child_dependency_id, class: self
7
6
  many_to_many :parents, join_table: :ancestries, left_key: :child_dependency_id, right_key: :parent_dependency_id, class: self
8
7
  many_to_many :bundler_groups
9
8
 
10
9
  dataset_module do
11
- def bundler
12
- exclude(manual: true)
10
+ def managed
11
+ manually_managed.invert
13
12
  end
14
13
 
15
- def non_bundler
16
- bundler.invert
14
+ def manually_managed
15
+ where(manual: true)
17
16
  end
18
17
 
19
18
  def obsolete(current)
@@ -26,32 +25,58 @@ module LicenseFinder
26
25
  end
27
26
 
28
27
  def self.named(name)
29
- d = find_or_create(name: name.to_s)
30
- d.ensure_approval_exists!
31
- d
28
+ find_or_create(name: name.to_s)
29
+ end
30
+
31
+ def bundler_group_names=(names)
32
+ current_groups = names.map { |name| BundlerGroup.named(name) }
33
+
34
+ remove, add = set_diff(bundler_groups, current_groups)
35
+
36
+ remove.each { |g| remove_bundler_group(g) }
37
+ add.each { |g| add_bundler_group(g) }
38
+ end
39
+
40
+ def children_names=(names)
41
+ current_children = names.map { |name| Dependency.named(name) }
42
+
43
+ remove, add = set_diff(children, current_children)
44
+
45
+ remove.each { |c| remove_child(c) }
46
+ add.each { |c| add_child(c) }
32
47
  end
33
48
 
34
49
  def approve!
35
- approval.state = true
36
- approval.save
50
+ self.manually_approved = true
51
+ save
37
52
  end
38
53
 
39
54
  def approved?
40
- # jruby adapter receives approval.state as Fixnum '0', which ruby evaluates
41
- # as truthy, so we catch this here for jruby support.
42
- (license && license.whitelisted?) || (approval.state && approval.state != 0)
55
+ (license && license.whitelisted?) || manually_approved?
43
56
  end
44
57
 
45
58
  def set_license_manually!(license_name)
46
- self.license = LicenseAlias.find_or_create(name: license_name)
59
+ self.license = LicenseAlias.named(license_name)
47
60
  self.license_manual = true
48
61
  save
49
62
  end
50
63
 
51
- def ensure_approval_exists!
52
- return if approval
53
- self.approval = Approval.create
54
- save
64
+ def apply_better_license(license_name)
65
+ return if license_manual
66
+ if license.nil? || license.name != license_name
67
+ self.license = LicenseAlias.named(license_name)
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ # Foreign method, belongs on Set
74
+ #
75
+ # Returns a pair of sets, which contain the elements that would have to be
76
+ # removed from (and respectively added to) the first set in order to obtain
77
+ # the second set.
78
+ def set_diff(older, newer)
79
+ return older - newer, newer - older
55
80
  end
56
81
  end
57
82
  end
@@ -1,5 +1,9 @@
1
1
  module LicenseFinder
2
2
  class LicenseAlias < Sequel::Model
3
+ def self.named(name)
4
+ find_or_create(name: name)
5
+ end
6
+
3
7
  def initialize(*args)
4
8
  super
5
9
  self.url = LicenseUrl.find_by_name name
@@ -40,8 +40,7 @@ module LicenseFinder
40
40
  def convert
41
41
  @dep = create_dependency
42
42
  @dep.license = create_license
43
- @dep.approval = create_approval
44
- @dep.manual = non_bundler_source?
43
+ @dep.manual = manually_managed?
45
44
  associate_bundler_groups
46
45
  @dep.save
47
46
  end
@@ -58,8 +57,8 @@ module LicenseFinder
58
57
  end
59
58
  end
60
59
 
61
- def non_bundler_source?
62
- @legacy_attrs['source'] == "bundle" ? false : true
60
+ def manually_managed?
61
+ @legacy_attrs['source'] != "bundle"
63
62
  end
64
63
 
65
64
  def create_dependency
@@ -70,10 +69,6 @@ module LicenseFinder
70
69
  LicenseAlias.find_or_create(name: legacy_attrs['license'])
71
70
  end
72
71
 
73
- def create_approval
74
- Sql::Approval.convert(legacy_attrs)
75
- end
76
-
77
72
  def find_children
78
73
  Sql::Dependency.where(name: legacy_attrs['children'])
79
74
  end
@@ -85,38 +80,35 @@ module LicenseFinder
85
80
  end
86
81
 
87
82
  module Sql
88
- module Convertable
89
- def convert(attrs)
83
+ class Dependency < Sequel::Model
84
+ plugin :boolean_readers
85
+
86
+ many_to_one :license, class: LicenseAlias
87
+ many_to_many :children, join_table: :ancestries, left_key: :parent_dependency_id, right_key: :child_dependency_id, class: self
88
+ many_to_many :bundler_groups
89
+
90
+ VALID_ATTRIBUTES = {
91
+ 'name' => 'name',
92
+ 'version' => 'version',
93
+ 'summary' => 'summary',
94
+ 'description' => 'description',
95
+ 'homepage' => 'homepage',
96
+ 'approved' => 'manually_approved'
97
+ }
98
+
99
+ def self.convert(attrs)
90
100
  create remap_attrs(attrs)
91
101
  end
92
102
 
93
- def remap_attrs(legacy_attrs)
94
- self::VALID_ATTRIBUTES.each_with_object({}) do |(legacy_key, new_key), new_attrs|
103
+ def self.remap_attrs(legacy_attrs)
104
+ VALID_ATTRIBUTES.each_with_object({}) do |(legacy_key, new_key), new_attrs|
95
105
  new_attrs[new_key] = legacy_attrs[legacy_key]
96
106
  end
97
107
  end
98
108
  end
99
109
 
100
- class Dependency < Sequel::Model
101
- extend Convertable
102
- VALID_ATTRIBUTES = Hash[*%w[name version summary description homepage].map { |k| [k, k] }.flatten]
103
-
104
- many_to_one :license, class: LicenseAlias
105
- many_to_one :approval
106
- many_to_many :children, join_table: :ancestries, left_key: :parent_dependency_id, right_key: :child_dependency_id, class: self
107
- many_to_many :bundler_groups
108
- end
109
-
110
110
  class BundlerGroup < Sequel::Model
111
111
  end
112
-
113
- class Approval < Sequel::Model
114
- extend Convertable
115
-
116
- VALID_ATTRIBUTES = {
117
- 'approved' => 'state'
118
- }
119
- end
120
112
  end
121
113
  end
122
114
  end