nod 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/nod.rb +42 -18
  2. data/lib/nod/license_hunch.rb +4 -2
  3. metadata +1 -1
data/lib/nod.rb CHANGED
@@ -4,6 +4,7 @@ require 'nod/git_cloner'
4
4
  require 'nod/license_hunch'
5
5
  require 'nod/bundle_parser'
6
6
  require 'fileutils'
7
+ require 'open-uri'
7
8
 
8
9
  class Nod
9
10
 
@@ -18,39 +19,62 @@ class Nod
18
19
  url = @gem_savvy.git_clone_url name
19
20
  report = {}
20
21
 
22
+ tmp_dir = "#{@basedir}/#{name}"
21
23
  if !url.nil?
22
- tmp_dir = "#{@basedir}/#{name}"
23
24
  successful_clone = @git_cloner.clone_url url, tmp_dir
24
25
  if successful_clone && File.exists?(tmp_dir)
25
26
  files_in_repo = Dir.entries(tmp_dir)
26
- licenses = files_in_repo.select { |file_name| file_name if file_name =~ /license/i }
27
- # puts "licenses is #{licenses}"
28
- if !licenses.nil? && licenses.size > 0
29
- license_type = LicenseHunch.determine_license_from_file licenses[0], tmp_dir #File.expand_path(File.dirname(__FILE__) + "/../#{tmp_dir}")
30
- report = {:gem_name => name, :gem_home_page => @gem_savvy.gem_home(name), :gem_source_url => url, :gem_license => license_type}
31
- else
32
- # puts "no license found for #{@gem_savvy.info_for(name)}"
33
- readme = files_in_repo.select { |file_name| file_name if file_name =~ /readme/i }
34
- if !readme.nil? && readme.size > 0
35
- # puts "\n\ngoing readme route with #{readme}"
36
- license_type = LicenseHunch.determine_license_from_file readme[0], tmp_dir #File.expand_path(File.dirname(__FILE__) + "/../#{tmp_dir}")
37
- # puts "got? #{license_type}"
38
- report = {:gem_name => name, :gem_home_page => @gem_savvy.gem_home(name), :gem_source_url => url, :gem_license => license_type}
27
+ licenses_files = files_in_repo.select { |file_name| file_name if (file_name =~ /license/i || file_name =~ /readme/i || file_name =~ /copying/i)}
28
+ if !licenses_files.nil? && licenses_files.size > 0
29
+ licenses_files.each do | file |
30
+ license_type = LicenseHunch.determine_license_from_file file, tmp_dir
31
+ unless license_type.nil?
32
+ return get_report name, url, license_type
33
+ end
39
34
  end
35
+ report = get_report name, url, 'Unknown'
36
+ else
37
+ report = get_report name, url, 'Unknown'
40
38
  end
41
- else
39
+ else
42
40
  # TODO handle this
43
41
  puts "in else? file didn't exist? gem is #{name}"
44
42
  end #file exists
45
43
  else
46
44
  info = @gem_savvy.info_for(name)
47
- # puts "info for #{name} is #{info}"
48
- report = {:gem_name => name, :gem_home_page => info['homepage_uri'], :gem_source_url => info['source_code_uri'], :gem_license => 'Cannot determine'}
45
+ license_type = get_license_from_home_page info['homepage_uri'], tmp_dir
46
+ report = {:gem_name => name, :gem_home_page => info['homepage_uri'], :gem_source_url => info['source_code_uri'], :gem_license => license_type }
49
47
  end
50
- # puts "returning report for gem #{name} -> #{report}"
51
48
  return report
52
49
  end
53
50
 
51
+ def get_license_from_home_page(web_page, tmp_dir)
52
+
53
+ if web_page.nil?
54
+ return nil
55
+ end
56
+
57
+ unless web_page.start_with?('http://')
58
+ web_page = "http://#{web_page}"
59
+ end
60
+
61
+ license_type = 'Cannot determine'
62
+ unless web_page.nil?
63
+ unless Dir.exists?(tmp_dir)
64
+ Dir.mkdir tmp_dir
65
+ end
66
+ File.open("#{tmp_dir}/webpage_source", 'w') do |file|
67
+ file << open(web_page).read
68
+ end
69
+ license_type = LicenseHunch.determine_license_from_file 'webpage_source', tmp_dir
70
+ end
71
+ license_type
72
+ end
73
+
74
+ def get_report(gem_name, url, license_type)
75
+ {:gem_name => gem_name, :gem_home_page => @gem_savvy.gem_home(gem_name), :gem_source_url => url, :gem_license => license_type}
76
+ end
77
+
54
78
  def get_licenses_for(gemfile_path)
55
79
  parser = BundleParser.new(gemfile_path)
56
80
  all_names = parser.gem_names
@@ -11,7 +11,9 @@ class LicenseHunch
11
11
  file_path = "#{base_path}/#{license_name}"
12
12
  is_mit_lic = false
13
13
  File.open(file_path, 'r').each_line do | line |
14
- return 'MIT' if line =~ /MIT License/i
14
+ # puts "line is #{line}"
15
+ return 'MIT' if (line =~ /MIT License/i || line =~ /MIT/i || line =~ /mit-license/i)
16
+ return 'Ruby License' if (line =~ /Ruby license/i)
15
17
  end
16
18
 
17
19
  all_text = File.open(file_path).read.gsub(/\n/,' ')
@@ -23,7 +25,7 @@ class LicenseHunch
23
25
  phrase_5 = "distribute, sublicense"
24
26
  return 'MIT' if (all_text =~ /#{phrase_1}/i && all_text =~ /#{phrase_2}/i && all_text =~ /#{phrase_3}/i && all_text =~ /#{phrase_4}/ && all_text =~ /#{phrase_5}/ )
25
27
 
26
- return 'Unknown'
28
+ return nil
27
29
 
28
30
  end
29
31
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: nod
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Glover