~ubuntu-branches/ubuntu/karmic/sbuild/karmic-proposed

« back to all changes in this revision

Viewing changes to lib/Buildd.pm

  • Committer: Bazaar Package Importer
  • Author(s): Roger Leigh, Roger Leigh
  • Date: 2009-05-17 15:52:53 UTC
  • mfrom: (8.1.7 upstream) (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090517155253-fbxadfsyaf940ete
Tags: 0.58.3-1
[ Roger Leigh ]
* New release.
* debian/control:
  - Update to Standards Version 3.8.1.
  - Add buildd package.
  - Add libsbuild-perl package.
  - All packages depend upon libsbuild-perl.
* Add support for appending a tag to version numbers (Closes: #475777).
  Thanks to Timothy G Abbott for this patch.
* When using the --help or --version options, don't abort if not
  in the sbuild group (Closes: #523670).  Group membership is now
  only performed after options parsing, and only if required.
* Allow config files to use $HOME (Closes: #524564).  Thanks to
  James Vega for this patch.
* Restore buildd package.
* Split common library functions into new libsbuild-perl package.
* debian/sbuild.(preinst|postinst|postrm):
  - Remove special cases for versions older than oldstable.  Update
    addition and removal of sbuild group to use return value of getent
    rather than parsing getent output.
  - Use addgroup/delgroup in place of adduser/deluser.
  - Use --system when adding and deleting group, to ensure creation
    of a system group.  Migrate existing non-system group and group
    members if the system group is not present.
  - Handle removal of 50sbuild setup script.
* debian/buildd.(preinst|postinst|postrm): Add maintainer scripts for
  buildd package.  Move configuration file from /etc/buildd.conf to
  /etc/buildd/buildd.conf if present.  Also create buildd user and
  group for running the buildd daemon.
* Sbuild::Conf: Don't default MAINTAINER_NAME to $DEBEMAIL if unset
  in the configuration file (Closes: #520158).
* /etc/schroot/setup.d/50sbuild: Remove.  The setup tasks performed by
  this script are now handled internally by sbuild.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
use warnings;
27
27
use POSIX;
28
28
use FileHandle;
 
29
use Sbuild::LogBase;
29
30
 
30
31
require Exporter;
31
32
@Buildd::ISA = qw(Exporter);
32
33
 
33
34
@Buildd::EXPORT = qw(unset_env lock_file unlock_file open_log
34
 
                     reopen_log close_log logger send_mail
35
 
                     ll_send_mail exitstatus write_stats);
 
35
                     reopen_log close_log send_mail
 
36
                     ll_send_mail exitstatus isin
 
37
                     wannabuild_command);
36
38
 
37
39
$Buildd::lock_interval = 15;
38
40
$Buildd::max_lock_trys = 120;
39
41
($Buildd::progname = $0) =~ s,.*/,,;
 
42
$Buildd::progpid = $$;
40
43
my @pwinfo = getpwuid($>);
41
44
$Buildd::username = $pwinfo[0];
42
45
$Buildd::gecos = $pwinfo[6];
48
51
sub unset_env ();
49
52
sub lock_file ($;$);
50
53
sub unlock_file ($);
51
 
sub write_stats ($$);
52
 
sub open_log ();
53
 
sub logger (@);
54
 
sub close_log ();
55
 
sub reopen_log ();
 
54
sub open_log ($);
 
55
sub close_log ($);
 
56
sub reopen_log ($);
56
57
sub send_mail ($$$;$);
57
58
sub ll_send_mail ($$);
58
59
sub exitstatus ($);
 
60
sub wannabuild_command ($);
59
61
 
60
62
sub isin ($@) {
61
63
    my $val = shift;
136
138
    unlink( $lockfile );
137
139
}
138
140
 
139
 
 
140
 
sub write_stats ($$) {
141
 
    my ($cat, $val) = @_;
142
 
    local( *F );
143
 
 
144
 
    lock_file( "$main::HOME/stats" );
145
 
    open( F, ">>$main::HOME/stats/$cat" );
146
 
    print F "$val\n";
147
 
    close( F );
148
 
    unlock_file( "$main::HOME/stats" );
149
 
}
150
 
 
151
 
sub open_log () {
152
 
    open( LOG, ">>$main::HOME/daemon.log" )
153
 
        or die "$0: Cannot open my logfile $main::HOME/daemon.log: $!\n";
154
 
    chmod( 0640, "$main::HOME/daemon.log" )
155
 
        or die "$0: Cannot set modes of $main::HOME/daemon.log: $!\n";
156
 
    select( (select(LOG), $| = 1)[0] );
157
 
    open( STDOUT, ">&LOG" )
158
 
        or die "$0: Can't redirect stdout to $main::HOME/daemon.log: $!\n";
159
 
    open( STDERR, ">&LOG" )
160
 
        or die "$0: Can't redirect stderr to $main::HOME/daemon.log: $!\n";
161
 
}
162
 
 
163
 
sub logger (@) {
164
 
    my $t;
165
 
    my $text = "";
166
 
 
167
 
    # omit weekday and year for brevity
168
 
    ($t = localtime) =~ /^\w+\s(.*)\s\d+$/; $t = $1;
169
 
    foreach (@_) { $text .= $_; }
170
 
    $text =~ s/\n+$/\n/; # remove newlines at end
171
 
    $text .= "\n" if $text !~ /\n$/; # ensure newline at end
172
 
    $text =~ s/^/$t $Buildd::progname: /mg;
173
 
    print LOG $text;
174
 
}
175
 
 
176
 
sub close_log () {
177
 
    close( LOG );
178
 
    close( STDOUT );
179
 
    close( STDERR );
180
 
}
181
 
 
182
 
sub reopen_log () {
 
141
sub open_log ($) {
 
142
    my $conf = shift;
 
143
 
 
144
    my $logfile = $conf->get('DAEMON_LOG_FILE');
 
145
 
 
146
    my $log = new FileHandle(">>$logfile")
 
147
        or die "$0: Cannot open logfile $logfile: $!\n";
 
148
    chmod( 0640, "$logfile" )
 
149
        or die "$0: Cannot set modes of $logfile: $!\n";
 
150
 
 
151
    my $logfunc = sub {
 
152
        my $F = shift;
 
153
        my $message = shift;
 
154
 
 
155
        my $t;
 
156
        my $text = "";
 
157
 
 
158
        # omit weekday and year for brevity
 
159
        ($t = localtime) =~ /^\w+\s(.*)\s\d+$/; $t = $1;
 
160
        $message =~ s/\n+$//; # remove newlines at end
 
161
        $message = "$t $Buildd::progname\[$Buildd::progpid\]: $message\n";
 
162
 
 
163
        print $F $message;
 
164
    };
 
165
 
 
166
    return Sbuild::LogBase::open_log($conf, $log, $logfunc);
 
167
}
 
168
 
 
169
sub close_log ($) {
 
170
    my $conf = shift;
 
171
 
 
172
    Sbuild::LogBase::close_log($conf);
 
173
}
 
174
 
 
175
sub reopen_log ($) {
 
176
    my $conf = shift;
 
177
 
183
178
    my $errno = $!;
184
 
    close_log();
185
 
    open_log();
 
179
 
 
180
    close_log($conf);
 
181
    open_log($conf);
186
182
    $! = $errno;
187
183
}
188
184
 
206
202
    my $text = shift;
207
203
    local( *MAIL );
208
204
 
 
205
    # TODO: Don't log to STDERR: Implement as class method using
 
206
    # standard pipe interface using normal log streams.
 
207
 
209
208
    $text =~ s/^\.$/../mg;
210
209
    local $SIG{'PIPE'} = 'IGNORE';
211
210
    if (!open( MAIL, "|/usr/sbin/sendmail -oem '$to'" )) {
212
 
        logger( "Could not open pipe to /usr/sbin/sendmail: $!\n" );
 
211
        print STDERR "Could not open pipe to /usr/sbin/sendmail: $!\n";
213
212
        return 0;
214
213
    }
215
214
    print MAIL $text;
216
215
    if (!close( MAIL )) {
217
 
        logger( "sendmail failed (exit status ", exitstatus($?), ")\n" );
 
216
        print STDERR "sendmail failed (exit status ", exitstatus($?), ")\n";
218
217
        return 0;
219
218
    }
220
219
    return 1;
221
220
}
222
221
 
223
 
 
224
222
sub exitstatus ($) {
225
223
    my $stat = shift;
226
224
 
227
225
    return ($stat >> 8) . "/" . ($stat % 256);
228
226
}
229
227
 
 
228
sub wannabuild_command ($) {
 
229
    my $conf = shift;
 
230
 
 
231
    my @command = ($conf->get('SSH_CMD'), 'wanna-build');
 
232
    push(@command, "--database=" . $conf->get('WANNA_BUILD_DBBASE'))
 
233
        if $conf->get('WANNA_BUILD_DBBASE');
 
234
    push(@command, "--user=" . $conf->get('WANNA_BUILD_USER'))
 
235
        if $conf->get('WANNA_BUILD_USER');
 
236
 
 
237
    return @command;
 
238
}
230
239
 
231
240
1;