Fix multiple lines returned for animal on status page
[buildfarm-server.git] / scripts / gen_alias_list.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use DBI;
6 use Data::Dumper;
7 use Getopt::Long;
8
9 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
10             $user_list_format
11 );
12
13 use FindBin qw($RealBin);
14 require "$RealBin/../BuildFarmWeb.pl";
15
16 die "no dbname" unless $dbname;
17 die "no dbuser" unless $dbuser;
18
19 my %opts = ( outfile => "/etc/mail/lists/farmers" );
20 GetOptions( \%opts,
21   'outfile:s',
22 );
23
24 my $dsn="dbi:Pg:dbname=$dbname";
25 $dsn .= ";host=$dbhost" if $dbhost;
26 $dsn .= ";port=$dbport" if $dbport;
27
28 my $db = DBI->connect($dsn,$dbuser,$dbpass);
29
30 die $DBI::errstr unless $db;
31
32 my $sth = $db->prepare(q[ 
33        SELECT owner_email
34        FROM buildsystems AS b
35        ORDER BY owner_email ASC
36       ]);
37 $sth->execute();
38
39 my %list;
40 while (my $row = $sth->fetchrow_hashref)
41 {
42   $list{$row->{owner_email}}++;
43 }
44 $db->disconnect();
45
46 open(my $fh, ">", $opts{outfile});
47 if ($fh) {
48   print $fh (join "\n", (keys %list));
49 }
50 close $fh;