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

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

Lots of various fixes for CVS support with LinuxCOE

  • Property svn:executable set to *
File size: 16.1 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 %filteredfiles %pbrc $debug $LOG);
24
25$debug = 0; # Debug level
26$LOG = *STDOUT; # Where to log
27use lib qw (lib);
28use ProjectBuilder::Distribution qw (pb_distro_init);
29use ProjectBuilder::Changelog qw (pb_changelog);
30use ProjectBuilder::Version qw (pb_version_init);
31use ProjectBuilder::Base qw (pb_conf_read pb_conf_get pb_cms_init pb_mkdir_p pb_system pb_rm_rf pb_get_filters pb_filter_file pb_filter_file_pb pb_cms_export pb_cms_log);
32
33my %opts; # CLI Options
34my $action; # action to realize
35my $test = "FALSE";
36my $option = "";
37my @pkgs;
38my $pbtag; # Global Tag variable
39my $pbver; # Global Version variable
40my %pbver; # per package
41my %pbtag; # per package
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:m:p:qr:tv',\%opts);
47
48my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
49if (defined $opts{'h'}) {
50 pb_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 pb_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# Handle virtual machines if any
80if (defined $opts{'m'}) {
81 $ENV{'PBVM'} = $opts{'m'};
82}
83# Handles project name if any
84if (defined $opts{'p'}) {
85 $ENV{'PBPROJ'} = pb_env_init($opts{'p'});
86} else {
87 $ENV{'PBPROJ'} = pb_env_init();
88}
89
90print $LOG "Project $ENV{'PBPROJ'}\n" if ($debug >= 0);
91print $LOG "Action: $action\n" if ($debug >= 0);
92
93# Keep those project values to store them at the end each time
94my $pbprojtag = $ENV{'PBTAG'};
95my $pbprojver = $ENV{'PBVER'};
96
97# Act depending on action
98if ($action =~ /^cms2build$/) {
99 pb_cms2build();
100} elsif ($action =~ /^build2pkg$/) {
101 pb_build2pkg();
102} elsif ($action =~ /^cms2pkg$/) {
103 pb_cms2build();
104 pb_build2pkg();
105} elsif ($action =~ /^build2ssh$/) {
106 pb_build2ssh();
107} elsif ($action =~ /^pkg2ssh$/) {
108 pb_pkg2ssh();
109} elsif ($action =~ /^build2vm$/) {
110 pb_build2vm();
111} elsif ($action =~ /^cms2vm$/) {
112 pb_cms2build();
113 pb_build2vm();
114} elsif ($action =~ /^cms2ssh$/) {
115 pb_cms2build();
116 pb_build2vm();
117 pb_build2ssh();
118 pb_pkg2ssh();
119} elsif ($action =~ /^clean$/) {
120} else {
121 print $LOG "'$action' is not available\n";
122 pb_syntax();
123}
124
125sub pb_cms2build {
126
127 my $ptr = pb_get_pkg();
128 @pkgs = @$ptr;
129 my $cms=pb_cms_init($ENV{'PBPROJ'},$pbdate);
130
131 my ($pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb","pkgver","pkgtag");
132 foreach my $pbpkg (@pkgs) {
133 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
134 $pbver = $pkgv->{$pbpkg};
135 } else {
136 $pbver = $ENV{'PBVER'};
137 }
138 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
139 $pbtag = $pkgt->{$pbpkg};
140 } else {
141 $pbtag = $ENV{'PBTAG'};
142 }
143
144 $pbrev = $ENV{'PBREVISION'};
145 print $LOG "\n" if ($debug >= 0);
146 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n" if ($debug >= 0);
147 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
148 # Clean up dest if necessary. The export will recreate it
149 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
150 pb_rm_rf($dest) if (-d $dest);
151
152 # Export CMS tree for the concerned package to dest
153 # And generate some additional files
154 $OUTPUT_AUTOFLUSH=1;
155
156 # computes in which dir we have to work
157 my $dir = $defpkgdir{$pbpkg};
158 $dir = $extpkgdir{$pbpkg} if (not defined $dir);
159 pb_cms_export($cms,$pbdate,"$ENV{'PBROOT'}/$dir",$dest);
160
161 # Extract cms log history and store it
162 pb_cms_log($cms,"$ENV{'PBROOT'}/$dir","$dest/$ENV{'PBCMSLOGFILE'}");
163
164 my %build;
165
166 my ($ptr) = pb_conf_get("vmlist");
167 foreach my $d (split(/,/,$ptr->{$ENV{'PBPROJ'}})) {
168 my ($name,$ver) = split(/_/,$d);
169 chomp($ver);
170 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
171 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n" if ($debug >= 1);
172 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
173
174 # Filter build files from the less precise up to the most with overloading
175 # Filter all files found, keeping the name, and generating in dest
176
177 # Find all build files first relatively to PBROOT
178 my %bfiles;
179 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
180 $build{"$ddir-$dver"} = "yes";
181 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
182 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
183 foreach my $f (readdir(BDIR)) {
184 next if ($f =~ /^\./);
185 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
186 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
187 }
188 closedir(BDIR);
189 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
190 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
191 foreach my $f (readdir(BDIR)) {
192 next if ($f =~ /^\./);
193 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
194 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
195 }
196 closedir(BDIR);
197 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
198 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
199 foreach my $f (readdir(BDIR)) {
200 next if ($f =~ /^\./);
201 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
202 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
203 }
204 closedir(BDIR);
205 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
206 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
207 foreach my $f (readdir(BDIR)) {
208 next if ($f =~ /^\./);
209 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
210 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
211 }
212 closedir(BDIR);
213 } else {
214 $build{"$ddir-$dver"} = "no";
215 next;
216 }
217 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
218
219 # Get all filters to apply
220 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
221
222 # Apply now all the filters on all the files concerned
223 # destination dir depends on the type of file
224 if (defined $ptr) {
225 foreach my $f (values %bfiles) {
226 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$pbsuf,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate);
227 }
228 if (defined $filteredfiles{$pbpkg}) {
229 foreach my $f (split(/,/,$filteredfiles{$pbpkg})) {
230 pb_filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f",$pbsuf,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate);
231 }
232 }
233 }
234 }
235 if ($debug >= 0) {
236 my @found;
237 my @notfound;
238 foreach my $b (keys %build) {
239 push @found,$b if ($build{$b} =~ /yes/);
240 push @notfound,$b if ($build{$b} =~ /no/);
241 }
242 print $LOG "Build files generated for ".join(',',@found)."\n";
243 print $LOG "No Build files found for ".join(',',@notfound)."\n";
244 }
245 # Prepare the dest directory for archive
246 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
247 pb_system("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit","Executing init script $ENV{'PBCONF'}/$pbpkg/pbinit");
248 }
249
250 # Archive dest dir
251 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
252 # Possibility to look at PBSRC to guess more the filename
253 pb_system("tar cfz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
254 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
255
256 # Keep track of what is generated for default
257 open(LAST,"> $pbrc{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc{$ENV{'PBPROJ'}}";
258 print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
259 close(LAST);
260
261 # Keep track of per package version
262 if (! -f "$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") {
263 open(PKG,">$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
264 print PKG "# Empty\n";
265 close(PKG);
266 }
267 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
268 $pkg = { } if (not defined $pkg);
269 if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
270 $pkg->{$pbpkg} = "$pbver-$pbtag";
271 }
272
273 print $LOG "DEBUG pkg: ".Dumper($pkg)."\n" if ($debug >= 1);
274 open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
275 foreach my $p (keys %$pkg) {
276 print PKG "pbpkg $p = $pkg->{$p}\n";
277 }
278 close(PKG);
279 }
280}
281
282sub pb_build2pkg {
283
284 # Get list of packages to build
285 my $ptr = pb_get_pkg();
286 @pkgs = @$ptr;
287
288 # Get the running distro to build on
289 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
290 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
291
292 # Get content saved in cms2build
293 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
294 $pkg = { } if (not defined $pkg);
295
296 chdir "$ENV{'PBBUILDDIR'}";
297 foreach my $pbpkg (@pkgs) {
298 my $vertag = $pkg->{$pbpkg};
299 # get the version of the current package - maybe different
300 ($pbver,$pbtag) = split(/-/,$vertag);
301
302 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
303 print $LOG "Source file: $src\n" if ($debug >= 0);
304
305 if ($dtype eq "rpm") {
306 print $LOG "Working under $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
307 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
308 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
309 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$d") || die "Please ensure that you can write into $ENV{'PBBUILDDIR'} to create $d\nchown the $ENV{'PBBUILDDIR'} directory to your uid";
310 }
311 }
312
313 # We need to first extract the spec file
314 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
315 my @specfile;
316 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
317
318 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
319 # set LANGUAGE to check for correct log messages
320 $ENV{'LANGUAGE'}="C";
321 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
322 foreach my $f (@specfile) {
323 if ($f =~ /\.spec$/) {
324 pb_system("rpmbuild --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}");
325 last;
326 }
327 }
328 } elsif ($dtype eq "tgz") {
329 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
330 } elsif ($dtype eq "ebuild") {
331 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage") if (! -d "$ENV{'PBBUILDDIR'}/portage");
332 } else {
333 }
334 }
335}
336
337sub pb_build2ssh {
338 pb_send2ssh("Sources");
339}
340
341sub pb_pkg2ssh {
342 pb_send2ssh("Packages");
343}
344
345sub pb_send2ssh {
346
347 my $cmt = shift;
348
349 my @src;
350
351 # Get list of packages to build
352 my $ptr = pb_get_pkg();
353 @pkgs = @$ptr;
354
355 # Get the running distro to build on
356 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
357 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
358
359 # Get content saved in cms2build
360 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
361 $pkg = { } if (not defined $pkg);
362
363 chdir "$ENV{'PBBUILDDIR'}";
364 my $src;
365 foreach my $pbpkg (@pkgs) {
366 my $vertag = $pkg->{$pbpkg};
367 # get the version of the current package - maybe different
368 ($pbver,$pbtag) = split(/-/,$vertag);
369
370 if ($cmt eq "Sources") {
371 $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
372 } elsif ($cmt eq "Packages") {
373 if ($dtype eq "rpm") {
374 $src="$ENV{'PBBUILDDIR'}/RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm $ENV{'PBBUILDDIR'}/SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm"
375 } elsif ($dtype eq "deb") {
376 my $tmp = "$ENV{'PBBUILDDIR'}/$pbpkg";
377 $src="$tmp"."_*.deb $tmp"."_*.dsc $tmp"."_*.tar.gz"
378 } elsif ($dtype eq "ebuild") {
379 $src="$ENV{'PBBUILDDIR'}/portage/*/$pbpkg/$pbpkg-$pbver.ebuild"
380 } elsif ($dtype eq "slackware") {
381 $src="$ENV{'PBBUILDDIR'}/build-$pbpkg/$pbpkg-$pbver-*-$pbtag.tgz"
382 } else {
383 die "Unknown dtype format $dtype";
384 }
385 }
386 print $LOG "$cmt: $src\n" if ($debug >= 0);
387 push @src, $src;
388 }
389 my ($sshhost,$sshlogin,$sshdir) = pb_conf_get("sshhost", "sshlogin", "sshdir");
390 my $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
391 my $dir;
392 if ($cmt eq "Sources") {
393 $dir = "$sshdir->{$ENV{'PBPROJ'}}/src";
394 } elsif ($cmt eq "Packages") {
395 $dir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
396 } else {
397 return;
398 }
399 $src = join(' ',@src);
400 pb_system("ssh -q $mac \"mkdir -p $dir ; cd $dir ; rm -f $src\"","Preparing $dir on $mac");
401 pb_system("scp -p $src $mac:$dir","$cmt delivery in $dir on $mac");
402}
403
404sub pb_build2vm {
405 my ($vm,$all) = pb_get_vm();
406
407 # Send tar files when we do a global generation
408 pb_build2ssh() if ($all == 1);
409}
410
411sub pb_get_pkg {
412
413my @pkgs;
414
415# Get packages list
416if (not defined $ARGV[0]) {
417 @pkgs = keys %defpkgdir;
418} elsif ($ARGV[0] =~ /^all$/) {
419 @pkgs = keys %defpkgdir;
420 if (defined %extpkgdir) {
421 my $k = keys %extpkgdir;
422 if (defined $k) {
423 push(@pkgs, keys %extpkgdir);
424 }
425 }
426} else {
427 @pkgs = @ARGV;
428}
429print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
430return(\@pkgs);
431}
432
433#
434# Return the list of VMs we are working on
435# $all is a flag to know if we return all of them
436# or only some (if all we publish also tar files in addition to pkgs
437#
438sub pb_get_vm {
439
440my @vm;
441my $all = 0;
442
443# Get VM list
444if ((not defined $ENV{'PBVM'}) || ($ENV{'PBVM'} =~ /^all$/)) {
445 my $ptr = pb_conf_get("vmlist");
446 $ENV{'PBVM'} = $ptr->{$ENV{'PBPROJ'}};
447 $all = 1;
448}
449@vm = split(/,/,$ENV{'PBVM'});
450print $LOG "VMs: ".join(',',@vm)."\n" if ($debug >= 0);
451return(\@vm,$all);
452}
453
454sub pb_extract_build_files {
455
456my $src=shift;
457my $dir=shift;
458my $ddir=shift;
459my @files;
460
461pb_system("tar xfpz $src $dir","Extracting build files");
462opendir(DIR,"$dir") || die "Unable to open directory $dir";
463foreach my $f (readdir(DIR)) {
464 next if ($f =~ /^\./);
465 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
466 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
467 push @files,"$ddir/$f";
468}
469closedir(DIR);
470# Not enough but still a first cleanup
471pb_rm_rf("$dir");
472return(@files);
473}
474
475sub pb_syntax {
476
477 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
478 print "\n";
479 print "Syntax: pb [-vhqt][-r pbroot][-p project][-m \"mach-1[,...]\"] <action> [<pkg1>...]\n";
480 print "\n";
481 print "-h : This help file\n";
482 print "-q : Quiet mode\n";
483 print "-t : Test mode (not done yet)\n";
484 print "-v : Verbose mode\n";
485 print "\n";
486 print "-m machine : Name of the virtual Machines you want\n";
487 print " to build on (space separated) \n";
488 print " (or use the env variable PBVM) \n";
489 print "\n";
490 print "-p project : Name of the project you're working on\n";
491 print " (or use the env variable PBPROJ) \n";
492 print "\n";
493 print "-r pbroot : Path Name of project under the CMS \n";
494 print " (or use the env variable PBROOT) \n";
495 print "\n";
496 print "<params> can be the name of packages\n";
497 print "<action> can be:\n";
498 print "\n";
499 print "\tcms2build: Create tar files for the project under your CMS\n";
500 print "\t CMS supported are SVN and CVS\n";
501 print "\t parameters are packages to build\n";
502 print "\t if not using default list\n";
503 print "\n";
504 print "\tbuild2pkg: Create packages for your running distribution \n";
505 print "\t first parameter is version-tag to build\n";
506 print "\t if not using default version-tag\n";
507 print "\t following parameters are packages to build\n";
508 print "\t if not using default list\n";
509 print "\n";
510 print "\tbuild2ssh: Send the tar files to a SSH host \n";
511 print "\n";
512 print "\tpkg2ssh: Send the packages built to a SSH host \n";
513 print "\n";
514 print "\tcms2pkg: cms2build + build2pkg\n";
515 print "\n";
516}
Note: See TracBrowser for help on using the repository browser.