Added some code to peer into a data structure in Maemian/Schedule.pm. Also added the
[maemian] / lib / Maemian / Schedule.pm
1 # Copyright (C) 2008 Frank Lichtenheld <frank@lichtenheld.de>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, you can find it on the World Wide
15 # Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
16 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
17 # MA 02110-1301, USA.
18
19 package Maemian::Schedule;
20
21 use strict;
22 use warnings;
23
24 use Util;
25
26 sub new {
27     my ($class, %options) = @_;
28     my $self = {};
29
30     bless($self, $class);
31
32     $self->{opts} = \%options;
33     $self->{schedule} = [];
34     $self->{unique} = {};
35
36     return $self;
37 }
38
39 # schedule a package for processing
40 sub add_file {
41     my ($self, $type, $file, %pkg_info) = @_;
42
43     my ($pkg, $ver, $arch);
44     if ($type eq 's') {
45         ($pkg, $ver, $arch) =
46             (@pkg_info{qw(source version)}, 'source');
47     } else {
48         ($pkg, $ver, $arch) =
49             @pkg_info{qw(package version architecture)};
50     }
51     $pkg  ||= '';
52     $ver  ||= '';
53     $arch ||= '';
54
55     my $s = "$type $pkg $ver $arch $file";
56     my %h = ( type => $type, package => $pkg, version => $ver,
57               architecture => $arch, file => $file );
58
59     if ( $self->{unique}{$s}++ ) {
60         if ($self->{opts}{verbose}) {
61             printf "N: Ignoring duplicate %s package $pkg (version $ver)\n",
62                 $type eq 'b' ? 'binary' : ($type eq 's' ? 'source': 'udeb');
63         }
64         return;
65     }
66
67     push(@{$self->{schedule}}, \%h);
68     return 1;
69 }
70
71 # add_deb takes the name of a binary deb and the letter "b" as args.
72 sub add_deb {
73   my ($self, $type, $file) = @_;
74
75   my $info = get_deb_info($file);
76 # use the following code to peer into the control file of the deb
77 #  print map { $_, ": ",
78 #    %$info->{$_} . "\n" } keys %$info;
79   return unless defined $info;
80   return $self->add_file($type, $file, %$info);
81 }
82
83 sub add_dsc {
84     my ($self, $file) = @_;
85
86     my $info = get_dsc_info($file);
87     return unless defined $info;
88     return $self->add_file('s', $file, %$info);
89 }
90
91 sub add_pkg_list {
92     my ($self, $packages_file) = @_;
93
94     open(IN, '<', $packages_file)
95         or die("cannot open packages file $packages_file for reading: $!");
96     while (<IN>) {
97         chomp;
98         my ($type, $pkg, $ver, $file) = split(/\s+/, $_, 4);
99         if ($type eq 's') {
100             $self->add_file($type, $file, source => $pkg, version => $ver);
101         } else {
102             $self->add_file($type, $file, package => $pkg, version => $ver);
103         }
104     }
105     close(IN);
106 }
107
108 # for each package (the sort is to make sure that source packages are
109 # before the corresponding binary packages--this has the advantage that binary
110 # can use information from the source packages if these are unpacked)
111 my %type_sort = ('b' => 1, 'u' => 1, 's' => 2 );
112 sub get_all {
113     return sort({$type_sort{$b->{type}} <=> $type_sort{$a->{type}}}
114                 @{$_[0]->{schedule}});
115 }
116
117 sub count {
118     return scalar @{$_[0]->{schedule}};
119 }
120
121 1;