48abecbedb0af87435ce6f4fae81015d965cb905
[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 sub add_deb {
72     my ($self, $type, $file) = @_;
73
74     my $info = get_deb_info($file);
75     return unless defined $info;
76     return $self->add_file($type, $file, %$info);
77 }
78
79 sub add_dsc {
80     my ($self, $file) = @_;
81
82     my $info = get_dsc_info($file);
83     return unless defined $info;
84     return $self->add_file('s', $file, %$info);
85 }
86
87 sub add_pkg_list {
88     my ($self, $packages_file) = @_;
89
90     open(IN, '<', $packages_file)
91         or die("cannot open packages file $packages_file for reading: $!");
92     while (<IN>) {
93         chomp;
94         my ($type, $pkg, $ver, $file) = split(/\s+/, $_, 4);
95         if ($type eq 's') {
96             $self->add_file($type, $file, source => $pkg, version => $ver);
97         } else {
98             $self->add_file($type, $file, package => $pkg, version => $ver);
99         }
100     }
101     close(IN);
102 }
103
104 # for each package (the sort is to make sure that source packages are
105 # before the corresponding binary packages--this has the advantage that binary
106 # can use information from the source packages if these are unpacked)
107 my %type_sort = ('b' => 1, 'u' => 1, 's' => 2 );
108 sub get_all {
109     return sort({$type_sort{$b->{type}} <=> $type_sort{$a->{type}}}
110                 @{$_[0]->{schedule}});
111 }
112
113 sub count {
114     return scalar @{$_[0]->{schedule}};
115 }
116
117 1;