#!/usr/bin/perl -w ############### # These 2 are configurable, change as needed: $user_agent = 'Mozilla/5.0 (compatible; ropeylog-repology-bot/1.0; +https://github.com/SlackBuildsOrg/slackbuilds)'; $git_root = $ENV{SBO_GITROOT} || "/slackbuilds/www/slackbuilds/15.0/"; $outdir = "$ENV{HOME}/www"; ############### use JSON::XS; # from SBo perl-JSON-XS use POSIX qw/strftime getcwd/; # bundled with Slackware's perl ($SELF = $0) =~ s,.*/,,; @reports = ( [ "Maintainer", '', \&sort_abc ], [ "Last Git (+)", '-bygit_up', \&sort_git_up ], [ "Last Git (-)", '-bygit_down', \&sort_git_down ], [ "Num Builds (+)", '-bynum_up', \&sort_num_up ], [ "Num Builds (-)", '-bynum_down', \&sort_num_down ], ); ############### sub usage { print <[0], get_page_name($_->[1]); } } ############### sub make_api_url { my $last = shift; my $url = "https://repology.org/api/v1/projects/"; $url .= "$last/" if defined $last; $url .= "?status=outdated&inrepo=slackbuilds"; } sub wget { my $url = shift; my $output = shift; my $cmd = "wget --compression=gzip --user-agent '$user_agent' -O$output '$url'"; warn "$cmd\n"; system($cmd); sleep 1; } sub download { my $firstpkg = $_[0] || ""; my $lastpkg = ""; my $filename = sprintf("%s/%03d.json", $cache_dir, $_[1]); wget(make_api_url($_[0]), $filename); my $j = parse_one_file($filename); for $proj (keys %{$j}) { $lastpkg = $proj if $proj gt $lastpkg; } return (($lastpkg eq $firstpkg) ? undef : $lastpkg); } sub download_json_files { my $lastpkg; my $count = 1; do { $lastpkg = download($lastpkg, $count++); } while defined $lastpkg; } ############### sub read_file { undef local $/; local @ARGV = shift; $_ = <>; } sub parse_one_file { my $file = shift; my $j; eval { $j = decode_json(read_file($file)); }; die "failed to decode JSON: $@\n" if $@; return $j; } sub parse_json_files { die "$SELF: nothing to parse, run '$SELF download' first.\n" unless -e "$cache_dir/001.json"; for(<$cache_dir/*.json>) { my $j = parse_one_file($_); for my $proj (keys %{$j}) { my $arr = $$j{$proj}; for my $hr (@{$arr}) { next unless $hr->{repo} eq 'slackbuilds'; my $catbuild = $hr->{visiblename}; $outdated{$catbuild}++ if $hr->{status} eq 'outdated'; } } } } ############### sub read_info_file { my $file = shift; my $maint; my $email; open my $f, "<$file" or die $!; while(<$f>) { chomp; if(/^MAINTAINER="(.*?)"/) { $maint = $1; } elsif(/^EMAIL="(.*?)"/) { $email = $1; } } close $f; return ($maint, $email); } sub parse_info_files { my $olddir = getcwd(); chdir($git_root) or die "$!"; my $files = `git ls-files '*/*/*.info'`; for my $info (split /\n/, $files) { $total_builds++; my ($maintainer, $email) = read_info_file($info); if($email eq 'NOEMAIL') { $total_orphans++; next; } my $catbuild = $info; $catbuild =~ s,/[^/]*$,,; $name2email{$maintainer} = $email; $lastgitdate{$maintainer} = $lastgithash{$maintainer} = 0; push @{$maintbuilds{$maintainer}}, $catbuild; } chdir($olddir); @maintainers = keys %name2email; } sub get_git_maintainers { my $olddir = getcwd(); chdir($git_root) or die "$!"; open my $in, "git log --pretty=format:%h%x09%an%x09%ae%x09%as%x09%s|" or die $!; while(<$in>) { chomp; my ($hash, $name, $email, $date, $catbuild, undef) = split /\t/, $_, 6; $catbuild =~ s/:.*$//; if(!($lastgitdate{$name})) { $lastgitdate{$name} = $date; $lastgithash{$name} = $hash; } } close $in; chdir($olddir); } ############### sub sort_abc { lc($a) cmp lc($b); } sub sort_git_up { $lastgitdate{$a} cmp $lastgitdate{$b}; } sub sort_git_down { $lastgitdate{$b} cmp $lastgitdate{$a}; } sub sort_num_up { scalar(@{$maintbuilds{$a}}) <=> scalar(@{$maintbuilds{$b}}); } sub sort_num_down { scalar(@{$maintbuilds{$b}}) <=> scalar(@{$maintbuilds{$a}}); } ############### sub url_escape { my $s = shift; $s =~ s{[^-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz]}{sprintf("%%%x", ord($&))}ge; return $s; } sub get_percent { my $a = shift; my $b = shift; if(!$b) { print "0"; return; } return sprintf("%d (%.1f%%)", $a, ($a / $b) * 100); } sub print_sort_links { for my $r (@reports) { my $page = get_page_name($r->[1]); print <$r->[0]
EOF } } sub print_report { my $title = shift; my $sortfunc = shift; my $stamp = strftime("%Y-%m-%d %H:%M:%S", gmtime); my $maintcount = @maintainers; my $total_outdated = keys %outdated; my $print_uptodate = get_percent($total_builds - $total_outdated, $total_builds); my $print_outdated = get_percent($total_outdated, $total_builds); $total_orphans ||= 0; my $print_orphaned = get_percent($total_orphans, $total_builds); print < SBo maintainer status report

SBo maintainer status report

Sorted by $title

EOF print_sort_links(); print <Date: $stamp
SlackBuilds: $total_builds
Maintainers: $maintcount
Total up-to-date builds: $print_uptodate
Total outdated builds: $print_outdated
Orphaned builds: $print_orphaned

EOF for my $maintainer (sort $sortfunc @maintainers) { my $count = 0; my $outdated_count = 0; for(@{$maintbuilds{$maintainer}}) { $count++; $outdated_count++ if $outdated{$_}; } my $newest = get_percent($count - $outdated_count, $count); my $outdated = get_percent($outdated_count, $count); my $maint_email = $name2email{$maintainer}; my $repology_link = ""; my $count_link = $count; if(defined $maint_email) { my $escmail = url_escape($maint_email); my $escmaint = url_escape($maintainer); $count_link = "$count"; if($maint_email =~ /\@/) { $repology_link = "✉ " . "$maint_email"; } else { $repology_link = $maint_email; } } my $git_hash = $lastgithash{$maintainer}; my $git_date = $lastgitdate{$maintainer}; my $git_link = ""; if(defined $git_hash && defined $git_date) { $git_link = "$git_date"; } print < EOF } print <
MaintainerLast gitSlackBuildsUp to dateOut of date
$maintainer
$repology_link
$git_link $count_link $newest $outdated
EOF } sub get_page_name { return "maintainer_status$_[0].html"; } sub print_all_reports { for my $rep (@reports) { my ($title, $fname, $sortfunc) = @$rep; $fname = "$outdir/" . get_page_name($fname); open my $f, ">$fname" or die $!; my $oldf = select $f; print_report($title, $sortfunc); select $oldf; close $f; } } ############### # main() $cache_dir = "$ENV{HOME}/.$SELF"; system("mkdir -p $cache_dir"); if(@ARGV) { if($ARGV[0] eq '-d') { shift; for(<$cache_dir/*.json>) { unlink $_; } download_json_files(); } elsif($ARGV[0] =~ /help/) { usage(); exit 0; } } $outdir = $ARGV[0] if $ARGV[0]; parse_json_files(); parse_info_files(); get_git_maintainers(); print_all_reports();