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

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