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

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

Additional version allow now for optimal reuse of parameters

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