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

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

Use more nv variables to deal with filters easily even outside ou pb

  • Property svn:executable set to *
File size: 12.9 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_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$ENV{'PBDATE'}=$pbdate;
46
47getopts('hl:p:qr:tv',\%opts);
48
49my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
50if (defined $opts{'h'}) {
51 pb_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 pb_syntax() if (not defined $action);
75
76# Handle root of the project if defined
77if (defined $opts{'r'}) {
78 $ENV{'PBROOT'} = $opts{'r'};
79}
80# Handles project name if any
81if (defined $opts{'p'}) {
82 $ENV{'PBPROJ'} = pb_env_init($opts{'p'});
83} else {
84 $ENV{'PBPROJ'} = pb_env_init();
85}
86
87print $LOG "Project $ENV{'PBPROJ'}\n" if ($debug >= 0);
88print $LOG "Action: $action\n" if ($debug >= 0);
89
90# Act depending on action
91if ($action =~ /^cms2build$/) {
92 pb_cms2build();
93} elsif ($action =~ /^build2pkg$/) {
94 pb_build2pkg();
95} elsif ($action =~ /^cms2pkg$/) {
96 pb_cms2build();
97 pb_build2pkg();
98} else {
99 print $LOG "'$action' is not available\n";
100 pb_syntax();
101}
102
103sub pb_cms2build {
104
105 my $ptr = pb_get_pkg();
106 @pkgs = @$ptr;
107 pb_cms_init($ENV{'PBPROJ'});
108
109 foreach my $pbpkg (@pkgs) {
110 if (-f "$ENV{'PBROOT'}/$pbpkg/VERSION") {
111 open(V,"$ENV{'PBROOT'}/$pbpkg/VERSION") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/VERSION";
112 $pbver = <V>;
113 chomp($pbver);
114 close(V);
115 } else {
116 $pbver = $ENV{'PBVER'};
117 }
118
119 if (-f "$ENV{'PBROOT'}/$pbpkg/TAG") {
120 open(T,"$ENV{'PBROOT'}/$pbpkg/TAG") || die "Unable to open $ENV{'PBROOT'}/$pbpkg/TAG";
121 $pbtag = <T>;
122 chomp($pbtag);
123 close(T);
124 } else {
125 $pbtag = $ENV{'PBTAG'};
126 }
127 $pbrev = $ENV{'PBREVISION'};
128 print $LOG "\n" if ($debug >= 0);
129 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n" if ($debug >= 0);
130 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
131 # Put some var in env to allow filtering
132 $ENV{'PBVER'}=$pbver;
133 $ENV{'PBTAG'}=$pbtag;
134 $ENV{'PBPKG'}=$pbpkg;
135 $ENV{'PBREV'}=$pbrev;
136 # Clean up dest if necessary. The export will recreate it
137 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
138 pb_rm_rf($dest) if (-d $dest);
139
140 # Export CMS tree for the concerned package to dest
141 # And generate some additional files
142 $OUTPUT_AUTOFLUSH=1;
143
144 # computes in which dir we have to work
145 my $dir = $defpkgdir{$pbpkg};
146 $dir = $extpkgdir{$pbpkg} if (not defined $dir);
147 pb_system("$ENV{'PBCMSEXP'} $option $ENV{'PBROOT'}/$dir $dest 1>/dev/null", "Exporting $ENV{'PBROOT'}/$dir");
148
149 # Creates a REVISION file
150 open(R,"> $dest/REVISION") || die "Unable to create $dest/REVISION";
151 print R "$pbrev\n";
152 close(R);
153
154 # Extract cms log history and store it
155 pb_system("$ENV{'PBCMSLOG'} $option $ENV{'PBROOT'}/$dir > $dest/$ENV{'PBCMSLOGFILE'}", "Extracting log info");
156
157 my %build;
158 open(D,"$ENV{'PBCONF'}/DISTROS") || die "Unable to find $ENV{'PBCONF'}/DISTROS\n";
159 while (<D>) {
160 my $d = $_;
161 my ($ndir,$ver) = split(/_/,$d);
162 chomp($ver);
163 my ($ddir, $dver, $dfam, $dtype, $dsuf) = pb_distro_init($ndir,$ver);
164 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $dsuf)."\n" if ($debug >= 1);
165 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
166
167 # Filter build files from the less precise up to the most with overloading
168 # Filter all files found, keeping the name, and generating in dest
169
170 # Find all build files first relatively to PBROOT
171 my %bfiles;
172 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
173 $build{"$ddir-$dver"} = "yes";
174 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
175 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
176 foreach my $f (readdir(BDIR)) {
177 next if ($f =~ /^\./);
178 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
179 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
180 }
181 closedir(BDIR);
182 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
183 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
184 foreach my $f (readdir(BDIR)) {
185 next if ($f =~ /^\./);
186 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
187 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
188 }
189 closedir(BDIR);
190 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
191 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
192 foreach my $f (readdir(BDIR)) {
193 next if ($f =~ /^\./);
194 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
195 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
196 }
197 closedir(BDIR);
198 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
199 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
200 foreach my $f (readdir(BDIR)) {
201 next if ($f =~ /^\./);
202 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
203 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
204 }
205 closedir(BDIR);
206 } else {
207 $build{"$ddir-$dver"} = "no";
208 next;
209 }
210 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
211
212 # Get all filters to apply
213 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
214
215 # Apply now all the filters on all the files concerned
216 # destination dir depends on the type of file
217 if (defined $ptr) {
218 foreach my $f (values %bfiles) {
219 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$dsuf);
220 }
221 if (defined $filteredfiles{$dir}) {
222 foreach my $f (split(/,/,$filteredfiles{$dir})) {
223 pb_filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f");
224 }
225 }
226 }
227 }
228 if ($debug >= 0) {
229 my @found;
230 my @notfound;
231 foreach my $b (keys %build) {
232 push @found,$b if ($build{$b} =~ /yes/);
233 push @notfound,$b if ($build{$b} =~ /no/);
234 }
235 print $LOG "Build files generated for ".join(',',@found)."\n";
236 print $LOG "No Build files found for ".join(',',@notfound)."\n";
237 }
238 close(D);
239 # Prepare the dest directory for archive
240 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
241 pb_system("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit","Executing init script $ENV{'PBCONF'}/$pbpkg/pbinit");
242 }
243
244 # Archive dest dir
245 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
246 # Possibility to look at PBSRC to guess more the filename
247 pb_system("tar cfpz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
248 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
249 # Keep track of what is generated for default
250 $pbver{$pbpkg} = $pbver;
251 $pbtag{$pbpkg} = $pbtag;
252 }
253 open(LAST,"> $pbrc{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc{$ENV{'PBPROJ'}}";
254 foreach my $v (keys %pbver) {
255 print LAST "pbroot $v-_-$pbver{$v}-$pbtag{$v} = $ENV{'PBROOT'}\n";
256 }
257 close(LAST);
258}
259
260sub pb_build2pkg {
261
262 # Get list of packages to build
263 my $ptr = pb_get_pkg();
264 @pkgs = @$ptr;
265
266 # Check whether we have a specific version to build
267 my $vertag = shift @ARGV;
268
269 # Get the running distro to build on
270 my ($ddir, $dver, $dfam, $dtype, $dsuf) = pb_distro_init();
271 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $dsuf))."\n" if ($debug >= 1);
272
273 chdir "$ENV{'PBBUILDDIR'}";
274 foreach my $pbpkg (@pkgs) {
275 # get the version of the current package - maybe different
276 if (not defined $vertag) {
277 if (-f $pbrc{$ENV{'PBPROJ'}}) {
278 my $pbroot = pb_conf_read($pbrc{$ENV{'PBPROJ'}},"pbroot");
279 # All lines should point to the same pbroot so take the first
280 $ENV{'PBROOT'} = (values %$pbroot)[0];
281 foreach my $k (keys %$pbroot) {
282 if ($k =~ /^$pbpkg-_-/) {
283 ($ptr,$vertag) = split(/-_-/,$k);
284 last;
285 }
286 }
287 die "Unable to find $pbpkg in $pbrc{$ENV{'PBPROJ'}}\nYou may want to precise as parameter version-tag" if (not defined $vertag);
288 } else {
289 die "Unable to open $pbrc{$ENV{'PBPROJ'}}\nYou may want to precise as parameter version-tag";
290 }
291 }
292 ($pbver,$pbtag) = split(/-/,$vertag);
293
294 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
295 print $LOG "Source file: $src\n" if ($debug >= 0);
296
297 if ($dtype eq "rpm") {
298 # rpm has its own standard build directory
299 my $tmp=`rpmquery --eval '%{_topdir}' 2> /dev/null`;
300 chomp($tmp);
301 $ENV{'PBBUILDDIR'}=$tmp;
302 print $LOG "Working under $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
303 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
304 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
305 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";
306 }
307 }
308
309 # We need to first extract the spec file
310 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
311 my @specfile;
312 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
313
314 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
315 # set LANGUAGE to check for correct log messages
316 $ENV{'LANGUAGE'}="C";
317 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
318 foreach my $f (@specfile) {
319 if ($f =~ /\.spec$/) {
320 pb_system("rpmbuild -ba $f","Building package with $f");
321 last;
322 }
323 }
324 } elsif ($dtype eq "tgz") {
325 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
326 } elsif ($dtype eq "ebuild") {
327 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage") if (! -d "$ENV{'PBBUILDDIR'}/portage");
328 } else {
329 }
330 }
331}
332
333sub pb_get_pkg {
334
335my @pkgs;
336
337# Get packages list
338if (not defined $ARGV[0]) {
339 @pkgs = keys %defpkgdir;
340} elsif ($ARGV[0] =~ /^all$/) {
341 @pkgs = keys %defpkgdir;
342 if (defined %extpkgdir) {
343 my $k = keys %extpkgdir;
344 if (defined $k) {
345 push(@pkgs, keys %extpkgdir);
346 }
347 }
348} else {
349 @pkgs = @ARGV;
350}
351print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
352return(\@pkgs);
353}
354
355sub pb_extract_build_files {
356
357my $src=shift;
358my $dir=shift;
359my $ddir=shift;
360my @files;
361
362pb_system("tar xfpz $src $dir >/dev/null","Extracting build files");
363opendir(DIR,"$dir") || die "Unable to open directory $dir";
364foreach my $f (readdir(DIR)) {
365 next if ($f =~ /^\./);
366 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
367 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
368 push @files,"$ddir/$f";
369}
370closedir(DIR);
371# Not enough but still a first cleanup
372pb_rm_rf("$dir");
373return(@files);
374}
375
376sub pb_syntax {
377
378 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
379 print "\n";
380 print "Syntax: pb [-vhqt][-r pbroot][-p project] <action> [<params>...]\n";
381 print "\n";
382 print "-h : This help file\n";
383 print "-q : Quiet mode\n";
384 print "-t : Test mode (not done yet)\n";
385 print "-v : Verbose mode\n";
386 print "\n";
387 print "-r pbroot : Path Name of project under the CMS \n";
388 print " (or use the env variable PBROOT) \n";
389 print "\n";
390 print "-p project : Name of the project you're working on\n";
391 print " (or use the env variable PBPROJ) \n";
392 print "\n";
393 print "<action> can be:\n";
394 print "\n";
395 print "\tcms2build: Create a tar file of the project under your CMS\n";
396 print "\t CMS supported are SVN and CVS\n";
397 print "\t parameters are packages to build\n";
398 print "\t if not using default list\n";
399 print "\n";
400 print "\tbuild2pkg: Create packages for your running distribution \n";
401 print "\t first parameter is version-tag to build\n";
402 print "\t if not using default version-tag\n";
403 print "\t following parameters are packages to build\n";
404 print "\t if not using default list\n";
405 print "\n";
406 print "\tcms2pkg: cms2build + build2pkg\n";
407 print "\n";
408}
Note: See TracBrowser for help on using the repository browser.