source: ProjectBuilder/devel/pb/bin/pb.pl@ 18

Last change on this file since 18 was 18, checked in by Bruno Cornec, 17 years ago

cms2build roughly working

  • Property svn:executable set to *
File size: 9.3 KB
Line 
1#!/usr/bin/perl -w
2#
3# Project Builder main application
4#
5# $Id$
6#
7# Copyright B. Cornec 2007
8# Provided under the GPL v2
9
10# Syntax: pb [-p project] <action> [<params>...]
11
12use strict 'vars';
13use Switch;
14use Getopt::Std;
15use Data::Dumper;
16use English;
17use AppConfig qw(:argcount :expand);
18use File::Basename;
19use Archive::Tar;
20use Time::localtime qw(localtime);
21use POSIX qw(strftime);
22
23use vars qw (%defpkgdir %extpkgdir %version %confparam %filteredfiles);
24use lib qw (lib);
25use common qw (env_init);
26use pb qw (pb_init);
27use distro qw (distro_init);
28use cms;
29use changelog qw (changelog);
30
31my %opts; # CLI Options
32my $action; # action to realize
33my $test = "FALSE";
34my $option = "";
35my @pkgs;
36my $pbtag; # Global TAG variable
37my $pbver; # Global VERSION variable
38my $pbrev; # GLOBAL REVISION variable
39my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
40my $pbdate = strftime("%Y-%m-%d", @date);
41
42getopts('p:t',\%opts);
43
44# Handles project name if any
45if (defined $opts{'p'}) {
46 $ENV{'PBPROJ'} = env_init($opts{'p'});
47} else {
48 $ENV{'PBPROJ'} = env_init();
49}
50# Handles test option
51if (defined $opts{'t'}) {
52 $test = "TRUE";
53 # Works only for SVN
54 $option = "-r BASE";
55}
56
57# Get Action
58$action = shift @ARGV;
59die "Syntax: pb [-p project] <action> [<params>...]" if (not defined $action);
60
61print "Project $ENV{'PBPROJ'}\n";
62#print "Action: $action - ARGV:".Dumper(\@ARGV);
63
64# Act depending on action
65if ($action =~ /^cms2build$/) {
66 print "Action: cms2build\n";
67 # Get packages list
68 if (not defined $ARGV[0]) {
69 @pkgs = keys %defpkgdir;
70 } elsif ($ARGV[0] =~ /^all$/) {
71 @pkgs = keys %defpkgdir;
72 if (defined %extpkgdir) {
73 my $k = keys %extpkgdir;
74 if (defined $k) {
75 push(@pkgs, keys %extpkgdir);
76 }
77 }
78 } else {
79 @pkgs = @ARGV;
80 }
81 print "Packages:\n";
82 print Dumper(@pkgs);
83 cms_init();
84
85 foreach my $pkg (@pkgs) {
86
87 if (-f "$ENV{'PBROOT'}/$pkg/VERSION") {
88 open(V,"$ENV{'PBROOT'}/$pkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pkg/VERSION";
89 $pbver = <V>;
90 chomp($pbver);
91 close(V);
92 } else {
93 $pbver = $ENV{'PBVER'};
94 }
95
96 if (-f "$ENV{'PBROOT'}/$pkg/TAG") {
97 open(T,"$ENV{'PBROOT'}/$pkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pkg/TAG";
98 $pbtag = <T>;
99 chomp($pbtag);
100 close(T);
101 } else {
102 $pbtag = $ENV{'PBTAG'};
103 }
104 $pbrev = $ENV{'PBREVISION'};
105 print "Management of $pkg $pbver-$pbtag (rev $pbrev)\n";
106 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
107 # Clean up dest if necessary. The export will recreate it
108 my $dest = "$ENV{'PBDESTDIR'}/$pkg-$pbver";
109 pbrm_rf($dest) if (-d $dest);
110
111 # Export CMS tree for the concerned package to dest
112 # And generate some additional files
113 $OUTPUT_AUTOFLUSH=1;
114 print "$ENV{'PBCMSEXP'} of $pkg...";
115 # computes in which dir we have to work
116 my $dir = $defpkgdir{$pkg};
117 $dir = $extpkgdir{$pkg} if (not defined $dir);
118 system("$ENV{'PBCMSEXP'} $option $ENV{'PBROOT'}/$dir $dest 1>/dev/null");
119 if ($? == -1) {
120 print "failed to execute: $!\n";
121 } elsif ($? & 127) {
122 printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
123 } else {
124 print " Done under $dest\n";
125 }
126
127 # Creates a REVISION file
128 open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
129 print R "$pbrev\n";
130 close(R);
131
132 # Extract cms log history and store it
133 system("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}");
134 print "$ENV{'PBCMSLOG'} of $pkg...";
135 if ($? == -1) {
136 print "failed to execute: $!\n";
137 } elsif ($? & 127) {
138 printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
139 } else {
140 print " OK\n";
141 }
142 open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
143 while (<D>) {
144 my $d = $_;
145 my ($dir,$ver) = split(/_/,$d);
146 chomp($ver);
147 print "Generating build files for $dir ($ver)\n";
148 my ($ddir, $dver, $dfam, $dtype, $dsuf) = distro_init($dir,$ver);
149 #print Dumper($ddir, $dver, $dfam, $dtype, $dsuf);
150 #print "Filtering DDD => $pbdate, TTT => $pbtag, RRR => $pbtag$dsuf, VVV => $pbver\n";
151
152 # Filter build files from the less precise up to the most with overloading
153 # Filter all files found, keeping the name, and generating in dest
154
155 # Find all build files first relatively to PBROOT
156 my %bfiles;
157 #print "dir: $ENV{'PBCONF'}/$pkg\n";
158 if (-d "$ENV{'PBCONF'}/$pkg/$dtype") {
159 opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$dtype" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$dtype: $!");
160 foreach my $f (readdir(BDIR)) {
161 next if ($f =~ /^\./);
162 $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$dtype/$f";
163 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
164 }
165 closedir(BDIR);
166 } elsif (-d "$ENV{'PBCONF'}/$pkg/$dfam") {
167 opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$dfam" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$dfam: $!");
168 foreach my $f (readdir(BDIR)) {
169 next if ($f =~ /^\./);
170 $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$dfam/$f";
171 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
172 }
173 closedir(BDIR);
174 } elsif (-d "$ENV{'PBCONF'}/$pkg/$ddir") {
175 opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$ddir" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$ddir: $!");
176 foreach my $f (readdir(BDIR)) {
177 next if ($f =~ /^\./);
178 $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$ddir/$f";
179 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
180 }
181 closedir(BDIR);
182 } elsif (-d "$ENV{'PBCONF'}/$pkg/$ddir-$dver") {
183 opendir(BDIR,"$ENV{'PBCONF'}/$pkg/$ddir-$dver" || die "Unable to open dir $ENV{'PBCONF'}/$pkg/$ddir-$dver: $!");
184 foreach my $f (readdir(BDIR)) {
185 next if ($f =~ /^\./);
186 $bfiles{$f} = "$ENV{'PBCONF'}/$pkg/$ddir-$dver/$f";
187 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
188 }
189 closedir(BDIR);
190 } else {
191 print "No Build Files found for $ddir-$dver\n";
192 next;
193 }
194 print "bfiles: ".Dumper(\%bfiles)."\n";
195
196 # Get all filters to apply
197 # They're cumulative from less specific to most specific
198 # suffix is .pbf
199 my @ffiles;
200 my ($ffile0, $ffile1, $ffile2, $ffile3);
201 if (-d "$ENV{'PBCONF'}/$pkg/pbfilter") {
202 $ffile0 = "$ENV{'PBCONF'}/$pkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$dtype.pbf");
203 $ffile1 = "$ENV{'PBCONF'}/$pkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$dfam.pbf");
204 $ffile2 = "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir.pbf");
205 $ffile3 = "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pkg/pbfilter/$ddir-$dver.pbf");
206 push @ffiles,$ffile0 if (defined $ffile0);
207 push @ffiles,$ffile1 if (defined $ffile1);
208 push @ffiles,$ffile2 if (defined $ffile2);
209 push @ffiles,$ffile3 if (defined $ffile3);
210 }
211 my $config = AppConfig->new({
212 # Auto Create variables mentioned in Conf file
213 CREATE => 1,
214 DEBUG => 0,
215 GLOBAL => {
216 # Each conf item is a hash
217 ARGCOUNT => AppConfig::ARGCOUNT_HASH
218 }
219 });
220 print "ffiles: ".Dumper(\@ffiles)."\n";
221 if (@ffiles) {
222 $config->file(@ffiles);
223 my $ptr = $config->get("filter");
224 print "f:".Dumper($ptr)."\n";
225
226 # Apply now all the filters on all the files concerned
227 # All files are relative to PBROOT
228 # destination dir depends on the type of file
229 if (defined $ptr) {
230 foreach my $f (values %bfiles) {
231 filter_file($f,$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$pkg,$dtype,$dsuf);
232 }
233 foreach my $f (keys %filteredfiles) {
234 filter_file($f,$ptr,"$dest/$f",$pkg,$dtype,$dsuf);
235 }
236 }
237 }
238 }
239 close(D);
240 # Prepare the dest directory for archive
241 if (-x "$ENV{'PBCONF'}/$pkg/pbpkginit") {
242 system("cd $dest ; $ENV{'PBCONF'}/$pkg/pbinit");
243 if ($? == -1) {
244 print "failed to execute: $!\n";
245 } elsif ($? & 127) {
246 printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
247 } else {
248 print " $dest\n";
249 }
250 }
251 # Archive dest dir
252 chdir "$dest/..";
253 print "Creating $pkg tar files (gzip... ";
254 system("tar cfphz $pkg-$pbver.tar.gz $pkg-$pbver");
255 if ($? == -1) {
256 print "failed to execute: $!\n";
257 } elsif ($? & 127) {
258 printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
259 } else {
260 print " OK)\n";
261 print "Under $dest/../$pkg-$pbver.tar.gz\n";
262 }
263 }
264} else {
265 print "'$action' is not available\n";
266 print "Available actions are:\n";
267 print " cms2build\n";
268}
269
270# Function which applies filter on files
271sub filter_file {
272
273my $f=shift;
274my $ptr=shift;
275my %filter=%$ptr;
276my $destfile=shift;
277my $pkg=shift;
278my $dtype=shift;
279my $dsuf=shift;
280
281print "From $f to $destfile\n";
282pbmkdir_p(dirname($destfile)) if (! -d dirname($destfile));
283open(DEST,"> $destfile") || die "Unable to create $destfile";
284open(FILE,"$ENV{'PBROOT'}/$f") || die "Unable to open $f: $!";
285while (<FILE>) {
286 my $line = $_;
287 foreach my $s (keys %filter) {
288 # Process single variables
289 #print "debug: $filter{$s}\n";
290 my $tmp = $filter{$s};
291 next if (not defined $tmp);
292 # Expand variables if any single one found
293 if ($tmp =~ /\$/) {
294 eval { $tmp =~ s/(\$\w+)/$1/eeg };
295 # special case for ChangeLog
296 } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/)) {
297 my $p = $defpkgdir{$pkg};
298 $p = $extpkgdir{$pkg} if (not defined $p);
299 $tmp = changelog($dtype, $pkg, $pbtag, $dsuf, $p, \*DEST);
300 }
301 $line =~ s|$s|$tmp|;
302 }
303 print DEST $line;
304}
305close(FILE);
306close(DEST);
307}
Note: See TracBrowser for help on using the repository browser.