po: Update German man pages translation
[dpkg.git] / scripts / Dpkg / Source / Format.pm
blob01af7c98096c5e6db72cba3136f5296434f86800
1 # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2008-2018 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
17 =encoding utf8
19 =head1 NAME
21 Dpkg::Source::Format - manipulate debian/source/format files
23 =head1 DESCRIPTION
25 This module provides a class that can manipulate Debian source
26 package F<debian/source/format> files.
28 =cut
30 package Dpkg::Source::Format 1.00;
32 use strict;
33 use warnings;
35 use Dpkg::Gettext;
36 use Dpkg::ErrorHandling;
38 use parent qw(Dpkg::Interface::Storable);
40 =head1 METHODS
42 =over 4
44 =item $f = Dpkg::Source::Format->new(%opts)
46 Creates a new object corresponding to a source package's
47 F<debian/source/format> file.
49 Options:
51 =over
53 =item B<filename>
55 Set the filename to use to parse and set the format.
57 =item B<format>
59 Set and validate the format to use instead of loading the default file,
60 if no filename has been specified.
62 =back
64 =cut
66 sub new {
67 my ($this, %opts) = @_;
68 my $class = ref($this) || $this;
69 my $self = {
70 filename => undef,
71 major => undef,
72 minor => undef,
73 variant => undef,
75 bless $self, $class;
77 if (exists $opts{filename}) {
78 $self->load($opts{filename}, compression => 0);
79 } elsif ($opts{format}) {
80 $self->set($opts{format});
82 return $self;
85 =item $f->set_from_parts($major[, $minor[, $variant]])
87 Sets the source format from its parts. The $major part is mandatory.
88 The $minor and $variant parts are optional.
90 B<Notice>: This function performs no validation.
92 =cut
94 sub set_from_parts {
95 my ($self, $major, $minor, $variant) = @_;
97 $self->{major} = $major;
98 $self->{minor} = $minor // 0;
99 $self->{variant} = $variant;
102 =item ($major, $minor, $variant) = $f->set($format)
104 Sets (and validates) the source $format specified. Will return the parsed
105 format parts as a list, the optional $minor and $variant parts might be
106 undef.
108 =cut
110 sub set {
111 my ($self, $format) = @_;
113 if ($format =~ /^(\d+)(?:\.(\d+))?(?:\s+\(([a-z0-9]+)\))?$/) {
114 my ($major, $minor, $variant) = ($1, $2, $3);
116 $self->set_from_parts($major, $minor, $variant);
118 return ($major, $minor, $variant);
119 } else {
120 error(g_("source package format '%s' is invalid"), $format);
124 =item ($major, $minor, $variant) = $f->get()
126 =item $format = $f->get()
128 Gets the source format, either as properly formatted scalar, or as a list
129 of its parts, where the optional $minor and $variant parts might be undef.
131 =cut
133 sub get {
134 my $self = shift;
136 if (wantarray) {
137 return ($self->{major}, $self->{minor}, $self->{variant});
138 } else {
139 my $format = "$self->{major}.$self->{minor}";
140 $format .= " ($self->{variant})" if defined $self->{variant};
142 return $format;
146 =item $count = $f->parse($fh, $desc)
148 Parse the source format string from $fh, with filehandle description $desc.
150 =cut
152 sub parse {
153 my ($self, $fh, $desc) = @_;
155 my $format = <$fh>;
156 chomp $format if defined $format;
157 error(g_('%s is empty'), $desc)
158 unless defined $format and length $format;
160 $self->set($format);
162 return 1;
165 =item $count = $f->load($filename)
167 Parse $filename contents for a source package format string.
169 =item $str = $f->output([$fh])
171 =item "$f"
173 Returns a string representing the source package format version.
174 If $fh is set, it prints the string to the filehandle.
176 =cut
178 sub output {
179 my ($self, $fh) = @_;
181 my $str = $self->get();
183 print { $fh } "$str\n" if defined $fh;
185 return $str;
188 =item $f->save($filename)
190 Save the source package format into the given $filename.
192 =back
194 =head1 CHANGES
196 =head2 Version 1.00 (dpkg 1.19.3)
198 Mark the module as public.
200 =cut