chiark / gitweb /
WIP commod-update-receiver testing - seems to work
[ypp-sc-tools.db-live.git] / pctb / Commods.pm
index 6f3a177ee8870c8ca471521058c4b4d1b0d20728..5eb9c7bba749cb1a09888c0b32c9bd9608cae499 100644 (file)
@@ -1,6 +1,7 @@
 
 package Commods;
 use IO::File;
+use HTTP::Request::Common ();
 
 use strict;
 use warnings;
@@ -10,15 +11,20 @@ BEGIN {
     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
     $VERSION     = 1.00;
     @ISA         = qw(Exporter);
-    @EXPORT      = qw(&parse_masters %oceans %commods
-                     &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap);
+    @EXPORT      = qw(&parse_masters %oceans %commods %clients
+                     &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap
+                     &get_our_version
+                     &pipethrough_prep &pipethrough_run
+                     &pipethrough_run_gzip
+                     &cgipostform);
     %EXPORT_TAGS = ( );
 
     @EXPORT_OK   = qw();
 }
 
-our %oceans; # eg $oceans{'Midnight'}{'Ruby'}{'Eta Island'}= $sources
+our %oceans; # eg $oceans{'Midnight'}{'Ruby'}{'Eta Island'}= $sources;
 our %commods; # eg $commods{'Fine black cloth'}= $sources;
+our %clients; # eg $clients{'ypp-sc-tools'}= [ qw(last-page) ];
 # $sources = 's[l]b';
 #       's' = Special Circumstances; 'l' = local ; B = with Bleach
 
@@ -50,6 +56,13 @@ sub parse_master_master1 ($$) {
                    $oceans{$ocean}{$arch}{$_} .= $src;
                };
            });
+       } elsif (m/^client (\S+.*\S)$/) {
+           my $client= $1;
+           $clients{$client}= [ ];
+           @ctx= (sub {
+               my $bug= $_;
+               push @{ $clients{$client} }, $bug;
+           });
        } elsif (s/^ +//) {
            my $indent= length $&;
            die "wrong indent $indent" unless defined $ctx[$indent-1];
@@ -100,4 +113,77 @@ sub parse_pctb_commodmap () {
     return 1;
 }
 
+sub get_our_version ($$) {
+    my ($aref,$prefix) = @_;
+    $aref->{"${prefix}name"}= 'ypp-sc-tools yarrg';
+    $aref->{"${prefix}fixes"}= 'lastpage';
+
+    my $version= `git-describe --tags HEAD`; $? and die $?;
+    chomp($version);
+    $aref->{"${prefix}version"}= $version;
+    return $aref;
+}
+
+sub pipethrough_prep () {
+    my $tf= IO::File::new_tmpfile() or die $!;
+    return $tf;
+}
+    
+sub pipethrough_run ($$$@) {
+    my ($tf, $childprep, $cmd, @a) = @_;
+    $tf->flush or die $!;
+    $tf->seek(0,0) or die $!;
+    my $child= open GZ, "-|"; defined $child or die $!;
+    if (!$child) {
+       open STDIN, "<&", $tf;
+       &$childprep() if defined $childprep;
+       exec $cmd @a; die $!;
+    }
+    my $r;
+    { undef $/; $!=0; $r= <GZ>; }
+    defined $r or die $!;
+    close GZ or die "$! $?";  die $? if $?;
+    return $r;
+}
+sub pipethrough_run_gzip ($) {
+    pipethrough_run($_[0],undef,'gzip','gzip');
+}
+
+sub cgipostform ($$$) {
+    my ($ua, $url, $form) = @_;
+    my $req= HTTP::Request::Common::POST($url,
+                                        Content => $form,
+                                        Content_Type => 'form-data');
+    if ($url =~ m,^\.?/,) {
+       my $tf= pipethrough_prep();
+       print $tf $req->content() or die $!;
+#print STDERR "[[[",$req->content(),"]]]";
+       my $out= pipethrough_run($tf, sub {
+           $ENV{'REQUEST_METHOD'}= 'POST';
+           $ENV{'QUERY_STRING'}= '';
+           $ENV{'PATH_TRANSLATED'}= $url;
+           $ENV{'PATH_INFO'}= '';
+           $ENV{'HTTP_HOST'}= 'localhost';
+           $ENV{'REMOTE_ADDR'}= '127.0.0.1';
+           $ENV{'GATEWAY_INTERFACE'}= 'CGI/1.1';
+           $ENV{'DOCUMENT_ROOT'}= '.';
+           $ENV{'SCRIPT_FILENAME'}= $url;
+           $ENV{'SCRIPT_NAME'}= $url;
+           $ENV{'HTTP_USER_AGENT'}= 'Commods.pm local test';
+
+           foreach my $f (qw(Content_Length Content_Type)) {
+               $ENV{uc $f}= $req->header($f);
+           }
+#system 'printenv >&2';
+       }, "$url", "$url");
+       $out =~ s/\r\n/\n/g;
+       $out =~ m,^Content-Type: text/plain.*\n\n, or die "$out ?";
+       return $';
+    } else {
+       my $resp= $ua->request($url,$req);
+       die $resp->status_line unless $resp->is_success;
+       return $resp->content();
+    }
+}
+
 1;