r29486 - /trunk/dh-make-perl/lib/DhMakePerl/Config.pm

dmn at users.alioth.debian.org dmn at users.alioth.debian.org
Fri Jan 9 14:24:03 UTC 2009


Author: dmn
Date: Fri Jan  9 14:24:01 2009
New Revision: 29486

URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=29486
Log:
Config: add parse_config_file and dump_config methods; add --config-file option

Modified:
    trunk/dh-make-perl/lib/DhMakePerl/Config.pm

Modified: trunk/dh-make-perl/lib/DhMakePerl/Config.pm
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/dh-make-perl/lib/DhMakePerl/Config.pm?rev=29486&op=diff
==============================================================================
--- trunk/dh-make-perl/lib/DhMakePerl/Config.pm (original)
+++ trunk/dh-make-perl/lib/DhMakePerl/Config.pm Fri Jan  9 14:24:01 2009
@@ -6,21 +6,21 @@
 use base 'Class::Accessor';
 
 use constant options => (
-    'arch=s',         'basepkgs=s',
-    'bdepends=s',     'bdependsi=s',
-    'build!',         'closes=i',
-    'core-ok',        'cpan-mirror=s',
-    'cpan=s',         'cpanplus=s',
-    'data-dir=s',     'dbflags=s',
-    'depends=s',      'desc=s',
-    'dh=i',           'dist=s',
-    'email|e=s',      'exclude|i:s{,}',
-    'help',           'home-dir=s',
-    'install!',       'nometa',
-    'notest',         'packagename|p=s',
-    'pkg-perl!',      'requiredeps',
-    'sources-list=s', 'verbose!',
-    'version=s',
+    'arch=s',          'basepkgs=s',
+    'bdepends=s',      'bdependsi=s',
+    'build!',          'closes=i',
+    'config-file=s',   'core-ok',
+    'cpan-mirror=s',   'cpan=s',
+    'cpanplus=s',      'data-dir=s',
+    'dbflags=s',       'depends=s',
+    'desc=s',          'dh=i',
+    'dist=s',          'email|e=s',
+    'exclude|i:s{,}',  'help',
+    'home-dir=s',      'install!',
+    'nometa',          'notest',
+    'packagename|p=s', 'pkg-perl!',
+    'requiredeps',     'sources-list=s',
+    'verbose!',        'version=s',
 );
 
 use constant commands => ( 'refresh|R', 'refresh-cache' );
@@ -35,9 +35,13 @@
         @opts;
     },
     'command',
+    '_explicitly_set',
 );
 
+use File::Spec::Functions qw(catfile);
 use Getopt::Long;
+use Tie::IxHash ();
+use YAML        ();
 
 use constant DEFAULTS => {
     data_dir     => '/usr/share/dh-make-perl',
@@ -56,6 +60,10 @@
     my $values = shift || {};
 
     my $self = $class->SUPER::new( { %{ $class->DEFAULTS }, @_ } );
+
+    $self->_explicitly_set( {} );
+
+    return $self;
 }
 
 sub parse_command_line_options {
@@ -77,6 +85,7 @@
         my $field = $k;
         $field =~ s/-/_/g;
         $self->$field( $opts{$k} );
+        $self->_explicitly_set->{$k} = 1;
     }
 
     # see what are we told to do
@@ -96,4 +105,53 @@
     $self->command( ( keys %opts )[0] );
 }
 
+sub parse_config_file {
+    my $self = shift;
+
+    my $fn = $self->config_file
+        || catfile( $self->home_dir, 'dh-make-perl.conf' );
+
+    if ( -e $fn ) {
+        local $@;
+        my $yaml = eval { YAML::Load($fn) };
+
+        die "Error parsing $fn: $@" if $@;
+
+        die
+            "Error parsing $fn: config-file is not allowed in the configuration file"
+            if $yaml->{'config-file'};
+
+        for ( $self->options ) {
+            ( my $key = $_ ) =~ s/_/-/g;
+
+            next unless exists $yaml->{$key};
+            next
+                if $self->_explicitly_set
+                    ->{$key};    # cmd-line opts take precedence
+
+            $self->$_( delete $yaml->{$key} );
+        }
+
+        die "Error parsing $fn: the following keys are not known:\n"
+            . join( "\n", map( "  - $_", keys %$yaml ) )
+            if %$yaml;
+    }
+}
+
+sub dump_config {
+    my $self = shift;
+
+    my %hash;
+    tie %hash, 'Tie::IxHash';
+
+    for my $opt ( $self->options ) {
+        $opt =~ s/[=!|].*//;
+        ( my $field = $opt ) =~ s/-/_/g;
+        $hash{$opt} = '' . $self->$field    # stringified
+            if defined $self->$field;
+    }
+
+    return YAML::Dump( \%hash );
+}
+
 1;




More information about the Pkg-perl-cvs-commits mailing list