Added some code to peer into a data structure in Maemian/Schedule.pm. Also added the
[maemian] / nokia-lintian / lib / Lintian / Collect.pm
1 # -*- perl -*-
2 # Lintian::Collect -- interface to package data collection
3
4 # Copyright (C) 2008 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 Lintian::Collect;
20 use strict;
21
22 use Lintian::Collect::Binary;
23 use Lintian::Collect::Source;
24
25 # Take the package name and type, initialize an appropriate collect object
26 # based on the package type, and return it.  Returns undef for an unknown
27 # package type.
28 sub new {
29     my ($class, $pkg, $type) = @_;
30     my $object;
31     if ($type eq 'source') {
32         $object = Lintian::Collect::Source->new ($pkg);
33     } elsif ($type eq 'binary' or $type eq 'udeb') {
34         $object = Lintian::Collect::Binary->new ($pkg);
35     } else {
36         return;
37     }
38     $object->{name} = $pkg;
39     $object->{type} = $type;
40     return $object;
41 }
42
43 # Return the package name.
44 sub name {
45     my ($self) = @_;
46     return $self->{name};
47 }
48
49 # Return the package type.
50 sub type {
51     my ($self) = @_;
52     return $self->{type};
53 }
54
55 # Return the value of the specified control field of the package, or undef if
56 # that field wasn't present in the control file for the package.  For source
57 # packages, this is the *.dsc file; for binary packages, this is the control
58 # file in the control section of the package.
59 sub field {
60     my ($self, $field) = @_;
61     return $self->{field}{$field} if exists $self->{field}{$field};
62     if (open(FIELD, '<', "fields/$field")) {
63         local $/;
64         my $value = <FIELD>;
65         close FIELD;
66         $value =~ s/\n\z//;
67         $self->{field}{$field} = $value;
68     } else {
69         $self->{field}{$field} = undef;
70     }
71     return $self->{field}{$field};
72 }
73
74 =head1 NAME
75
76 Lintian::Collect - Lintian interface to package data collection
77
78 =head1 SYNOPSIS
79
80     my $collect = Lintian::Collect->new($name, $type);
81     $name = $collect->name;
82     $type = $collect->type;
83
84 =head1 DESCRIPTION
85
86 Lintian::Collect provides the shared interface to package data used by
87 source, binary, and udeb packages.  It creates an object of the
88 appropriate type and provides common functions used by the collection
89 interface to all three types of packages.
90
91 This module is in its infancy.  Most of Lintian still reads all data from
92 files in the laboratory whenever that data is needed and generates that
93 data via collect scripts.  The goal is to eventually access all data via
94 this module and its subclasses so that the module can cache data where
95 appropriate and possibly retire collect scripts in favor of caching that
96 data in memory.
97
98 =head1 CLASS METHODS
99
100 =over 4
101
102 =item new(PACKAGE, TYPE)
103
104 Creates a new object appropriate to the package type.  Currently, the only
105 TYPE supported is C<source>, which creates a new Lintian::Collect::Source
106 object and returns it.  TYPE can be retrieved later with the type()
107 method.  Returns undef an unknown TYPE.
108
109 PACKAGE is the name of the package and is stored in the collect object.
110 It can be retrieved with the name() method.
111
112 =back
113
114 =head1 INSTANCE METHODS
115
116 In addition to the instance methods documented here, see the documentation
117 of Lintian::Collect::Source for instance methods specific to source
118 packages.
119
120 =over 4
121
122 =item field(FIELD)
123
124 Returns the value of the control field FIELD in the control file for the
125 package.  For a source package, this is the *.dsc file; for a binary
126 package, this is the control file in the control section of the package.
127 The value will be read from the F<fields/> subdirectory of the current
128 directory if it hasn't previously been requested and cached in memory so
129 that subsequent requests for the same field can be answered without file
130 accesses.
131
132 =item name()
133
134 Returns the name of the package.
135
136 =item type()
137
138 Returns the type of the package.
139
140 =back
141
142 =head1 AUTHOR
143
144 Originally written by Russ Allbery <rra@debian.org> for Lintian.
145
146 =head1 SEE ALSO
147
148 lintian(1), Lintian::Collect::Source(3)
149
150 =cut
151
152 1;
153
154 # Local Variables:
155 # indent-tabs-mode: nil
156 # cperl-indent-level: 4
157 # End:
158 # vim: syntax=perl sw=4 sts=4 ts=4 et shiftround