sanitize code, no functional changes

This commit is contained in:
espie 2010-04-17 09:33:18 +00:00
parent c0da600c3c
commit 9bc804c209
3 changed files with 162 additions and 166 deletions

View File

@ -1,5 +1,5 @@
#! /usr/bin/perl
# $OpenBSD: Inserter.pm,v 1.2 2010/04/13 10:56:42 espie Exp $
# $OpenBSD: Inserter.pm,v 1.3 2010/04/17 09:33:18 espie Exp $
#
# Copyright (c) 2006-2010 Marc Espie <espie@openbsd.org>
#
@ -18,20 +18,92 @@
use strict;
use warnings;
package InserterList;
sub new
{
my $class = shift;
bless [], $class;
}
sub add
{
my $self = shift;
push(@$self, @_);
}
sub AUTOLOAD
{
our $AUTOLOAD;
my $fullsub = $AUTOLOAD;
(my $sub = $fullsub) =~ s/.*:://o;
return if $sub eq 'DESTROY'; # special case
# verify it makes sense
if (NormalInserter->can($sub)) {
no strict "refs";
# create the sub to avoid regenerating further calls
*$fullsub = sub {
my $self = shift;
$self->visit($sub, @_);
};
# and jump to it
goto &$fullsub;
} else {
die "Can't call $sub on ", __PACKAGE__;
}
}
sub visit
{
my ($self, $method, @r) = @_;
for my $i (@$self) {
$i->$method(@r);
}
}
package AbstractInserter;
# this is the object to use to put stuff into the db...
sub new
{
my ($class, $db, $i) = @_;
my ($class, $db, $i, $verbose) = @_;
bless {
db => $db,
transaction => 0,
threshold => $i,
vars => {},
tables_created => {}
tables_created => {},
verbose => $verbose,
}, $class;
}
sub create_tables
{
my ($self, $vars) = @_;
$self->create_path_table;
while (my ($name, $varclass) = each %$vars) {
$self->handle_column($varclass->column($name));
$varclass->create_table($self);
}
$self->create_ports_table;
$self->prepare_normal_inserter('Ports', @{$self->{varlist}});
$self->prepare_normal_inserter('Paths', 'PKGPATH');
$self->create_view_info;
$self->db->commit;
print '-'x50, "\n" if $self->{verbose};
}
sub handle_column
{
my ($self, $column) = @_;
push(@{$self->{varlist}}, $column->{name});
push(@{$self->{columnlist}}, $column);
}
sub create_view_info
{
}
sub make_table
{
my ($self, $class, $constraint, @columns) = @_;
@ -78,7 +150,8 @@ sub new_table
return if defined $self->{tables_created}->{$name};
$self->db->do("DROP TABLE IF EXISTS $name");
print "CREATE TABLE $name (".join(', ', @cols).")\n" if $main::opt_v;
print "CREATE TABLE $name (".join(', ', @cols).")\n"
if $self->{verbose};
$self->db->do("CREATE TABLE $name (".join(', ', @cols).")");
$self->{tables_created}->{$name} = 1;
}
@ -122,17 +195,14 @@ sub add_to_port
$self->{vars}->{$var} = $value;
}
sub create_other_tables
sub create_ports_table
{
my $self = shift;
$self->db->commit;
my @columns = sort {$a->name cmp $b->name} @{$self->{columnlist}};
unshift(@columns, PathColumn->new);
my @l = map {$_->normal_schema($self)} @columns;
$self->new_table("Ports", @l, "UNIQUE(FULLPKGPATH)");
$self->prepare_normal_inserter('Ports', @{$self->{varlist}});
$self->prepare_normal_inserter('Paths', 'PKGPATH');
}
sub ref
@ -148,6 +218,18 @@ sub insert
$self->insert_done;
}
sub add_var
{
my ($self, $v) = @_;
$v->add($self);
}
sub commit_to_db
{
my $self = shift;
$self->db->commit;
}
package CompactInserter;
our @ISA=(qw(AbstractInserter));
@ -204,7 +286,7 @@ sub create_view
my @j = map {$_->join_schema($table)} @columns;
my $v = "CREATE VIEW $name AS SELECT ".join(", ", @l). " FROM ".$table.' '.join(' ', @j);
$self->db->do("DROP VIEW IF EXISTS $name");
print "$v\n" if $main::opt_v;
print "$v\n" if $self->{verbose};
$self->db->do($v);
}
@ -218,24 +300,25 @@ sub make_table
$self->create_view($class->table, @columns);
}
sub create_tables
sub create_path_table
{
my ($self, $vars) = @_;
# create the various tables, dropping old versions
my $self = shift;
$self->new_table("Paths", "ID INTEGER PRIMARY KEY",
"FULLPKGPATH TEXT NOT NULL UNIQUE", "PKGPATH INTEGER");
while (my ($name, $class) = each %$vars) {
my $c = $class->column($name);
if (!defined( $class->table )) {
push(@{$self->{varlist}}, $name);
push(@{$self->{columnlist}}, $c);
}
$class->create_table($self);
}
}
$self->create_other_tables;
sub handle_column
{
my ($self, $column) = @_;
if (!defined($column->{vartype}->table)) {
$self->SUPER::handle_column($column);
}
}
sub create_view_info
{
my $self = shift;
my @columns = sort {$a->name cmp $b->name} @{$self->{columnlist}};
$self->create_view("Ports", @columns);
$self->{find_pathkey} =
@ -317,21 +400,11 @@ sub convert_depends
return $c->{$value};
}
sub create_tables
sub create_path_table
{
my ($self, $vars) = @_;
# create the various tables, dropping old versions
my $self = shift;
$self->new_table("Paths", "FULLPKGPATH TEXT NOT NULL PRIMARY KEY",
"PKGPATH TEXT NOT NULL");
while (my ($name, $class) = each %$vars) {
push(@{$self->{varlist}}, $name);
push(@{$self->{columnlist}}, $class->column($name));
$class->create_table($self);
}
$self->create_other_tables;
}
sub pathref

