File Coverage

blib/lib/Parse/RPM/Spec.pm
Criterion Covered Total %
statement 45 49 91.8
branch 30 36 83.3
condition n/a
subroutine 8 8 100.0
pod 1 2 50.0
total 84 95 88.4


line stmt bran cond sub pod time code
1             package Parse::RPM::Spec;
2              
3 2     2   149 use 5.006000;
  2         24  
  2         10  
4 2     2   26 use strict;
  2         10  
  2         88  
5 2     2   28 use warnings;
  2         11  
  2         40  
6              
7 2     2   26 use Carp;
  2         10  
  2         38  
8 2     2   68 use Moose;
  2         16  
  2         45  
9              
10             our $VERSION = '0.04';
11              
12             has file => ( is => 'rw', isa => 'Str', required => 1 );
13             has name => ( is => 'rw', isa => 'Str' );
14             has version => ( is => 'rw', isa => 'Str' );
15             has release => ( is => 'rw', isa => 'Str' );
16             has summary => ( is => 'rw', isa => 'Str' );
17             has license => ( is => 'rw', isa => 'Str' );
18             has group => ( is => 'rw', isa => 'Str' );
19             has url => ( is => 'rw', isa => 'Str' );
20             has source => ( is => 'rw', isa => 'Str' );
21             has buildroot => ( is => 'rw', isa => 'Str' );
22             has buildarch => ( is => 'rw', isa => 'Str' );
23             has buildrequires => ( is => 'rw', isa => 'ArrayRef[Str]' );
24             has requires => ( is => 'rw', isa => 'ArrayRef[Str]' );
25              
26             sub BUILD {
27 2     2 0 666   my $self = shift;
28              
29 2         20   $self->parse_file;
30              
31 2         87   return $self;
32             }
33              
34             sub parse_file {
35 2     2 1 14   my $self = shift;
36              
37 2 50       19   $self->file(shift) if @_;
38              
39 2         22   my $file = $self->file;
40              
41 2 50       68   unless (defined $file) {
42 0         0     croak "No spec file to parse\n";
43               }
44              
45 2 50       102   unless (-e $file) {
46 0         0     croak "Spec file $file doesn't exist\n";
47               }
48              
49 2 50       35   unless (-r $file) {
50 0         0     croak "Cannot read spec file $file\n";
51               }
52              
53 2 50       31   unless (-s $file) {
54 0         0     croak "Spec file $file is empty\n";
55               }
56              
57 2 50       187   open my $fh, $file or croak "Cannot open $file: $!\n";
58              
59 2         155   while (<$fh>) {
60 96 100       683     /^Name:\s*(\S+)/ and $self->{name} = $1;
61 96 100       542     /^Version:\s*(\S+)/ and $self->{version} = $1;
62 96 100       517     /^Release:\s*(\S+)/ and $self->{release} = $1;
63 96 100       507     /^Summary:\s*(\S+)/ and $self->{summary} = $1;
64 96 100       508     /^License:\s*(.+)/ and $self->{license} = $1;
65 96 100       512     /^Group:\s*(\S+)/ and $self->{group} = $1;
66 96 100       513     /^URL:\s*(\S+)/ and $self->{url} = $1;
67 96 100       612     /^Source0?:\s*(\S+)/ and $self->{source} = $1;
68 96 100       507     /^BuildRoot:\s*(\S+)/ and $self->{buildroot} = $1;
69 96 100       493     /^BuildArch:\s*(\S+)/ and $self->{buildarch} = $1;
70              
71 96 100       517     /^BuildRequires:\s*(.+)/ and push @{$self->{buildrequires}}, $1;
  4         64  
72 96 100       974     /^Requires:\s*(.+)/ and push @{$self->{requires}}, $1;
  2         46  
73               }
74              
75 2         15   return $self;
76             }
77              
78 2     2   71 no Moose;
  2         14  
  2         30  
79             __PACKAGE__->meta->make_immutable;
80              
81             1;
82             __END__
83            
84             =head1 NAME
85            
86             Parse::RPM::Spec - Perl extension to parse RPM spec files.
87            
88             =head1 SYNOPSIS
89            
90             use Parse::RPM::Spec;
91            
92             my $spec = Parse::RPM::Spec->new( { file => 'some_package.spec' } );
93            
94             print $spec->name; # some_package
95             print $spec->version; # 0.01 (for example)
96            
97             =head1 DESCRIPTION
98            
99             RPM is the package management system used on Linux distributions based on
100             Red Hat Linux. These days that includes Fedora, Red Hat Enterprise Linux,
101             Centos, SUSE, Mandriva and many more.
102            
103             RPMs are build from the source of a packages along with a spec file. The
104             spec file controls how the RPM is built.
105            
106             This module creates Perl objects which module spec files. Currently it gives
107             you simple access to various pieces of information from the spec file.
108            
109             =head1 CAVEAT
110            
111             This is still in development. I particular it doesn't currently parse all of
112             a spec file. It just does the bits that I currently use. I will be adding
113             support for the rest of the file very soon.
114            
115             =head1 METHODS
116            
117             =head2 $spec = Parse::RPM::Spec->new('some_package.spec')
118            
119             Creates a new Parse::EPM::Spec object. Takes one mandatory parameter which
120             is the path to the spec file that you are interested in. Throws an exception
121             if it doesn't find a valid spec.
122            
123             =head2 $spec->parse_file('some_package.spec')
124            
125             Parses the given spec file. This is called as part of the initialisation
126             carried out by the C<new> method, so there is generally no need to call it
127             yourself.
128            
129             =head2 $spec->name, $spec->version, $spec->release, $spec->summary, $spec->license, $spec->group, $spec->url, $spec->source, $spec->buildroot, $spec->buildarch, $spec->buildrequires, $spec->requires
130            
131             Attribute accessors for the spec file object. Each one returns a piece of
132             information from the spec file header. The C<buildrequires> and C<requires>
133             methods are slightly different. Because these keys can have multiple values,
134             they return a reference to an array of values.
135            
136             =head2 EXPORT
137            
138             None.
139            
140             =head1 TO DO
141            
142             Plenty still to do here. Firstly, and most importantly, parsing the rest
143             of the spec file.
144            
145             =head1 SEE ALSO
146            
147             =over 4
148            
149             =item *
150            
151             Red Hat RPM Guide - L<http://docs.fedoraproject.org/drafts/rpm-guide-en/index.html>
152            
153             =item *
154            
155             Maximum RPM - L<http://www.rpm.org/max-rpm/s1-rpm-file-format-rpm-file-format.html>
156            
157             =back
158            
159             =head1 AUTHOR
160            
161             Dave Cross, E<lt>dave@mag-sol.com<gt>
162            
163             =head1 COPYRIGHT AND LICENSE
164            
165             Copyright (C) 2008 by Magnum Solutions Ltd.
166            
167             This library is free software; you can redistribute it and/or modify
168             it under the same terms as Perl itself, either Perl version 5.10.0 or,
169             at your option, any later version of Perl 5 you may have available.
170            
171             =cut
172