File Coverage

File:Dpkg/Source/Format.pm
Coverage:75.8%

linestmtbrancondsubpodtimecode
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
17package Dpkg::Source::Format;
18
19=encoding utf8
20
21 - 30
=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
31
32
4
4
4
8
3
47
use strict;
33
4
4
4
6
3
91
use warnings;
34
35our $VERSION = '1.00';
36
37
4
4
4
112
3
89
use Dpkg::Gettext;
38
4
4
4
128
1
115
use Dpkg::ErrorHandling;
39
40
4
4
4
6
3
8
use parent qw(Dpkg::Interface::Storable);
41
42 - 53
=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
54
55sub new {
56
4
1
4
    my ($this, %opts) = @_;
57
4
10
    my $class = ref($this) || $this;
58
4
8
    my $self = {
59        filename => undef,
60        major => undef,
61        minor => undef,
62        variant => undef,
63    };
64
4
5
    bless $self, $class;
65
66
4
5
    if (exists $opts{filename}) {
67
0
0
        $self->load($opts{filename}, compression => 0);
68    } elsif ($opts{format}) {
69
0
0
        $self->set($opts{format});
70    }
71
4
7
    return $self;
72}
73
74 - 81
=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
82
83sub set_from_parts {
84
3
1
2
    my ($self, $major, $minor, $variant) = @_;
85
86
3
15
    $self->{major} = $major;
87
3
5
    $self->{minor} = $minor // 0;
88
3
3
    $self->{variant} = $variant;
89}
90
91 - 97
=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
98
99sub set {
100
11
1
7
    my ($self, $format) = @_;
101
102
11
26
    if ($format =~ /^(\d+)(?:\.(\d+))?(?:\s+\(([a-z0-9]+)\))?$/) {
103
3
7
        my ($major, $minor, $variant) = ($1, $2, $3);
104
105
3
4
        $self->set_from_parts($major, $minor, $variant);
106
107
3
2
        return ($major, $minor, $variant);
108    } else {
109
8
5
        error(g_("source package format '%s' is invalid"), $format);
110    }
111}
112
113 - 120
=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
121
122sub get {
123
5
1
2
    my $self = shift;
124
125
5
5
    if (wantarray) {
126
1
2
        return ($self->{major}, $self->{minor}, $self->{variant});
127    } else {
128
4
4
        my $format = "$self->{major}.$self->{minor}";
129
4
4
        $format .= " ($self->{variant})" if defined $self->{variant};
130
131
4
5
        return $format;
132    }
133}
134
135 - 139
=item $count = $f->parse($fh, $desc)

Parse the source format string from $fh, with filehandle description $desc.

=cut
140
141sub parse {
142
0
1
0
    my ($self, $fh, $desc) = @_;
143
144
0
0
    my $format = <$fh>;
145
0
0
    chomp $format if defined $format;
146
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 - 165
=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
166
167sub output {
168
1
1
1
    my ($self, $fh) = @_;
169
170
1
1
    my $str = $self->get();
171
172
1
0
2
0
    print { $fh } "$str\n" if defined $fh;
173
174
1
11
    return $str;
175}
176
177 - 189
=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
190
1911;