4 use HTTP::Request::Common ();
11 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
14 @EXPORT = qw(&parse_masters %oceans %commods %clients
15 &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap
17 &pipethrough_prep &pipethrough_run
25 our %oceans; # eg $oceans{'Midnight'}{'Ruby'}{'Eta Island'}= $sources;
26 our %commods; # eg $commods{'Fine black cloth'}= $sources;
27 our %clients; # eg $clients{'ypp-sc-tools'}= [ qw(last-page) ];
29 # 's' = Special Circumstances; 'l' = local ; B = with Bleach
31 our (%pctb_commodmap,@pctb_commodmap);
33 my %colours; # eg $colours{'c'}{'black'}= $sources
34 my @rawcm; # eg $rawcm[0]='fine rum'; $rawcm[1]='fine %c cloth'
36 sub parse_master_master1 ($$) {
38 my $mm= new IO::File $mmfn, 'r' or die "$mmfn $!";
46 @ctx= (sub { $colours{$colourkind}{lc $_} .= $src; });
47 } elsif (m/^commods$/) {
48 @ctx= (sub { push @rawcm, lc $_; });
49 } elsif (m/^ocean (\w+)$/) {
52 $ocean or die; # ref to $ocean needed to work
56 $oceans{$ocean}{$arch}{$_} .= $src;
59 } elsif (m/^client (\S+.*\S)$/) {
61 $clients{$client}= [ ];
64 push @{ $clients{$client} }, $bug;
67 my $indent= length $&;
68 die "wrong indent $indent" unless defined $ctx[$indent-1];
69 &{ $ctx[$indent-1] }();
74 $mm->error and die $!;
77 #print Dumper(\%oceans);
78 #print Dumper(\@rawcm);
85 if ($s !~ m/\%(\w+)/) { $commods{ucfirst $s} .= $ss; return; }
86 die "unknown $&" unless defined $colours{$1};
87 foreach my $c (keys %{ $colours{$1} }) {
88 &$ca($`.$c.$', $ss .'%'. $colours{$1}{$c});
91 foreach (@rawcm) { &$ca($_,$src); }
94 sub parse_masters () {
95 parse_master_master1('master-master.txt','s');
98 sub parse_pctb_commodmap () {
99 undef %pctb_commodmap;
100 foreach my $commod (keys %commods) { $commods{$commod} =~ s/b//; }
102 my $c= new IO::File '_commodmap.tsv' or die $!;
103 if (!$c) { $!==&ENOENT or die $!; return 0; }
106 m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
107 die if defined $pctb_commodmap{$1}; $pctb_commodmap{$1}= $2;
108 die if defined $pctb_commodmap[$2]; $pctb_commodmap[$2]= $1;
111 $c->error and die $!;
116 sub get_our_version ($$) {
117 my ($aref,$prefix) = @_;
118 $aref->{"${prefix}name"}= 'ypp-sc-tools yarrg';
119 $aref->{"${prefix}fixes"}= 'lastpage';
121 my $version= `git-describe --tags HEAD`; $? and die $?;
123 $aref->{"${prefix}version"}= $version;
127 sub pipethrough_prep () {
128 my $tf= IO::File::new_tmpfile() or die $!;
132 sub pipethrough_run ($$$@) {
133 my ($tf, $childprep, $cmd, @a) = @_;
134 $tf->flush or die $!;
135 $tf->seek(0,0) or die $!;
136 my $child= open GZ, "-|"; defined $child or die $!;
138 open STDIN, "<&", $tf;
139 &$childprep() if defined $childprep;
140 exec $cmd @a; die $!;
143 { undef $/; $!=0; $r= <GZ>; }
144 defined $r or die $!;
145 close GZ or die "$! $?"; die $? if $?;
148 sub pipethrough_run_gzip ($) {
149 pipethrough_run($_[0],undef,'gzip','gzip');
152 sub cgipostform ($$$) {
153 my ($ua, $url, $form) = @_;
154 my $req= HTTP::Request::Common::POST($url,
156 Content_Type => 'form-data');
157 if ($url =~ m,^\.?/,) {
158 my $tf= pipethrough_prep();
159 print $tf $req->content() or die $!;
160 #print STDERR "[[[",$req->content(),"]]]";
161 my $out= pipethrough_run($tf, sub {
162 $ENV{'REQUEST_METHOD'}= 'POST';
163 $ENV{'QUERY_STRING'}= '';
164 $ENV{'PATH_TRANSLATED'}= $url;
165 $ENV{'PATH_INFO'}= '';
166 $ENV{'HTTP_HOST'}= 'localhost';
167 $ENV{'REMOTE_ADDR'}= '127.0.0.1';
168 $ENV{'GATEWAY_INTERFACE'}= 'CGI/1.1';
169 $ENV{'DOCUMENT_ROOT'}= '.';
170 $ENV{'SCRIPT_FILENAME'}= $url;
171 $ENV{'SCRIPT_NAME'}= $url;
172 $ENV{'HTTP_USER_AGENT'}= 'Commods.pm local test';
174 foreach my $f (qw(Content_Length Content_Type)) {
175 $ENV{uc $f}= $req->header($f);
177 #system 'printenv >&2';
180 $out =~ m,^Content-Type: text/plain.*\n\n, or die "$out ?";
183 my $resp= $ua->request($url,$req);
184 die $resp->status_line unless $resp->is_success;
185 return $resp->content();