source: devel/pb/bin/pb @ 80

Revision 80, 12.8 KB checked in by bruno, 6 years ago (diff)

Back to variables sue to pb with var substitution for env var

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