source: devel/pb/lib/ProjectBuilder/Base.pm @ 288

Last change on this file since 288 was 288, checked in by Bruno Cornec, 13 years ago
  • Document -i option
  • send packages before halting the VM (useful when you wait for them :-)
File size: 31.3 KB
Line 
1#!/usr/bin/perl -w
2#
3# Base subroutines for the Project-Builder project
4#
5# $Id$
6#
7
8use strict;
9use lib qw (lib);
10use File::Basename;
11use File::Path;
12use File::Temp qw /tempdir/;
13use AppConfig qw(ARGCOUNT_HASH);
14use Data::Dumper;
15
16use ProjectBuilder::Changelog qw (pb_changelog);
17
18$ENV{'PBETC'} = "$ENV{'HOME'}/.pbrc";
19
20sub pb_env_init {
21
22my $proj=shift || undef;
23my $pbinit=shift || undef;
24my $ver;
25my $tag;
26
27# For the moment not dynamic
28my $debug = 0;                  # Debug level
29my $LOG = *STDOUT;              # Where to log
30
31#
32# Check project name
33# Could be with env var PBPROJ
34# or option -p
35# if not define take the first in conf file
36#
37if ((defined $ENV{'PBPROJ'}) &&
38    (not (defined $proj))) {
39    $proj = $ENV{'PBPROJ'};
40}
41
42#
43# We get the pbrc file for that project
44# and use its content
45#
46my ($pbrc) = pb_conf_read("$ENV{'PBETC'}","pbrc");
47print "DEBUG pbrc: ".Dumper($pbrc)."\n" if ($debug >= 1);
48
49my %pbrc = %$pbrc;
50if (not defined $proj) {
51    # Take the first as the default project
52    $proj = (keys %pbrc)[0];
53    print $LOG "Using $proj as default project as none has been specified\n" if (($debug >= 0) and (defined $proj));
54}
55die "No project defined - use env var PBPROJ or -p proj" if (not (defined $proj));
56
57#
58# Set delivery directory
59#
60if (not defined ($pbrc{$proj})) {
61    die "Please create a pbrc reference for project $proj in $ENV{'PBETC'}\n";
62}
63
64my $topdir=dirname($pbrc{$proj});
65# Expand potential env variable in it
66eval { $topdir =~ s/(\$ENV.+\})/$1/eeg };
67chdir $topdir || die "Unable to change directory to $topdir";
68$pbrc{$proj} = $topdir."/pbrc";
69$ENV{'PBDESTDIR'}=$topdir."/delivery";
70
71#
72# Use project configuration file if needed
73#
74if (not defined $ENV{'PBROOT'}) {
75    if (-f $pbrc{$proj}) {
76        my ($pbroot) = pb_conf_read($pbrc{$proj},"pbroot");
77        my %pbroot = %$pbroot;
78        # All lines should point to the same pbroot so take the first
79        $ENV{'PBROOT'} = (values %$pbroot)[0] if (defined $pbroot); 
80        print $LOG "Using $ENV{'PBROOT'} as default pbroot from $pbrc{$proj}\n" if (($debug >= 0) and (defined $ENV{'PBROOT'}));
81    }
82    die "No pbroot defined - use env var PBROOT or -r pbroot " if (not defined $ENV{'PBROOT'});
83}
84
85#
86# Check pb conf compliance
87#
88$ENV{'PBCONF'} = "$ENV{'PBROOT'}/pbconf";
89if (defined $pbinit) {
90    print $LOG "Creating $ENV{'PBCONF'} directory\n";
91    pb_mkdir_p("$ENV{'PBCONF'}");
92    }
93die "Project $proj not Project-Builder compliant. Please populate $ENV{'PBCONF'}" if ( not -d "$ENV{'PBCONF'}");
94
95my %version = ();
96my %defpkgdir = ();
97my %extpkgdir = ();
98my %filteredfiles = ();
99
100if ((-f "$ENV{'PBCONF'}/$proj.pb") and (not defined $pbinit)) {
101    # List of pkg to build by default (mandatory)
102    # List of additional pkg to build when all is called (optional)
103    # Valid version names (optional)
104    # List of files to filter (optional)
105    my ($defpkgdir, $extpkgdir, $version, $filteredfiles, $pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$proj.pb","defpkgdir","extpkgdir","version","filteredfiles","projver","projtag");
106    print "DEBUG: defpkgdir: ".Dumper($defpkgdir)."\n" if ($debug >= 1);
107    print "DEBUG: extpkgdir: ".Dumper($extpkgdir)."\n" if ($debug >= 1);
108    print "DEBUG: version: ".Dumper($version)."\n" if ($debug >= 1);
109    print "DEBUG: filteredfiles: ".Dumper($filteredfiles)."\n" if ($debug >= 1);
110    die "Unable to find defpkgdir in $ENV{'PBCONF'}/$proj.pb" if (not defined $defpkgdir);
111    # Global
112    %defpkgdir = %$defpkgdir;
113    # Global
114    %extpkgdir = %$extpkgdir if (defined $extpkgdir);
115    %version = %$version if (defined $version);
116    # Global
117    %filteredfiles = %$filteredfiles if (defined $filteredfiles);
118    #
119    # Get global Version/Tag
120    #
121
122    if (not defined $ENV{'PBVER'}) {
123        if ((defined $pkgv) && (defined $pkgv->{$proj})) {
124            $ENV{'PBVER'}=$pkgv->{$proj};
125        } else {
126            die "No projver found in $ENV{'PBCONF'}/$proj.pb";
127        }
128    }
129    die "Invalid version name $ENV{'PBVER'} in $ENV{'PBCONF'}/$proj.pb" if (($ENV{'PBVER'} !~ /[0-9.]+/) && (not exists $version{$ENV{'PBVER'}}));
130   
131    if (not defined $ENV{'PBTAG'}) {
132        if ((defined $pkgt) && (defined $pkgt->{$proj})) {
133            $ENV{'PBTAG'}=$pkgt->{$proj};
134        } else {
135            die "No projtag found in $ENV{'PBCONF'}/$proj.pb";
136        }
137    }
138    die "Invalid tag name $ENV{'PBTAG'} in $ENV{'PBCONF'}/$proj.pb" if ($ENV{'PBTAG'} !~ /[0-9.]+/);
139} else {
140    if (defined $pbinit) {
141        open(CONF,"> $ENV{'PBCONF'}/$proj.pb") || die "Unable to create $ENV{'PBCONF'}/$proj.pb";
142        print CONF << "EOF";
143#
144# Project Builder configuration file
145# For project $proj
146#
147# \$Id\$
148#
149
150#
151# Which CMS system is used (Subversion, CVS or tar file content extracted)
152#
153#cms $proj = svn
154#cms $proj = cvs
155#cms $proj = flat
156
157#
158# Packager label
159#
160#packager $proj = "William Porte <bill\@$proj.org>"
161#
162
163# For delivery to a machine by SSH (potentially the FTP server)
164# Needs hostname, account and directory
165#
166#sshhost $proj = www.$proj.org
167#sshlogin $proj = bill
168#sshdir $proj = /$proj/ftp
169#sshport $proj = 22
170
171#
172# For Virtual machines management
173# Naming convention to follow: distribution name (as per ProjectBuilder::Distribution)
174# followed by '_' and by release number
175# a .vmtype extension will be added to the resulting string
176# a QEMU rhel_3 here means that the VM will be named rhel_3.qemu
177#
178#vmlist $proj = mandrake_10.1,mandrake_10.2,mandriva_2006.0,mandriva_2007.0,mandriva_2007.1,mandriva_2008.0,redhat_7.3,redhat_9,fedora_4,fedora_5,fedora_6,fedora_7,rhel_3,rhel_4,rhel_5,suse_10.0,suse_10.1,suse_10.2,suse_10.3,sles_9,sles_10,gentoo_nover,debian_3.1,debian_4.0,ubuntu_6.06,ubuntu_7.04,ubuntu_7.10
179
180#
181# Valid values for vmtype are
182# qemu, (vmware, xen, ... TBD)
183#vmtype $proj = qemu
184
185# Hash for VM stuff on vmtype
186#vmntp default = pool.ntp.org
187
188# We suppose we can commmunicate with the VM through SSH
189#vmhost $proj = localhost
190#vmlogin $proj = pb
191#vmport $proj = 2222
192
193# Timeout to wait when VM is launched/stopped
194#vmtmout default = 120
195
196# per VMs needed paramaters
197#vmopt $proj = -m 384 -daemonize
198#vmpath $proj = /home/qemu
199#vmsize $proj = 5G
200
201#
202# Global version/tag for the project
203#
204#projver $proj = devel
205#projtag $proj = 1
206
207# Adapt to your needs:
208# Optional if you need to overwrite the global values above
209#
210#pkgver pkg1 = stable
211#pkgtag pkg1 = 3
212#pkgver nil
213#pkgtag nil
214
215# Hash of default package/package directory
216#defpkgdir pkg1 = pkg1dir
217
218# Hash of additional package/package directory
219#extpkgdir pkg1-doc = pkg1-docdir
220
221# Hash of valid version names
222#version devel
223#version stable
224
225# List of files per pkg on which to apply filters
226# Files are mentioned relatively to pbroot/defpkgdir
227#filteredfiles pkg1 = Makefile.PL
228#filteredfiles pkg1-doc = configure.in
229EOF
230        close(CONF);
231        pb_mkdir_p("$ENV{'PBCONF'}/pbfilter") || die "Unable to create $ENV{'PBCONF'}/pbfilter";
232        open(CONF,"> $ENV{'PBCONF'}/pbfilter/all.pbf") || die "Unable to create $ENV{'PBCONF'}/pbfilter/all.pbf";
233        print CONF << "EOF";
234#
235# \$Id\$
236#
237# Filter for all files
238#
239# PBSRC is replaced by the source package format
240#filter PBSRC = ftp://ftp.$proj.org/src/%{name}-%{version}.tar.gz
241
242# PBVER is replaced by the version (\$pbver in code)
243#filter PBVER = \$pbver
244
245# PBDATE is replaced by the date (\$pbdate in code)
246#filter PBDATE = \$pbdate
247
248# PBLOG is replaced by the changelog if value is yes
249#filter PBLOG = yes
250
251# PBTAG is replaced by the tag (\$pbtag in code)
252#filter PBTAG = \$pbtag
253
254# PBREV is replaced by the revision (\$pbrev in code)
255#filter PBREV = \$pbrev
256
257# PBPKG is replaced by the package name (\$pbpkg in code)
258#filter PBPKG = \$pbpkg
259
260# PBPACKAGER is replaced by the packager name (\$pbpackager in code)
261#filter PBPACKAGER = \$pbpackager
262
263# PBDESC contains the description of the package
264#filter PBDESC = "Bla-Bla"
265
266# PBURL contains the URL of the Web site of the project
267#filter PBURL = http://www.$proj.org
268EOF
269        close(CONF);
270        open(CONF,"> $ENV{'PBCONF'}/pbfilter/rpm.pbf") || die "Unable to create $ENV{'PBCONF'}/pbfilter/rpm.pbf";
271        print CONF << "EOF";
272#
273# \$Id\$
274#
275# Filter for rpm build
276#
277
278# PBGRP is replaced by the RPM group of apps
279# Cf: http://fedoraproject.org/wiki/RPMGroups
280#filter PBGRP = Applications/Archiving
281
282# PBDEP is replaced by the list of dependencies
283#filter PBDEP =
284
285# PBSUF is replaced by the package name (\$pbpkg in code)
286#filter PBSUF = \$pbsuf
287
288# PBOBS is replaced by the Obsolete line
289#filter PBOBS =
290
291EOF
292        close(CONF);
293        open(CONF,"> $ENV{'PBCONF'}/pbfilter/deb.pbf") || die "Unable to create $ENV{'PBCONF'}/pbfilter/deb.pbf";
294        print CONF << "EOF";
295#
296# \$Id\$
297#
298# Filter for debian build
299#
300# PBGRP is replaced by the group of apps
301#filter PBGRP = utils
302
303# PBVER is replaced by the version (\$pbver in code)
304#filter PBVER = \$pbver
305
306# PBDEP is replaced by the list of dependencies
307#filter PBDEP =
308
309# PBSUG is replaced by the list of suggestions
310#filter PBSUG =
311
312# PBREC is replaced by the list of recommandations
313#filter PBREC =
314
315# PBLOG is replaced by the changelog if value is yes
316#filter PBLOG = yes
317
318# PBPKG is replaced by the package name (\$pbpkg in code)
319#filter PBPKG = \$pbpkg
320
321# PBPACKAGER is replaced by the packager name (\$pbpackager in code)
322#filter PBPACKAGER = \$pbpackager
323
324EOF
325        close(CONF);
326        open(CONF,"> $ENV{'PBCONF'}/pbfilter/md.pbf") || die "Unable to create $ENV{'PBCONF'}/pbfilter/md.pbf";
327        print CONF << "EOF";
328# Specific group for Mandriva for $proj
329filter PBGRP = Archiving/Backup
330EOF
331        close(CONF);
332        open(CONF,"> $ENV{'PBCONF'}/pbfilter/novell.pbf") || die "Unable to create $ENV{'PBCONF'}/pbfilter/novell.pbf";
333        print CONF << "EOF";
334# Specific group for SuSE for $proj
335filter PBGRP = Productivity/Archiving/Backup
336EOF
337        close(CONF);
338        pb_mkdir_p("$ENV{'PBCONF'}/pkg1/deb") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb";
339        open(CONF,"> $ENV{'PBCONF'}/pkg1/deb/control") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb/control";
340        print CONF << "EOF";
341Source: PBPKG
342Section: PBGRP
343Priority: optional
344Maintainer: PBPACKAGER
345Build-Depends: debhelper (>= 4.2.20), PBDEP
346Standards-Version: 3.6.1
347
348Package: PBPKG
349Architecture: amd64 i386 ia64
350Section: PBGRP
351Priority: optional
352Depends: \${shlibs:Depends}, \${misc:Depends}, PBDEP
353Recommends: PBREC
354Suggests: PBSUG
355Description:
356 PBDESC
357 .
358 Homepage: PBURL
359
360EOF
361        close(CONF);
362        open(CONF,"> $ENV{'PBCONF'}/pkg1/deb/copyright") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb/copyright";
363        print CONF << "EOF";
364This package is debianized by PBPACKAGER
365`date`
366
367The current upstream source was downloaded from
368ftp://ftp.$proj.org/src/.
369
370Upstream Authors: Put their name here
371
372Copyright:
373
374   This package is free software; you can redistribute it and/or modify
375   it under the terms of the GNU General Public License as published by
376   the Free Software Foundation; version 2 dated June, 1991.
377
378   This package is distributed in the hope that it will be useful,
379   but WITHOUT ANY WARRANTY; without even the implied warranty of
380   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
381   GNU General Public License for more details.
382
383   You should have received a copy of the GNU General Public License
384   along with this package; if not, write to the Free Software
385   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
386   MA 02110-1301, USA.
387
388On Debian systems, the complete text of the GNU General
389Public License can be found in /usr/share/common-licenses/GPL.
390
391EOF
392        close(CONF);
393        open(CONF,"> $ENV{'PBCONF'}/pkg1/deb/changelog") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb/changelog";
394        print CONF << "EOF";
395PBLOG
396EOF
397        close(CONF);
398        open(CONF,"> $ENV{'PBCONF'}/pkg1/deb/compat") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb/compat";
399        print CONF << "EOF";
4004
401EOF
402        close(CONF);
403        open(CONF,"> $ENV{'PBCONF'}/pkg1/deb/pkg1.dirs") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb/pkg1.dirs";
404        print CONF << "EOF";
405EOF
406        close(CONF);
407        open(CONF,"> $ENV{'PBCONF'}/pkg1/deb/pkg1.docs") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb/pkg1.docs";
408        print CONF << "EOF";
409INSTALL
410COPYING
411AUTHORS
412NEWS
413README
414EOF
415        close(CONF);
416        open(CONF,"> $ENV{'PBCONF'}/pkg1/deb/rules") || die "Unable to create $ENV{'PBCONF'}/pkg1/deb/rules";
417        print CONF << 'EOF';
418#!/usr/bin/make -f
419# -*- makefile -*-
420# Sample debian/rules that uses debhelper.
421# GNU copyright 1997 to 1999 by Joey Hess.
422#
423# $Id$
424#
425
426# Uncomment this to turn on verbose mode.
427#export DH_VERBOSE=1
428
429# Define package name variable for a one-stop change.
430PACKAGE_NAME = PBPKG
431
432# These are used for cross-compiling and for saving the configure script
433# from having to guess our platform (since we know it already)
434DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
435DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
436
437CFLAGS = -Wall -g
438
439ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
440        CFLAGS += -O0
441else
442        CFLAGS += -O2
443endif
444ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
445        INSTALL_PROGRAM += -s
446endif
447config.status: configure
448        dh_testdir
449
450        # Configure the package.
451        CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr
452 --mandir=\$${prefix}/share/man
453
454# Build both architecture dependent and independent
455build: build-arch build-indep
456
457# Build architecture dependent
458build-arch: build-arch-stamp
459
460build-arch-stamp:  config.status
461        dh_testdir
462
463        # Compile the package.
464        $(MAKE)
465
466        touch build-stamp
467
468# Build architecture independent
469build-indep: build-indep-stamp
470
471build-indep-stamp:  config.status
472        # Nothing to do, the only indep item is the manual which is available as html in original source
473        touch build-indep-stamp
474
475# Clean up
476clean:
477        dh_testdir
478        dh_testroot
479        rm -f build-arch-stamp build-indep-stamp #CONFIGURE-STAMP#
480        # Clean temporary document directory
481        rm -rf debian/doc-temp
482        # Clean up.
483        -$(MAKE) distclean
484        rm -f config.log
485ifneq "$(wildcard /usr/share/misc/config.sub)" ""
486        cp -f /usr/share/misc/config.sub config.sub
487endif
488ifneq "$(wildcard /usr/share/misc/config.guess)" ""
489        cp -f /usr/share/misc/config.guess config.guess
490endif
491
492        dh_clean
493
494# Install architecture dependent and independent
495install: install-arch install-indep
496
497# Install architecture dependent
498install-arch: build-arch
499        dh_testdir
500        dh_testroot
501        dh_clean -k -s
502        dh_installdirs -s
503
504        # Install the package files into build directory:
505        # - start with upstream make install
506        $(MAKE) install prefix=$(CURDIR)/debian/$(PACKAGE_NAME)/usr mandir=$(CURDIR)/debian/$(PACKAGE_NAME)/us
507r/share/man
508        # - copy html manual to temporary location for renaming
509        mkdir -p debian/doc-temp
510        dh_install -s
511
512# Install architecture independent
513install-indep: build-indep
514        dh_testdir
515        dh_testroot
516        dh_clean -k -i
517        dh_installdirs -i
518        dh_install -i
519
520# Must not depend on anything. This is to be called by
521# binary-arch/binary-indep
522# in another 'make' thread.
523binary-common:
524        dh_testdir
525        dh_testroot
526        dh_installchangelogs ChangeLog
527        dh_installdocs
528        dh_installman
529        dh_link
530        dh_strip
531        dh_compress
532        dh_fixperms
533        dh_installdeb
534        dh_shlibdeps
535        dh_gencontrol
536        dh_md5sums
537        dh_builddeb
538
539# Build architecture independant packages using the common target.
540binary-indep: build-indep install-indep
541        $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common
542
543# Build architecture dependant packages using the common target.
544binary-arch: build-arch install-arch
545        $(MAKE) -f debian/rules DH_OPTIONS=-a binary-common
546
547# Build architecture depdendent and independent packages
548binary: binary-arch binary-indep
549.PHONY: clean binary
550
551EOF
552        close(CONF);
553        pb_mkdir_p("$ENV{'PBCONF'}/pkg1/rpm") || die "Unable to create $ENV{'PBCONF'}/pkg1/rpm";
554        open(CONF,"> $ENV{'PBCONF'}/pkg1/rpm/pkg1.spec") || die "Unable to create $ENV{'PBCONF'}/pkg1/rpm/pkg1.spec";
555        print CONF << 'EOF';
556#
557# $Id$
558#
559
560Summary:        bla-bla
561Summary(fr):    french bla-bla
562
563Name:           PBPKG
564Version:        PBVER
565Release:        PBTAGPBSUF
566License:        GPL
567Group:          PBGRP
568Url:            PBURL
569Source:         PBSRC
570BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(id -u -n)
571Requires:       PBDEP
572
573%description
574PBDESC
575
576%description -l fr
577french desc
578
579%prep
580%setup -q
581
582%build
583%configure
584make %{?_smp_mflags}
585
586%install
587%{__rm} -rf $RPM_BUILD_ROOT
588make DESTDIR=$RPM_BUILD_ROOT install
589
590%clean
591%{__rm} -rf $RPM_BUILD_ROOT
592
593%files
594%defattr(-,root,root)
595%doc ChangeLog
596%doc INSTALL COPYING README AUTHORS NEWS
597
598%changelog
599PBLOG
600
601EOF
602        close(CONF);
603        pb_mkdir_p("$ENV{'PBCONF'}/pkg1/pbfilter") || die "Unable to create $ENV{'PBCONF'}/pkg1/pbfilter";
604
605        print "\nDo not to forget to commit the pbconf directory in your CMS if needed\n";
606        print "After having renamed the pkg1 directory to your package's name      \n\n";
607    } else {
608        die "Unable to open $ENV{'PBCONF'}/$proj.pb";
609    }
610}
611
612#
613# Set temp directory
614#
615if (not defined $ENV{'TMPDIR'}) {
616    $ENV{'TMPDIR'}="/tmp";
617}
618$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
619
620#
621# Removes all directory existing below the delivery dir
622# as they are temp dir only
623# Files stay and have to be cleaned up manually
624#
625if (-d $ENV{'PBDESTDIR'}) {
626    opendir(DIR,$ENV{'PBDESTDIR'}) || die "Unable to open directory $ENV{'PBDESTDIR'}: $!";
627    foreach my $d (readdir(DIR)) {
628        next if ($d =~ /^\./);
629        next if (-f "$ENV{'PBDESTDIR'}/$d");
630        pb_rm_rf("$ENV{'PBDESTDIR'}/$d") if (-d "$ENV{'PBDESTDIR'}/$d");
631    }
632    closedir(DIR);
633}
634if (! -d "$ENV{'PBDESTDIR'}") {
635    pb_mkdir_p($ENV{'PBDESTDIR'}) || die "Unable to recursively create $ENV{'PBDESTDIR'}";
636}
637
638#
639# Set build directory
640#
641$ENV{'PBBUILDDIR'}=$topdir."/build";
642if (! -d "$ENV{'PBBUILDDIR'}") {
643    pb_mkdir_p($ENV{'PBBUILDDIR'}) || die "Unable to recursively create $ENV{'PBBUILDDIR'}";
644}
645
646umask 0022;
647return($proj,$debug,$LOG,\%pbrc, \%filteredfiles, \%defpkgdir, \%extpkgdir);
648}
649
650# Internal mkdir -p function
651sub pb_mkdir_p {
652my @dir = @_;
653my $ret = mkpath(@dir, 0, 0755);
654return($ret);
655}
656
657# Internal rm -rf function
658sub pb_rm_rf {
659my @dir = @_;
660my $ret = rmtree(@dir, 0, 0);
661return($ret);
662}
663
664# Internal system function
665sub pb_system {
666
667my $cmd=shift;
668my $cmt=shift || $cmd;
669
670print "$cmt... ";
671#system("$cmd 2>&1 > $ENV{'PBTMP'}/system.log");
672system("$cmd");
673if ($? == -1) {
674    print "failed to execute ($cmd) : $!\n";
675    pb_display_file("$ENV{'PBTMP'}/system.log");
676} elsif ($? & 127) {
677    printf "child ($cmd) died with signal %d, %s coredump\n", ($? & 127),  ($? & 128) ? 'with' : 'without';
678    pb_display_file("$ENV{'PBTMP'}/system.log");
679} elsif ($? == 0) {
680    print "OK\n";
681} else {
682    printf "child ($cmd) exited with value %d\n", $? >> 8;
683    pb_display_file("$ENV{'PBTMP'}/system.log");
684}
685}
686
687sub pb_display_file {
688
689my $file=shift;
690
691return if (not -f $file);
692open(FILE,"$file");
693while (<FILE>) {
694    print $_;
695}
696close(FILE);
697}
698
699# Function which returns a pointer on a table
700# corresponding to a set of values queried in the conf file
701# and test the returned vaue as they need to exist in that case
702sub pb_conf_get {
703
704my @param = @_;
705
706# Everything is returned via ptr1
707my @ptr1 = pb_conf_read("$ENV{'PBETC'}", @param);
708my @ptr2 = pb_conf_read("$ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb", @param);
709
710my $p1;
711my $p2;
712
713#print "DEBUG: param1: ".Dumper(@ptr1)."\n"; # if ($debug >= 1);
714#print "DEBUG: param2: ".Dumper(@ptr2)."\n"; # if ($debug >= 1);
715
716foreach my $i (0..$#param) {
717    $p1 = $ptr1[$i];
718    $p2 = $ptr2[$i];
719    die "No $param[$i] defined for $ENV{'PBPROJ'}" if ((not @ptr1) && (not @ptr2));
720    die "No $param[$i] defined for $ENV{'PBPROJ'}" if ((not defined $p1) && (not defined $p2));
721    # Always try to take the param from the home dir conf file in priority
722    # in order to mask what could be defined under the CMS to allow for overloading
723    if (not defined $p2) {
724        # No ref in CMS project conf file so use the home dir one.
725        $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (not defined $p1->{$ENV{'PBPROJ'}});
726    } else {
727        # Ref found in CMS project conf file
728        if (not defined $p1) {
729            # No ref in home dir project conf file so use the CMS one.
730            $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if (not defined $p2->{$ENV{'PBPROJ'}});
731            $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
732        } else {
733            # Both are defined - handling the overloading
734            if (not defined $p1->{'default'}) {
735                if (defined $p2->{'default'}) {
736                    $p1->{'default'} = $p2->{'default'};
737                }
738            }
739
740            if (not defined $p1->{$ENV{'PBPROJ'}}) {
741                if (defined $p2->{$ENV{'PBPROJ'}}) {
742                    $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
743                } else {
744                    $p1->{$ENV{'PBPROJ'}} = $p1->{'default'};
745                }
746            }
747        }
748    }
749    die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $p1->{$ENV{'PBPROJ'}});
750    $ptr1[$i] = $p1;
751    #print "DEBUG: param ptr1: ".Dumper(@ptr1)."\n"; # if ($debug >= 1);
752}
753return(@ptr1);
754}
755
756sub pb_no_err {
757}
758
759# Function which returns a pointer on a hash
760# corresponding to a declaration (arg2) in a conf file (arg1)
761sub pb_conf_read {
762
763my $conffile = shift;
764my @param = @_;
765my $trace;
766my @ptr;
767
768my $debug = 0;
769
770if ($debug > 0) {
771    $trace = 1;
772} else {
773    $trace = 0;
774}
775
776
777my $config = AppConfig->new({
778                            # Auto Create variables mentioned in Conf file
779                            CREATE => 1,
780                            DEBUG => $trace,
781                            ERROR => \&pb_no_err,
782                            GLOBAL => {
783                                # Each conf item is a hash
784                                ARGCOUNT => ARGCOUNT_HASH,
785                            },
786                        });
787$config->file($conffile);
788for my $param (@param) {
789    push @ptr,$config->get($param);
790}
791print "DEBUG: params: ".Dumper(@param)." ".Dumper(@ptr)."\n" if ($debug >= 1);
792return(@ptr);
793}
794
795# Setup environment for CMS system
796sub pb_cms_init {
797
798my $proj = shift || undef;
799my $ret;
800
801my ($cms) = pb_conf_get("cms");
802
803if ($cms->{$proj} eq "svn") {
804    $ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; svnversion .)`;
805    chomp($ENV{'PBREVISION'});
806    $ENV{'PBCMSLOGFILE'}="svn.log";
807} elsif ($cms->{$proj} eq "flat") {
808    $ENV{'PBREVISION'}="flat";
809    $ENV{'PBCMSLOGFILE'}="flat.log";
810} elsif ($cms->{$proj} eq "cvs") {
811    # Way too slow
812    #$ENV{'PBREVISION'}=`(cd "$ENV{'PBROOT'}" ; cvs rannotate  -f . 2>&1 | awk '{print \$1}' | grep -E '^[0-9]' | cut -d. -f2 |sort -nu | tail -1)`;
813    #chomp($ENV{'PBREVISION'});
814    $ENV{'PBREVISION'}="CVS";
815    $ENV{'PBCMSLOGFILE'}="cvs.log";
816    #
817    # Export content if needed
818    #
819    my ($cvsroot,$cvsrsh) = pb_conf_get("cvsroot","cvsrsh");
820    $ENV{'CVSROOT'} = $cvsroot->{$proj} if (defined $cvsroot->{$proj});
821    $ENV{'CVSRSH'} = $cvsrsh->{$proj} if (defined $cvsrsh->{$proj});
822} else {
823    die "cms $cms->{$proj} unknown";
824}
825return($cms);
826}
827
828sub pb_cms_export {
829my $cms = shift;
830my $pbdate = shift || undef;
831my $source = shift;
832my $destdir = shift;
833my $tmp;
834my $tmp1;
835
836if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
837    if (-d $source) {
838        $tmp = $destdir;
839    } else {
840        $tmp = $destdir."/".basename($source);
841    }
842    pb_system("svn export $source $tmp","Exporting $source from SVN to $tmp");
843} elsif ($cms->{$ENV{'PBPROJ'}} eq "flat") {
844    if (-d $source) {
845        $tmp = $destdir;
846    } else {
847        $tmp = $destdir."/".basename($source);
848    }
849    pb_system("cp -a $source $tmp","Exporting $source from DIR to $tmp");
850} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
851    my $dir=dirname($destdir);
852    my $base=basename($destdir);
853    if (-d $source) {
854        $tmp1 = $source;
855        $tmp1 =~ s|$ENV{'PBROOT'}/||;
856    } else {
857        $tmp1 = dirname($source);
858        $tmp1 =~ s|$ENV{'PBROOT'}/||;
859        $tmp1 = $tmp1."/".basename($source);
860    }
861    # CVS needs a relative path !
862    pb_system("cd $dir ; cvs export -D \"$pbdate\" -d $base $tmp1","Exporting $source from CVS to $destdir");
863} else {
864    die "cms $cms->{$ENV{'PBPROJ'}} unknown";
865}
866}
867
868
869sub pb_create_authors {
870
871my $authors=shift;
872my $dest=shift;
873my $cms=shift;
874
875return if ($authors eq "/dev/null");
876open(SAUTH,$authors) || die "Unable to open $authors";
877open(DAUTH,"> $dest/AUTHORS") || die "Unable to create $dest/AUTHORS";
878print DAUTH "Authors of the project are:\n";
879print DAUTH "===========================\n";
880while (<SAUTH>) {
881    my ($nick,$gcos) = split(/:/);
882    chomp($gcos);
883    print DAUTH "$gcos";
884    if (defined $cms) {
885        print DAUTH " ($nick under $cms)\n";
886    } else {
887        print DAUTH "\n";
888    }
889}
890close(DAUTH);
891close(SAUTH);
892}
893
894sub pb_cms_log {
895my $cms = shift;
896my $pkgdir = shift;
897my $dest = shift;
898my $chglog = shift;
899my $authors = shift;
900
901pb_create_authors($authors,$dest,$cms->{$ENV{'PBPROJ'}});
902
903if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
904    if (! -f "$dest/ChangeLog") {
905        if (-x "/usr/bin/svn2cl") {
906            pb_system("/usr/bin/svn2cl --group-by-day --authors=$authors -i -o $dest/ChangeLog $pkgdir","Generating ChangeLog from SVN");
907        } else {
908            # To be written from pbcl
909            pb_system("svn log -v $pkgdir > $dest/$ENV{'PBCMSLOGFILE'}","Extracting log info from SVN");
910        }
911    }
912} elsif ($cms->{$ENV{'PBPROJ'}} eq "flat") {
913    if (! -f "$dest/ChangeLog") {
914        pb_system("echo ChangeLog for $pkgdir > $dest/ChangeLog","Empty ChangeLog file created");
915    }
916} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
917    my $tmp=basename($pkgdir);
918    # CVS needs a relative path !
919    if (! -f "$dest/ChangeLog") {
920        if (-x "/usr/bin/cvs2cl") {
921            pb_system("/usr/bin/cvs2cl --group-by-day -U $authors -f $dest/ChangeLog $pkgdir","Generating ChangeLog from CVS");
922        } else {
923            # To be written from pbcl
924            pb_system("cvs log $tmp > $dest/$ENV{'PBCMSLOGFILE'}","Extracting log info from CVS");
925        }
926    }
927} else {
928    die "cms $cms->{$ENV{'PBPROJ'}} unknown";
929}
930}
931
932sub pb_cms_getinfo {
933my $cms = shift;
934my $url = "";
935my $void = "";
936
937if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
938    open(PIPE,"LANGUAGE=C svn info $ENV{'PBROOT'} |") || die "Unable to get svn info from $ENV{'PBROOT'}";
939    while (<PIPE>) {
940        ($void,$url) = split(/^URL:/) if (/^URL:/);
941    }
942    close(PIPE);
943    chomp($url);
944} elsif ($cms->{$ENV{'PBPROJ'}} eq "flat") {
945} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
946} else {
947    die "cms $cms->{$ENV{'PBPROJ'}} unknown";
948}
949return($url);
950}
951
952sub pb_cms_copy {
953my $cms = shift;
954my $oldurl = shift;
955my $newurl = shift;
956
957if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
958    pb_system("svn copy -m \"Creation of $newurl from $oldurl\" $oldurl $newurl","Copying $oldurl to $newurl ");
959} elsif ($cms->{$ENV{'PBPROJ'}} eq "flat") {
960} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
961} else {
962    die "cms $cms->{$ENV{'PBPROJ'}} unknown";
963}
964}
965
966sub pb_cms_checkout {
967my $cms = shift;
968my $url = shift;
969my $destination = shift;
970
971if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
972    pb_system("svn co $url $destination","Checking $url to $destination ");
973} elsif ($cms->{$ENV{'PBPROJ'}} eq "flat") {
974} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
975} else {
976    die "cms $cms->{$ENV{'PBPROJ'}} unknown";
977}
978}
979
980sub pb_cms_checkin {
981my $cms = shift;
982my $dir = shift;
983
984my $ver = basename($dir);
985if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
986    pb_system("svn ci -m \"Updated to $ver\" $dir","Checking in $dir");
987    pb_system("svn up $dir","Updating $dir");
988} elsif ($cms->{$ENV{'PBPROJ'}} eq "flat") {
989} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
990} else {
991    die "cms $cms->{$ENV{'PBPROJ'}} unknown";
992}
993}
994
995sub pb_cms_isdiff {
996my $cms = shift;
997
998if ($cms->{$ENV{'PBPROJ'}} eq "svn") {
999    open(PIPE,"svn diff $ENV{'PBROOT'} |") || die "Unable to get svn diff from $ENV{'PBROOT'}";
1000    my $l = 0;
1001    while (<PIPE>) {
1002        $l++;
1003    }
1004    return($l);
1005} elsif ($cms->{$ENV{'PBPROJ'}} eq "flat") {
1006} elsif ($cms->{$ENV{'PBPROJ'}} eq "cvs") {
1007} else {
1008    die "cms $cms->{$ENV{'PBPROJ'}} unknown";
1009}
1010}
1011
1012# Get all filters to apply
1013# They're cumulative from less specific to most specific
1014# suffix is .pbf
1015
1016sub pb_get_filters {
1017
1018# For the moment not dynamic
1019my $debug = 0;                  # Debug level
1020my $LOG = *STDOUT;              # Where to log
1021
1022my @ffiles;
1023my ($ffile00, $ffile0, $ffile1, $ffile2, $ffile3);
1024my ($mfile00, $mfile0, $mfile1, $mfile2, $mfile3);
1025my $pbpkg = shift || die "No package specified";
1026my $dtype = shift || "";
1027my $dfam = shift || "";
1028my $ddir = shift || "";
1029my $dver = shift || "";
1030my $ptr; # returned value pointer on the hash of filters
1031my %ptr;
1032
1033# Global filter files first, then package specificities
1034if (-d "$ENV{'PBCONF'}/pbfilter") {
1035    $mfile00 = "$ENV{'PBCONF'}/pbfilter/all.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/all.pbf");
1036    $mfile0 = "$ENV{'PBCONF'}/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$dtype.pbf");
1037    $mfile1 = "$ENV{'PBCONF'}/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$dfam.pbf");
1038    $mfile2 = "$ENV{'PBCONF'}/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$ddir.pbf");
1039    $mfile3 = "$ENV{'PBCONF'}/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/pbfilter/$ddir-$dver.pbf");
1040
1041    push @ffiles,$mfile00 if (defined $mfile00);
1042    push @ffiles,$mfile0 if (defined $mfile0);
1043    push @ffiles,$mfile1 if (defined $mfile1);
1044    push @ffiles,$mfile2 if (defined $mfile2);
1045    push @ffiles,$mfile3 if (defined $mfile3);
1046}
1047
1048if (-d "$ENV{'PBCONF'}/$pbpkg/pbfilter") {
1049    $ffile00 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/all.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/all.pbf");
1050    $ffile0 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dtype.pbf");
1051    $ffile1 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$dfam.pbf");
1052    $ffile2 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir.pbf");
1053    $ffile3 = "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf" if (-f "$ENV{'PBCONF'}/$pbpkg/pbfilter/$ddir-$dver.pbf");
1054
1055    push @ffiles,$ffile00 if (defined $ffile00);
1056    push @ffiles,$ffile0 if (defined $ffile0);
1057    push @ffiles,$ffile1 if (defined $ffile1);
1058    push @ffiles,$ffile2 if (defined $ffile2);
1059    push @ffiles,$ffile3 if (defined $ffile3);
1060}
1061if (@ffiles) {
1062    print $LOG "DEBUG ffiles: ".Dumper(\@ffiles)."\n" if ($debug >= 1);
1063
1064    my $config = AppConfig->new({
1065        # Auto Create variables mentioned in Conf file
1066        CREATE => 1,
1067        DEBUG => 0,
1068        GLOBAL => {
1069            # Each conf item is a hash
1070            ARGCOUNT => AppConfig::ARGCOUNT_HASH
1071        }
1072    });
1073
1074    $config->file(@ffiles);
1075    $ptr = $config->get("filter");
1076    print $LOG "DEBUG f:".Dumper($ptr)."\n" if ($debug >= 1);
1077} else {
1078    $ptr = { };
1079}
1080%ptr = %$ptr;
1081return(\%ptr);
1082}
1083
1084# Function which applies filter on pb build files
1085sub pb_filter_file_pb {
1086
1087my $f=shift;
1088my $ptr=shift;
1089my %filter=%$ptr;
1090my $destfile=shift;
1091my $dtype=shift;
1092my $pbsuf=shift;
1093my $pbpkg=shift;
1094my $pbver=shift;
1095my $pbtag=shift;
1096my $pbrev=shift;
1097my $pbdate=shift;
1098my $defpkgdir = shift;
1099my $extpkgdir = shift;
1100my $pbpackager = shift;
1101my $chglog = shift || undef;
1102
1103# For the moment not dynamic
1104my $debug = 0;                  # Debug level
1105my $LOG = *STDOUT;              # Where to log
1106
1107print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
1108pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
1109open(DEST,"> $destfile") || die "Unable to create $destfile";
1110open(FILE,"$f") || die "Unable to open $f: $!";
1111while (<FILE>) {
1112    my $line = $_;
1113    foreach my $s (keys %filter) {
1114        # Process single variables
1115        print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug >= 1);
1116        my $tmp = $filter{$s};
1117        next if (not defined $tmp);
1118        # Expand variables if any single one found
1119        print $LOG "DEBUG tmp: $tmp\n" if ($debug >= 1);
1120        if ($tmp =~ /\$/) {
1121            eval { $tmp =~ s/(\$\w+)/$1/eeg };
1122        # special case for ChangeLog only for pb
1123        } elsif (($s =~ /^PBLOG$/) && ($line =~ /^PBLOG$/)) {
1124            my $p = $defpkgdir->{$pbpkg};
1125            $p = $extpkgdir->{$pbpkg} if (not defined $p);
1126            pb_changelog($dtype, $pbpkg, $pbver, $pbtag, $pbsuf, $p, \*DEST, $tmp, $chglog);
1127            $tmp = "";
1128        }
1129        $line =~ s|$s|$tmp|;
1130    }
1131    print DEST $line;
1132}
1133close(FILE);
1134close(DEST);
1135}
1136
1137# Function which applies filter on files (external call)
1138sub pb_filter_file {
1139
1140my $f=shift;
1141my $ptr=shift;
1142my %filter=%$ptr;
1143my $destfile=shift;
1144my $pbpkg=shift;
1145my $pbver=shift;
1146my $pbtag=shift;
1147my $pbrev=shift;
1148my $pbdate=shift;
1149my $pbpackager=shift;
1150
1151# For the moment not dynamic
1152my $debug = 0;                  # Debug level
1153my $LOG = *STDOUT;              # Where to log
1154
1155print $LOG "DEBUG: From $f to $destfile\n" if ($debug >= 1);
1156pb_mkdir_p(dirname($destfile)) if (! -d dirname($destfile));
1157open(DEST,"> $destfile") || die "Unable to create $destfile";
1158open(FILE,"$f") || die "Unable to open $f: $!";
1159while (<FILE>) {
1160    my $line = $_;
1161    foreach my $s (keys %filter) {
1162        # Process single variables
1163        print $LOG "DEBUG filter{$s}: $filter{$s}\n" if ($debug > 1);
1164        my $tmp = $filter{$s};
1165        next if (not defined $tmp);
1166        # Expand variables if any single one found
1167        if ($tmp =~ /\$/) {
1168            eval { $tmp =~ s/(\$\w+)/$1/eeg };
1169        }
1170        $line =~ s|$s|$tmp|;
1171    }
1172    print DEST $line;
1173}
1174close(FILE);
1175close(DEST);
1176}
1177
1178
11791;
Note: See TracBrowser for help on using the repository browser.