Skip to content

Instantly share code, notes, and snippets.

@stevenwilliamson
Created December 13, 2016 11:07
Show Gist options
  • Save stevenwilliamson/001bd6d88db63cf88f509b2b5c0f0013 to your computer and use it in GitHub Desktop.
Save stevenwilliamson/001bd6d88db63cf88f509b2b5c0f0013 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# Quick and dirty process license information from pkg_src summary files
# Package categories to skip
SKIP_CATEGORIES = [
"games",
"emulators",
"japanese",
"chinese",
"chat",
"cad",
"biology"
]
def audited_category(categories)
categories.split(" ").each do |c|
if SKIP_CATEGORIES.include?(c)
return false
end
end
return true
end
class PackageInfo
attr_accessor :data
# Type of value if not present we assume
# it;s a single value type ie it does not appear
# multiple times
KEY_TYPES = {
"REQUIRES" => :MULTI_VALUE,
"DESCRIPTION" => :MULTI_VALUE,
}
def initialize()
@data = {}
end
def self.package_from_summary(data)
pkg = PackageInfo.new
data.each_line do |l|
if l =~ /=/
key, value = l.split('=')
pkg.set_property(key, value)
end
end
return pkg
end
def set_property(key, value)
case KEY_TYPES[key]
when :MULTI_VALUE
set_multivalue_property(key, value)
else
set_singlevalue_property(key, value)
end
end
def set_multivalue_property(key, value)
if @data.key? key
@data[key] << value.strip!
else
@data[key] = [value.strip!]
end
end
def set_singlevalue_property(key, value)
@data[key] = value.strip!
end
end
in_pkg = false
pkg_data_buffer = ""
packages = []
File.open("pkg_summary").each_line do |l|
if l.length > 1
in_pkg = true
pkg_data_buffer << l
else
in_pkg = false
packages << PackageInfo.package_from_summary(pkg_data_buffer)
pkg_data_buffer = ""
end
end
licenses = {}
packages.each do |p|
license_data = p.data["LICENSE"].split(" ")
license_data.each do |l|
next if l == "AND" || l == "OR"
licenses[l] = 1
end
end
puts "There are #{licenses.keys.length} unique licenses"
licenses.keys.each do |l|
puts l
end
unlicensed = []
packages.each do |p|
if audited_category(p.data["CATEGORIES"])
if p.data["LICENSE"].length > 1
puts "#{p.data["PKGNAME"]} #{p.data["LICENSE"]}"
else
unlicensed << p
end
end
end
puts "Unlincensed packages or which there are #{unlicensed.length}"
unlicensed.each do |p|
puts "#{p.data["PKGNAME"]} #{p.data["HOMEPAGE"]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment