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

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

pb_cms_export ready to host the real stuff for file.

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