]> git.donarmstrong.com Git - debbugs.git/blobdiff - bin/local-debbugs
because pkgreport.cgi uses taint, use perl -I to set libraries
[debbugs.git] / bin / local-debbugs
index acdfa2eb6b47e90877a4dfa48cf3d5a339ec718a..070b5a4b73fc94554fcf8c41c4b049a539710c8d 100755 (executable)
@@ -17,7 +17,7 @@ local-debbugs - use a local mirror of debbugs
 
 =head1 SYNOPSIS
 
- [options]
local-debbugs [options]
 
  Options:
   --mirror, -M update local mirror
@@ -39,19 +39,19 @@ Update the local mirror of debbugs bugs
 =item B<--daemon, -D>
 
 Start up the daemon on the configured local port to serve bugs which
-have been previously retried
+have been previously retrieved.
 
 =item B<--search, -S>
 
 Cause the running daemon to show the pkgreport.cgi page corresponding
-to the search by invoking sensible-browser and an appropriate url
+to the search by invoking sensible-browser and an appropriate url.
 
 =item B<--show, -s>
 
 Cause the running daemon to show the bugreport.cgi page corresponding
-to the bug by invoking sensible-browser and an appropriate url
+to the bug by invoking sensible-browser and an appropriate url.
 
-=item B<--port,-p>
+=item B<--port, -p>
 
 The port that the daemon is running on (or will be running on.)
 
@@ -65,7 +65,7 @@ Defaults to ~/.debbugs/bugs_to_get
 
 =item B<--bug-site>
 
-Hostname for a site which is running a debbugs install
+Hostname for a site which is running a debbugs install.
 Defaults to bugs.debian.org
 
 =item B<--bug-mirror>
@@ -76,7 +76,7 @@ Defaults to bugs-mirror.debian.org
 
 =item B<--debug, -d>
 
-Debug verbosity. (Default 0)
+Debug verbosity.
 
 =item B<--help, -h>
 
@@ -90,6 +90,21 @@ Display this manual.
 
 =head1 EXAMPLES
 
+=over
+
+=item Update the local mirror
+
+ local-debbugs --mirror
+
+=item Start up the local-debbugs daemon
+
+ local-debbugs --daemon
+
+=item Search for bugs with severity serious
+
+ local-debbugs --search severity:serious
+
+=back
 
 =cut
 
@@ -98,16 +113,15 @@ use vars qw($DEBUG);
 
 use User;
 use Config::Simple;
+use File::Basename qw(dirname);
 use File::Temp qw(tempdir);
 use Params::Validate qw(validate_with :types);
 use POSIX 'setsid';
-use Debbugs::Common qw(checkpid lockpid get_hashname);
-use Debbugs::Mail qw(get_addresses);
 use SOAP::Lite;
 use IPC::Run;
 use IO::File;
 use File::Path;
