OpenPKG CVS Repository
  http://cvs.openpkg.org/
  ____________________________________________________________________________

  Server: cvs.openpkg.org                  Name:   Michael van Elst
  Root:   /e/openpkg/cvs                   Email:  [EMAIL PROTECTED]
  Module: openpkg-re                       Date:   25-Nov-2002 15:58:48
  Branch: HEAD                             Handle: 2002112514584700

  Modified files:
    openpkg-re              openpkg-index

  Log:
    -r option now gets full resource string
    scan directories recursively and reference existing index files.
    -c option pipes output through bzip2

  Summary:
    Revision    Changes     Path
    1.17        +100 -32    openpkg-re/openpkg-index
  ____________________________________________________________________________

  Index: openpkg-re/openpkg-index
  ============================================================
  $ cvs diff -u -r1.16 -r1.17 openpkg-index
  --- openpkg-re/openpkg-index  25 Nov 2002 13:40:19 -0000      1.16
  +++ openpkg-re/openpkg-index  25 Nov 2002 14:58:47 -0000      1.17
  @@ -25,17 +25,20 @@
   ##  SUCH DAMAGE.
   ##
   
  +require 5;
  +
   use strict;
   
   use Getopt::Std;
   getopts('r:p:o:i');
  -use vars qw/$opt_r $opt_p $opt_o $opt_i/;
  +use vars qw/$opt_r $opt_p $opt_o $opt_c $opt_i/;
   
   use FileHandle;
   use DirHandle;
   
   my $RPM = 'rpm';
   my $R2C = 'rpm2cpio';
  +my $BZ  = 'bzip2 -9';
   
   #########################################################################
   
  @@ -318,12 +321,12 @@
   # start of XML file
   #
   sub xml_head ($$) {
  -    my($fh,$rel) = @_;
  +    my($fh,$res) = @_;
       print $fh <<EOFEOF;
   <?xml version="1.0" encoding="iso-8859-1"?>
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
            xmlns="http://www.openpkg.org/xml-rdf-index/0.9";>
  -  <Repository rdf:resource="OpenPKG-$rel/Source/">
  +  <Repository rdf:resource="$res">
   EOFEOF
   }
   
  @@ -421,7 +424,18 @@
   }
   
   #
  -# translate attributs from %$a as generated by package2data
  +# send out reference to another RDF
  +#
  +sub xml_reference ($$$) {
  +    my($fh, $res, $href) = @_;
  +
  +    print $fh <<EOFEOF;
  +    <Repository rdf:resource="$res" href="$href"/>
  +EOFEOF
  +}
  +
  +#
  +# translate attributes from %$a as generated by package2data
   # into XML and write to file $fh
   #
   sub xml_record ($$$) {
  @@ -530,7 +544,7 @@
   
   sub rpm2spec ($) {
       my($fn) = @_;
  -    my($pipe) = new FileHandle("$R2C '$fn' |")
  +    my($pipe) = new FileHandle "$R2C '$fn' |"
           or die "FATAL: cannot read '$fn' ($!)\n";
       my($buf,@hdr,$n,$m,$name,$step);
       my($spec);
  @@ -607,7 +621,7 @@
   Description %{Description}
   EOFEOF
   
  -    $pipe = new FileHandle("$RPM -qp --qf '$q' '$fn' |")
  +    $pipe = new FileHandle "$RPM -qp --qf '$q' '$fn' |"
           or die "FATAL: cannot read '$fn' ($!)\n";
       while (<$pipe>) {
           if (/^(\S+)\s+(.*?)\s*$/) {
  @@ -649,6 +663,16 @@
   
   #####################################################################
   
  +sub getindex ($) {
  +    my($dir) = @_;
  +    my(@idx) = sort { -M $a <=> -M $b; }
  +               grep { -f $_ }
  +                ( <$dir/00INDEX.rdf>, <$dir/00INDEX.rdf.*> );
  +
  +    return unless @idx;
  +    return $idx[0];
  +}
  +
   sub list_specdir ($) {
       my($dir) = @_;
       my($dh,$d,$path);
  @@ -656,7 +680,7 @@
   
       $dh = new DirHandle($dir);
       while ($d = $dh->read) {
  -        next if $d =~ /^\.$/;
  +        next if $d =~ /^\./;
           $path = "$dir/$d/$d.spec";
           push @list, $path if -f $path;
       }
  @@ -667,49 +691,82 @@
   sub list_rpmdir ($) {
       my($dir) = @_;
       my($dh,$d,$path);
  -    my(@list);
  +    my(@list,$idx,$sub);
   
       $dh = new DirHandle($dir);
       while ($d = $dh->read) {
  -        next if $d =~ /^\.$/ || $d !~ /\.rpm$/;
  +        next if $d =~ /^\./;
           $path = "$dir/$d";
  -        push @list, $path if -f $path;
  +        if (-d $path) {
  +            $idx = getindex($path);
  +            if (defined $idx) {
  +                push @list, $idx;
  +            } else {
  +                $sub = list_rpmdir($path);
  +                push @list, @$sub;
  +                undef $sub;
  +            }
  +        } else {
  +            next unless $d =~ /\.rpm$/ && -f $path;
  +            push @list, $path;
  +        }
       }
   
       return \@list;
   }
   
  +#####################################################################
  +
   sub readfile ($) {
       my($fn) = @_;
  -    my($fh) = new FileHandle $fn,'r'
  +    my($fh) = new FileHandle "< $fn"
           or die "FATAL: cannot read '$fn' ($!)\n";
       my(@l) = <$fh>;
       $fh->close;
       return join('',@l);
   }
   
  +sub relpath ($$) {
  +    my($prefix,$path) = @_;
  +    $path =~ s/^\Q$prefix\E\///s;
  +    return $path;
  +}
  +
  +sub dirname ($) {
  +    my($path) = @_;
  +    $path =~ s/\/[^\/]*$//s;
  +    return $path.'/';
  +}
  +
   #####################################################################
   
  -sub write_index ($$$$) {
  -    my($fh,$release,$platform,$list) = @_;
  -    my($a,$h,$s);
  +sub write_index ($$$$$) {
  +    my($fh,$prefix,$resource,$platform,$list) = @_;
  +    my($a,$h,$r,$spec);
   
       foreach (@$list) {
           $a = undef;
           $h = undef;
  +        $r = undef;
           if (/\.spec$/) {
  -            $s = readfile($_);
  -            $a = spec2data($s);
  +            $spec = readfile($_);
  +            $a    = spec2data($spec);
           } elsif (/([^\/]+\.src\.rpm)$/) {
  -            $h = $1;
  -            $s = rpm2spec($_);
  -            $a = spec2data($s);
  +            $h    = relpath($prefix, $_);
  +            $spec = rpm2spec($_);
  +            $a    = spec2data($spec);
           } elsif (/([^\/]+\.rpm)$/) {
  -            $h = $1;
  -            $a = rpm2data($_, $platform);
  +            $h    = relpath($prefix, $_);
  +            $a    = rpm2data($_, $platform);
  +        } elsif (/([^\/]+\.rdf[^\/]*)$/) {
  +            $h    = relpath($prefix, $_);
  +            $r    = $resource.dirname($h);
           }
  +
           if ($a) {
               xml_record($fh, $a, $h);
  +        } elsif ($r) {
  +            xml_reference($fh, $r, $h);
           } else {
               warn "ERROR: cannot process $_\n";
           }
  @@ -721,24 +778,34 @@
   my($prefix,$list,$fh);
   
   if ($#ARGV < 0) {
  -    print "usage: $0 [-r release] [-p platform] [-o index.rdf] [-i] dir ...\n";
  +    print "usage: $0 [-r resource] [-p platform] [-o index.rdf] [-c] [-i] dir 
...\n";
       die "\n";
   }
   
  -$opt_r = 'CURRENT' unless defined $opt_r;
  +$opt_r = 'OpenPKG-CURRENT/Source/' unless defined $opt_r;
   $opt_p = 'unknown' unless defined $opt_p;
   
  -if ($opt_r !~ /^(CURRENT|\d+\.\d+)$/) {
  -    die "FATAL: you must specify a release tag (CURRENT or x.y)\n";
  -}
  -
   if (defined $opt_o) {
  -    $fh = new FileHandle $opt_o,'w'
  -        or die "FATAL: cannot write '$opt_o' ($!)\n";
  +    if ($opt_c) {
  +        $fh = new FileHandle "| $BZ -c > '$opt_o'"
  +            or die "FATAL: cannot write '$opt_o' ($!)\n";
  +    } else {
  +        $fh = new FileHandle "> $opt_o"
  +            or die "FATAL: cannot write '$opt_o' ($!)\n";
  +    }
   } else {
  -    $fh = \*STDOUT;
  +    if ($opt_c) {
  +        $fh = new FileHandle "| $BZ -c"
  +            or die "FATAL: cannot write to stdout ($!)\n";
  +    } else {
  +        $fh = new FileHandle ">&=1"
  +            or die "FATAL: cannot write to stdout ($!)\n";
  +    }
   }
   
  +# sanitize resource path
  +$opt_r =~ s/\/*$/\//s;
  +
   xml_head($fh, $opt_r);
   foreach $prefix (@ARGV) {
       die "FATAL: $prefix is not a directory\n" unless -d $prefix;
  @@ -747,8 +814,9 @@
       } else {
           $list = list_specdir($prefix);
       }
  -    write_index($fh, $opt_r, $opt_p, $list);
  +    write_index($fh, $prefix, $opt_r, $opt_p, $list);
   }
   xml_foot($fh);
  -$fh->close if defined $opt_o;
   
  +$fh->close
  +    or die "FATAL: write error on output ($!)\n";
______________________________________________________________________
The OpenPKG Project                                    www.openpkg.org
CVS Repository Commit List                     [EMAIL PROTECTED]

Reply via email to