Skip to content

Instantly share code, notes, and snippets.

@cloudify
Created July 20, 2011 16:36
Show Gist options
  • Save cloudify/1095308 to your computer and use it in GitHub Desktop.
Save cloudify/1095308 to your computer and use it in GitHub Desktop.
Find licenses of the gems you're using
#!/usr/bin/ruby
gem_root = '/Users/federico/.rvm/gems/ruby-1.8.7-p330@coderloop/gems'
Dir.open(gem_root) do |dir|
gems = {}
dir.each do |gemfolder|
gemname = gemfolder.match(/^.+-/)
next if gemname.nil?
gemname = gemname[0]
if gems[gemname].nil? || gems[gemname][:folder] < gemfolder
gems[gemname] = { :folder => gemfolder }
end
end
gems.each do |gemname, data|
Dir.open(File.join(gem_root, data[:folder])) do |gem_dir|
gem_dir.each do |gem_file|
if gem_file.match(/GPL|license|MIT|COPYING/i)
data[:license_file] = gem_file
next
end
end
unless data[:license_file]
gem_dir.each do |gem_file|
if gem_file.match(/readme/i)
File.open(File.join(gem_root, data[:folder], gem_file)) do |file|
content = file.read
if content.match(/license/im)
data[:license_file] = gem_file
end
end
end
end
end
next unless data[:license_file]
File.open(File.join(gem_root, data[:folder], data[:license_file])) do |file|
content = file.read.gsub(/\n/, ' ').gsub(/\s+/,' ')
if content.match(/MIT\s+LICENSE|Permission is hereby granted, free of charge, to any person obtaining/im)
data[:license] = 'MIT'
next
end
if content.match(/GPL|GNU GENERAL PUBLIC LICENSE/im)
data[:license] = 'GPL'
next
end
if content.match(/ruby/im)
data[:license] = 'RUBY'
next
end
if content.match(/Academic Free License|AFL/im)
data[:license] = 'AFL'
next
end
if content.match(/Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met/)
data[:license] = 'BSD'
next
end
if content.match(/apache/im)
data[:license] = 'APACHE'
next
end
end
end
end
gems.each do |gemname,data|
puts "#{data[:folder]}\t#{data[:license_file] || 'NOT FOUND'}\t#{data[:license] || 'UNKNOWN'}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment