Parse/Entry.pm

filter: Statementstmt 100% 6 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 8 / 8 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Parse/Available.pm ↑ Summary Parse/Packages.pm »
1 #
2 # DPKG::Parse::Entry.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 # Created on: 12/19/2005 02:22:57 PM PST
5 #
6 # $Id: $
7  
8 =head1 NAME
9  
10 DPKG::Parse::Entry - Parse a Package style entry
11  
12 =head1 SYNOPSIS
13
14 use DPKG::Parse::Entry;
15  
16 my $data = <<EOH;
17 Package: kernel-image-2.6.12.2
18 Source: kernel-source-2.6.12.2
19 Version: wrk01
20 Priority: optional
21 Section: base
22 Maintainer: Jamie Heilman <jamie@marchex.com>
23 Depends: coreutils | fileutils (>= 4.0)
24 Suggests: lilo (>= 19.1) | grub, fdutils, kernel-doc-2.6.12.2 | kernel-source-2.6.12.2
25 Provides: kernel-image, kernel-image-2.6
26 Architecture: i386
27 Filename: packages/./kernel-image-2.6.12.2_wrk01_i386.deb
28 Size: 4293154
29 Installed-Size: 10312
30 MD5sum: 2acf846b127b71a1fa1143214b2b85a9
31 Description: Linux kernel binary image for version 2.6.12.2.
32 EOH
33  
34 my $entry = DPKG::Parse::Entry->new('data' => $data);
35  
36 print $entry->package . " " . $entry->version . "\n";
37  
38 $entry->package("kernel-foobar");
39  
40 =head1 DESCRIPTION
41  
42 L<DPKG::Parse::Entry> parses a dpkg "Package" file entry, creating a new
43 L<DPKG::Parse::Entry> instance for it. You submit the contents, beginning
44 with a "Package:" line, as the 'data' argument to new. After that, all
45 the data is populated as lowercased instance methods. For example, if
46 we used the above to create an $entry object, we would have:
47  
48 $entry->package == "kernel-image-2.6.12.2"
49 $entry->size == 4293154
50 $entry->md5sum == 2acf846b127b71a1fa1143214b2b85a9
51  
52 L<DPKG::Parse::Entry> will skip any attribute it does not know about. You
53 can see what it has skipped by passing a true value to the 'debug' option to
54 'new()'.
55  
56 It should know all the attributes present in a Packages, available, and
57 status file.
58  
59 See L<DPKG::Parse::Status>, L<DPKG::Parse::Available>, and
60 L<DPKG::Parse::Packages> for more information on how to easily generate
61 DPKG::Parse::Entry objects.
62  
63 =head1 METHODS
64  
65 =over 4
66  
67 =cut
68  
69 package DPKG::Parse::Entry;
70  
71 5 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 5 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 use Carp;
74 use strict;
75 use warnings;
76  
77 DPKG::Parse::Entry->mk_accessors(qw(
78 architecture
79 bugs
80 build_essential
81 conflicts
82 config_version
83 conffiles
84 depends
85 description
86 enhances
87 essential
88 filename
89 installed_size
90 maintainer
91 md5sum
92 origin
93 package
94 priority
95 provides
96 pre_depends
97 recommends
98 replaces
99 size
100 source
101 section
102 suggests
103 status
104 task
105 tag
106 url
107 version
108 ));
109  
110 =item Accessor Methods
111  
112 The following accessor methods correspond directly to the values found in
113 the parsed Package block, with one exception: "-" characters are replaced
114 with "_". So, "build-essential" becomes "build_essential".
115  
116 The accessors are:
117  
118 architecture
119 bugs
120 build_essential
121 conflicts
122 config_version
123 conffiles
124 depends
125 description
126 enhances
127 essential
128 filename
129 installed_size
130 maintainer
131 md5sum
132 origin
133 package
134 priority
135 provides
136 pre_depends
137 recommends
138 replaces
139 size
140 source
141 section
142 suggests
143 status
144 task
145 tag
146 url
147 version
148  
149 =item new('data' => $data, 'debug' => 1)
150  
151 Creates a new L<DPKG::Parse::Entry> object. 'data' should be a scalar that
152 contains the text of a dpkg-style Package entry. If the 'debug' flag is
153 set, we will Carp about entries we don't have accessors for.
154  
155 =cut
156 sub new {
157 my $pkg = shift;
158 my %p = validate(@_,
159 {
160 'data' => { 'type' => SCALAR, 'optional' => 1 },
161 'debug' => { 'type' => SCALAR, 'default' => 0, 'optional' => 1 },
162 }
163 );
164 my $ref = {};
165 bless($ref, $pkg);
166 if ($p{'data'}) {
167 $ref->parse('data' => $p{'data'});
168 };
169 return $ref;
170 }
171  
172 =item parse('data' => $data);
173  
174 Does the actual parsing of the Package block given to new(). Probably
175 should only be called once per object.
176  
177 =cut
178 sub parse {
179 my $pkg = shift;
180 my %p = validate(@_,
181 {
182 'data' => { 'type' => SCALAR, 'optional' => 1 },
183 },
184 );
185 my $field;
186 my $contents;
187 foreach my $line (split(/\n/, $p{'data'})) {
188 if ($line =~ /^([\w|-]+): (.+)$/) {
189 $field = $1;
190 $contents = $2;
191 $field = lc($field);
192 $field =~ s/-/_/g;
193 if ($pkg->can($field)) {
194 $pkg->$field($contents);
195 } else {
196 if ($pkg->debug) {
197 carp "I don't know about field $field\n";
198 }
199 next;
200 }
201 } elsif ($line =~ /^ / && $field) {
202 $line =~ s/^ //g;
203 $line =~ s/^.$//g;
204 if ($contents !~ /\n$/) {
205 $contents = $contents . "\n" . $line . "\n";
206 } else {
207 $contents = $contents . $line;
208 }
209 if ($pkg->can($field)) {
210 $pkg->$field($contents);
211 } else {
212 if ($pkg->debug) {
213 carp "I don't know about field $field\n";
214 }
215 next;
216 }
217 } elsif ($line =~ /^([\w|-]+):$/) {
218 $field = $1;
219 $field = lc($field);
220 $field =~ s/-/_/g;
221 if ($pkg->can($field)) {
222 $pkg->$field("");
223 } else {
224 if ($pkg->debug) {
225 carp "I don't know about field $field\n";
226 }
227 next;
228 }
229 } else {
230 die "I have no idea what to do with $line!\n";
231 }
232 }
233 }
234  
235 1;
236  
« Parse/Available.pm ↑ Summary Parse/Packages.pm »