summaryrefslogtreecommitdiff
path: root/scripts/pkg-repgen.pl
blob: 1545b658463667928d9dba1ad5d2a5ed6335f120 (plain)
    1 #!/usr/bin/env perl
    2 #
    3 # pkg-repgen: generates a binary repository for pkg-get
    4 #
    5 # requires prt-get
    6 #
    7 # html index generation code adapted from Jukka Heino's portspage
    8 #
    9 # usage: pkg-repgen [<pkgname1>..<pkgnameN>]
   10 #
   11 
   12 use warnings;
   13 use strict;
   14 use Getopt::Long;
   15 
   16 our $title = "CRUX Packages"; our $header; our $footer;
   17 GetOptions("title=s"=>\$title, "header=s"=>\$header, "footer=s"=>\$footer);
   18 
   19 if ($#ARGV >= 0) { # single packages
   20     pkgrepo_single();
   21     pkgdeps_single();
   22     pkgread();
   23     pkginst();
   24 } else {
   25     pkgrepo();
   26     pkgdeps();
   27     pkgread();
   28     pkginst();
   29 }
   30 
   31 ######################## single packages ########################
   32 
   33 # generate dependencies
   34 sub pkgdeps_single {
   35     print "+ Generating dependencies\n";
   36     my $hasnew = 0;
   37     foreach my $p (@ARGV) {
   38         my @packages = glob("$p#*.pkg.tar.gz");
   39         if ($#packages == 0) {
   40             my $found = 0;
   41             my $package = $packages[0];
   42             $package =~ s/#.*//;
   43             my $deps = `prt-get printf "%e" --filter="$package"`;
   44             if ($deps ne "") {
   45                 my $isnew = `grep "$p .*:" PKGDEPS`;
   46                 if ($isnew eq ""){ # package is new, put deps at the end.
   47                     open (my $fh, '>>PKGDEPS');
   48                     printf $fh "%-30s : %-s\n", $package, $deps;
   49                     close $fh;
   50                     $hasnew = 1;
   51                 } else {
   52                     system("sed -i \"/^$p /s/: .*\$/: $deps/\" PKGDEPS");
   53                 }
   54             }
   55             
   56         } else {
   57             print "Package '$p' not found or duplicate\n"
   58         }
   59 	}
   60     if ($hasnew == 1){system("sort -o PKGDEPS PKGDEPS")};
   61 }
   62 
   63 # generate the main repository file
   64 sub pkgrepo_single {
   65     print "+ Generating repository\n";
   66     my $hasnew = 0;
   67     foreach my $p (@ARGV) {
   68         my @packages = glob("$p#*.pkg.tar.gz");
   69         if ($#packages == 0) {
   70             my $found = 0;
   71             my $package = $packages[0];
   72             my $name = $package;
   73             $name =~ s/#.*//;
   74             my $du = (-s $package);
   75             my $md5 = `md5sum $package`;
   76             $md5 =~ s/  .*$|\n//g;
   77             my $des=`prt-get printf %d --filter="$name"`;
   78             $des =~ s/:/ /g;
   79             if ($des eq ""){$des = "N.A."};
   80             my $flags=`prt-get printf %E:%O:%R --filter="$name"`;
   81             if ($flags eq "") {$flags = "no:no:no"}
   82             my $isnew = `grep "$p#" PKGREPO`;
   83             if ($isnew eq ""){ # package is new, put it at the end
   84                 open (my $fh, '>>PKGREPO');
   85                 printf $fh "%-s:%-s:%-s:%-s:%-s\n", $package,$du,$md5,$des,$flags;
   86                 close $fh;
   87                 $hasnew = 1;
   88             } else {
   89                 my $newp = "$package:$du:$md5:$des:$flags";
   90                 system("sed -i \"s/^$p#.*\$/$newp/\" PKGREPO");
   91             }
   92             #printf $fh "%-s:%-s:%-s:%-s\n", $du,$md5,$des,$flags;
   93         } else {
   94             print "Package '$p' not found or duplicate\n"
   95         }
   96     }
   97     if ($hasnew == 1){system("sort -o PKGREPO PKGREPO")};
   98 }
   99 
  100 
  101 ######################## full repository ########################
  102 
  103 # generate dependencies
  104 sub pkgdeps {
  105     print "+ Generating dependencies\n";
  106     my @packages = glob("*#*.pkg.tar.gz");
  107     open (my $fh, '>PKGDEPS');
  108 	foreach my $package (@packages) {
  109         $package =~ s/#.*//;
  110         my $deps = `prt-get printf "%e" --filter="$package"`;
  111         if ($deps ne "") {
  112             printf $fh "%-30s : %-s\n", $package, $deps;
  113         }
  114 	}
  115     close $fh;
  116 }
  117 
  118 # generate the main repository file and index page
  119 sub pkgrepo {
  120     print "+ Generating repository\n";
  121     my @packages = glob("*#*.pkg.tar.gz");
  122 	our $odd = "odd";
  123 	my $count = 0;
  124     open (my $fh, '>PKGREPO');
  125 	printheader();
  126 	open (my $ih, '>>index.html');
  127 	foreach my $package (@packages) {
  128 		my $date = (stat($package))[9];
  129 		$count++;
  130         $package =~ s/\n//g;
  131         my $name = $package;
  132         $name =~ s/#.*//;
  133         my $du = (-s $package);
  134         my $md5 = `md5sum $package`;
  135         $md5 =~ s/  .*$|\n//g;
  136         my $des=`prt-get printf %d --filter="$name"`;
  137         $des =~ s/:/ /g;
  138         if ($des eq ""){$des = "N.A."};
  139         my $flags=`prt-get printf %E:%O:%R --filter="$name"`;
  140         if ($flags eq "") {$flags = "no:no:no"}
  141         printf $fh "%-s:%-s:%-s:%-s:%-s\n", $package,$du,$md5,$des,$flags;
  142 		my $version = $package;
  143 		$version =~ s/^.*\#//;
  144 		$version =~ s/\.pkg\.tar\.gz//;
  145 		print $ih "   <tr class=\"$odd\">";
  146 		print $ih "<td>$name</td>";
  147 		my $url = $package;
  148 		$url =~ s/\#/\%23/;
  149 		print $ih "<td><a href=\"$url\">$version</a></td>";
  150 		print $ih "<td>$des</td>";
  151 		print $ih "<td>" . isotime($date, 1) . "</td>";
  152 		print $ih "</tr>\n";
  153 
  154 		if ($odd eq "odd") { $odd = "even"; }
  155 		else { $odd = "odd"; }
  156     }
  157     close $fh;
  158     close $ih;
  159 	printfooter($count);
  160 }
  161 
  162 # generate README file
  163 sub pkgread {
  164     print "+ Generating README\n";
  165     my @packages = glob("*#*.pkg.tar.gz");
  166     open (my $fh, '>PKGREAD');
  167     print $fh "# README files for repository. Do NOT remove this line.\n";
  168 	foreach my $package (@packages) {
  169         $package =~ s/#.*//;
  170         my $path = `prt-get path $package`;
  171         $path =~ s/\n//g;
  172         if (-f "$path/README"){
  173             print $fh "##### PKGREADME: $package\n";
  174             open(my $readme, "$path/README");
  175             while (<$readme>){
  176 	            my $line = $_;
  177                 print $fh $line;
  178             }
  179             close($readme);
  180         }
  181     }
  182     close $fh;
  183 }
  184 
  185 # generate pre-post install scripts file
  186 sub pkginst {
  187     print "+ Generating scripts\n";
  188     open (my $fh, '>PKGINST');
  189     print $fh 
  190 "
  191 #!/bin/bash
  192 #
  193 # PKGINST: pre-post install scripts for CRUX packages
  194 ";
  195     my @packages = glob("*#*.pkg.tar.gz");
  196 	foreach my $package (@packages) {
  197         $package =~ s/#.*//;
  198         my $path = `prt-get path $package`;
  199         $path =~ s/\n//g;
  200         my $normal= $package;
  201         $normal =~ s/[^[:alnum:]]/_/g;
  202         if (-f "$path/pre-install"){
  203             print $fh "${normal}_pre_install() {\n";
  204             open(my $pre, "$path/pre-install");
  205             while (<$pre>){
  206 	            my $line = $_;
  207                 print $fh $line;
  208             }
  209             close($pre);
  210             print $fh "}\n\n";
  211         }
  212         if (-f "$path/post-install"){
  213             print $fh "${normal}_post_install() {\n";
  214             open(my $post, "$path/post-install");
  215             while (<$post>){
  216 	            my $line = $_;
  217                 print $fh $line;
  218             }
  219             close($post);
  220             print $fh "}\n\n";
  221         }
  222     }
  223     print $fh "\n\n";
  224     print $fh 'if [ ! -z "$1" ]; then $1; fi';
  225     print $fh "\n";
  226     close $fh;
  227 }
  228 
  229 
  230 ######################## html index subs ########################
  231 
  232 sub printheader {
  233     open (my $ih, '>index.html');
  234 	print $ih <<EOH;
  235 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  236     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  237 <html xmlns="http://www.w3.org/1999/xhtml">
  238  <head>
  239 EOH
  240 
  241 	print $ih "  <title>$title</title>\n";
  242 
  243 	print $ih <<EOH;
  244   <style type="text/css">
  245    body
  246    {
  247     font-family: Verdana, sans-serif;
  248     font-size: 85%;
  249     padding: 2em;
  250    }
  251    a
  252    {
  253     color: #67550d;
  254    }
  255    table
  256    {
  257     border: solid #e5dccf 1px;
  258     font-size: 85%;
  259    }
  260    td
  261    {
  262     padding: 6px;
  263    }
  264    tr.header
  265    {
  266     background-color: #e5dccf;
  267    }
  268    tr.odd
  269    {
  270     background-color: #f7f3ed;
  271    }
  272    tr.even
  273    {
  274     background-color: #fcf9f8;
  275    }
  276   </style>
  277   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  278  </head>
  279  <body>
  280 EOH
  281 
  282 	print $ih "  <h2>$title</h2>\n";
  283 	if ($header) {
  284 		open(FILE, $header) or die "Couldn't open header file";
  285 		while (<FILE>) {
  286 			print $ih "  " . $_;
  287 		}
  288 		close(FILE);
  289 	}
  290 
  291 	print $ih "  <table width=\"100%\" cellspacing=\"0\">\n";
  292 	print $ih "   <tr class=\"header\"><td><b>Port</b></td><td><b>Version</b></td><td><b>Description</b></td>";
  293 	print $ih "<td><b>Last modified</b></td>";
  294 	print $ih "</tr>\n";
  295 	close($ih);
  296 }
  297 
  298 sub printfooter {
  299 	my $count = $_[0];
  300     open (my $ih, '>>index.html');
  301 	print $ih "  </table>\n";
  302 	print $ih "  <p><b>$count packages</b></p>\n";
  303 	if ($footer) {
  304 		open(FILE, $footer) or die "Couldn't open footer file";
  305 		while (<FILE>) {
  306 			print $ih "  " . $_;
  307 		}
  308 		close(FILE);
  309 	}
  310 	print $ih "  <p><i>Generated by <a href=\"http://www.varlock.com/blosxom.cgi/CruxProjects/pkgget/index.html\">pkg-repgen</a> on " . isotime() . ".</i></p>\n";
  311 	print $ih <<EOH;
  312  </body>
  313 </html>
  314 EOH
  315 	close($ih);
  316 
  317 }
  318 
  319 sub isotime {
  320 	my $time = (shift or time);
  321 	my $accuracy = (shift or 2);
  322 	my @t = gmtime ($time);
  323 	my $year = $t[5] + 1900;
  324 	my $month = sprintf("%02d", $t[4] + 1);
  325 	my $day = sprintf("%02d", $t[3]);
  326 
  327 	if ($accuracy == 1) {
  328 		return "$year-$month-$day";
  329 	}
  330 
  331 	return "$year-$month-$day " . sprintf("%02d:%02d:%02d UTC", $t[2], $t[1], $t[0]);
  332 }

Generated by cgit