source: ProjectBuilder/devel/pb/bin/pb@ 73

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

Modifications in progress...

  • Property svn:executable set to *
File size: 14.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: see at end
11
12use strict 'vars';
13use Getopt::Std;
14use Data::Dumper;
15use English;
16use AppConfig qw(:argcount :expand);
17use File::Basename;
18use File::Copy;
19use Time::localtime qw(localtime);
20use POSIX qw(strftime);
21
22# Global variables
23use vars qw (%defpkgdir %extpkgdir %version %confparam %filteredfiles %pbrc $debug $LOG $projectbuilderver $projectbuilderrev);
24
25$debug = 0; # Debug level
26$LOG = *STDOUT; # Where to log
27use lib qw (lib);
28use ProjectBuilder::common qw (env_init);
29use ProjectBuilder::distro qw (distro_init);
30use ProjectBuilder::cms;
31use ProjectBuilder::changelog qw (changelog);
32use ProjectBuilder::Version qw (version_init);
33use ProjectBuilder::pb qw (pb_init);
34
35my %opts; # CLI Options
36my $action; # action to realize
37my $test = "FALSE";
38my $option = "";
39my @pkgs;
40my $pbtag; # Global TAG variable
41my $pbver; # Global VERSION variable
42my $pbrev; # Global REVISION variable
43my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
44my $pbdate = strftime("%Y-%m-%d", @date);
45
46getopts('hl:p:qr:tv',\%opts);
47
48version_init();
49if (defined $opts{'h'}) {
50 syntax();
51 exit(0);
52}
53if (defined $opts{'v'}) {
54 $debug++;
55}
56if (defined $opts{'q'}) {
57 $debug=-1;
58}
59if (defined $opts{'l'}) {
60 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
61 $LOG = *LOG;
62 $debug = 0 if ($debug == -1);
63 }
64# Handles test option
65if (defined $opts{'t'}) {
66 $test = "TRUE";
67 # Works only for SVN
68 $option = "-r BASE";
69}
70
71# Get Action
72$action = shift @ARGV;
73die syntax() if (not defined $action);
74
75# Handle root of the project if defined
76if (defined $opts{'r'}) {
77 $ENV{'PBROOT'} = $opts{'r'};
78}
79# Handles project name if any
80if (defined $opts{'p'}) {
81 $ENV{'PBPROJ'} = env_init($opts{'p'});
82} else {
83 $ENV{'PBPROJ'} = env_init();
84}
85
86print $LOG "Project $ENV{'PBPROJ'}\n" if ($debug >= 0);
87print $LOG "Action: $action\n" if ($debug >= 0);
88
89# Act depending on action
90if ($action =~ /^cms2build$/) {
91 my $ptr = get_pkg();
92 @pkgs = @$ptr;
93 cms_init();
94
95 foreach my $pbpkg (@pkgs) {
96 if (-f "$ENV{'PBROOT'}/$pbpkg/VERSION") {
97 open(V,"$ENV{'PBROOT'}/$pbpkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/VERSION";
98 $pbver = <V>;
99 chomp($pbver);
100 close(V);
101 } else {
102 $pbver = $ENV{'PBVER'};
103 }
104
105 if (-f "$ENV{'PBROOT'}/$pbpkg/TAG") {
106 open(T,"$ENV{'PBROOT'}/$pbpkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/TAG";
107 $pbtag = <T>;
108 chomp($pbtag);
109 close(T);
110 } else {
111 $pbtag = $ENV{'PBTAG'};
112 }
113 $pbrev = $ENV{'PBREVISION'};
114 print $LOG "\n" if ($debug >= 0);
115 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n" if ($debug >= 0);
116 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
117 # Clean up dest if necessary. The export will recreate it
118 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
119 pbrm_rf($dest) if (-d $dest);
120
121 # Export CMS tree for the concerned package to dest
122 # And generate some additional files
123 $OUTPUT_AUTOFLUSH=1;
124
125 # computes in which dir we have to work
126 my $dir = $defpkgdir{$pbpkg};
127 $dir = $extpkgdir{$pbpkg} if (not defined $dir);
128 pbsystem("$ENV{'PBCMSEXP'} $option $ENV{'PBROOT'}/$dir $dest 1>/dev/null", "Exporting $ENV{'PBROOT'}/$dir");
129
130 # Creates a REVISION file
131 open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
132 print R "$pbrev\n";
133 close(R);
134
135 # Extract cms log history and store it
136 pbsystem("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}", "Extracting log info");
137
138 my %build;
139 open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
140 while (<D>) {
141 my $d = $_;
142 my ($ndir,$ver) = split(/_/,$d);
143 chomp($ver);
144 my ($ddir, $dver, $dfam, $dtype, $dsuf) = distro_init($ndir,$ver);
145 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $dsuf)."\n" if ($debug >= 1);
146 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
147
148 # Filter build files from the less precise up to the most with overloading
149 # Filter all files found, keeping the name, and generating in dest
150
151 # Find all build files first relatively to PBROOT
152 my %bfiles;
153 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
154 $build{"$ddir-$dver"} = "yes";
155 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
156 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
157 foreach my $f (readdir(BDIR)) {
158 next if ($f =~ /^\./);
159 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
160 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
161 }
162 closedir(BDIR);
163 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
164 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
165 foreach my $f (readdir(BDIR)) {
166 next if ($f =~ /^\./);
167 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
168 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
169 }
170 closedir(BDIR);
171 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
172 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
173 foreach my $f (readdir(BDIR)) {
174 next if ($f =~ /^\./);
175 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
176 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
177 }
178 closedir(BDIR);
179 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
180 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
181 foreach my $f (readdir(BDIR)) {
182 next if ($f =~ /^\./);
183 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
184 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
185 }
186 closedir(BDIR);
187 } else {
188 $build{"$ddir-$dver"} = "no";
189 next;
190 }
191 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
192
193 # Get all filters to apply
194 # They're cumulative from less specific to most specific
195 # suffix is .pbf
196 my @ffiles;
197 my ($ffile0, $ffile1, $ffile2, $ffile3);
198 if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
199 $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
200 $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
201 $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
202 $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
203 push @ffiles,$ffile0 if (defined $ffile0);
204 push @ffiles,$ffile1 if (defined $ffile1);
205 push @ffiles,$ffile2 if (defined $ffile2);
206 push @ffiles,$ffile3 if (defined $ffile3);
207 }
208 my $config = AppConfig->new({
209 # Auto Create variables mentioned in Conf file
210 CREATE => 1,
211 DEBUG => 0,
212 GLOBAL => {
213 # Each conf item is a hash
214 ARGCOUNT => AppConfig::ARGCOUNT_HASH
215 }
216 });
217 my $ptr;
218 if (@ffiles) {
219 print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
220 $config->file(@ffiles);
221 $ptr = $config->get("filter");
222 print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
223 } else {
224 $ptr = { };
225 }
226
227 # Apply now all the filters on all the files concerned
228 # destination dir depends on the type of file
229 if (defined $ptr) {
230 foreach my $f (values %bfiles) {
231 filter_file("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$pbpkg,$dtype,$dsuf);
232 }
233 if (defined $filteredfiles{$dir}) {
234 foreach my $f (split(/,/,$filteredfiles{$dir})) {
235 filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f",$pbpkg,$dtype,$dsuf);
236 }
237 }
238 }
239 }
240 if ($debug >= 0) {
241 my @found;
242 my @notfound;
243 foreach my $b (keys %build) {
244 push @found,$b if ($build{$b} =~ /yes/);
245 push @notfound,$b if ($build{$b} =~ /no/);
246 }
247 print $LOG "Build files generated for ".join(',',@found)."\n";
248 print $LOG "No Build files found for ".join(',',@notfound)."\n";
249 }
250 close(D);
251 # Prepare the dest directory for archive
252 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
253 pbsystem("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit","Executing init script $ENV{'PBCONF'}/$pbpkg/pbinit");
254 }
255
256 # Archive dest dir
257 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
258 # Possibility to look at PBSRC to guess more the filename
259 pbsystem("tar cfpz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
260 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
261 # Keep track of what is generated for default
262 open(LAST,"> $pbrc{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc{$ENV{'PBPROJ'}}";
263 print LAST "pbroot $pbver-$pbtag = $ENV{'PBROOT'}\n";
264 close(LAST);
265 }
266} elsif ($action =~ /^build2pkg$/) {
267 # Check whether we have a specific version to build
268 my $vertag = shift @ARGV;
269 if (not defined $vertag) {
270 if (-f $pbrc{$ENV{'PBPROJ'}}) {
271 my $pbroot = pb_init($pbrc{$ENV{'PBPROJ'}},"pbroot");
272 # There is only one line normaly
273 $vertag = (keys %$pbroot)[0]
274 $ENV{'PBROOT'} = (values %$pbroot)[0]
275 } else {
276 die "Unable to open $pbrc{$ENV{'PBPROJ'}}\nYou may want to precise as parameter version-tag";
277 }
278 }
279 ($pbver,$pbtag) = split(/-/,$vertag);
280
281 # Get list of packages to build
282 my $ptr = get_pkg();
283 @pkgs = @$ptr;
284
285 # Get the running distro to build on
286 my ($ddir, $dver, $dfam, $dtype, $dsuf) = distro_init();
287 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $dsuf))."\n" if ($debug >= 1);
288
289 chdir "$ENV{'PBBUILDDIR'}";
290 foreach my $pbpkg (@pkgs) {
291 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
292 print $LOG "Source file: $src\n" if ($debug >= 0);
293
294 if ($dtype eq "rpm") {
295 # rpm has its own standard build directory
296 my $tmp=`rpmquery --eval '%{_topdir}' 2> /dev/null`;
297 chomp($tmp);
298 $ENV{'PBBUILDDIR'}=$tmp;
299 print $LOG "Working under $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
300 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
301 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
302 pbmkdir_p("$ENV{'PBBUILDDIR'}/$d") || die "Please ensure that you can write into $ENV{'PBBUILDDIR'} to create $d\nSolution: setup _topdir in your ~/.rpmmacros or\nchown the $ENV{'PBBUILDDIR'} directory to your uid";
303 }
304 }
305
306 # We need to first extract the spec file
307 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
308 my @specfile;
309 @specfile = extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
310
311 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
312 # set LANGUAGE to check for correct log messages
313 $ENV{'LANGUAGE'}="C";
314 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
315 foreach my $f (@specfile) {
316 if ($f =~ /\.spec$/) {
317 pbsystem("rpmbuild -ba $f","Building package with $f");
318 last;
319 }
320 }
321 } elsif ($dtype eq "tgz") {
322 pbmkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
323 } elsif ($dtype eq "ebuild") {
324 pbmkdir_p("$ENV{'PBBUILDDIR'}/portage") if (! -d "$ENV{'PBBUILDDIR'}/portage");
325 } else {
326 }
327 }
328} else {
329 print $LOG "'$action' is not available\n";
330 syntax();
331}
332
333# Function which applies filter on files
334sub filter_file {
335
336my $f=shift;
337my $ptr=shift;
338my %filter=%$ptr;
339my $destfile=shift;
340my $pbpkg=shift;
341my $dtype=shift;
342my $dsuf=shift;
343
344print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
345pbmkdir_p(dirname($destfile)) if (! -d dirname($destfile));
346open(DEST,"> $destfile") || die "Unable to create $destfile";
347open(FILE,"$f") || die "Unable to open $f: $!";
348while (<FILE>) {
349 my $line = $_;
350 foreach my $s (keys %filter) {
351 # Process single variables
352 print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
353 my $tmp = $filter{$s};
354 next if (not defined $tmp);
355 # Expand variables if any single one found
356 if ($tmp =~ /\$/) {
357 eval { $tmp =~ s/(\$\w+)/$1/eeg };
358 # special case for ChangeLog
359 } elsif (($tmp =~ /^yes$/) && ($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
360 my $p = $defpkgdir{$pbpkg};
361 $p = $extpkgdir{$pbpkg} if (not defined $p);
362 changelog($dtype, $pbpkg, $pbtag, $dsuf, $p, \*DEST);
363 $tmp = "";
364 }
365 $line =~ s|$s|$tmp|;
366 }
367 print DEST $line;
368}
369close(FILE);
370close(DEST);
371}
372
373sub get_pkg {
374
375my @pkgs;
376
377# Get packages list
378if (not defined $ARGV[0]) {
379 @pkgs = keys %defpkgdir;
380} elsif ($ARGV[0] =~ /^all$/) {
381 @pkgs = keys %defpkgdir;
382 if (defined %extpkgdir) {
383 my $k = keys %extpkgdir;
384 if (defined $k) {
385 push(@pkgs, keys %extpkgdir);
386 }
387 }
388} else {
389 @pkgs = @ARGV;
390}
391print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
392return(\@pkgs);
393}
394
395sub extract_build_files {
396
397my $src=shift;
398my $dir=shift;
399my $ddir=shift;
400my @files;
401
402pbsystem("tar xfpz $src $dir >/dev/null","Extracting build files");
403opendir(DIR,"$dir") || die "Unable to open directory $dir";
404foreach my $f (readdir(DIR)) {
405 next if ($f =~ /^\./);
406 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
407 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
408 push @files,"$ddir/$f";
409}
410closedir(DIR);
411# Not enough but still a first cleanup
412pbrm_rf("$dir");
413return(@files);
414}
415
416sub syntax {
417
418 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
419 print "\n";
420 print "Syntax: pb [-vhqt][-r pbroot][-p project] <action> [<params>...]\n";
421 print "\n";
422 print "-h : This help file\n";
423 print "-q : Quiet mode\n";
424 print "-t : Test mode (not done yet)\n";
425 print "-v : Verbose mode\n";
426 print "\n";
427 print "-r pbroot : Path Name of project under the CMS \n";
428 print " (or use the env variable PBROOT) \n";
429 print "\n";
430 print "-p project : Name of the project you're working on\n";
431 print " (or use the env variable PBPROJ) \n";
432 print "\n";
433 print "<action> can be:\n";
434 print "\n";
435 print "\tcms2build: Create a tar file of the project under your CMS\n";
436 print "\t CMS supported are SVN and CVS\n";
437 print "\t parameters are packages to build\n";
438 print "\t if not using default list\n";
439 print "\n";
440 print "\tbuild2pkg: Create packages for your running distribution \n";
441 print "\t first parameter is version-tag to build\n";
442 print "\t if not using default version-tag\n";
443 print "\t following parameters are packages to build\n";
444 print "\t if not using default list\n";
445 print "\n";
446 print "\n";
447}
Note: See TracBrowser for help on using the repository browser.