Skip to content

Instantly share code, notes, and snippets.

@msepcot
Created September 5, 2012 16:26
Show Gist options
  • Save msepcot/3639386 to your computer and use it in GitHub Desktop.
Save msepcot/3639386 to your computer and use it in GitHub Desktop.
Look through gems and group by license
# encoding: UTF-8
require 'securerandom'
require 'fileutils'
dir = "tmp/#{SecureRandom.hex(4)}_gems"
gems = `bundle exec gem list`.split
licenses = {
'Apache 2.0' => [],
'MIT' => [],
'Unknown' => []
}
MIT = <<-LICENSE.strip.gsub(/\s+/, ' ').gsub(/[‘’\`'"]/, '')
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright
notice and this permission notice shall be included in all copies or substantial portions of the Software. THE
SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE
Dir.mkdir(dir)
Dir.chdir(dir) do
gems.each_slice(2) do |name, version|
version = version[1...-1]
`gem unpack #{name} -v '=#{version}'`
if path = Dir.glob("#{name}-#{version}/*LICENSE*").first
license = File.basename(path)
file = File.read(path).strip.gsub(/\s+/, ' ').gsub(/[‘’\`'"]/, '')
if license =~ /MIT-LICENSE/ || license =~ /LICENSE-MIT/ || file =~ /MIT License/ || file.include?(MIT)
licenses['MIT'] << name
elsif file =~ /http:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0/ || file =~ /Apache License Version 2\.0/
licenses['Apache 2.0'] << name
else
licenses['Unknown'] << name
FileUtils.move path, "#{name}-#{license}"
end
end
FileUtils.rm_r("#{name}-#{version}/")
end
end
puts licenses
FileUtils.rm_r(dir) if licenses['Unknown'].empty?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment