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 ################################################################################
21 my ($db, $logfile, $flavour, $help, $dpkgfile);
23 'database|db=s' => \$db,
24 'dpkg|d=s' => \$dpkgfile,
25 'logfile|l=s' => \$logfile,
26 'flavour|f=s' => \$flavour,
30 pod2usage(1) if $help;
32 pod2usage(-message => "$0: Need a sqlite database through --database ....\n") unless $db;
33 pod2usage(-message => "$0: Need a logfile to insert through --database ...\n") unless $logfile;
34 pod2usage(-message => "$0: Need the flavour information through --flavour ...\n") unless $flavour;
35 pod2usage(-message => "$0: Need the dpkg file through --dpkg ...\n") unless $dpkgfile;
37 open (my $fh, '<', $logfile) or die "Could not open $logfile: $!";
38 open (my $dpkg_handle, '<', $dpkgfile) or die "Could not open $dpkgfile: $!";
40 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","") or die "Could not connect to database: " . $DBI::err;
42 # We use foreign key - beware this needs sqlite > 3.6.19
43 $dbh->do("PRAGMA foreign_keys = ON");
45 # read content of log file - please do not try this at home :)
46 my $log = do { local $/; <$fh> };
48 my $identifier = "$flavour-". time2str('%Y%m%d%H%M%S', time());
50 # Prepare tables if not yet present {{{
51 my $create_table_build = $dbh->prepare("
52 CREATE TABLE if not exists build ( id integer primary key autoincrement,
53 identifier varchar(30),
58 or die "Could not create tables: " . $dbh->errstr."\n";
60 $create_table_build->execute()
61 or die "Can't execute SQL statement: " . $dbh->errstr."\n";
63 my $create_table_packages = $dbh->prepare("
64 CREATE TABLE if not exists packages ( id integer primary key autoincrement,
69 FOREIGN KEY(build) REFERENCES build(id));
71 or die "Could not create tables: " . $dbh->errstr."\n";
73 $create_table_packages->execute()
74 or die "Can't execute SQL statement: " . $dbh->errstr."\n";
78 # Write information to database {{{
79 my $sth = $dbh->prepare("INSERT into build ('identifier','flavour','date','logfile') VALUES (?,?,?,?)")
80 or die "Could not prepare db statement: " . $dbh->errstr;
83 $sth->execute($identifier, $flavour, time(), $log)
84 or die "Could not add build to db: " . $sth->errstr;
86 $sth = $dbh->prepare("SELECT id from build where identifier = ?");
87 $sth->execute($identifier) or die "Couldn't execute statement: " . $sth->errstr;
88 my $row = $sth->fetch;
91 die "No id?" unless $id;
93 $sth = $dbh->prepare("INSERT into packages (package, status, version, build) VALUES (?,?,?,?)")
94 or die "Could not prepare db statement: " . $dbh->errstr;
96 while (my $line = <$dpkg_handle>) {
97 next unless $line =~ /^[a-z]{2} /;
99 my ($status, $package, $version, $desc) = split (/\s+/, $line, 4);
100 $sth->execute($package, $status, $version, $id)
101 or die "Couldn't execute statement: " . $sth->errstr;
106 print "recorded buildinformation with identifier $identifier as id $id\n";
108 # perldoc -F ./dpkg-to-db
114 dpkg-to-db - add grml build information into a sqlite database
126 Print a brief help message and exits.
128 =item B<--database <database>>
132 =item B<--dpkg <dpkglist>>
134 `dpkg --list` output file of grml-live build.
136 =item B<--logfile <logfile>>
138 Logfile which should be added.
140 =item B<--flavour <flavour>>
142 Name of the grml-flavour the build is.
148 B<dpkg-to-db> will read the given input file(s) and stores the
149 information to the specified database.
151 =head1 USAGE EXAMPLES
153 Please see B<man 8 grml-live-db> for further information.