Browse Source

Dpkg::BuildOptions: Add new parse_features() method

This has been refactored from Dpkg::Vendor::Debian, to have a generic
option parser.
Guillem Jover 7 years ago
parent
commit
dad593660d
4 changed files with 88 additions and 32 deletions
  1. 2 0
      debian/changelog
  2. 43 2
      scripts/Dpkg/BuildOptions.pm
  3. 6 29
      scripts/Dpkg/Vendor/Debian.pm
  4. 37 1
      scripts/t/Dpkg_BuildOptions.t

+ 2 - 0
debian/changelog

@@ -34,6 +34,8 @@ dpkg (1.18.19) UNRELEASED; urgency=medium
       bogus POD documentation to match the code.
     - Add new Auto-Built-Package field to Dpkg::Control::Fields.
     - Add a new debug() reporting function, and switch code to use it.
+    - Add new Dpkg::BuildOption parse_features() method refactored from
+      Dpkg::Vendor::Debian.
   * Documentation:
     - Cleanup software requirements in README.
     - Move control member file references from dpkg(1) to deb(5).

+ 43 - 2
scripts/Dpkg/BuildOptions.pm

@@ -1,5 +1,5 @@
 # Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
-# Copyright © 2008, 2012-2015 Guillem Jover <guillem@debian.org>
+# Copyright © 2008, 2012-2017 Guillem Jover <guillem@debian.org>
 # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
 #
 # This program is free software; you can redistribute it and/or modify
@@ -20,7 +20,7 @@ package Dpkg::BuildOptions;
 use strict;
 use warnings;
 
-our $VERSION = '1.01';
+our $VERSION = '1.02';
 
 use Dpkg::Gettext;
 use Dpkg::ErrorHandling;
@@ -155,6 +155,43 @@ sub has {
     return exists $self->{options}{$key};
 }
 
