Ignore:
Timestamp:
Jul 31, 2017, 1:25:12 AM (2 years ago)
Author:
bruno
Message:

Start move to YAML conf files

File:
1 edited

Legend:

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

    r2176 r2239  
    1919use ProjectBuilder::Base;
    2020use ProjectBuilder::Version;
    21 #use YAML;
    2221
    2322# Inherit from the "Exporter" module which handles exporting functions.
    2423 
    25 use vars qw($VERSION $REVISION @ISA @EXPORT);
     24use vars qw($VERSION $REVISION $PBCONFVER @ISA @EXPORT);
    2625use Exporter;
    2726 
     
    3130our @ISA = qw(Exporter);
    3231our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_write pb_conf_get pb_conf_get_if pb_conf_get_all pb_conf_get_hash pb_conf_cache);
    33 ($VERSION,$REVISION) = pb_version_init();
     32($VERSION,$REVISION,$PBCONFVER) = pb_version_init();
    3433
    3534# Global hash of conf files
     
    7069to allow for overwrite to work:
    7170
    72 1. /usr/share/pb/pb.conf    - the read-only system conf file provided by install
    73 2. /etc/pb/pb.conf          - the same global conf file given to the sysadmin in order to make system wide modifications
     711. /usr/share/pb/pb.yml    - the read-only system conf file provided by install
     722. /etc/pb/pb.yml          - the same global conf file given to the sysadmin in order to make system wide modifications
    74733. /path/to/project.pb      - Configuration file for the project we're building for
    75744. /(vm|ve|rm)path/to/.pbrc - configuration file for VM, VE or RM specific parameters. Cumulative should be orthogonal
     
    124123my $cf = shift;
    125124my $lh = shift;
    126 my $confver = "0.14";
     125
     126my $ldfunc;
     127
     128eval {
     129    require YAML;
     130    YAML->import();
     131    $ldfunc = \&YAML::LoadFile;
     132};
     133if ($@) {
     134    # No YAML found using a more std but less complete one. Old perl only
     135    use Module::Build::YAML;
     136    $ldfunc = \&Module::Build::YAML::LoadFile;
     137}
    127138
    128139# Read the content of the config file and cache it in the %h hash then available for queries
    129 if ($confver < 0.15) {
     140if ($PBCONFVER < 1) {
    130141    open(CONF,$cf) || confess "Unable to open $cf";
    131142    # This is the original conf file format for versions up to 0.14
     
    139150    close(CONF);
    140151} else {
    141     $lh = LoadFile($cf);
     152    $lh = $ldfunc->($cf);
    142153}
    143154return($lh);
     
    251262my $conffile = shift;
    252263my $h = shift;
    253 my $confver = "0.14";
     264my $dpfunc;
     265
     266eval {
     267    require YAML;
     268    YAML->import();
     269    $dpfunc = \&YAML::Dump;
     270};
     271if ($@) {
     272    # No YAML found using a more std but less complete one. Old perl only
     273    use Module::Build::YAML;
     274    $dpfunc = \&Module::Build::YAML::Dump;
     275}
    254276
    255277confess "No configuration file defined to write into !" if (not defined $conffile);
     
    257279open(CONF,"> $conffile") || confess "Unable to write into $conffile";
    258280
    259 if ($confver < 0.15) {
     281if ($PBCONFVER < 1) {
    260282    # This is the original conf file format for versions up to 0.14
    261283    foreach my $p (sort keys %$h) {
     
    267289} else {
    268290    # This is the new YAML format
    269     print CONF Dump($h);
     291    print CONF $dpfunc->($h);
    270292}
    271293close(CONF);
Note: See TracChangeset for help on using the changeset viewer.