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

Last change on this file since 343 was 343, checked in by Bruno Cornec, 12 years ago

Review of newproj to have a correct netperf doc

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