deriving_license 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,13 +5,13 @@ Finds the license agreements for all gems in your Gemfile. This is achieved by r
5
5
 
6
6
  Strategies:
7
7
  * from\_gem\_specification
8
- * from\_license\_file (not yet implemented)
8
+ * from\_license\_file
9
9
  * from\_scraping\_homepage
10
10
  * from\_parsing\_readme (not yet implemented)
11
11
 
12
12
  Example output:
13
13
 
14
- $ deriving_license ../bc/A-A/Gemfile
14
+ $ deriving_license ~/Code/rails_sample_app/Gemfile
15
15
  Determining license for rails:
16
16
  Trying from_gem_specification strategy...FAILED
17
17
  Trying from_scraping_homepage strategy...SUCCESS
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'deriving_license'
3
- s.version = '0.2.1'
3
+ s.version = '0.2.2'
4
4
  s.summary = "Deriving Licence finds the license agreements for all gems in your Gemfile"
5
5
  s.description = "Deriving Licence finds the license agreements for all gems in your Gemfile if included in your project, or in a Gemfile passed to the included binary"
6
6
  s.authors = ["Tom Allen"]
@@ -2,6 +2,7 @@ require "gemnasium/parser"
2
2
  require "bundler"
3
3
  require "safe_yaml"
4
4
  require "curb"
5
+ require "find"
5
6
 
6
7
  class DerivingLicense
7
8
 
@@ -15,7 +16,8 @@ class DerivingLicense
15
16
  "MIT" => {name:"Expat License",link:"http://directory.fsf.org/wiki/License:Expat",tags:[:gpl_compatible, :has_restrictions]},
16
17
  "BSD" => {name:"FreeBSD Copyright",link:"http://www.freebsd.org/copyright/freebsd-license.html",tags:[:gpl_compatible, :copyleft_compatible, :has_restrictions]},
17
18
  "beerware" => {name:"Beerware License",link:"http://en.wikipedia.org/wiki/Beerware#License",tags:[]},
18
- "Ruby" => {name:"Ruby License",link:"http://www.ruby-lang.org/en/about/license.txt",tags:[:gpl_compatible, :has_restrictions]}
19
+ "Ruby" => {name:"Ruby License",link:"http://www.ruby-lang.org/en/about/license.txt",tags:[:gpl_compatible, :has_restrictions]},
20
+ "Apache" => {name:"Apache License",link:"http://www.apache.org/licenses/LICENSE-2.0",tags:[:has_restrictions]},
19
21
  }
20
22
 
21
23
  @@license_aliases = {
@@ -31,7 +33,8 @@ class DerivingLicense
31
33
  # order of fastest to slowest.
32
34
  @@strategies = [
33
35
  "from_gem_specification",
34
- "from_scraping_homepage"
36
+ "from_scraping_homepage",
37
+ "from_license_file"
35
38
  ]
36
39
 
37
40
  @@specs_cache = {} # Cache of gem specifications previously fetched.
@@ -121,7 +124,52 @@ class DerivingLicense
121
124
  end
122
125
 
123
126
  def self.from_license_file(dep)
124
- []
127
+ licenses = []
128
+ Bundler.with_clean_env do # This gets out of the bundler context.
129
+ @fetch_output = `gem fetch #{dep}`
130
+ @unpack_output = `gem unpack #{dep} --target=./deriving_license_tmp`
131
+ end
132
+ unpack_dir = /'([\/a-zA-Z0-9._\-]*)'/.match(@unpack_output)[1]
133
+ gem_filename = "#{unpack_dir.split("\/").last}.gem"
134
+ license_file_paths = []
135
+ Find.find(unpack_dir) do |path|
136
+ license_file_paths << path if path =~ /(license|LICENSE)$/
137
+ end
138
+ return [] unless license_file_paths
139
+
140
+ # Found filename with the word "license", so now look for known license
141
+ # names in the rest of this filename.
142
+ license_file_paths.each do |p|
143
+ (@@license_details.keys + @@license_aliases.keys).each do |n|
144
+ if /#{n}/.match(p)
145
+ licenses << n
146
+ end
147
+ end
148
+ end
149
+
150
+ if licenses.empty?
151
+ # Failing that, open each file and check the content in a similar manner.
152
+ license_file_paths.each do |p|
153
+ if File.exist?(p)
154
+ File.open(p).each_line do |l|
155
+ if /license/.match(l)
156
+ # Found the word "license", so now look for known license names.
157
+ (@@license_details.keys + @@license_aliases.keys).each do |n|
158
+ if /#{n}/.match(l)
159
+ licenses << n
160
+ return licenses
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
168
+
169
+ `rm -rf ./deriving_license_tmp` # Clean up tmp dir. Don't fuck this up.
170
+ `rm #{gem_filename}` # Remove fetched gem.
171
+ return licenses
172
+
125
173
  end
126
174
 
127
175
  def self.from_scraping_homepage(dep)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deriving_license
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: