summaryrefslogtreecommitdiff
path: root/new-infra/TLDB.pm
blob: 5d220b8c04d607362c97c5ac34ed77e70ec00aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#
# TLDB.pm
# module for using tldb files
# Copyright 2007 Norbert Preining
#
# This file is licensed under the GNU General Public Licence version 2
# or any later version

package TLDB;

use TLP;

sub new { 
	my $class = shift;
	my %params = @_;
	my $self = {
		location => $params{'location'};
	};
	if (defined($self->{'location'})) {
		$self->from_file($self->{'location'});
	}
	bless $self, $class;
	return $self;
}

sub add_tlp {
	my ($self,$tlp) = @_;
	$self->{$tlp->name} = $tlp;
}

sub from_file {
	my $self = shift;
	if (@_ != 1) {
		die("Need a filename for initialization!");
	}
	if (defined($self->{'location'})) {
		if ($self->location ne $_[0]) {
			printf STDERR "Initialisation from different location as originally given.\nHope you are sure!\n";
		}
	}
	$self->location($_[0]);
	open(TMP,"<$_[0]") || die("Cannot open tldb file: $_[0]");
	my $found = 0;
	do {
		my $tlp = TLP->new;
		$found = $tlp->from_fh(\*TMP,1);
		if ($found) {
			$self->add_tlp($tlp);
		}
	} until (!$found);
}

sub writeout {
	my $self = shift;
	my $fd = (@_ ? $_[0] : STDOUT);
	foreach (sort keys %$self) {
		$self->{$_}->writeout($fd);
		print $fd "\n";
	}
}

sub save {
	my $self = shift;
	open(FOO,">$self->{'location'}") || die("Cannot open $self->{'location'} for writing: $!");
	$self->writeout(\*FOO);
	close(FOO);
}

sub get_package {
	my ($self,$pkg) = @_;
	if (defined($self->{$pkg})) {
		return($self->{$pkg});
	} else {
		return(undef);
	}
}

sub package_revision {
	my ($self,$pkg) = @_;
	if (defined($self->{$pkg})) {
		return($self->{$pkg}->revision);
	} else {
		return(undef);
	}
}

sub generate_packagelist {
	my $self = shift;
	my $fd = (@_ ? $_[0] : STDOUT);
	foreach (sort keys %$self) {
		print $fd $self->{$_}->name, " ", $self->{$_}->revision, "\n";
	}
}

sub location {
	my $self = shift;
	if (@_) { $self->{'location'} = shift }
	return $self->{'location'};
}


1;

### Local Variables:
### perl-indent-level: 4
### tab-width: 4
### indent-tabs-mode: t
### End:
# vim:set tabstop=4: #