[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Support for optional substvars



Hi Niels!

Several months ago you asked on IRC about extending the substvars file
format to be able to mark substvars as optional (so that they would not
warn in case they end up not being used, like how several of the built-in
ones are handled). At the time I mentioned that I'd need to ponder about
it but had no energy (or motivation) do work on stuff.

I've not forgotten about it, and it has been on the back of my head,
and I've been converging on using «?=» as operator. Then rechecked the
IRC logs, and noticed that was exactly one of the formats you proposed
at the time (so either it stuck in the background and subconsciously
surfaced, or I came to the same conclusion :). In any case, I've prepared
the attached patch, which I'd be happy to include for 1.21.8, in case
something like this works for you?

Thanks,
Guillem
From 5c0820933a3d8dcf9b28bbce818f853e317b2468 Mon Sep 17 00:00:00 2001
From: Guillem Jover <guillem@debian.org>
Date: Mon, 11 Apr 2022 23:10:58 +0200
Subject: [PATCH] Dpkg::Substvars: Add support for optional substvars assigned
 with ?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Some packaging helpers (namely debhelper) set various substvars that
might usually be used by the packaging, but not necessarily always.

In the same way we mark some of the built-in variables as used, we add
a new operator «?=» to make it possible for other tools to mark those
other variables as optional too. Track those internally too so that they
can be printed back properly.

Proposed-by: Niels Thykier <niels@thykier.net>
---
 man/deb-substvars.pod               |  5 ++++-
 scripts/Dpkg/Substvars.pm           | 13 ++++++++++---
 scripts/t/Dpkg_Substvars.t          |  4 +++-
 scripts/t/Dpkg_Substvars/substvars1 |  1 +
 4 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/man/deb-substvars.pod b/man/deb-substvars.pod
index c8fcb4c24..f8b80fecd 100644
--- a/man/deb-substvars.pod
+++ b/man/deb-substvars.pod
@@ -90,7 +90,10 @@ B<debian/substvars>
 (or whatever other file is specified using the
 B<-T>
 option). This file consists of lines of the form
-I<name>B<=>I<value>.
+I<name>B<=>I<value> or I<name>B<?=>I<value>.
+The B<=> operator assigns a normal substitution variable, while the B<?=>
+operator (since dpkg 1.21.8) assigns an optional substitution variable
+which will emit no warnings even if unused.
 Trailing whitespace on each line, blank lines, and
 lines starting with a
 B<#>
diff --git a/scripts/Dpkg/Substvars.pm b/scripts/Dpkg/Substvars.pm
index 497d51a9a..177cfd5bf 100644
--- a/scripts/Dpkg/Substvars.pm
+++ b/scripts/Dpkg/Substvars.pm
@@ -48,6 +48,7 @@ use constant {
     SUBSTVAR_ATTR_USED => 1,
     SUBSTVAR_ATTR_AUTO => 2,
     SUBSTVAR_ATTR_AGED => 4,
+    SUBSTVAR_ATTR_OPT  => 8,
 };
 
 =head1 METHODS
@@ -185,13 +186,18 @@ sub parse {
 
     binmode($fh);
     while (<$fh>) {
+        my $attr;
+
 	next if m/^\s*\#/ || !m/\S/;
 	s/\s*\n$//;
-	if (! m/^(\w[-:0-9A-Za-z]*)\=(.*)$/) {
+	if (! m/^(\w[-:0-9A-Za-z]*)(\?)?\=(.*)$/) {
 	    error(g_('bad line in substvars file %s at line %d'),
 		  $varlistfile, $.);
 	}
-	$self->set($1, $2);
+        if (defined $2) {
+            $attr = (SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_OPT) if $2 eq '?';
+        }
+        $self->set($1, $3, $attr);
         $count++;
     }
 
@@ -426,7 +432,8 @@ sub output {
     # Store all non-automatic substitutions only
     foreach my $vn (sort keys %{$self->{vars}}) {
 	next if $self->{attr}{$vn} & SUBSTVAR_ATTR_AUTO;
-	my $line = "$vn=" . $self->{vars}{$vn} . "\n";
+        my $op = $self->{attr}{$vn} & SUBSTVAR_ATTR_OPT ? '?=' : '=';
+        my $line = "$vn$op" . $self->{vars}{$vn} . "\n";
 	print { $fh } $line if defined $fh;
 	$str .= $line;
     }
diff --git a/scripts/t/Dpkg_Substvars.t b/scripts/t/Dpkg_Substvars.t
index 31d66b16c..e3f935414 100644
--- a/scripts/t/Dpkg_Substvars.t
+++ b/scripts/t/Dpkg_Substvars.t
@@ -16,7 +16,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 51;
+use Test::More tests => 52;
 use Test::Dpkg qw(:paths);
 
 use Dpkg ();
@@ -40,12 +40,14 @@ is($s->get('var1'), 'Some value', 'var1');
 is($s->get('var2'), 'Some other value', 'var2');
 is($s->get('var3'), 'Yet another value', 'var3');
 is($s->get('var4'), undef, 'no var4');
+is($s->get('optional-var5'), 'Optionally used value', 'optional-var5');
 
 # Set automatic variable
 $s->set_as_auto('var_auto', 'auto');
 is($s->get('var_auto'), 'auto', 'get var_auto');
 
 $expected = <<'VARS';
+optional-var5?=Optionally used value
 var1=Some value
 var2=Some other value
 var3=Yet another value
diff --git a/scripts/t/Dpkg_Substvars/substvars1 b/scripts/t/Dpkg_Substvars/substvars1
index f71c95361..a2196c8b0 100644
--- a/scripts/t/Dpkg_Substvars/substvars1
+++ b/scripts/t/Dpkg_Substvars/substvars1
@@ -7,3 +7,4 @@ var2=Some other value
  # this line has a late comment
 var3=Yet another value
 #var4=This is not set
+optional-var5?=Optionally used value
-- 
2.35.1


Reply to: