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

« back to all changes in this revision

Viewing changes to lib/Sbuild/DB/Base.pm

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2009-05-09 16:06:44 UTC
  • mfrom: (8.1.6 upstream) (3.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090509160644-9k0fgp6c2ajcu54h
Tags: 0.58.2-1ubuntu1
* Merge from debian unstable, remaining changes:
  - bin/sbuild, lib/Sbuild/{Base,Conf,Options}.pm: add --setup-hook
    to allow pre-build modifications to underlying chroots (needed
    to adjust pockets and components in sources.list).  (debian bug
    500746).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Base.pm: Base class for database abstraction
 
3
# Copyright © 1998      Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
 
4
# Copyright © 2005      Ryan Murray <rmurray@debian.org>
 
5
# Copyright © 2005-2008 Roger Leigh <rleigh@debian.org>
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation, either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
# General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program.  If not, see
 
19
# <http://www.gnu.org/licenses/>.
 
20
#
 
21
#######################################################################
 
22
 
 
23
package Sbuild::DB::Base;
 
24
 
 
25
use strict;
 
26
use warnings;
 
27
 
 
28
use Sbuild qw(debug isin);
 
29
use Sbuild::Base;
 
30
 
 
31
BEGIN {
 
32
    use Exporter ();
 
33
    our (@ISA, @EXPORT);
 
34
 
 
35
    @ISA = qw(Exporter Sbuild::Base);
 
36
 
 
37
    @EXPORT = qw();
 
38
}
 
39
 
 
40
sub dump {
 
41
    my $self = shift;
 
42
    my $file = shift;
 
43
 
 
44
    my $db = $self->get('DB');
 
45
 
 
46
    my($name,$pkg,$key);
 
47
 
 
48
    print "Writing ASCII database to $file..." if $self->get_conf('VERBOSE') >= 1;
 
49
    CORE::open( F, ">$file" ) or
 
50
        die "Can't open database $file: $!\n";
 
51
 
 
52
    foreach $name ($self->list_packages()) {
 
53
        my $pkg = $self->get_package($name);
 
54
        foreach $key (keys %{$pkg}) {
 
55
            my $val = $pkg->{$key};
 
56
            chomp( $val );
 
57
            $val =~ s/\n/\n /g;
 
58
            print F "$key: $val\n";
 
59
        }
 
60
        print F "\n";
 
61
    }
 
62
 
 
63
    foreach my $user ($self->list_users()) {
 
64
        my $ui = $self->get_user($user);
 
65
        print F "User: $user\n"
 
66
            if (!defined($ui->{'User'}));
 
67
        foreach $key (keys %{$ui}) {
 
68
            my $val = $ui->{$key};
 
69
            chomp($val);
 
70
            $val =~ s/\n/\n /g;
 
71
            print F "$key: $val\n";
 
72
        }
 
73
        print F "\n";
 
74
    }
 
75
 
 
76
    CORE::close(F);
 
77
    print "done\n" if $self->get_conf('VERBOSE') >= 1;
 
78
}
 
79
 
 
80
sub restore {
 
81
    my $self = shift;
 
82
    my $file = shift;
 
83
 
 
84
    my $db = $self->get('DB');
 
85
 
 
86
    print "Reading ASCII database from $file..." if $self->get_conf('VERBOSE') >= 1;
 
87
    CORE::open( F, "<$file" ) or
 
88
        die "Can't open database $file: $!\n";
 
89
 
 
90
    local($/) = ""; # read in paragraph mode
 
91
    while( <F> ) {
 
92
        my( %thispkg, $name );
 
93
        s/[\s\n]+$//;
 
94
        s/\n[ \t]+/\376\377/g;  # fix continuation lines
 
95
        s/\376\377\s*\376\377/\376\377/og;
 
96
 
 
97
        while( /^(\S+):[ \t]*(.*)[ \t]*$/mg ) {
 
98
            my ($key, $val) = ($1, $2);
 
99
            $val =~ s/\376\377/\n/g;
 
100
            $thispkg{$key} = $val;
 
101
        }
 
102
        $self->check_entry( \%thispkg );
 
103
        # add to db
 
104
        if (exists($thispkg{'Package'})) {
 
105
            $self->set_package(\%thispkg);
 
106
        } elsif(exists($thispkg{'User'})) {
 
107
            $self->set_user(\%thispkg);
 
108
        }
 
109
    }
 
110
    CORE::close( F );
 
111
    print "done\n" if $self->get_conf('VERBOSE') >= 1;
 
112
}
 
113
 
 
114
sub check_entry {
 
115
    my $self = shift;
 
116
    my $pkg = shift;
 
117
    my $field;
 
118
 
 
119
    # TODO: Why should manual editing disable sanity checking?
 
120
    return if $self->get_conf('DB_OPERATION') eq "manual-edit"; # no checks then
 
121
 
 
122
    # check for required fields
 
123
    if (!exists $pkg->{'Package'} && !exists $pkg->{'User'}) {
 
124
        print STDERR "Bad entry: ",
 
125
        join( "\n", map { "$_: $pkg->{$_}" } keys %$pkg ), "\n";
 
126
        die "Database entry lacks Package or User: field\n";
 
127
    }
 
128
 
 
129
    if (exists $pkg->{'Package'}) {
 
130
        if (!exists $pkg->{'Version'}) {
 
131
            die "Database entry for package $pkg->{'Package'} lacks Version: field\n";
 
132
        }
 
133
        # if no State: field, generate one (for old db compat)
 
134
        if (!exists($pkg->{'State'})) {
 
135
            $pkg->{'State'} =
 
136
                exists $pkg->{'Failed'} ? 'Failed' : 'Building';
 
137
        }
 
138
        # check state field
 
139
        die "Bad state $pkg->{'State'} of package $pkg->{Package}\n"
 
140
            if !isin($pkg->{'State'},
 
141
                     qw(Needs-Build Building Built Build-Attempted
 
142
                        Uploaded Installed Dep-Wait Failed
 
143
                        Failed-Removed Not-For-Us) );
 
144
    }
 
145
    if (exists $pkg->{'User'}) {
 
146
        if (!exists $pkg->{'Last-Seen'}) {
 
147
            die "Database entry for user $pkg->{'User'} lacks Last-Seen: field\n";
 
148
        }
 
149
    }
 
150
}
 
151
 
 
152
1;