-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlicense_analysis.rake
More file actions
126 lines (103 loc) · 4.72 KB
/
Copy pathlicense_analysis.rake
File metadata and controls
126 lines (103 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
namespace :licenses do
desc 'Analyze most frequent license values globally and per ecosystem'
task analyze: :environment do
top_registries_count = 9
# Helper method to format arrays as comma-separated values
def format_license(license)
if license.is_a?(Array)
if license.empty? || license == ['']
"''"
else
license.join(', ')
end
elsif license.nil?
'NULL'
elsif license == ''
"''"
else
license.to_s
end
end
# Helper method to format numbers with commas
def format_number(num)
num.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end
puts "\n# License Field Analysis\n"
puts "This is an extraction from the https://packages.ecosyste.ms/ service, analyzing the top 9 registries by package count and examining the most commonly used licenses across all active packages. This analysis uses the raw `licenses` field as reported by each package registry, with the analysis covering both the general package population and packages marked as critical infrastructure.\n"
puts "Code for this script lives here: https://github.com/ecosyste-ms/packages/blob/main/lib/tasks/license_analysis.rake\n"
# Find top 9 registries by package count
top_registries = Registry.order(packages_count: :desc).limit(top_registries_count)
# Global analysis
puts "\n## Global Analysis\n"
total_packages = Package.active.count
puts "Total packages: #{format_number(total_packages)}\n"
puts "\n### Top licenses values:\n"
global_licenses = Package.active
.group(:licenses)
.count
.sort_by { |_k, v| -v }
.first(20)
puts "| License | Count | Percentage |"
puts "|---------|-------|------------|"
global_licenses.each do |(license, count)|
percentage = (count.to_f / total_packages * 100).round(2)
puts "| #{format_license(license)} | #{format_number(count)} | #{percentage}% |"
end
# Per registry analysis
puts "\n## Per Registry Analysis\n"
top_registries.each do |registry|
reg_total = registry.packages.active.count
puts "\n### #{registry.name} (#{registry.ecosystem})"
puts "Total packages: #{format_number(reg_total)}\n"
registry_licenses = registry.packages.active
.group(:licenses)
.count
.sort_by { |_k, v| -v }
.first(10)
puts "\n#### Top licenses values:\n"
puts "| License | Count | Percentage |"
puts "|---------|-------|------------|"
registry_licenses.each do |(license, count)|
percentage = (count.to_f / reg_total * 100).round(2)
puts "| #{format_license(license)} | #{format_number(count)} | #{percentage}% |"
end
end
# Critical packages global analysis
puts "\n## Critical Packages Global Analysis\n"
critical_count = Package.active.critical.count
puts "Total critical packages: #{format_number(critical_count)}\n"
puts "\n### Top licenses values:\n"
critical_licenses = Package.active
.critical
.group(:licenses)
.count
.sort_by { |_k, v| -v }
.first(10)
puts "| License | Count | Percentage |"
puts "|---------|-------|------------|"
critical_licenses.each do |(license, count)|
percentage = (count.to_f / critical_count * 100).round(2)
puts "| #{format_license(license)} | #{format_number(count)} | #{percentage}% |"
end
# Critical packages per registry analysis
puts "\n## Critical Packages Per Registry Analysis\n"
top_registries.each do |registry|
critical_total = registry.packages.active.critical.count
puts "\n### #{registry.name} (#{registry.ecosystem})"
puts "Total critical packages: #{format_number(critical_total)}\n"
registry_critical_licenses = registry.packages.active
.critical
.group(:licenses)
.count
.sort_by { |_k, v| -v }
.first(5)
puts "\n#### Top licenses values:\n"
puts "| License | Count | Percentage |"
puts "|---------|-------|------------|"
registry_critical_licenses.each do |(license, count)|
percentage = (count.to_f / critical_total * 100).round(2)
puts "| #{format_license(license)} | #{format_number(count)} | #{percentage}% |"
end
end
end
end