File Coverage

blib/lib/DPKG/Parse.pm
Criterion Covered Total %
statement 55 57 96.4
branch 5 8 62.5
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 76 81 93.8


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             DPKG::Parse - Parse various dpkg files into Perl Objects
4              
5             =head1 SYNOPSIS
6              
7             use DPKG::Parse::Status;
8             my $status = DPKG::Parse::Status->new;
9             while (my $entry = $status->next_package) {
10             print $entry->package . " " . $entry->version . "\n";
11             }
12              
13             use DPKG::Parse::Available;
14             my $available = DPKG::Parse::Available->new;
15             while (my $entry = $available->next_package) {
16             print $entry->package . " " . $entry->version . "\n";
17             }
18              
19             =head1 DESCRIPTION
20              
21             DPKG::Parse contains utilities to parse the various files created by
22             dpkg and turn them into helpful Perl objects. Current files understood
23             by various DPKG::Parse modules:
24              
25             /var/lib/dpkg/status - DPKG::Parse::Status
26             /var/lib/dpkg/available - DPKG::Parse::Available
27             Packages.gz - DPKG::Parse::Packages
28              
29             See each module's documentation for particulars - You should not be calling
30             DPKG::Parse directly.
31              
32             =head1 METHODS
33              
34             =over 4
35              
36             =cut
37              
38             package DPKG::Parse; # git description: v0.01-9-gbe9fadb
39              
40 4     4   98 use Params::Validate qw(:all);
  4         5  
  4         681  
41 4     4   753 use DPKG::Parse::Entry;
  4         8  
  4         28  
42 4     4   132 use Class::C3;
  4         4  
  4         21  
43 4     4   103 use base qw(Class::Accessor);
  4         5  
  4         334  
44              
45 4     4   17 use strict;
  4         5  
  4         89  
46 4     4   17 use warnings;
  4         5  
  4         1728  
47              
48             DPKG::Parse->mk_accessors(qw(filename entryarray entryhash));
49             DPKG::Parse->mk_ro_accessors('debug');
50              
51             our $VERSION = '0.02'; # TRIAL
52              
53             =item filename($filename)
54              
55             A simple accessor for the file currently being parsed.
56              
57             =item entryarray
58              
59             Access to the raw array of entries in a given file.
60              
61             =item entryhash
62              
63             Access to the raw hash of entries. The key is determined by the module,
64             but is usually the Package name.
65              
66             =item new('filename' => '/var/lib/dpkg/status');
67              
68             A generic new function; takes a filename and calls the filename() accessor
69             with it. Should not be called directly, but through on of the children of
70             this package.
71              
72             =cut
73             sub new {
74 6     6 1 136 my $pkg = shift;
75 6         85 my %p = validate(@_,
76             {
77             'filename' => { 'type' => SCALAR, },
78             'debug' => { 'type' => SCALAR, 'default' => 0, 'optional' => 1 }
79             }
80             );
81 6         19 my $ref = {};
82 6 50       18 if ($p{'filename'}) {
83 6         12 $ref->{'filename'} = $p{'filename'};
84             };
85 6         10 $ref->{debug} = $p{debug};
86 6         12 $ref->{'entryarray'} = [];
87 6         9 $ref->{'entryhash'} = {};
88 6         12 bless($ref, $pkg);
89 6         21 return $ref;
90             }
91              
92             =item parse
93              
94             A default parse function; simply calls parse_package_format.
95              
96             =cut
97             sub parse {
98 3     3 1 21 my $pkg = shift;
99 3         20 $pkg->parse_package_format;
100             }
101              
102             =item parse_package_format
103              
104             Takes a file in a format similar to the dpkg "available" file, and creates
105             L objects from each entry.
106              
107             =cut
108             sub parse_package_format {
109 3     3 1 5 my $pkg = shift;
110 3 50       20 if (! -f $pkg->filename) {
111 0         0 die "Cannot find " . $pkg->filename . ", or it's not a file at all!";
112             }
113 3         189 open(STATUS, $pkg->filename);
114 3         133 my $entry;
115 3         7 my $line_num = -1;
116 3         5 my $entry_line = 0;
117 3         122 STATUSLINE: while (my $line = ) {
118 162         110 ++$line_num;
119 162 100       267 if ($line =~ /^\n$/) {
120 10         46 my $dpkg_entry = DPKG::Parse::Entry->new('data' => $entry, debug => $pkg->debug, line_num => $entry_line);
121 10         13 push(@{$pkg->{'entryarray'}}, $dpkg_entry);
  10         21  
122 10         27 $pkg->{'entryhash'}->{$dpkg_entry->package} = $dpkg_entry;
123 10         72 $entry = undef;
124 10         14 $entry_line = $line_num + 1;
125 10         61 next STATUSLINE;
126             }
127 152         293 $entry = $entry . $line;
128             }
129 3         28 close(STATUS);
130             }
131              
132             =item get_package('name' => 'postfix', 'hash' => 'entryhash');
133              
134             The value of a hash, if it exists. By default, it uses the value returned
135             by the "entryhash" accessor, but that can be overridden with the "hash"
136             parameter. Usually returns a L object.
137              
138             =cut
139             sub get_package {
140 6     6 1 3797 my $pkg = shift;
141 6         105 my %p = validate( @_,
142             {
143             'name' => { 'type' => SCALAR, },
144             'hash' => { 'type' => SCALAR, 'default' => 'entryhash', },
145             },
146             );
147 6 50       39 if (exists($pkg->{$p{'hash'}}->{$p{'name'}})) {
148 6         24 return $pkg->{$p{'hash'}}->{$p{'name'}};
149             } else {
150 0         0 return undef;
151             }
152             }
153              
154             =item next_package
155              
156             Shifts the next value off the array stored in the entryarray() accessor.
157             If you want to access the raw values, do not use this function! It shifts!
158              
159             =cut
160             sub next_package {
161 13     13 1 4481 my $pkg = shift;
162 13         15 return shift(@{$pkg->{'entryarray'}});
  13         30  
163             }
164              
165             1;
166              
167             __END__