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

Last change on this file since 298 was 298, checked in by bruno, 12 years ago

Addition of pbproj as a filtered variable for dploy needs also in pb_filter_file

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