Added lots more modules from lintian. Maemian appears to work.
[maemian] / lib / Maemian / Collect.pm
1 # Maemian::Collect -- interface to package data collection
2
3 # Copyright (C) 2009 Jeremiah C. Foster
4 # Based on the work of Russ Allbery
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2 of the License, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14 # more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 package Maemian::Collect;
20 use strict;
21
22 # Take the package name and type, initialize an appropriate collect object
23 # based on the package type, and return it.  Returns undef for an unknown
24 # package type.
25 sub new {
26     my ($class, $pkg, $type) = @_;
27     my $object;
28     if ($type eq 'source') {
29         require Maemian::Collect::Source;
30         $object = Maemian::Collect::Source->new ($pkg);
31     } elsif ($type eq 'binary' or $type eq 'udeb') {
32         require Maemian::Collect::Binary;
33         $object = Maemian::Collect::Binary->new ($pkg);
34     } else {
35         return;
36     }
37     $object->{name} = $pkg;
38     $object->{type} = $type;
39     return $object;
40 }
41
42 # Return the package name.
43 # sub name Needs-Info <>
44 sub name {
45     my ($self) = @_;
46     return $self->{name};
47 }
48
49 # Return the package type.
50 # sub type Needs-Info <>
51 sub type {
52     my ($self) = @_;
53     return $self->{type};
54 }
55
56 # Return the value of the specified control field of the package, or undef if
57 # that field wasn't present in the control file for the package.  For source
58 # packages, this is the *.dsc file; for binary packages, this is the control
59 # file in the control section of the package.
60 # sub field Needs-Info <>
61 sub field {
62     my ($self, $field) = @_;
63     return $self->{field}{$field} if exists $self->{field}{$field};
64     if (open(FIELD, '<', "fields/$field")) {
65         local $/;
66         my $value = <FIELD>;
67         close FIELD;
68         $value =~ s/\n\z//;
69         $self->{field}{$field} = $value;
70     } else {
71         $self->{field}{$field} = undef;
72     }
73     return $self->{field}{$field};
74 }
75
76 =head1 NAME
77
78 Maemian::Collect - Maemian interface to package data collection
79
80 =head1 SYNOPSIS
81
82     my ($name, $type) = ('foobar', 'udeb');
83     my $collect = Maemian::Collect->new($name, $type);
84     $name = $collect->name;
85     $type = $collect->type;
86
87 =head1 DESCRIPTION
88
89 Maemian::Collect provides the shared interface to package data used by
90 source, binary, and udeb packages.  It creates an object of the
91 appropriate type and provides common functions used by the collection
92 interface to all three types of packages.
93
94 This module is in its infancy.  Most of Maemian still reads all data from
95 files in the laboratory whenever that data is needed and generates that
96 data via collect scripts.  The goal is to eventually access all data via
97 this module and its subclasses so that the module can cache data where
98 appropriate and possibly retire collect scripts in favor of caching that
99 data in memory.
100
101 =head1 CLASS METHODS
102
103 =over 4
104
105 =item new(PACKAGE, TYPE)
106
107 Creates a new object appropriate to the package type.  Currently, the only
108 TYPE supported is C<source>, which creates a new Maemian::Collect::Source
109 object and returns it.  TYPE can be retrieved later with the type()
110 method.  Returns undef an unknown TYPE.
111
112 PACKAGE is the name of the package and is stored in the collect object.
113 It can be retrieved with the name() method.
114
115 =back
116
117 =head1 INSTANCE METHODS
118
119 In addition to the instance methods documented here, see the documentation
120 of Maemian::Collect::Source for instance methods specific to source
121 packages.
122
123 =over 4
124
125 =item field(FIELD)
126
127 Returns the value of the control field FIELD in the control file for the
128 package.  For a source package, this is the *.dsc file; for a binary
129 package, this is the control file in the control section of the package.
130 The value will be read from the F<fields/> subdirectory of the current
131 directory if it hasn't previously been requested and cached in memory so
132 that subsequent requests for the same field can be answered without file
133 accesses.
134
135 =item name()
136
137 Returns the name of the package.
138
139 =item type()
140
141 Returns the type of the package.
142
143 =back
144
145 =head1 AUTHOR
146
147 Originally written by Russ Allbery <rra@debian.org> for Maemian.
148
149 =head1 SEE ALSO
150
151 maemian(1), Maemian::Collect::Source(3)
152
153 =cut
154
155 1;