-
+use File::Spec;
 
 my %options = (debug           => 0,
               help            => 0,
@@ -115,8 +129,7 @@ my %options = (debug           => 0,
               verbose         => 0,
               quiet           => 0,
               detach          => 1,
-              cgi_bin         => '/var/lib/debbugs/www/cgi',
-              css             => '/var/lib/debbugs/www/bugs.css',
+              git_mode        => -d (dirname(__FILE__).'/../.git') ? 1 : 0,
               bug_site        => 'bugs.debian.org',
               bug_mirror      => 'bugs-mirror.debian.org',
               );
@@ -128,7 +141,7 @@ my %option_defaults = (port => 8080,
                      );
 
 GetOptions(\%options,
-          'daemon|D','show|s','search|select|S','mirror|M', 'stop',
+          'daemon|D','show|s','search|select|S','mirror|M', 'stop|exit|quit',
           'detach!',
           'css=s','cgi_bin|cgi-bin|cgi=s',
           'verbose|v+','quiet|q+',
@@ -136,6 +149,22 @@ GetOptions(\%options,
           'bug_mirror|bug-mirror=s',
           'debug|d+','help|h|?','man|m');
 
+if ($options{git_mode}) {
+    my $base_dir = dirname(File::Spec->rel2abs(dirname(__FILE__)));
+    $options{cgi_bin} = "$base_dir/cgi" unless defined $options{cgi_bin};
+    $options{css} = "$base_dir/html/bugs.css" unless defined $options{css};
+    $options{template_dir} = "$base_dir/templates";
+    $options{base_dir} = $base_dir;
+    eval "use lib '$options{base_dir}'";
+} else {
+    $options{cgi_bin} = '/var/lib/debbugs/www/cgi';
+    $options{css} = '/var/lib/debbugs/www/bugs.css';
+    $options{template_dir} = "/usr/share/debbugs/templates";
+}
+
+eval "use Debbugs::Common qw(checkpid lockpid get_hashname)";
+eval "use Debbugs::Mail qw(get_addresses)";
+
 pod2usage() if $options{help};
 pod2usage({verbose=>2}) if $options{man};
 
@@ -143,7 +172,7 @@ $DEBUG = $options{debug};
 
 my @USAGE_ERRORS;
 if (1 != grep {exists $options{$_}} qw(daemon show search mirror stop)) {
-     push @USAGE_ERRORS,"You must pass one (and only one) of --daemon --show --search or --mirror";
+     push @USAGE_ERRORS,"You must pass one (and only one) of --daemon --show --search --mirror or --stop";
 }
 $options{verbose} = $options{verbose} - $options{quiet};
 
@@ -168,6 +197,25 @@ if ($options{daemon}) {
          print STDERR "Unable to determine if daemon is running: $!\n";
          exit 1;
      }
+     my $conf = IO::File->new($options{mirror_location}.'/debbugs_config_local','w') or
+        die "Unable to open $options{mirror_location}/debbugs_config_local for writing: $!";
+     print {$conf} <<"EOF";
+\$gConfigDir = "$options{mirror_location}";
+\$gSpoolDir = "$options{mirror_location}";
+\$gTemplateDir = "$options{template_dir}";
+\$gWebHost = 'localhost:$options{port}';
+\$gPackageSource = '';
+\$gPseudoDescFile = '';
+\$gPseudoMaintFile = '';
+\$gMaintainerFile = '';
+\$gMaintainerFileOverride = '';
+\$config{source_maintainer_file} = '';
+\$config{source_maintainer_file_override} = '';
+\$gProject = 'Local Debbugs';
+1;
+EOF
+     close $conf;
+     $ENV{DEBBUGS_CONFIG_FILE} = $options{mirror_location}.'/debbugs_config_local';
      # ok, now lets daemonize
 
      # XXX make sure that all paths have been turned into absolute
@@ -191,7 +239,8 @@ if ($options{daemon}) {
          package local_debbugs::server;
          use IO::File;
          use HTTP::Server::Simple;
-         use base qw(HTTP::Server::Simple::CGI);
+         use File::Basename qw(dirname);
+         use base qw(HTTP::Server::Simple::CGI HTTP::Server::Simple::CGI::Environment);
 
          sub net_server {
               return 'Net::Server::Fork';
@@ -207,6 +256,7 @@ if ($options{daemon}) {
          sub handle_request {
               my ($self,$cgi) = @_;
 
+              $ENV{DEBBUGS_CONFIG_FILE} = $options{mirror_location}.'/debbugs_config_local';
               my $base_uri = 'http://'.$cgi->virtual_host;
               if ($cgi->virtual_port ne 80) {
                    $base_uri .= ':'.$cgi->virtual_port;
@@ -245,11 +295,32 @@ if ($options{daemon}) {
                    redirect($cgi,$base_uri."/cgi-bin/pkgreport.cgi?pkg=$1");
               }
               elsif ($path =~ m{^/?cgi(?:-bin)?/((?:(?:bug|pkg)report|version)\.cgi)}) {
-                   # dispatch to pkgreport.cgi
-                   print "HTTP/1.1 200 OK\n";
-                   exec("$options{cgi_bin}/$1") or
-                        die "Unable to execute $options{cgi_bin}/$1";
-              }
+                  my @exec_options = "$options{cgi_bin}/$1";
+                  if ($options{git_mode}) {
+                      unshift @exec_options,
+                          'perl','-I',$options{base_dir},'-T';
+                  }
+                   open(my $fh,'-|',@exec_options) or
+                       die "Unable to execute $options{cgi_bin}/$1";
+                   my $status;
+                   my $cache = '';
+                   while (<$fh>) {
+                       if (/Status: (\d+\s+.+?)\n?$/) {
+                           $status = $1;
+                           print "HTTP/1.1 $status\n";
+                           print STDERR "'$status'\n";
+                           last;
+                       }
+                       $cache .= $_;
+                       if (/^$/) {
+                           print "HTTP/1.1 200 OK\n";
+                           last;
+                       }
+                   }
+                   print $cache;
+                   print <$fh>;
+                   close($fh) or die "Unable to close";
+               }
               elsif ($path =~ m{^/?css/bugs.css}) {
                    my $fh = IO::File->new($options{css},'r') or
                         die "Unable to open $options{css} for reading: $!";
@@ -359,7 +430,7 @@ elsif ($options{search}) {
      my $pid = checkpid($options{mirror_location}.'/local-debbugs.pid');
      if (not defined $pid or $pid == 0) {
          print STDERR "Unable to open pidfile or daemon not running: $!\n";
-         print STDERR qq(Mr. T: "I pity da fool who tries to show a bug without a running daemon"\n);
+         print STDERR qq(Mr. T: "I pity da fool who tries to search for bugs without a running daemon"\n);
          print STDERR "Hint: try the --daemon option first\n";
          exit 1;
      }
@@ -423,9 +494,8 @@ sub run_rsync{
                              );
      my ($output,$error) = ('','');
      my $h = IPC::Run::start(['rsync',@{$param{options}}],
-                            \undef,\$output,\$error);
+                            \undef,$param{log},$param{log});
      while ($h->pump) {
-        print {$param{log}} $output,$error;
         #print {$param{debug}} $error if defined $param{debug};
      }
      $h->finish();
@@ -497,7 +567,7 @@ sub select_bugs{
               elsif (/\s\w+\:/) {
                    push @bug_selections, $_;
               }
-         }
+          }
      }
      # Split archive:both into archive:1 and archive:0
      @bug_selections =
@@ -548,6 +618,10 @@ sub select_bugs{
             $bugs{$archived_bugs}{$_} = 1 for @{$bugs};
         }
      }
+     for my $bug (@bugs) {
+        $bugs{archived}{$bug} = 1;
+        $bugs{unarchived}{$bug} = 1;
+     }
      return \%bugs;
 }