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

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

First version for pkg2ssh

  • Property svn:executable set to *
File size: 15.2 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);
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: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# Handles project name if any
80if (defined $opts{'p'}) {
81 $ENV{'PBPROJ'} = pb_env_init($opts{'p'});
82} else {
83 $ENV{'PBPROJ'} = pb_env_init();
84}
85
86print $LOG "Project $ENV{'PBPROJ'}\n" if ($debug >= 0);
87print $LOG "Action: $action\n" if ($debug >= 0);
88
89# Keeps those project value to store at end each time
90my $pbprojtag = $ENV{'PBTAG'};
91my $pbprojver = $ENV{'PBVER'};
92
93# Act depending on action
94if ($action =~ /^cms2build$/) {
95 pb_cms2build();
96} elsif ($action =~ /^build2pkg$/) {
97 pb_build2pkg();
98} elsif ($action =~ /^cms2pkg$/) {
99 pb_cms2build();
100 pb_build2pkg();
101} elsif ($action =~ /^build2ssh$/) {
102 pb_build2ssh();
103} elsif ($action =~ /^pkg2ssh$/) {
104 pb_pkg2ssh();
105} else {
106 print $LOG "'$action' is not available\n";
107 pb_syntax();
108}
109
110sub pb_cms2build {
111
112 my $ptr = pb_get_pkg();
113 @pkgs = @$ptr;
114 pb_cms_init($ENV{'PBPROJ'});
115
116 foreach my $pbpkg (@pkgs) {
117 if (-f "$ENV{'PBROOT'}/$pbpkg/VERSION") {
118 open(V,"$ENV{'PBROOT'}/$pbpkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/VERSION";
119 $pbver = <V>;
120 chomp($pbver);
121 close(V);
122 } else {
123 $pbver = $ENV{'PBVER'};
124 }
125
126 if (-f "$ENV{'PBROOT'}/$pbpkg/TAG") {
127 open(T,"$ENV{'PBROOT'}/$pbpkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/TAG";
128 $pbtag = <T>;
129 chomp($pbtag);
130 close(T);
131 } else {
132 $pbtag = $ENV{'PBTAG'};
133 }
134 $pbrev = $ENV{'PBREVISION'};
135 print $LOG "\n" if ($debug >= 0);
136 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n" if ($debug >= 0);
137 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
138 # Clean up dest if necessary. The export will recreate it
139 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
140 pb_rm_rf($dest) if (-d $dest);
141
142 # Export CMS tree for the concerned package to dest
143 # And generate some additional files
144 $OUTPUT_AUTOFLUSH=1;
145
146 # computes in which dir we have to work
147 my $dir = $defpkgdir{$pbpkg};
148 $dir = $extpkgdir{$pbpkg} if (not defined $dir);
149 pb_system("$ENV{'PBCMSEXP'} $option $ENV{'PBROOT'}/$dir $dest 1>/dev/null", "Exporting $ENV{'PBROOT'}/$dir");
150
151 # Creates a REVISION file
152 open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
153 print R "$pbrev\n";
154 close(R);
155
156 # Extract cms log history and store it
157 pb_system("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}", "Extracting log info");
158
159 my %build;
160 open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
161 while (<D>) {
162 my $d = $_;
163 my ($ndir,$ver) = split(/_/,$d);
164 chomp($ver);
165 my ($ddir, $dver, $dfam, $dtype, $dsuf) = pb_distro_init($ndir,$ver);
166 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $dsuf)."\n" if ($debug >= 1);
167 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
168
169 # Filter build files from the less precise up to the most with overloading
170 # Filter all files found, keeping the name, and generating in dest
171
172 # Find all build files first relatively to PBROOT
173 my %bfiles;
174 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
175 $build{"$ddir-$dver"} = "yes";
176 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
177 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
178 foreach my $f (readdir(BDIR)) {
179 next if ($f =~ /^\./);
180 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
181 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
182 }
183 closedir(BDIR);
184 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
185 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
186 foreach my $f (readdir(BDIR)) {
187 next if ($f =~ /^\./);
188 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
189 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
190 }
191 closedir(BDIR);
192 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
193 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
194 foreach my $f (readdir(BDIR)) {
195 next if ($f =~ /^\./);
196 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
197 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
198 }
199 closedir(BDIR);
200 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
201 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
202 foreach my $f (readdir(BDIR)) {
203 next if ($f =~ /^\./);
204 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
205 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
206 }
207 closedir(BDIR);
208 } else {
209 $build{"$ddir-$dver"} = "no";
210 next;
211 }
212 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
213
214 # Get all filters to apply
215 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
216
217 # Apply now all the filters on all the files concerned
218 # destination dir depends on the type of file
219 if (defined $ptr) {
220 foreach my $f (values %bfiles) {
221 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$dsuf,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate);
222 }
223 if (defined $filteredfiles{$dir}) {
224 foreach my $f (split(/,/,$filteredfiles{$dir})) {
225 pb_filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f",$pbpkg,$pbver,$pbtag,$pbrev,$pbdate);
226 }
227 }
228 }
229 }
230 if ($debug >= 0) {
231 my @found;
232 my @notfound;
233 foreach my $b (keys %build) {
234 push @found,$b if ($build{$b} =~ /yes/);
235 push @notfound,$b if ($build{$b} =~ /no/);
236 }
237 print $LOG "Build files generated for ".join(',',@found)."\n";
238 print $LOG "No Build files found for ".join(',',@notfound)."\n";
239 }
240 close(D);
241 # Prepare the dest directory for archive
242 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
243 pb_system("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit","Executing init script $ENV{'PBCONF'}/$pbpkg/pbinit");
244 }
245
246 # Archive dest dir
247 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
248 # Possibility to look at PBSRC to guess more the filename
249 pb_system("tar cfpz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
250 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
251
252 # Keep track of what is generated for default
253 open(LAST,"> $pbrc{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc{$ENV{'PBPROJ'}}";
254 print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
255 close(LAST);
256
257 # Keep track of per package version
258 if (! -f "$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") {
259 open(PKG,">$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
260 print PKG "# Empty\n";
261 close(PKG);
262 }
263 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
264 $pkg = { } if (not defined $pkg);
265 if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
266 $pkg->{$pbpkg} = "$pbver-$pbtag";
267 }
268
269 print $LOG "DEBUG pkg: ".Dumper($pkg)."\n" if ($debug >= 1);
270 open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
271 foreach my $p (keys %$pkg) {
272 print PKG "pbpkg $p = $pkg->{$p}\n";
273 }
274 close(PKG);
275 }
276}
277
278sub pb_build2pkg {
279
280 # Get list of packages to build
281 my $ptr = pb_get_pkg();
282 @pkgs = @$ptr;
283
284 # Get the running distro to build on
285 my ($ddir, $dver, $dfam, $dtype, $dsuf) = pb_distro_init();
286 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $dsuf))."\n" if ($debug >= 1);
287
288 # Get content saved in cms2build
289 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
290 $pkg = { } if (not defined $pkg);
291
292 chdir "$ENV{'PBBUILDDIR'}";
293 foreach my $pbpkg (@pkgs) {
294 my $vertag = $pkg->{$pbpkg};
295 # get the version of the current package - maybe different
296 ($pbver,$pbtag) = split(/-/,$vertag);
297
298 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
299 print $LOG "Source file: $src\n" if ($debug >= 0);
300
301 if ($dtype eq "rpm") {
302 # rpm has its own standard build directory
303 my $tmp=`rpmquery --eval '%{_topdir}' 2> /dev/null`;
304 chomp($tmp);
305 $ENV{'PBBUILDDIR'}=$tmp;
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\nSolution: setup _topdir in your ~/.rpmmacros or\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 -ba $f","Building package with $f");
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, $dsuf) = pb_distro_init();
357 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $dsuf))."\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$dsuf.*.rpm $ENV{'PBBUILDDIR'}/SRPMS/$pbpkg$pbver-$pbtag$dsuf.src.rpm"
375 } elsif ($dtype eq "deb") {
376 $src="$ENV{'PBBUILDDIR'}/$pbpkg_*.deb $ENV{'PBBUILDDIR'}/$pbpkg_*.dsc $ENV{'PBBUILDDIR'}/$pbpkg_*.tag.gz"
377 } elsif ($dtype eq "ebuild") {
378 $src="$ENV{'PBBUILDDIR'}/portage/*/$pbpkg/$pbpkg$pbver.ebuild"
379 } elsif ($dtype eq "slackware") {
380 $src="$ENV{'PBBUILDDIR'}/build-$pbpkg/$pbpkg$pbver-*-$pbtag.tgz"
381 } else {
382 die "Unknown dtype format $dtype";
383 }
384 }
385 print $LOG "$cmt: $src\n" if ($debug >= 0);
386 push @src, $src;
387 }
388 my ($pt) = pb_conf_get("sshhost", "sshlogin", "sshdir");
389 my ($sshhost,$sshlogin,$sshdir) = @$pt;
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_get_pkg {
405
406my @pkgs;
407
408# Get packages list
409if (not defined $ARGV[0]) {
410 @pkgs = keys %defpkgdir;
411} elsif ($ARGV[0] =~ /^all$/) {
412 @pkgs = keys %defpkgdir;
413 if (defined %extpkgdir) {
414 my $k = keys %extpkgdir;
415 if (defined $k) {
416 push(@pkgs, keys %extpkgdir);
417 }
418 }
419} else {
420 @pkgs = @ARGV;
421}
422print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
423return(\@pkgs);
424}
425
426sub pb_extract_build_files {
427
428my $src=shift;
429my $dir=shift;
430my $ddir=shift;
431my @files;
432
433pb_system("tar xfpz $src $dir >/dev/null","Extracting build files");
434opendir(DIR,"$dir") || die "Unable to open directory $dir";
435foreach my $f (readdir(DIR)) {
436 next if ($f =~ /^\./);
437 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
438 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
439 push @files,"$ddir/$f";
440}
441closedir(DIR);
442# Not enough but still a first cleanup
443pb_rm_rf("$dir");
444return(@files);
445}
446
447sub pb_syntax {
448
449 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
450 print "\n";
451 print "Syntax: pb [-vhqt][-r pbroot][-p project] <action> [<params>...]\n";
452 print "\n";
453 print "-h : This help file\n";
454 print "-q : Quiet mode\n";
455 print "-t : Test mode (not done yet)\n";
456 print "-v : Verbose mode\n";
457 print "\n";
458 print "-r pbroot : Path Name of project under the CMS \n";
459 print " (or use the env variable PBROOT) \n";
460 print "\n";
461 print "-p project : Name of the project you're working on\n";
462 print " (or use the env variable PBPROJ) \n";
463 print "\n";
464 print "<action> can be:\n";
465 print "\n";
466 print "\tcms2build: Create a tar file of the project under your CMS\n";
467 print "\t CMS supported are SVN and CVS\n";
468 print "\t parameters are packages to build\n";
469 print "\t if not using default list\n";
470 print "\n";
471 print "\tbuild2pkg: Create packages for your running distribution \n";
472 print "\t first parameter is version-tag to build\n";
473 print "\t if not using default version-tag\n";
474 print "\t following parameters are packages to build\n";
475 print "\t if not using default list\n";
476 print "\n";
477 print "\tcms2pkg: cms2build + build2pkg\n";
478 print "\n";
479}
Note: See TracBrowser for help on using the repository browser.