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

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

Try to fix a difference between generated script and mine

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