Changeset 16 in ProjectBuilder for devel/pb


Ignore:
Timestamp:
Jul 30, 2007, 6:07:31 PM (17 years ago)
Author:
Bruno Cornec
Message:

Works for build files generation wtih filter and variable expansion. Happy ;-)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb/bin/pb.pl

    r15 r16  
    1515use Data::Dumper;
    1616use English;
     17use AppConfig qw(:argcount :expand);
     18use File::Basename;
    1719use Time::localtime qw(localtime);
    1820use POSIX qw(strftime);
     
    3032my $option = "";
    3133my @pkgs;
     34my $pbtag;                  # Global TAG variable
     35my $pbver;                  # Global VERSION variable
     36my $pbrev;                  # GLOBAL REVISION variable
     37my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
     38my $pbdate = strftime("%Y-%m-%d", @date);
    3239
    3340getopts('p:t',\%opts);
     
    7178    foreach my $p (@pkgs) {
    7279
    73         my $v;
    74 
    7580        if (-f "$ENV{'PBROOT'}/$p/VERSION") {
    7681            open(V,"$ENV{'PBROOT'}/$p/VERSION") || die "Unable to open $ENV{'PBROOT'}/$p/VERSION";
    77             $v = <V>;
    78             chomp($v);
     82            $pbver = <V>;
     83            chomp($pbver);
    7984            close(V);
    8085        } else {
    81             $v = $ENV{'PBVER'};
    82         }
    83 
    84         my $tag;
     86            $pbver = $ENV{'PBVER'};
     87        }
    8588
    8689        if (-f "$ENV{'PBROOT'}/$p/TAG") {
    8790            open(T,"$ENV{'PBROOT'}/$p/TAG") || die "Unable to open $ENV{'PBROOT'}/$p/TAG";
    88             $tag = <T>;
    89             chomp($tag);
     91            $pbtag = <T>;
     92            chomp($pbtag);
    9093            close(T);
    9194        } else {
    92             $tag = $ENV{'PBTAG'};
    93         }
    94         print "Management of $p $v-$tag (rev $ENV{'PBREVISION'})\n";
     95            $pbtag = $ENV{'PBTAG'};
     96        }
     97        $pbrev = $ENV{'PBREVISION'};
     98        print "Management of $p $pbver-$pbtag (rev $pbrev)\n";
    9599        die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
    96         # Clean up dest if necessary
    97         my $dest = "$ENV{'PBDESTDIR'}/$p-$v";
     100        # Clean up dest if necessary. The export will recreate it
     101        my $dest = "$ENV{'PBDESTDIR'}/$p-$pbver";
    98102        pbrm_rf($dest) if (-d $dest);
    99103
     
    116120        # Creates a REVISION file
    117121        open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
    118         print R "$ENV{'PBREVISION'}\n";
     122        print R "$pbrev\n";
    119123        close(R);
    120124
     
    129133            print " OK\n";
    130134        }
    131         my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
    132         my $pbdate = strftime("%Y-%m-%d", @date);
    133 
    134135        open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
    135136        while (<D>) {
     
    140141            my ($ddir, $dver, $dfam, $dtype, $dsuf) = distro_init($dir,$ver);
    141142            #print Dumper($ddir, $dver, $dfam, $dtype, $dsuf);
    142             #print "Filtering DDD => $pbdate, TTT => $tag, RRR => $tag$dsuf, VVV => $v\n";
    143 
    144             # Filter build files from the most precise up to the less
     143            #print "Filtering DDD => $pbdate, TTT => $pbtag, RRR => $pbtag$dsuf, VVV => $pbver\n";
     144
     145            # Filter build files from the less precise up to the most with overloading
    145146            # Filter all files found, keeping the name, and generating in dest
    146             my @bfiles;
     147
     148            # Find all build files first relatively to PBROOT
     149            my %bfiles;
    147150            #print "dir: $ENV{'PBCONF'}/$p\n";
    148             if (-d "$ENV{'PBCONF'}/$p/$ddir-$dver") {
    149                 opendir(BDIR,"$ENV{'PBCONF'}/$p/$ddir-$dver" || die "Unable to open dir $ENV{'PBCONF'}/$p/$ddir-$dver: $!");
    150                 @bfiles = grep { ! /^\./ && -f "$ENV{'PBCONF'}/$p/$ddir-$dver/$_" } readdir(BDIR);
     151            if (-d "$ENV{'PBCONF'}/$p/$dtype") {
     152                opendir(BDIR,"$ENV{'PBCONF'}/$p/$dtype" || die "Unable to open dir $ENV{'PBCONF'}/$p/$dtype: $!");
     153                foreach my $f (readdir(BDIR)) {
     154                    next if ($f =~ /^\./);
     155                    $bfiles{$f} = "$ENV{'PBCONF'}/$p/$dtype/$f";
     156                    $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
     157                }
     158                closedir(BDIR);
     159            } elsif (-d "$ENV{'PBCONF'}/$p/$dfam") {
     160                opendir(BDIR,"$ENV{'PBCONF'}/$p/$dfam" || die "Unable to open dir $ENV{'PBCONF'}/$p/$dfam: $!");
     161                foreach my $f (readdir(BDIR)) {
     162                    next if ($f =~ /^\./);
     163                    $bfiles{$f} = "$ENV{'PBCONF'}/$p/$dfam/$f";
     164                    $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
     165                }
    151166                closedir(BDIR);
    152167            } elsif (-d "$ENV{'PBCONF'}/$p/$ddir") {
    153168                opendir(BDIR,"$ENV{'PBCONF'}/$p/$ddir" || die "Unable to open dir $ENV{'PBCONF'}/$p/$ddir: $!");
    154                 @bfiles = grep { ! /^\./ && -f "$ENV{'PBCONF'}/$p/$ddir/$_" } readdir(BDIR);
    155                 closedir(BDIR);
    156             } elsif (-d "$ENV{'PBCONF'}/$p/$dfam") {
    157                 opendir(BDIR,"$ENV{'PBCONF'}/$p/$dfam" || die "Unable to open dir $ENV{'PBCONF'}/$p/$dfam: $!");
    158                 @bfiles = grep { ! /^\./ && -f "$ENV{'PBCONF'}/$p/$dfam/$_" } readdir(BDIR);
    159                 closedir(BDIR);
    160             } elsif (-d "$ENV{'PBCONF'}/$p/$dtype") {
    161                 opendir(BDIR,"$ENV{'PBCONF'}/$p/$dtype" || die "Unable to open dir $ENV{'PBCONF'}/$p/$dtype: $!");
    162                 @bfiles = grep { ! /^\./ && -f "$ENV{'PBCONF'}/$p/$dtype/$_" } readdir(BDIR);
     169                foreach my $f (readdir(BDIR)) {
     170                    next if ($f =~ /^\./);
     171                    $bfiles{$f} = "$ENV{'PBCONF'}/$p/$ddir/$f";
     172                    $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
     173                }
     174                closedir(BDIR);
     175            } elsif (-d "$ENV{'PBCONF'}/$p/$ddir-$dver") {
     176                opendir(BDIR,"$ENV{'PBCONF'}/$p/$ddir-$dver" || die "Unable to open dir $ENV{'PBCONF'}/$p/$ddir-$dver: $!");
     177                foreach my $f (readdir(BDIR)) {
     178                    next if ($f =~ /^\./);
     179                    $bfiles{$f} = "$ENV{'PBCONF'}/$p/$ddir-$dver/$f";
     180                    $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
     181                }
    163182                closedir(BDIR);
    164183            } else {
     
    166185                next;
    167186            }
     187            print "bfiles: ".Dumper(\%bfiles)."\n";
    168188
    169189            # Get all filters to apply
    170190            # They're cumulative from less specific to most specific
    171191            # suffix is .pbf
     192            my @ffiles;
     193            my ($ffile0, $ffile1, $ffile2, $ffile3);
    172194            if (-d "$ENV{'PBCONF'}/$p/pbfilter") {
    173                 opendir(BDIR,"$ENV{'PBCONF'}/$p/pbfilter" || die "Unable to open dir $ENV{'PBCONF'}/$p/pbfilter: $!");
    174                 foreach my $f (readdir(BDIR)) {
    175                     if (-f "$ENV{'PBCONF'}/$p/pbfilter/$dtype.pbf") {
    176 
    177                 @bfiles = grep { ! /^\./ && -f "$ENV{'PBCONF'}/$p/$dtype/$_" } readdir(BDIR);
    178                 closedir(BDIR);
     195                $ffile0 = "$ENV{'PBCONF'}/$p/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$p/pbfilter/$dtype.pbf");
     196                $ffile1 = "$ENV{'PBCONF'}/$p/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$p/pbfilter/$dfam.pbf");
     197                $ffile2 = "$ENV{'PBCONF'}/$p/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$p/pbfilter/$ddir.pbf");
     198                $ffile3 = "$ENV{'PBCONF'}/$p/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$p/pbfilter/$ddir-$dver.pbf");
     199                push @ffiles,$ffile0 if (defined $ffile0);
     200                push @ffiles,$ffile1 if (defined $ffile1);
     201                push @ffiles,$ffile2 if (defined $ffile2);
     202                push @ffiles,$ffile3 if (defined $ffile3);
    179203            }
    180 
     204            my $config = AppConfig->new({
     205                            # Auto Create variables mentioned in Conf file
     206                            CREATE => 1,
     207                            DEBUG => 0,
     208                            GLOBAL => {
     209                                # Each conf item is a hash
     210                                ARGCOUNT => AppConfig::ARGCOUNT_HASH
     211                            }
     212                        });
     213            print "ffiles: ".Dumper(\@ffiles)."\n";
     214            if (@ffiles) {
     215                $config->file(@ffiles);
     216                my $ptr = $config->get("filter");
     217                print "f:".Dumper($ptr)."\n";
     218
     219                # Apply now all the filters on all the files concerned
     220                # All files are relative to PBROOT
     221                # destination dir depends on the type of file
     222                if (defined $ptr) {
     223                    foreach my $f (values %bfiles) {
     224                        filter_file($f,$ptr,"$dest/pbconf/$ddir-$dver/".basename($f));
     225                    }
     226                    foreach my $f (@filteredfiles) {
     227                        filter_file($f,$ptr,"$dest/$f");
     228                    }
     229                }
     230            }
    181231        }
    182232        close(D);
    183 
    184233    }
    185234} else {
     
    188237    print "    cms2build\n";
    189238}
     239
     240sub filter_file {
     241
     242my $f=shift;
     243my $ptr=shift;
     244my %filter=%$ptr;
     245my $destfile=shift;
     246
     247print "From $f to $destfile\n";
     248pbmkdir_p(dirname($destfile)) if (! -d dirname($destfile));
     249open(DEST,"> $destfile") || die "Unable to create $destfile";
     250open(FILE,"$ENV{'PBROOT'}/$f") || die "Unable to open $f: $!";
     251while (<FILE>) {
     252    my $line = $_;
     253    foreach my $s (keys %filter) {
     254        # Process single variables
     255        #print "debug: $filter{$s}\n";
     256        my $tmp = $filter{$s};
     257        next if (not defined $tmp);
     258        # Expand variables if any single one found
     259        if ($tmp =~ /\$/) {
     260            eval { $tmp =~ s/(\$\w+)/$1/eeg };
     261        }
     262        $line =~ s|$s|$tmp|;
     263    }
     264    print DEST $line;
     265    }
     266close(FILE);
     267close(DEST);
     268}
Note: See TracChangeset for help on using the changeset viewer.