pkg-get/scripts/pkg-repgen.pl

346 lines
9.0 KiB
Perl
Executable File

#!/usr/bin/env perl
#
# pkg-repgen: generates a binary repository for pkg-get
#
# requires prt-get
#
# html index generation code adapted from Jukka Heino's portspage
#
# usage: pkg-repgen [<pkgname1>..<pkgnameN>]
#
use warnings;
use strict;
use Getopt::Long;
our $prtget = "/usr/bin/prt-get"; our $prtdir;
our $title = "CRUX Packages"; our $header; our $footer;
GetOptions("prtdir=s"=>\$prtdir, "title=s"=>\$title, "header=s"=>\$header, "footer=s"=>\$footer);
# Use compression-mode defined in pkgmk-conf
our $compress = "gz";
open CONFIG, "/etc/pkgmk.conf" or die "Could not open /etc/pkgmk.conf";
while (<CONFIG>) {
$compress = $1 if m/^PKGMK_COMPRESSION_MODE="(.*)"\n/;
}
close CONFIG;
if ($#ARGV >= 0) { # single packages
pkgrepo_single();
pkgdeps_single();
pkgread();
pkginst();
} else {
if ($prtdir) {
$prtget = "$prtget --no-std-config --config-set=\"prtdir $prtdir\"";
}
pkgrepo();
pkgdeps();
pkgread();
pkginst();
}
######################## single packages ########################
# generate dependencies
sub pkgdeps_single {
print "+ Generating dependencies\n";
my $hasnew = 0;
foreach my $p (@ARGV) {
my @packages = glob("$p#*.pkg.tar.$compress");
if ($#packages == 0) {
my $found = 0;
my $package = $packages[0];
$package =~ s/#.*//;
my $deps = `$prtget printf "%e" --filter="$package"`;
if ($deps ne "") {
my $isnew = `grep "$p .*:" PKGDEPS`;
if ($isnew eq ""){ # package is new, put deps at the end.
open (my $fh, '>>PKGDEPS');
printf $fh "%-30s : %-s\n", $package, $deps;
close $fh;
$hasnew = 1;
} else {
system("sed -i \"/^$p /s/: .*\$/: $deps/\" PKGDEPS");
}
}
} else {
print "Package '$p' not found or duplicate\n"
}
}
if ($hasnew == 1){system("sort -o PKGDEPS PKGDEPS")};
}
# generate the main repository file
sub pkgrepo_single {
print "+ Generating repository\n";
my $hasnew = 0;
foreach my $p (@ARGV) {
my @packages = glob("$p#*.pkg.tar.$compress");
if ($#packages == 0) {
my $found = 0;
my $package = $packages[0];
my $name = $package;
$name =~ s/#.*//;
my $du = (-s $package);
my $md5 = `md5sum $package`;
$md5 =~ s/ .*$|\n//g;
my $des=`$prtget printf %d --filter="$name"`;
$des =~ s/:/ /g;
if ($des eq ""){$des = "N.A."};
my $flags=`$prtget printf %E:%O:%R --filter="$name"`;
if ($flags eq "") {$flags = "no:no:no"}
my $isnew = `grep "$p#" PKGREPO`;
if ($isnew eq ""){ # package is new, put it at the end
open (my $fh, '>>PKGREPO');
printf $fh "%-s:%-s:%-s:%-s:%-s\n", $package,$du,$md5,$des,$flags;
close $fh;
$hasnew = 1;
} else {
my $newp = "$package:$du:$md5:$des:$flags";
system("sed -i \"s/^$p#.*\$/$newp/\" PKGREPO");
}
#printf $fh "%-s:%-s:%-s:%-s\n", $du,$md5,$des,$flags;
} else {
print "Package '$p' not found or duplicate\n"
}
}
if ($hasnew == 1){system("sort -o PKGREPO PKGREPO")};
}
######################## full repository ########################
# generate dependencies
sub pkgdeps {
print "+ Generating dependencies\n";
my @packages = glob("*#*.pkg.tar.$compress");
open (my $fh, '>PKGDEPS');
foreach my $package (@packages) {
$package =~ s/#.*//;
my $deps = `$prtget printf "%e" --filter="$package"`;
if ($deps ne "") {
printf $fh "%-30s : %-s\n", $package, $deps;
}
}
close $fh;
}
# generate the main repository file and index page
sub pkgrepo {
print "+ Generating repository\n";
my @packages = glob("*#*.pkg.tar.$compress");
our $odd = "odd";
my $count = 0;
open (my $fh, '>PKGREPO');
printheader();
open (my $ih, '>>index.html');
foreach my $package (@packages) {
my $date = (stat($package))[9];
$count++;
$package =~ s/\n//g;
my $name = $package;
$name =~ s/#.*//;
my $du = (-s $package);
my $md5 = `md5sum $package`;
$md5 =~ s/ .*$|\n//g;
my $des=`$prtget printf %d --filter="$name"`;
$des =~ s/:/ /g;
if ($des eq ""){$des = "N.A."};
my $flags=`$prtget printf %E:%O:%R --filter="$name"`;
if ($flags eq "") {$flags = "no:no:no"}
printf $fh "%-s:%-s:%-s:%-s:%-s\n", $package,$du,$md5,$des,$flags;
my $version = $package;
$version =~ s/^.*\#//;
$version =~ s/\.pkg\.tar\.[gbx]z*//;
print $ih "<tr class=\"$odd\">";
print $ih "<td>$name</td>";
my $url = $package;
$url =~ s/\#/\%23/;
print $ih "<td><a href=\"$url\">$version</a></td>";
print $ih "<td>$des</td>";
print $ih "<td>" . isotime($date, 1) . "</td>";
print $ih "</tr>\n";
if ($odd eq "odd") { $odd = "even"; }
else { $odd = "odd"; }
}
close $fh;
close $ih;
printfooter($count);
}
# generate README file
sub pkgread {
print "+ Generating README\n";
my @packages = glob("*#*.pkg.tar.$compress");
open (my $fh, '>PKGREAD');
print $fh "# README files for repository. Do NOT remove this line.\n";
foreach my $package (@packages) {
$package =~ s/#.*//;
my $path = `$prtget path $package`;
$path =~ s/\n//g;
if (-f "$path/README"){
print $fh "##### PKGREADME: $package\n";
open(my $readme, "$path/README");
while (<$readme>){
my $line = $_;
print $fh $line;
}
close($readme);
}
}
close $fh;
}
# generate pre-post install scripts file
sub pkginst {
print "+ Generating scripts\n";
open (my $fh, '>PKGINST');
print $fh
"
#!/bin/bash
#
# PKGINST: pre-post install scripts for CRUX packages
";
my @packages = glob("*#*.pkg.tar.$compress");
foreach my $package (@packages) {
$package =~ s/#.*//;
my $path = `$prtget path $package`;
$path =~ s/\n//g;
my $normal= $package;
$normal =~ s/[^[:alnum:]]/_/g;
if (-f "$path/pre-install"){
print $fh "${normal}_pre_install() {\n";
open(my $pre, "$path/pre-install");
while (<$pre>){
my $line = $_;
print $fh $line;
}
close($pre);
print $fh "}\n\n";
}
if (-f "$path/post-install"){
print $fh "${normal}_post_install() {\n";
open(my $post, "$path/post-install");
while (<$post>){
my $line = $_;
print $fh $line;
}
close($post);
print $fh "}\n\n";
}
}
print $fh "\n\n";
print $fh 'if [ ! -z "$1" ]; then $1; fi';
print $fh "\n";
close $fh;
}
######################## html index subs ########################
sub printheader {
open (my $ih, '>index.html');
print $ih <<EOH;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
EOH
print $ih " <title>$title</title>\n";
print $ih <<EOH;
<style type="text/css">
body
{
font-family: Verdana, sans-serif;
font-size: 85%;
padding: 2em;
}
a
{
color: #67550d;
}
table
{
border: solid #e5dccf 1px;
font-size: 85%;
}
td
{
padding: 6px;
}
tr.header
{
background-color: #e5dccf;
}
tr.odd
{
background-color: #f7f3ed;
}
tr.even
{
background-color: #fcf9f8;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
EOH
print $ih " <h2>$title</h2>\n";
if ($header) {
open(FILE, $header) or die "Couldn't open header file";
while (<FILE>) {
print $ih " " . $_;
}
close(FILE);
}
print $ih " <table width=\"100%\" cellspacing=\"0\">\n";
print $ih " <tr class=\"header\"><td><b>Port</b></td><td><b>Version</b></td><td><b>Description</b></td>";
print $ih "<td><b>Last modified</b></td>";
print $ih "</tr>\n";
close($ih);
}
sub printfooter {
my $count = $_[0];
open (my $ih, '>>index.html');
print $ih " </table>\n";
print $ih " <p><b>$count packages</b></p>\n";
if ($footer) {
open(FILE, $footer) or die "Couldn't open footer file";
while (<FILE>) {
print $ih " " . $_;
}
close(FILE);
}
print $ih " <p><i>Generated by <a href=\"http://www.varlock.com\">pkg-repgen</a> on " . isotime() . ".</i></p>\n";
print $ih <<EOH;
</body>
</html>
EOH
close($ih);
}
sub isotime {
my $time = (shift or time);
my $accuracy = (shift or 2);
my @t = gmtime ($time);
my $year = $t[5] + 1900;
my $month = sprintf("%02d", $t[4] + 1);
my $day = sprintf("%02d", $t[3]);
if ($accuracy == 1) {
return "$year-$month-$day";
}
return "$year-$month-$day " . sprintf("%02d:%02d:%02d UTC", $t[2], $t[1], $t[0]);
}