+=item $bo->parse_features($option, $use_feature)
+
+Parse the $option values, as a set of known features to enable or disable,
+as specified in the $use_feature hash reference.
+
+Each feature is prefixed with a ‘B<+>’ or a ‘B<->’ character as a marker
+to enable or disable it. The special feature “B<all>” can be used to act
+on all known features.
+
+Unknown of malformed features will emit warnings.
+
+=cut
+
+sub parse_features {
+    my ($self, $option, $use_feature) = @_;
+
+    foreach my $feature (split(/,/, $self->get($option) // '')) {
+        $feature = lc $feature;
+        if ($feature =~ s/^([+-])//) {
+            my $value = ($1 eq '+') ? 1 : 0;
+            if ($feature eq 'all') {
+                $use_feature->{$_} = $value foreach keys %{$use_feature};
+            } else {
+                if (exists $use_feature->{$feature}) {
+                    $use_feature->{$feature} = $value;
+                } else {
+                    warning(g_('unknown %s feature in %s variable: %s'),
+                            $option, $self->{envvar}, $feature);
+                }
+            }
+        } else {
+            warning(g_('incorrect value in %s option of %s variable: %s'),
+                    $option, $self->{envvar}, $feature);
+        }
+    }
+}
+
 =item $string = $bo->output($fh)
 
 Return a string representation of the build options suitable to be
@@ -191,6 +228,10 @@ sub export {
 
 =head1 CHANGES
 
+=head2 Version 1.02 (dpkg 1.18.19)
+
+New method: $bo->parse_features().
+
 =head2 Version 1.01 (dpkg 1.16.1)
 
 Enable to use another environment variable instead of DEB_BUILD_OPTIONS.

+ 6 - 29
scripts/Dpkg/Vendor/Debian.pm

@@ -1,5 +1,5 @@
 # Copyright © 2009-2011 Raphaël Hertzog <hertzog@debian.org>
-# Copyright © 2009, 2011-2015 Guillem Jover <guillem@debian.org>
+# Copyright © 2009, 2011-2017 Guillem Jover <guillem@debian.org>
 #
 # Hardening build flags handling derived from work of:
 # Copyright © 2009-2011 Kees Cook <kees@debian.org>
@@ -91,37 +91,14 @@ sub run_hook {
     }
 }
 
-sub _parse_build_options {
-    my ($self, $variable, $area, $use_feature) = @_;
-
-    # Adjust features based on user or maintainer's desires.
-    my $opts = Dpkg::BuildOptions->new(envvar => $variable);
-    foreach my $feature (split(/,/, $opts->get($area) // '')) {
-	$feature = lc($feature);
-	if ($feature =~ s/^([+-])//) {
-	    my $value = ($1 eq '+') ? 1 : 0;
-	    if ($feature eq 'all') {
-		$use_feature->{$_} = $value foreach keys %{$use_feature};
-	    } else {
-		if (exists $use_feature->{$feature}) {
-		    $use_feature->{$feature} = $value;
-		} else {
-		    warning(g_('unknown %s feature in %s variable: %s'),
-		            $area, $variable, $feature);
-		}
-	    }
-	} else {
-	    warning(g_('incorrect value in %s option of %s variable: %s'),
-	            $area, $variable, $feature);
-	}
-    }
-}
-
 sub _parse_feature_area {
     my ($self, $area, $use_feature) = @_;
 
-    $self->_parse_build_options('DEB_BUILD_OPTIONS', $area, $use_feature);
-    $self->_parse_build_options('DEB_BUILD_MAINT_OPTIONS', $area, $use_feature);
+    # Adjust features based on user or maintainer's desires.
+    my $opts = Dpkg::BuildOptions->new(envvar => 'DEB_BUILD_OPTIONS');
+    $opts->parse_features($area, $use_feature);
+    $opts = Dpkg::BuildOptions->new(envvar => 'DEB_BUILD_MAINT_OPTIONS');
+    $opts->parse_features($area, $use_feature);
 }
 
 sub _add_qa_flags {

+ 37 - 1
scripts/t/Dpkg_BuildOptions.t

@@ -16,7 +16,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 24;
+use Test::More tests => 28;
 
 use Dpkg::ErrorHandling;
 
@@ -71,3 +71,39 @@ is($dbo->output(), 'foobar noopt', 'output');
 $dbo = Dpkg::BuildOptions->new(envvar => 'OTHER_VARIABLE');
 is($dbo->get('parallel'), 5, 'import from other variable, check parallel');
 ok($dbo->has('noopt'), 'import from other variable, check noopt');
+
+my %theme = (
+    metal => undef,
+    pink => undef,
+    rusty => undef,
+    sky => undef,
+);
+my %theme_ref;
+
+$dbo = Dpkg::BuildOptions->new();
+
+$theme_ref{$_} = 1 foreach keys %theme;
+$dbo->set('theme', '+all');
+$dbo->parse_option('theme', \%theme);
+is_deeply(\%theme, \%theme_ref, 'features set with +all');
+
+$theme{$_} = undef foreach keys %theme;
+$theme_ref{$_} = 1 foreach keys %theme;
+$theme_ref{rusty} = 0;
+$dbo->set('theme', '+all,-rusty');
+$dbo->parse_option('theme', \%theme);
+is_deeply(\%theme, \%theme_ref, 'features set with +all,-rusty');
+
+$theme{$_} = undef foreach keys %theme;
+$theme_ref{$_} = 0 foreach keys %theme;
+$theme_ref{metal} = 1;
+$dbo->set('theme', '-all,+metal');
+$dbo->parse_option('theme', \%theme);
+is_deeply(\%theme, \%theme_ref, 'features set with +all,-rusty');
+
+$theme{$_} = $theme_ref{$_} = undef foreach keys %theme;
+$theme_ref{pink} = 1;
+$theme_ref{sky} = 0;
+$dbo->set('theme', '+pink,-sky');
+$dbo->parse_option('theme', \%theme);
+is_deeply(\%theme, \%theme_ref, 'features set with +pink,-sky');