File: | Dpkg/Source/Format.pm |
Coverage: | 75.8% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org> | ||||||
2 | # Copyright © 2008-2018 Guillem Jover <guillem@debian.org> | ||||||
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 <https://www.gnu.org/licenses/>. | ||||||
16 | |||||||
17 | =encoding utf8 | ||||||
18 | |||||||
19 - 28 | =head1 NAME Dpkg::Source::Format - manipulate debian/source/format files =head1 DESCRIPTION This module provides a class that can manipulate Debian source package F<debian/source/format> files. =cut | ||||||
29 | |||||||
30 | package Dpkg::Source::Format 1.00; | ||||||
31 | |||||||
32 | 12 12 12 | 27 3 152 | use strict; | ||||
33 | 12 12 12 | 14 7 198 | use warnings; | ||||
34 | |||||||
35 | 12 12 12 | 346 10 303 | use Dpkg::Gettext; | ||||
36 | 12 12 12 | 455 6 396 | use Dpkg::ErrorHandling; | ||||
37 | |||||||
38 | 12 12 12 | 26 3 108 | use parent qw(Dpkg::Interface::Storable); | ||||
39 | |||||||
40 - 51 | =head1 METHODS =over 4 =item $f = Dpkg::Source::Format->new(%opts) Creates a new object corresponding to a source package's F<debian/source/format> file. When the key B<filename> is set, it will be used to parse and set the format. Otherwise if the B<format> key is set it will be validated and used to set the format. =cut | ||||||
52 | |||||||
53 | sub new { | ||||||
54 | 12 | 1 | 17 | my ($this, %opts) = @_; | |||
55 | 12 | 44 | my $class = ref($this) || $this; | ||||
56 | 12 | 30 | my $self = { | ||||
57 | filename => undef, | ||||||
58 | major => undef, | ||||||
59 | minor => undef, | ||||||
60 | variant => undef, | ||||||
61 | }; | ||||||
62 | 12 | 10 | bless $self, $class; | ||||
63 | |||||||
64 | 12 | 25 | if (exists $opts{filename}) { | ||||
65 | 0 | 0 | $self->load($opts{filename}, compression => 0); | ||||
66 | } elsif ($opts{format}) { | ||||||
67 | 0 | 0 | $self->set($opts{format}); | ||||
68 | } | ||||||
69 | 12 | 36 | return $self; | ||||
70 | } | ||||||
71 | |||||||
72 - 79 | =item $f->set_from_parts($major[, $minor[, $variant]]) Sets the source format from its parts. The $major part is mandatory. The $minor and $variant parts are optional. B<Notice>: This function performs no validation. =cut | ||||||
80 | |||||||
81 | sub set_from_parts { | ||||||
82 | 9 | 1 | 10 | my ($self, $major, $minor, $variant) = @_; | |||
83 | |||||||
84 | 9 | 56 | $self->{major} = $major; | ||||
85 | 9 | 19 | $self->{minor} = $minor // 0; | ||||
86 | 9 | 10 | $self->{variant} = $variant; | ||||
87 | } | ||||||
88 | |||||||
89 - 95 | =item ($major, $minor, $variant) = $f->set($format) Sets (and validates) the source $format specified. Will return the parsed format parts as a list, the optional $minor and $variant parts might be undef. =cut | ||||||
96 | |||||||
97 | sub set { | ||||||
98 | 33 | 1 | 31 | my ($self, $format) = @_; | |||
99 | |||||||
100 | 33 | 99 | if ($format =~ /^(\d+)(?:\.(\d+))?(?:\s+\(([a-z0-9]+)\))?$/) { | ||||
101 | 9 | 16 | my ($major, $minor, $variant) = ($1, $2, $3); | ||||
102 | |||||||
103 | 9 | 12 | $self->set_from_parts($major, $minor, $variant); | ||||
104 | |||||||
105 | 9 | 9 | return ($major, $minor, $variant); | ||||
106 | } else { | ||||||
107 | 24 | 39 | error(g_("source package format '%s' is invalid"), $format); | ||||
108 | } | ||||||
109 | } | ||||||
110 | |||||||
111 - 118 | =item ($major, $minor, $variant) = $f->get() =item $format = $f->get() Gets the source format, either as properly formatted scalar, or as a list of its parts, where the optional $minor and $variant parts might be undef. =cut | ||||||
119 | |||||||
120 | sub get { | ||||||
121 | 15 | 1 | 16 | my $self = shift; | |||
122 | |||||||
123 | 15 | 14 | if (wantarray) { | ||||
124 | 3 | 7 | return ($self->{major}, $self->{minor}, $self->{variant}); | ||||
125 | } else { | ||||||
126 | 12 | 13 | my $format = "$self->{major}.$self->{minor}"; | ||||
127 | 12 | 17 | $format .= " ($self->{variant})" if defined $self->{variant}; | ||||
128 | |||||||
129 | 12 | 15 | return $format; | ||||
130 | } | ||||||
131 | } | ||||||
132 | |||||||
133 - 137 | =item $count = $f->parse($fh, $desc) Parse the source format string from $fh, with filehandle description $desc. =cut | ||||||
138 | |||||||
139 | sub parse { | ||||||
140 | 0 | 1 | 0 | my ($self, $fh, $desc) = @_; | |||
141 | |||||||
142 | 0 | 0 | my $format = <$fh>; | ||||
143 | 0 | 0 | chomp $format if defined $format; | ||||
144 | 0 | 0 | error(g_('%s is empty'), $desc) | ||||
145 | unless defined $format and length $format; | ||||||
146 | |||||||
147 | 0 | 0 | $self->set($format); | ||||
148 | |||||||
149 | 0 | 0 | return 1; | ||||
150 | } | ||||||
151 | |||||||
152 - 163 | =item $count = $f->load($filename) Parse $filename contents for a source package format string. =item $str = $f->output([$fh]) =item "$f" Returns a string representing the source package format version. If $fh is set, it prints the string to the filehandle. =cut | ||||||
164 | |||||||
165 | sub output { | ||||||
166 | 3 | 1 | 3 | my ($self, $fh) = @_; | |||
167 | |||||||
168 | 3 | 4 | my $str = $self->get(); | ||||
169 | |||||||
170 | 3 0 | 5 0 | print { $fh } "$str\n" if defined $fh; | ||||
171 | |||||||
172 | 3 | 5 | return $str; | ||||
173 | } | ||||||
174 | |||||||
175 - 187 | =item $f->save($filename) Save the source package format into the given $filename. =back =head1 CHANGES =head2 Version 1.00 (dpkg 1.19.3) Mark the module as public. =cut | ||||||
188 | |||||||
189 | 1; |