Selaa lähdekoodia

Update Dpkg::Substvars to use Dpkg::Interface::Storable

The parse() function is replaced by load() for most users.
Raphaël Hertzog 16 vuotta sitten
vanhempi
commit
aba76e6de2

+ 49 - 18
scripts/Dpkg/Substvars.pm

@@ -1,4 +1,4 @@
-# Copyright © 2007,2009 Raphaël Hertzog <hertzog@debian.org>
+# Copyright © 2007-2010 Raphaël Hertzog <hertzog@debian.org>
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -25,6 +25,8 @@ use Dpkg::Gettext;
 
 use POSIX qw(:errno_h);
 
+use base qw(Dpkg::Interface::Storable);
+
 my $maxsubsts = 50;
 
 =head1 NAME
@@ -71,7 +73,7 @@ sub new {
     bless $self, $class;
     $self->no_warn($_) foreach keys %{$self->{'vars'}};
     if ($arg) {
-        $self->parse($arg);
+        $self->load($arg) if -e $arg;
     }
     return $self;
 }
@@ -122,28 +124,27 @@ sub no_warn {
     $self->{'used'}{$key}++;
 }
 
-=item $s->parse($file)
+=item $s->load($file)
 
 Add new substitutions read from $file.
 
+=item $s->parse($fh, $desc)
+
+Add new substitutions read from the filehandle. $desc is used to identify
+the filehandle in error messages.
+
 =cut
 
 sub parse {
-    my ($self, $varlistfile) = @_;
-    $varlistfile = "./$varlistfile" if $varlistfile =~ m/\s/;
-    if (open(SV, "<", $varlistfile)) {
-	binmode(SV);
-	while (<SV>) {
-	    next if m/^\s*\#/ || !m/\S/;
-	    s/\s*\n$//;
-	    m/^(\w[-:0-9A-Za-z]*)\=(.*)$/ ||
-		error(_g("bad line in substvars file %s at line %d"),
-		      $varlistfile, $.);
-	    $self->{'vars'}{$1} = $2;
-	}
-	close(SV);
-    } elsif ($! != ENOENT) {
-	error(_g("unable to open substvars file %s: %s"), $varlistfile, $!);
+    my ($self, $fh, $varlistfile) = @_;
+    binmode($fh);
+    while (<$fh>) {
+	next if m/^\s*\#/ || !m/\S/;
+	s/\s*\n$//;
+	m/^(\w[-:0-9A-Za-z]*)\=(.*)$/ ||
+	    error(_g("bad line in substvars file %s at line %d"),
+		  $varlistfile, $.);
+	$self->{'vars'}{$1} = $2;
     }
 }
 
@@ -238,6 +239,36 @@ sub warn_about_unused {
     }
 }
 
+=item $s->save($file)
+
+Store all substitutions variables except the automatic ones in the
+indicated file.
+
+=item "$s"
+
+Return a string representation of all substitutions variables except the
+automatic ones.
+
+=item $str = $s->output($fh)
+
+Print all substitutions variables except the automatic ones in the
+filehandle and return the content written.
+
+=cut
+
+sub output {
+    my ($self, $fh) = @_;
+    my $str = "";
+    # Store all non-automatic substitutions only
+    foreach my $vn (sort keys %{$self->{'vars'}}) {
+	next if /^(?:(?:dpkg|source|binary):(?:Source-)?Version|Space|Tab|Newline|Arch|Source-Version|F:.+)$/;
+	my $line = "$vn=" . $self->{vars}{$vn} . "\n";
+	print $fh $line if defined $fh;
+	$str .= $line;
+    }
+    return $str;
+}
+
 =back
 
 =head1 AUTHOR

+ 1 - 1
scripts/dpkg-genchanges.pl

@@ -210,7 +210,7 @@ my $control = Dpkg::Control::Info->new($controlfile);
 my $fields = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
 $substvars->set_version_substvars($changelog->{"Version"});
 $substvars->set_arch_substvars();
-$substvars->parse($varlistfile) if -e $varlistfile;
+$substvars->load($varlistfile) if -e $varlistfile;
 
 if (defined($prev_changelog) and
     version_compare_relation($changelog->{"Version"}, REL_LT,

+ 1 - 1
scripts/dpkg-gencontrol.pl

@@ -138,7 +138,7 @@ $options{"changelogformat"} = $changelogformat if $changelogformat;
 my $changelog = changelog_parse(%options);
 $substvars->set_version_substvars($changelog->{"Version"});
 $substvars->set_arch_substvars();
-$substvars->parse($varlistfile) if -e $varlistfile;
+$substvars->load($varlistfile) if -e $varlistfile;
 $substvars->set("binary:Version", $forceversion) if defined $forceversion;
 my $control = Dpkg::Control::Info->new($controlfile);
 my $fields = Dpkg::Control->new(type => CTRL_PKG_DEB);

+ 1 - 1
scripts/dpkg-source.pl

@@ -325,7 +325,7 @@ if ($options{'opmode'} =~ /^(-b|--print-format)$/) {
     # Write the .dsc
     my $dscname = $srcpkg->get_basename(1) . ".dsc";
     info(_g("building %s in %s"), $sourcepackage, $dscname);
-    $substvars->parse($varlistfile) if $varlistfile && -e $varlistfile;
+    $substvars->load($varlistfile) if $varlistfile && -e $varlistfile;
     $srcpkg->write_dsc(filename => $dscname,
 		       remove => \%remove,
 		       override => \%override,

+ 1 - 1
scripts/t/750_Dpkg_Substvars.t

@@ -28,7 +28,7 @@ my $datadir = $srcdir . '/t/750_Dpkg_Substvars';
 
 my $s = Dpkg::Substvars->new();
 
-$s->parse("$datadir/substvars1");
+$s->load("$datadir/substvars1");
 
 # simple value tests
 is($s->get('var1'), 'Some value', 'var1');