File Coverage

blib/lib/Dpkg/Source/Format.pm
Criterion Covered Total %
statement 41 50 82.0
branch 9 16 56.2
condition 3 8 37.5
subroutine 10 11 90.9
pod 6 6 100.0
total 69 91 75.8


line stmt bran cond sub pod time code
1             # Copyright © 2008-2011 Raphaël Hertzog
2             # Copyright © 2008-2018 Guillem Jover
3             #
4             # This program is free software; you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation; either version 2 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see .
16              
17             package Dpkg::Source::Format;
18              
19             =encoding utf8
20              
21             =head1 NAME
22              
23             Dpkg::Source::Format - manipulate debian/source/format files
24              
25             =head1 DESCRIPTION
26              
27             This module provides a class that can manipulate Debian source
28             package F files.
29              
30             =cut
31              
32 2     2   69450 use strict;
  2         13  
  2         59  
33 2     2   11 use warnings;
  2         4  
  2         93  
34              
35             our $VERSION = '1.00';
36              
37 2     2   480 use Dpkg::Gettext;
  2         6  
  2         131  
38 2     2   451 use Dpkg::ErrorHandling;
  2         5  
  2         225  
39              
40 2     2   530 use parent qw(Dpkg::Interface::Storable);
  2         301  
  2         11  
41              
42             =head1 METHODS
43              
44             =over 4
45              
46             =item $f = Dpkg::Source::Format->new(%opts)
47              
48             Creates a new object corresponding to a source package's
49             F file. When the key B is set, it will
50             be used to parse and set the format. Otherwise if the B key is
51             set it will be validated and used to set the format.
52              
53             =cut
54              
55             sub new {
56 1     1 1 100 my ($this, %opts) = @_;
57 1   33     7 my $class = ref($this) || $this;
58 1         7 my $self = {
59             filename => undef,
60             major => undef,
61             minor => undef,
62             variant => undef,
63             };
64 1         2 bless $self, $class;
65              
66 1 50       7 if (exists $opts{filename}) {
    50          
67 0         0 $self->load($opts{filename}, compression => 0);
68             } elsif ($opts{format}) {
69 0         0 $self->set($opts{format});
70             }
71 1         4 return $self;
72             }
73              
74             =item $f->set_from_parts($major[, $minor[, $variant]])
75              
76             Sets the source format from its parts. The $major part is mandatory.
77             The $minor and $variant parts are optional.
78              
79             B: This function performs no validation.
80              
81             =cut
82              
83             sub set_from_parts {
84 3     3 1 8 my ($self, $major, $minor, $variant) = @_;
85              
86 3         43 $self->{major} = $major;
87 3   100     16 $self->{minor} = $minor // 0;
88 3         7 $self->{variant} = $variant;
89             }
90              
91             =item ($major, $minor, $variant) = $f->set($format)
92              
93             Sets (and validates) the source $format specified. Will return the parsed
94             format parts as a list, the optional $minor and $variant parts might be
95             undef.
96              
97             =cut
98              
99             sub set {
100 11     11 1 3004 my ($self, $format) = @_;
101              
102 11 100       74 if ($format =~ /^(\d+)(?:\.(\d+))?(?:\s+\(([a-z0-9]+)\))?$/) {
103 3         12 my ($major, $minor, $variant) = ($1, $2, $3);
104              
105 3         11 $self->set_from_parts($major, $minor, $variant);
106              
107 3         9 return ($major, $minor, $variant);
108             } else {
109 8         27 error(g_("source package format '%s' is invalid"), $format);
110             }
111             }
112              
113             =item ($major, $minor, $variant) = $f->get()
114              
115             =item $format = $f->get()
116              
117             Gets the source format, either as properly formatted scalar, or as a list
118             of its parts, where the optional $minor and $variant parts might be undef.
119              
120             =cut
121              
122             sub get {
123 5     5 1 794 my $self = shift;
124              
125 5 100       14 if (wantarray) {
126 1         4 return ($self->{major}, $self->{minor}, $self->{variant});
127             } else {
128 4         11 my $format = "$self->{major}.$self->{minor}";
129 4 100       14 $format .= " ($self->{variant})" if defined $self->{variant};
130              
131 4         13 return $format;
132             }
133             }
134              
135             =item $count = $f->parse($fh, $desc)
136              
137             Parse the source format string from $fh, with filehandle description $desc.
138              
139             =cut
140              
141             sub parse {
142 0     0 1 0 my ($self, $fh, $desc) = @_;
143              
144 0         0 my $format = <$fh>;
145 0 0       0 chomp $format if defined $format;
146 0 0 0     0 error(g_('%s is empty'), $desc)
147             unless defined $format and length $format;
148              
149 0         0 $self->set($format);
150              
151 0         0 return 1;
152             }
153              
154             =item $count = $f->load($filename)
155              
156             Parse $filename contents for a source package format string.
157              
158             =item $str = $f->output([$fh])
159              
160             =item "$f"
161              
162             Returns a string representing the source package format version.
163             If $fh is set, it prints the string to the filehandle.
164              
165             =cut
166              
167             sub output {
168 1     1 1 3 my ($self, $fh) = @_;
169              
170 1         3 my $str = $self->get();
171              
172 1 50       4 print { $fh } "$str\n" if defined $fh;
  0         0  
173              
174 1         3 return $str;
175             }
176              
177             =item $f->save($filename)
178              
179             Save the source package format into the given $filename.
180              
181             =back
182              
183             =head1 CHANGES
184              
185             =head2 Version 1.00 (dpkg 1.19.3)
186              
187             Mark the module as public.
188              
189             =cut
190              
191             1;