Support for logging to database via grml-live-db. Changed logic of ZERO_LOGFILE to...
[grml-live.git] / db / dpkg-to-db
1 #!/usr/bin/perl -w
2 # Filename:      dpkg-to-db
3 # Purpose:       add grml build information into a sqlite database
4 # Authors:       grml-team (grml.org)
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2 or any later version.
7 ################################################################################
8 # Requires the following Debian packages (handled via grml-live-db depends):
9 # libdbd-sqlite3-perl libdbi-perl libtimedate-perl perl-doc sqlite3
10 ################################################################################
11
12 use strict;
13
14 use warnings;
15 use Getopt::Long;
16 use Pod::Usage;
17 use DBI;
18 use Date::Format;
19
20
21 my ($db, $logfile, $flavour, $help);
22 my $rc = GetOptions (
23                 'db|d=s' => \$db,
24                 'logfile|l=s' => \$logfile,
25                 'flavour|f=s' => \$flavour,
26                 'help|h' => \$help,
27         );
28
29 pod2usage(1) if $help;
30
31 pod2usage(-message => "$0: Need a sqlite database.\n") unless $db;
32 pod2usage(-message => "$0: Need a logfile to insert.\n") unless $logfile;
33 pod2usage(-message => "$0: Need the flavour information\n") unless $flavour;
34
35 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","") or die "Could not connect to database: " . $DBI::err;
36
37 # We use foreign key - beware this needs sqlite > 3.6.19
38 $dbh->do("PRAGMA foreign_keys = ON");
39
40 open (my $fh, '<', $logfile) or die "Could not open $logfile: $!";
41
42 # read content of log file - please do not try this at home :)
43 my $log = do { local $/; <$fh> };
44
45 my $identifier = "$flavour-". time2str('%Y%m%d%H%M%S', time());
46
47 # Prepare tables if not yet present {{{
48 my $create_table_build = $dbh->prepare("
49 CREATE TABLE if not exists build ( id integer primary key autoincrement,
50 identifier varchar(30),
51 flavour varchar(30),
52 date varchar(30),
53 logfile blob);
54 ")
55   or die "Could not create tables: " . $dbh->errstr."\n";
56
57 $create_table_build->execute()
58   or die "Can't execute SQL statement: " . $dbh->errstr."\n";
59
60 my $create_table_packages = $dbh->prepare("
61 CREATE TABLE if not exists packages (  id integer primary key autoincrement,
62 package varchar(30),
63 status varchar(2),
64 version varchar(30),
65 build integer,
66 FOREIGN KEY(build) REFERENCES build(id));
67 ")
68   or die "Could not create tables: " . $dbh->errstr."\n";
69
70 $create_table_packages->execute()
71   or die "Can't execute SQL statement: " . $dbh->errstr."\n";
72 # }}}
73
74
75 # Write information to database {{{
76 my $sth = $dbh->prepare("INSERT into build ('identifier','flavour','date','logfile') VALUES (?,?,?,?)")
77         or die "Could not prepare db statement: " . $dbh->errstr;
78
79 # Execute the query
80 $sth->execute($identifier, $flavour, time(), $log)
81         or die "Could not add build to db: " . $sth->errstr;
82
83 $sth = $dbh->prepare("SELECT id from build where identifier = ?");
84 $sth->execute($identifier) or die "Couldn't execute statement: " . $sth->errstr;
85 my $row = $sth->fetch;
86 my $id = $row->[0];
87
88 die "No id?" unless $id;
89
90 $sth = $dbh->prepare("INSERT into packages (package, status, version, build) VALUES (?,?,?,?)")
91         or die "Could not prepare db statement: " . $dbh->errstr;
92
93 while (my $line = <>) {
94         next unless $line =~ /^[a-z]{2} /;
95         # remove new lines
96         my ($status, $package, $version, $desc) = split (/\s+/, $line, 4);
97         $sth->execute($package, $status, $version, $id)
98                 or die "Couldn't execute statement: " . $sth->errstr;
99
100 }
101 # }}}
102
103 print "recorded buildinformation with identifier $identifier as id $id\n";
104
105 # perldoc -F ./dpkg-to-db
106
107 __END__
108
109 =head1 dpkg-to-db
110
111 dpkg-to-db - add grml build information into a sqlite database
112
113 =head1 SYNOPSIS
114
115 dpkg-to-db [options]
116
117   Options:
118    --help               brief help message
119    --db <database>      database file
120    --logfile <logfile>  logfile which should be added
121    --flavour <flavour>  which flavour the build is
122
123 =head1 OPTIONS
124
125 =over 8
126
127 =item B<-help>
128
129 Print a brief help message and exits.
130
131 =back
132
133 =head1 DESCRIPTION
134
135 B<dpkg-to-db> will read the given input file(s) which holds output of
136 `dpkg --list' and writes the information to the specified database.
137
138 =head1 USAGE EXAMPLES
139
140 Please see B<man 8 grml-live-db> for further information.
141
142 =cut