File: ExtRepoData.pm

package info (click to toggle)
extrepo-data 1.0.6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,544 kB
  • sloc: perl: 761; makefile: 5
file content (193 lines) | stat: -rw-r--r-- 3,755 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package ExtRepoData::Suite;

use strict;
use warnings;

use overload '<=>' => 'suitecmp', 'cmp' => 'suitecmp', '""' => 'output';

use Carp;
use Debian::DistroInfo;

my $info = DebianDistroInfo->new();

my %suites = ( map { $_ => $info->version($_) } $info->all );

$suites{sid} = "9999";
$suites{experimental} = "10000";

my %symbolic = (
	oldstable => $info->old,
	stable => $info->stable,
	testing => $info->testing,
	unstable => $info->devel,
);

sub new {
	my $class = shift;
	my $val = shift;

	if(exists($symbolic{$val})) {
		$val = $symbolic{$val};
	}

	croak "Unknown Debian release: $val" unless exists($suites{$val});
	return bless \$val, $class;
}

sub suitecmp {
	my $self = shift;
	my $other = shift;
	my $swapped = shift;

	if(exists($symbolic{$other})) {
		$other = $symbolic{$other};
	}

	croak "Unknown Debian release: $other" unless exists($suites{$other});

	if($swapped) {
		return $suites{$other} <=> $suites{$$self};
	} else {
		return $suites{$$self} <=> $suites{$other};
	}
}

sub output {
	my $self = shift;

	return $$self;
}

sub version_of($) {
	my $suite = shift;
	if(exists($symbolic{$suite})) {
		$suite = $symbolic{$suite};
	}
	return $suites{$suite};
}

package ExtRepoData;

use strict;
use warnings;

sub TIEHASH {
	my $class = shift;
	my $self = {};
	$self->{data} = shift;
	$self->{index} = {};

	if(exists($self->{data}{"<<"})) {
		foreach my $key(keys %{$self->{data}{"<<"}}) {
			if(exists($self->{data}{$key})) {
				next unless ref($self->{data}{$key}) eq "HASH";
				foreach my $subkey(keys %{$self->{data}{"<<"}{$key}}) {
					next if exists($self->{data}{$key}{$subkey});
					$self->{data}{$key}{$subkey} = $self->{data}{"<<"}{$key}{$subkey};
				}
			} else {
				$self->{data}{$key} = $self->{data}{"<<"}{$key};
			}
		}
		delete $self->{data}{"<<"};
	}
	foreach my $key(keys %{$self->{data}}) {
		$self->{index}{lc($key)} = $key;
		if (ref($self->{data}{$key}) eq "HASH") {
			$self->{tied}{$key} = tie my(%hash), 'ExtRepoData', $self->{data}{$key};
			$self->{data}{$key} = \%hash;
		}
	}

	bless $self, $class;
}

sub FETCH {
	my ($self, $key) = @_;

	if(exists($self->{suite})) {
		my $suitekey = "suite-" . $self->{suite} . "-" . lc($key);
		if(exists($self->{index}{$suitekey})) {
			return $self->{data}{$self->{index}{$suitekey}};
		}
	}
	return $self->{data}{$self->{index}{lc($key)}};
}

sub STORE {
	my ($self, $key, $value) = @_;

	if(ref($value) eq "HASH") {
		$self->{tied}{$key} = tie my (%hash), 'ExtRepoData', $value;
		$self->{tied}{$key}->suite($self->{suite});
		$value = \%hash;
	}
	$self->{data}{$key} = $value;
	$self->{index}{lc($key)} = $key;
}

sub DELETE {
	my ($self, $key) = @_;

	delete $self->{data}{$key};
	delete $self->{index}{lc($key)};
}

sub CLEAR {
	my $self = shift;

	delete $self->{data};
	delete $self->{index};
	delete $self->{tied};
}

sub EXISTS {
	my ($self, $key) = @_;

	return exists $self->{index}{lc($key)};
}

sub FIRSTKEY {
	my $self = shift;
	my $a = keys %{$self->{data}};
	return $self->NEXTKEY;
}

sub NEXTKEY {
	my $self = shift;
	my $rv;
	do {
		$rv = each %{$self->{data}};
		return undef unless defined($rv);
		return $rv unless exists($self->{suite});
		if($rv =~ /^suite-([^-]+)-(.*)$/) {
			if($1 eq $self->{suite}) {
				return $2;
			}
		} else {
			my $keyname = "suite-" . $self->{suite} . "-" . lc($rv);
			if(!exists($self->{index}{$keyname})) {
				return $rv;
			}
		}
	} while(defined($rv));
	return undef;
}

sub SCALAR {
	my $self = shift;
	return scalar %{$self->{data}};
}

sub suite {
	my $self = shift;
	$self->{suite} = shift;
	foreach my $key(keys %{$self->{data}}) {
		if (ref($self->{data}{$key}) eq "HASH") {
			$self->{tied}{$key}->suite($self->{suite});
		}
	}
	delete $self->{suite} unless defined $self->{suite};
}

1;