Просмотр исходного кода

Dpkg::Substvars: Switch used tracking from a counter to a bitfield

This will allow to easily add other variable attributes, and not have
to care about one hash per attribute per variable.
Guillem Jover лет назад: 12
Родитель
Сommit
b1737a26c9
1 измененных файлов с 31 добавлено и 17 удалено
  1. 31 17
      scripts/Dpkg/Substvars.pm

+ 31 - 17
scripts/Dpkg/Substvars.pm

@@ -1,4 +1,4 @@
-# Copyright © 2006-2009,2012 Guillem Jover <guillem@debian.org>
+# Copyright © 2006-2009,2012-2014 Guillem Jover <guillem@debian.org>
 # Copyright © 2007-2010 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
 # This program is free software; you can redistribute it and/or modify
@@ -44,6 +44,12 @@ Dpkg::Substvars - handle variable substitution in strings
 It provides some an object which is able to substitute variables in
 It provides some an object which is able to substitute variables in
 strings.
 strings.
 
 
+=cut
+
+use constant {
+    SUBSTVAR_ATTR_USED => 1,
+};
+
 =head1 METHODS
 =head1 METHODS
 
 
 =over 8
 =over 8
@@ -73,12 +79,14 @@ sub new {
             'dpkg:Version' => $Dpkg::PROGVERSION,
             'dpkg:Version' => $Dpkg::PROGVERSION,
             'dpkg:Upstream-Version' => $Dpkg::PROGVERSION,
             'dpkg:Upstream-Version' => $Dpkg::PROGVERSION,
             },
             },
-        used => {},
+        attr => {},
 	msg_prefix => '',
 	msg_prefix => '',
     };
     };
     $self->{vars}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
     $self->{vars}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
     bless $self, $class;
     bless $self, $class;
-    $self->mark_as_used($_) foreach keys %{$self->{vars}};
+
+    my $attr = SUBSTVAR_ATTR_USED;
+    $self->{attr}{$_} = $attr foreach keys %{$self->{vars}};
     if ($arg) {
     if ($arg) {
         $self->load($arg) if -e $arg;
         $self->load($arg) if -e $arg;
     }
     }
@@ -92,8 +100,12 @@ Add/replace a substitution.
 =cut
 =cut
 
 
 sub set {
 sub set {
-    my ($self, $key, $value) = @_;
+    my ($self, $key, $value, $attr) = @_;
+
+    $attr //= 0;
+
     $self->{vars}{$key} = $value;
     $self->{vars}{$key} = $value;
+    $self->{attr}{$key} = $attr;
 }
 }
 
 
 =item $s->set_as_used($key, $value)
 =item $s->set_as_used($key, $value)
@@ -105,8 +117,8 @@ even if unused).
 
 
 sub set_as_used {
 sub set_as_used {
     my ($self, $key, $value) = @_;
     my ($self, $key, $value) = @_;
-    $self->set($key, $value);
-    $self->mark_as_used($key);
+
+    $self->set($key, $value, SUBSTVAR_ATTR_USED);
 }
 }
 
 
 =item $s->get($key)
 =item $s->get($key)
@@ -128,7 +140,7 @@ Remove a given substitution.
 
 
 sub delete {
 sub delete {
     my ($self, $key) = @_;
     my ($self, $key) = @_;
-    delete $self->{used}{$key};
+    delete $self->{attr}{$key};
     return delete $self->{vars}{$key};
     return delete $self->{vars}{$key};
 }
 }
 
 
@@ -141,7 +153,7 @@ default.
 
 
 sub mark_as_used {
 sub mark_as_used {
     my ($self, $key) = @_;
     my ($self, $key) = @_;
-    $self->{used}{$key}++;
+    $self->{attr}{$key} |= SUBSTVAR_ATTR_USED;
 }
 }
 
 
 =item $s->no_warn($key)
 =item $s->no_warn($key)
@@ -179,7 +191,7 @@ sub parse {
 	    error(_g('bad line in substvars file %s at line %d'),
 	    error(_g('bad line in substvars file %s at line %d'),
 		  $varlistfile, $.);
 		  $varlistfile, $.);
 	}
 	}
-	$self->{vars}{$1} = $2;
+	$self->set($1, $2);
     }
     }
 }
 }
 
 
@@ -205,14 +217,14 @@ sub set_version_substvars {
     my $upstreamversion = $sourceversion;
     my $upstreamversion = $sourceversion;
     $upstreamversion =~ s/-[^-]*$//;
     $upstreamversion =~ s/-[^-]*$//;
 
 
-    $self->{vars}{'binary:Version'} = $binaryversion;
-    $self->{vars}{'source:Version'} = $sourceversion;
-    $self->{vars}{'source:Upstream-Version'} = $upstreamversion;
+    my $attr = SUBSTVAR_ATTR_USED;
 
 
-    # XXX: Source-Version is now deprecated, remove in the future.
-    $self->{vars}{'Source-Version'} = $binaryversion;
+    $self->set('binary:Version', $binaryversion, $attr);
+    $self->set('source:Version', $sourceversion, $attr);
+    $self->set('source:Upstream-Version', $upstreamversion, $attr);
 
 
-    $self->mark_as_used($_) foreach qw/binary:Version source:Version source:Upstream-Version Source-Version/;
+    # XXX: Source-Version is now deprecated, remove in the future.
+    $self->set('Source-Version', $binaryversion, $attr);
 }
 }
 
 
 =item $s->set_arch_substvars()
 =item $s->set_arch_substvars()
@@ -226,7 +238,9 @@ This will never be warned about when unused.
 sub set_arch_substvars {
 sub set_arch_substvars {
     my ($self) = @_;
     my ($self) = @_;
 
 
-    $self->set_as_used('Arch', get_host_arch());
+    my $attr = SUBSTVAR_ATTR_USED;
+
+    $self->set('Arch', get_host_arch(), $attr);
 }
 }
 
 
 =item $newstring = $s->substvars($string)
 =item $newstring = $s->substvars($string)
@@ -278,7 +292,7 @@ sub warn_about_unused {
     $opts{msg_prefix} = $self->{msg_prefix} unless exists $opts{msg_prefix};
     $opts{msg_prefix} = $self->{msg_prefix} unless exists $opts{msg_prefix};
 
 
     foreach my $vn (keys %{$self->{vars}}) {
     foreach my $vn (keys %{$self->{vars}}) {
-        next if $self->{used}{$vn};
+        next if $self->{attr}{$vn} & SUBSTVAR_ATTR_USED;
         # Empty substitutions variables are ignored on the basis
         # Empty substitutions variables are ignored on the basis
         # that they are not required in the current situation
         # that they are not required in the current situation
         # (example: debhelper's misc:Depends in many cases)
         # (example: debhelper's misc:Depends in many cases)