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

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