Changeset 1495


Ignore:
Timestamp:
May 7, 2012, 4:44:13 AM (7 years ago)
Author:
bruno
Message:

r4735@localhost: bruno | 2012-05-07 03:46:39 +0200

  • Conf.pm largely rewritten to cache all conf files into a local $h hash in which conf files are added in reverse order.

The new pb_conf_hash stores a configuration file into a hash structure passed in parameter.
pb_conf_add now triggers the computation of the hash structure with pb_conf_cache and adds it to the main $h hash.
pb_conf_read_if now just uses the content in the $h hash to return its table.
pb_conf_get_in_hash_if function added to do the same as pb_conf_read_if on the cached $h hash instead of a configuration file.
pb_conf_get_if now uses pb_conf_get_in_hash_if and the $h hash without re-reading conf files
pb_conf_add_last_in_hash adds the content of a hash behind the content of the $h main hash (was done in pb_conf_get before)
pb_env_init now calls pb_conf_init to have PBPROJ defined when needed.
pb seems to work with this new version (builds itself)

  • Add a new getconf option to pb in order to see the state of the current configuration parameters (now in memory)
Location:
devel
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • devel/pb-modules/lib/ProjectBuilder/Conf.pm

    r1156 r1495  
    2424 
    2525our @ISA = qw(Exporter);
    26 our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if);
     26our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if pb_conf_print);
    2727($VERSION,$REVISION) = pb_version_init();
    2828
     
    3232my %pbconffiles;
    3333
    34 # Global hash of cached values.
     34# Global hash of conf file content
     35# Key is the config keyword
     36# Value is a hash whose key depends on the nature of the config keyword as documented
     37# and value is the confguration value
    3538# We consider that values can not change during the life of pb
    36 # my %cachedval;
     39my $h = ();
    3740
    3841=pod
     
    7275my $proj=shift || undef;
    7376
     77pb_log(1,"Entering pb_conf_init\n");
    7478if (defined $proj) {
    7579    $ENV{'PBPROJ'} = $proj;
     
    7781    $ENV{'PBPROJ'} = "default";
    7882}
    79 }
    80 
    81 
     83pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
     84}
     85
     86
     87=item B<pb_conf_cache>
     88
     89This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
     90It returns the modified hash
     91Can be used in correlation with the %h hash to store permanently values or not if temporarily.
     92
     93=cut
     94
     95sub pb_conf_cache {
     96
     97my $cf = shift;
     98my $lh = shift;
     99
     100# Read the content of the config file and cache it in the %h hash further availble for queries
     101open(CONF,$cf) || die "Unable to open $cf";
     102while(<CONF>) {
     103    if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
     104        pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
     105        $lh->{$1}->{$2}=$3;
     106    }
     107}
     108close(CONF);
     109return($lh);
     110}
    82111
    83112=item B<pb_conf_add>
    84113
    85 This function adds the configuration file to the list last.
     114This function adds the configuration file to the list last, and cache their content in the %h hash
    86115
    87116=cut
     
    90119
    91120pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
     121my $lh;
    92122
    93123foreach my $cf (@_) {
     124    if (! -r $cf) {
     125        pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
     126        next;
     127    }
    94128    # Skip already used conf files
    95     next if (defined $pbconffiles{$cf});
     129    return($lh) if (defined $pbconffiles{$cf});
     130   
    96131    # Add the new one at the end
    97132    my $num = keys %pbconffiles;
    98     pb_log(2,"DEBUG: pb_conf_add $cf at position $num\n");
     133    pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
    99134    $pbconffiles{$cf} = $num;
    100     pb_log(0,"WARNING: pb_conf_add can not read $cf\n") if (! -r $cf);
    101 }
    102 }
     135
     136    # Read the content of the config file
     137    $lh = pb_conf_cache($cf,$lh);
     138    # and cache it in the %h hash for further queries but after the previous
     139    # as we load conf files in reverse order (most precise first)
     140    pb_conf_add_last_in_hash($lh)
     141}
     142}
     143
    103144
    104145=item B<pb_conf_read_if>
     
    131172Valid chars for keys and tags are letters, numbers, '-' and '_'.
    132173
     174The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
     175
    133176=cut
    134177
     
    153196my $conffile = shift;
    154197my @param = @_;
    155 my $trace;
    156198my @ptr;
    157 my %h;
    158 
    159 open(CONF,$conffile) || die "Unable to open $conffile";
    160 while(<CONF>) {
    161     if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
    162         pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
    163         $h{$1}{$2}=$3;
    164     }
    165 }
    166 close(CONF);
    167 
    168 for my $param (@param) {
    169     push @ptr,$h{$param};
     199my $lh;
     200
     201$lh = pb_conf_cache($conffile,$lh);
     202
     203foreach my $param (@param) {
     204    push @ptr,$lh->{$param};
    170205}
    171206return(@ptr);
    172207}
    173208
     209
     210
     211=item B<pb_conf_get_in_hash_if>
     212
     213This function returns a table, corresponding to a set of values queried in the hash passe in parameter or undef if it doen't exist. It takes a table of keys as an input parameter.
     214
     215=cut
     216
     217sub pb_conf_get_in_hash_if {
     218
     219my $lh = shift || return(());
     220my @params = @_;
     221my @ptr = ();
     222
     223pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
     224foreach my $k (@params) {
     225    push @ptr,$lh->{$k};
     226}
     227
     228pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
     229return(@ptr);
     230}
     231
     232
     233
    174234=item B<pb_conf_get_if>
    175235
    176 This function returns a table, corresponding to a set of values queried in the conf files or undef if it doen't exist. It takes a table of keys as an input parameter.
     236This function returns a table, corresponding to a set of values queried in the %h hash or undef if it doen't exist. It takes a table of keys as an input parameter.
    177237
    178238The format of the configurations file is as follows:
     
    180240key tag = value1,value2,...
    181241
    182 It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys, taking in account the order of conf files, to manage overloading.
     242It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
    183243
    184244  $ cat $HOME/.pbrc
     
    205265sub pb_conf_get_if {
    206266
    207 my @param = @_;
    208 
    209 my $ptr = undef;
    210 
    211 # the most important conf file is first, so read them in reverse order
    212 foreach my $f (reverse sort { $pbconffiles{$a} <=> $pbconffiles{$b} } keys %pbconffiles) {
    213     pb_log(2,"DEBUG: pb_conf_get_if in file $f\n");
    214     $ptr = pb_conf_get_fromfile_if("$f",$ptr,@param);
    215 }
    216 
    217 return(@$ptr);
    218 }
    219 
    220 =item B<pb_conf_fromfile_if>
    221 
    222 This function returns a pointer on a table, corresponding to a merge of values queried in the conf file and the pointer on another table passed as parameter. It takes a table of keys as last input parameter.
    223 
    224   my ($k1) = pb_conf_fromfile_if("$HOME/.pbrc",undef,"pbver","pblist");
    225   my ($k2) = pb_conf_fromfile_if("$HOME/.pbrc3",$k1,"pbver","pblist");
    226 
    227 It is used internally by pb_conf_get_if and is not exported yet.
    228 
    229 =cut
    230 
    231 
    232 sub pb_conf_get_fromfile_if {
    233 
    234 my $conffile = shift;
    235 my $ptr2 = shift || undef;
    236 my @param = @_;
    237 
    238 # Everything is returned via ptr1
    239 my @ptr1 = ();
    240 my @ptr2 = ();
    241 
    242 # @ptr1 contains the values overloading what @ptr2 may contain.
    243 @ptr1 = pb_conf_read_if("$conffile", @param) if (defined $conffile);
    244 @ptr2 = @$ptr2 if (defined $ptr2);
     267my @params = @_;
     268my @ptr = undef;
     269
     270return(pb_conf_get_in_hash_if($h,@params));
     271}
     272
     273=item B<pb_conf_add_last_in_hash>
     274
     275This function merges the values passed in the hash parameter into the %h hash, but only if itdoesn't already contain a value, or if the value is more precise (real value instead of default)
     276
     277It is used internally by pb_conf_add and is not exported.
     278
     279=cut
     280
     281sub pb_conf_add_last_in_hash {
     282
     283my $ptr = shift || undef;
     284
     285return if (not defined $ptr);
     286# TODO: test $ptr is a hash pointer
     287
     288my @params = (sort keys %$ptr);
     289
     290# Everything is returned via @h
     291# @h contains the values overloading what @ptr may contain.
     292my @h = pb_conf_get_if(@params);
     293my @ptr = pb_conf_get_in_hash_if($ptr,@params);
    245294
    246295my $p1;
    247296my $p2;
    248297
    249 pb_log(2,"DEBUG: pb_conf_get_from_file $conffile: ".Dumper(@ptr1)."\n");
    250 pb_log(2,"DEBUG: pb_conf_get_from_file input: ".Dumper(@ptr2)."\n");
    251 pb_log(2,"DEBUG: pb_conf_get_from_file param: ".Dumper(@param)."\n");
    252 
    253 foreach my $i (0..$#param) {
    254     $p1 = $ptr1[$i];
    255     # Optimisation doesn't seem useful
    256     # if ((defined $p1) && (defined $cachedval{$p1})) {
    257     # $ptr1[$i] = $cachedval{$p1};
    258     # next;
    259     # }
    260     $p2 = $ptr2[$i];
    261     # Always try to take the param from ptr1
    262     # in order to mask what could be defined already in ptr2
     298pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
     299pb_log(2,"DEBUG: pb_conf_add_last_in_hash hash: ".Dumper(@h)."\n");
     300pb_log(2,"DEBUG: pb_conf_add_last_in_hash input: ".Dumper(@ptr)."\n");
     301
     302foreach my $i (0..$#params) {
     303    $p1 = $h[$i];
     304    $p2 = $ptr[$i];
     305    # Always try to take the param from h
     306    # in order to mask what could be defined already in ptr
    263307    if (not defined $p2) {
    264308        # exit if no p1 either
     
    294338        }
    295339    }
    296     $ptr1[$i] = $p1;
    297     # Cache values to avoid redoing all that analyze when asked again on a known value
    298     # $cachedval{$p1} = $p1;
    299 }
    300 pb_log(2,"DEBUG: pb_conf_get output: ".Dumper(@ptr1)."\n");
    301 return(\@ptr1);
     340    $h->{$params[$i]} = $p1;
     341}
     342pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
    302343}
    303344
     
    328369}
    329370
     371
     372=item B<pb_conf_print>
     373
     374This function prints every configuration parameter in order to help debug stacking issues with conf files
     375
     376=cut
     377
     378sub pb_conf_print {
     379
     380pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
     381pb_log(0,"====================================\n");
     382foreach my $k (sort keys %$h) {
     383    pb_log(0,"$k => ".Dumper($h->{$k})."\n");
     384}
     385}
     386
    330387=back
    331388
  • devel/pb-modules/lib/ProjectBuilder/Env.pm

    r1469 r1495  
    145145my $tag;
    146146
     147pb_conf_init($proj);
    147148pb_env_init_pbrc();
    148149
     
    151152# Could be with env var PBPROJ
    152153# or option -p
    153 # if not define take the first in conf file
     154# if not defined take the first in conf file
    154155#
    155156if ((defined $ENV{'PBPROJ'}) &&
  • devel/pb/bin/pb

    r1486 r1495  
    383383
    384384Create tar files for the website under your CMS.
     385
     386=item B<getconf>
     387
     388Print the full configuration parameters as found in the various configuration files. help to debug conf issues.
    385389
    386390=item B<clean>
     
    713717} elsif ($action =~ /^newproj$/) {
    714718    # Nothing to do - already done in pb_env_init
     719} elsif ($action =~ /^getconf$/) {
     720    my $pbos = pb_distro_get_context();
     721    pb_conf_print();
    715722} elsif ($action =~ /^clean$/) {
    716723    pb_clean();
  • devel/pb/lib/ProjectBuilder/CMS.pm

    r1487 r1495  
    158158my $ver = basename($dir);
    159159my $msg = "updated to $ver";
    160 $msg = "Project $ENV{PBPROJ} creation" if (defined $pbinit);
     160$msg = "Project $ENV{'PBPROJ'} creation" if (defined $pbinit);
    161161
    162162pb_vcs_checkin($scheme,$dir,$msg);
Note: See TracChangeset for help on using the changeset viewer.