View File

@ -1,5 +1,5 @@
#! /usr/bin/perl
# $OpenBSD: Var.pm,v 1.2 2010/04/13 10:56:42 espie Exp $
# $OpenBSD: Var.pm,v 1.3 2010/04/17 09:33:18 espie Exp $
#
# Copyright (c) 2006-2010 Marc Espie <espie@openbsd.org>
#
@ -18,7 +18,6 @@
use strict;
use warnings;
use subs qw(main::words);
# use a Template Method approach to store the variable values.
# rule: we store each value in the main table, after converting YesNo
@ -26,6 +25,10 @@ use subs qw(main::words);
# to store them in secondary tables (because of one/many associations).
package AnyVar;
sub columntype() { 'OptTextColumn' }
sub table() { undef }
sub keyword_table() { undef }
sub new
{
my ($class, $var, $value) = @_;
@ -42,6 +45,15 @@ sub value
return shift->[1];
}
sub words
{
my $self = shift;
my $v = $self->value;
$v =~ s/^\s+//;
$v =~ s/\s+$//;
return split(/\s+/, $v);
}
sub add
{
my ($self, $ins) = @_;
@ -60,21 +72,6 @@ sub column
return $self->columntype->new($name)->set_vartype($self);
}
sub columntype
{
return 'OptTextColumn';
}
sub table()
{
return undef;
}
sub keyword_table()
{
return undef;
}
sub keyword
{
my ($self, $ins, $value) = @_;
@ -111,25 +108,23 @@ sub normal_insert
package KeyVar;
our @ISA=(qw(AnyVar));
sub columntype() { 'ValueColumn' }
sub add
{
my ($self, $ins) = @_;
$self->add_value($ins, $self->keyword($ins, $self->value));
}
sub columntype
{
return 'ValueColumn';
}
package ArchKeyVar;
our @ISA=(qw(KeyVar));
sub keyword_table()
{ 'Arch' }
sub keyword_table() { 'Arch' }
package OptKeyVar;
our @ISA=(qw(KeyVar));
sub columntype() { 'OptValueColumn' }
sub add
{
my ($self, $ins) = @_;
@ -138,13 +133,9 @@ sub add
}
}
sub columntype
{
return 'OptValueColumn';
}
package YesNoVar;
our @ISA=(qw(AnyVar));
sub columntype() { 'OptIntegerColumn' }
sub add
{
@ -152,11 +143,6 @@ sub add
$self->add_value($ins, $self->value =~ m/^Yes/i ? 1 : undef);
}
sub columntype
{
return 'OptIntegerColumn';
}
# variable is always defined, but we don't need to store empty values.
package DefinedVar;
our @ISA=(qw(AnyVar));
@ -171,14 +157,15 @@ sub add
# all the dependencies are converted into list. Stuff like LIB_DEPENDS will
# end up being treated as WANTLIB as well.
package DependsVar;
our @ISA=(qw(AnyVar));
sub table() { 'Depends' }
sub add
{
my ($self, $ins) = @_;
$self->SUPER::add($ins);
for my $depends (main::words $self->value) {
for my $depends ($self->words) {
my ($libs, $pkgspec, $pkgpath2, $rest) = split(/\:/, $depends);
if (!defined $pkgpath2) {
print STDERR "Wrong depends $depends\n";
@ -213,11 +200,6 @@ sub add_lib
{
}
sub table()
{
return "Depends";
}
package LibDependsVar;
our @ISA=(qw(DependsVar));
sub depends_type() { 'Library' }
@ -243,6 +225,8 @@ sub depends_type() { 'Regress' }
# Stuff that gets stored in another table
package SecondaryVar;
our @ISA=(qw(KeyVar));
sub keyword_table() { undef }
sub add_value
{
my ($self, $ins, $value) = @_;
@ -264,13 +248,10 @@ sub create_table
$inserter->prepare_normal_inserter($self->table, "VALUE");
}
sub keyword_table()
{
return undef;
}
package MasterSitesVar;
our @ISA=(qw(OptKeyVar));
sub table() { 'MasterSites' }
sub add
{
my ($self, $ins) = @_;
@ -292,47 +273,33 @@ sub create_table
$inserter->prepare_normal_inserter($self->table, "N", "VALUE");
}
sub table()
{
return "MasterSites";
}
# Generic handling for any blank-separated list
package ListVar;
our @ISA=(qw(SecondaryVar));
sub columntype() { 'OptTextColumn' }
sub add
{
my ($self, $ins) = @_;
$self->AnyVar::add($ins);
for my $d (main::words $self->value) {
for my $d ($self->words) {
$self->add_value($ins, $d) if $d ne '';
}
}
sub columntype
{
my ($self, $name) = @_;
return 'OptTextColumn';
}
package ListKeyVar;
our @ISA=(qw(SecondaryVar));
sub keyword_table() { 'Keywords' }
sub add
{
my ($self, $ins) = @_;
$self->AnyVar::add($ins);
for my $d (main::words $self->value) {
for my $d ($self->words) {
$self->add_keyword($ins, $d) if $d ne '';
}
}
sub keyword_table()
{
return "Keywords";
}
package QuotedListVar;
our @ISA=(qw(ListVar));
@ -340,7 +307,7 @@ sub add
{
my ($self, $ins) = @_;
$self->AnyVar::add($ins);
my @l = (main::words $self->value);
my @l = ($self->words);
while (my $v = shift @l) {
while ($v =~ m/^[^']*\'[^']*$/ || $v =~m/^[^"]*\"[^"]*$/) {
$v.=' '.shift @l;
@ -357,6 +324,8 @@ sub add
package DefinedListKeyVar;
our @ISA=(qw(ListKeyVar));
sub columntype() { 'OptValueColumn' }
sub add
{
my ($self, $ins) = @_;
@ -364,11 +333,6 @@ sub add
$self->SUPER::add($ins);
}
sub columntype
{
return 'OptValueColumn';
}
package FlavorsVar;
our @ISA=(qw(DefinedListKeyVar));
sub table() { 'Flavors' }
@ -422,6 +386,8 @@ sub table() { 'ConfigureArgs' }
package WantlibVar;
our @ISA=(qw(ListVar));
sub table() { 'Wantlib' }
sub keyword_table() { 'Library' }
sub _add
{
my ($self, $ins, $value, $extra) = @_;
@ -450,8 +416,6 @@ sub create_table
$inserter->prepare_normal_inserter($self->table, "VALUE", "EXTRA");
}
sub keyword_table() { 'Library' }
package OnlyForArchVar;
our @ISA=(qw(DefinedListKeyVar));
sub table() { 'OnlyForArch' }
@ -459,6 +423,7 @@ sub keyword_table() { 'Arches' }
package FileVar;
our @ISA=(qw(SecondaryVar));
sub table() { 'Descr' }
sub add
{
@ -469,16 +434,16 @@ sub add
$self->add_value($ins, <$file>);
}
sub table() { 'Descr' }
package SharedLibsVar;
our @ISA=(qw(KeyVar));
sub table() { 'Shared_Libs' }
sub keyword_table() { 'Library' }
sub add
{
my ($self, $ins) = @_;
$self->AnyVar::add($ins);
my %t = main::words($self->value);
my %t = $self->words;
while (my ($k, $v) = each %t) {
$self->normal_insert($ins, $self->keyword($ins, $k), $v);
}
@ -494,36 +459,16 @@ sub create_table
$inserter->prepare_normal_inserter($self->table, "LIBNAME", "VERSION");
}
sub table()
{
"Shared_Libs"
}
sub keyword_table()
{
return "Library";
}
package EmailVar;
our @ISA=(qw(KeyVar));
sub keyword_table()
{
return "Email";
}
sub keyword_table() { 'Email' }
package YesKeyVar;
our @ISA=(qw(KeyVar));
sub keyword_table()
{
return "Keywords2";
}
sub keyword_table() { 'Keywords2' }
package AutoVersionVar;
our @ISA=(qw(OptKeyVar));
sub keyword_table()
{
return "AutoVersion";
}
sub keyword_table() { 'AutoVersion' }
1;

View File

@ -1,5 +1,5 @@
#! /usr/bin/perl
# $OpenBSD: mksqlitedb,v 1.20 2010/04/13 10:23:53 espie Exp $
# $OpenBSD: mksqlitedb,v 1.21 2010/04/17 09:33:18 espie Exp $
#
# Copyright (c) 2006-2010 Marc Espie <espie@openbsd.org>
#
@ -33,14 +33,6 @@ use DBI;
our ($opt_v);
sub words($)
{
my $v = shift;
$v =~ s/^\s+//;
$v =~ s/\s+$//;
return split(/\s+/, $v);
}
getopts('v');
my $dbname;
if (@ARGV > 0) {
@ -48,13 +40,13 @@ if (@ARGV > 0) {
} else {
$dbname = 'sqlports';
}
my @inserters;
my $inserter = InserterList->new;
my $db =DBI->connect("dbi:SQLite:dbname=$dbname", '', '', {AutoCommit => 0});
my $db2 =DBI->connect("dbi:SQLite:dbname=$dbname-compact", '', '',
{AutoCommit => 0});
push(@inserters, NormalInserter->new($db, 1000));
push(@inserters, CompactInserter->new($db2, 1000));
$inserter->add(NormalInserter->new($db, 1000, $opt_v),
CompactInserter->new($db2, 1000, $opt_v));
my $vars = {
@ -118,12 +110,7 @@ my $vars = {
WANTLIB => 'WantlibVar',
};
for my $inserter (@inserters) {
$inserter->create_tables($vars);
if ($opt_v) {
print "-"x50, "\n";
}
}
$inserter->create_tables($vars);
my $lastkey;
my $unknown = {};
@ -144,28 +131,19 @@ while (<STDIN>) {
if (!(defined $lastkey) || $key ne $lastkey) {
if (defined $lastkey) {
for my $inserter (@inserters) {
$inserter->finish_port;
}
}
for my $inserter (@inserters) {
$inserter->set_newkey($key);
$inserter->finish_port;
}
$inserter->set_newkey($key);
$lastkey = $key;
}
if (!defined $vars->{$var}) {
$unknown->{$var} //= $key;
if (defined $vars->{$var}) {
$inserter->add_var($vars->{$var}->new($var, $value));
} else {
my $v = $vars->{$var}->new($var, $value);
for my $inserter (@inserters) {
$v->add($inserter);
}
$unknown->{$var} //= $key;
}
}
for my $inserter (@inserters) {
$inserter->finish_port;
$inserter->db->commit;
}
$inserter->finish_port;
$inserter->commit_to_db;
while (my ($k, $v) = each %$unknown) {
print STDERR "Unknown variable $k in $v\n";