File Coverage

blib/lib/DPKG/Parse/Entry.pm
Criterion Covered Total %
statement 42 57 73.6
branch 8 22 36.3
condition 1 3 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 60 91 65.9


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