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

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

fix some typos

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