source: ProjectBuilder/devel/pb/lib/ProjectBuilder/CMS.pm@ 2032

Last change on this file since 2032 was 2032, checked in by Bruno Cornec, 8 years ago
  • Copyright update for 2016
File size: 11.3 KB
Line 
1#!/usr/bin/perl -w
2#
3# Project Builder CMS module
4# CMS subroutines brought by the the Project-Builder project
5# which can be easily used by pbinit scripts
6#
7# $Id$
8#
9# Copyright B. Cornec 2007-2016
10# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
11# Provided under the GPL v2
12
13package ProjectBuilder::CMS;
14
15use strict 'vars';
16use Carp 'confess';
17use Data::Dumper;
18use English;
19use File::Basename;
20use File::Copy;
21use POSIX qw(strftime);
22use lib qw (lib);
23use ProjectBuilder::Version;
24use ProjectBuilder::Base;
25use ProjectBuilder::Conf;
26use ProjectBuilder::VCS;
27
28# Inherit from the "Exporter" module which handles exporting functions.
29
30use vars qw($VERSION $REVISION @ISA @EXPORT);
31use Exporter;
32
33# Export, by default, all the functions into the namespace of
34# any code which uses this module.
35
36our @ISA = qw(Exporter);
37our @EXPORT = qw(pb_cms_init pb_cms_get_pkg pb_cms_get_real_pkg pb_cms_log);
38($VERSION,$REVISION) = pb_version_init();
39
40=pod
41
42=head1 NAME
43
44ProjectBuilder::CMS, part of the project-builder.org
45
46=head1 DESCRIPTION
47
48This modules provides configuration management system functions suitable for pbinit calls.
49
50=head1 USAGE
51
52=over 4
53
54=item B<pb_cms_init>
55
56This function setup the environment for the CMS system related to the URL given by the pburl configuration parameter.
57The potential parameter indicates whether we should inititate the context or not.
58It sets up environement variables (PBPROJDIR, PBDIR, PBREVISION, PBCMSLOGFILE)
59
60=cut
61
62sub pb_cms_init {
63
64my $pbinit = shift;
65my $param = shift;
66
67my ($pburl) = pb_conf_get("pburl");
68pb_log(2,"DEBUG: Project URL of $ENV{'PBPROJ'}: $pburl->{$ENV{'PBPROJ'}}\n");
69my ($scheme, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
70my $vcscmd = pb_vcs_cmd($scheme);
71
72my ($pbprojdir) = pb_conf_get_if("pbprojdir");
73
74if ((defined $pbprojdir) && (defined $pbprojdir->{$ENV{'PBPROJ'}})) {
75 $ENV{'PBPROJDIR'} = $pbprojdir->{$ENV{'PBPROJ'}};
76} else {
77 $ENV{'PBPROJDIR'} = "$ENV{'PBDEFDIR'}/$ENV{'PBPROJ'}";
78}
79# Expand potential env variable in it to allow string replacement
80eval { $ENV{'PBPROJDIR'} =~ s/(\$ENV.+\})/$1/eeg };
81
82
83# Computing the default dir for PBDIR.
84# what we have is PBPROJDIR so work from that.
85# Tree identical between PBCONFDIR and PBROOTDIR on one side and
86# PBPROJDIR and PBDIR on the other side.
87
88my $tmp = $ENV{'PBROOTDIR'};
89$tmp =~ s|^$ENV{'PBCONFDIR'}/||;
90# If no subdir, then replace again
91$tmp =~ s|^$ENV{'PBCONFDIR'}||;
92
93#
94# Check project cms compliance
95#
96my $turl = "$pburl->{$ENV{'PBPROJ'}}/$tmp";
97$turl = $pburl->{$ENV{'PBPROJ'}} if ($scheme =~ /^(flat)|(ftp)|(http)|(file)|(git)\b/o);
98# git svn is like svn
99$turl = "$pburl->{$ENV{'PBPROJ'}}/$tmp" if ($scheme =~ /^git\+svn/o);
100pb_vcs_compliant(undef,'PBDIR',"$ENV{'PBPROJDIR'}/$tmp",$turl,$pbinit);
101
102
103if ($scheme =~ /^hg/) {
104 $tmp = `(cd "$ENV{'PBDIR'}" ; $vcscmd identify )`;
105 chomp($tmp);
106 $tmp =~ s/^.* //;
107 $ENV{'PBREVISION'}=$tmp;
108 $ENV{'PBCMSLOGFILE'}="hg.log";
109} elsif ($scheme =~ /^git/) {
110 if ($scheme =~ /svn/) {
111 $tmp = `(cd "$ENV{'PBDIR'}" ; LANGUAGE=C $vcscmd info | grep -E '^Revision:' | cut -d: -f2)`;
112 $tmp =~ s/\s+//;
113 } else {
114 $tmp = `(cd "$ENV{'PBDIR'}" ; $vcscmd log | head -1 | cut -f2)`;
115 $tmp =~ s/^.* //;
116 }
117 chomp($tmp);
118 $ENV{'PBREVISION'}=$tmp;
119 $ENV{'PBCMSLOGFILE'}="git.log";
120} elsif ($scheme =~ /^(flat)|(ftp)|(http)|(file)\b/o) {
121 $ENV{'PBREVISION'}="flat";
122 $ENV{'PBCMSLOGFILE'}="flat.log";
123} elsif ($scheme =~ /^svn/) {
124 # svnversion more precise than svn info if sbx
125 if ((defined $param) && ($param eq "CMS")) {
126 $tmp = `(LANGUAGE=C $vcscmd info $pburl->{$ENV{'PBPROJ'}} | grep -E '^Revision:' | cut -d: -f2)`;
127 $tmp =~ s/\s+//;
128 } else {
129 $tmp = `(cd "$ENV{'PBDIR'}" ; $vcscmd"version" .)`;
130 }
131 chomp($tmp);
132 $ENV{'PBREVISION'}=$tmp;
133 $ENV{'PBCMSLOGFILE'}="svn.log";
134} elsif ($scheme =~ /^svk/) {
135 $tmp = `(cd "$ENV{'PBDIR'}" ; LANGUAGE=C $vcscmd info . | grep -E '^Revision:' | cut -d: -f2)`;
136 $tmp =~ s/\s+//;
137 chomp($tmp);
138 $ENV{'PBREVISION'}=$tmp;
139 $ENV{'PBCMSLOGFILE'}="svk.log";
140} elsif ($scheme =~ /^cvs/) {
141 # Way too slow
142 #$ENV{'PBREVISION'}=`(cd "$ENV{'PBROOTDIR'}" ; cvs rannotate -f . 2>&1 | awk '{print \$1}' | grep -E '^[0-9]' | cut -d. -f2 |sort -nu | tail -1)`;
143 #chomp($ENV{'PBREVISION'});
144 $ENV{'PBREVISION'}="cvs";
145 $ENV{'PBCMSLOGFILE'}="cvs.log";
146 $ENV{'CVS_RSH'} = "ssh" if ($scheme =~ /ssh/);
147} else {
148 die "cms $scheme unknown";
149}
150
151pb_log(1,"pb_cms_init returns $scheme,$pburl->{$ENV{'PBPROJ'}}\n");
152return($scheme,$pburl->{$ENV{'PBPROJ'}});
153}
154
155=item B<pb_cms_get_pkg>
156
157This function returns the list of packages we are working on in a CMS action.
158The first parameter is the default list of packages from the configuration file.
159The second parameter is the optional list of packages from the configuration file.
160
161=cut
162
163sub pb_cms_get_pkg {
164
165my @pkgs = ();
166my $defpkgdir = shift;
167my $extpkgdir = shift;
168
169# Get packages list
170if (not defined $ARGV[0]) {
171 @pkgs = keys %$defpkgdir if (defined $defpkgdir);
172} elsif ($ARGV[0] =~ /^all$/) {
173 @pkgs = keys %$defpkgdir if (defined $defpkgdir);
174 push(@pkgs, keys %$extpkgdir) if (defined $extpkgdir);
175} else {
176 @pkgs = @ARGV;
177}
178pb_log(0,"Packages: ".join(',',@pkgs)."\n");
179return(\@pkgs);
180}
181
182=item B<pb_cms_get_real_pkg>
183
184This function returns the real name of a virtual package we are working on in a CMS action.
185It supports the following types: perl.
186The first parameter is the virtual package name
187
188=cut
189
190sub pb_cms_get_real_pkg {
191
192my $pbpkg = shift;
193my $dtype = shift;
194my $pbpkgreal = $pbpkg;
195
196my @nametype = pb_conf_get_if("namingtype");
197my $type = $nametype[0]->{$pbpkg};
198if (defined $type) {
199 if ($type eq "perl") {
200 if ($dtype eq "rpm") {
201 $pbpkgreal = "perl-".$pbpkg;
202 } elsif ($dtype eq "deb") {
203 # Only lower case allowed in Debian
204 # Cf: http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Package
205 $pbpkgreal = "lib".lc($pbpkg)."-perl";
206 } elsif ($dtype eq "ebuild") {
207 $pbpkgreal = $pbpkg;
208 } elsif ($dtype eq "hpux") {
209 $pbpkgreal = $pbpkg;
210 } elsif ($dtype eq "pkg") {
211 $pbpkgreal = "PB$pbpkg";
212 } else {
213 die "pb_cms_get_real_pkg not implemented for $dtype yet";
214 }
215 } else {
216 die "nametype $type not implemented yet";
217 }
218}
219
220pb_log(2,"pb_cms_get_real_pkg returns $pbpkgreal\n");
221return($pbpkgreal);
222}
223
224=item B<pb_cms_create_authors>
225
226This function creates a AUTHORS files for the project. It call it AUTHORS.pb if an AUTHORS file already exists.
227The first parameter is the source file for authors information.
228The second parameter is the directory where to create the final AUTHORS file.
229The third parameter is the schema of the CMS systems (svn, cvs, svn+ssh, ...)
230
231=cut
232
233sub pb_cms_create_authors {
234
235my $authors=shift;
236my $dest=shift;
237my $scheme=shift;
238
239return if ($authors eq "/dev/null");
240open(SAUTH,$authors) || die "Unable to open $authors";
241# Save a potentially existing AUTHORS file and write instead to AUTHORS.pb
242my $ext = "";
243if (-f "$dest/AUTHORS") {
244 $ext = ".pb";
245}
246open(DAUTH,"> $dest/AUTHORS$ext") || die "Unable to create $dest/AUTHORS$ext";
247print DAUTH "Authors of the project are:\n";
248print DAUTH "===========================\n";
249while (<SAUTH>) {
250 my ($nick,$gcos) = split(/:/);
251 chomp($gcos);
252 print DAUTH "$gcos";
253 if (defined $scheme) {
254 # Do not give a scheme for flat types
255 my $endstr="";
256 if ("$ENV{'PBREVISION'}" ne "flat") {
257 $endstr = " under $scheme";
258 }
259 print DAUTH " ($nick$endstr)\n";
260 } else {
261 print DAUTH "\n";
262 }
263}
264close(DAUTH);
265close(SAUTH);
266}
267
268=item B<pb_cms_log>
269
270This function creates a ChangeLog file for the project.
271The first parameter is the schema of the CMS systems (svn, cvs, svn+ssh, ...)
272The second parameter is the directory where the CMS content was checked out.
273The third parameter is the directory where to create the final ChangeLog file.
274The fourth parameter is unused.
275The fifth parameter is the source file for authors information.
276
277It may use a tool like svn2cl or cvs2cl to generate it if present, or the log file from the CMS if not.
278
279=cut
280
281
282sub pb_cms_log {
283
284my $scheme = shift;
285my $pkgdir = shift;
286my $dest = shift;
287my $chglog = shift;
288my $authors = shift;
289my $testver = shift;
290
291pb_cms_create_authors($authors,$dest,$scheme);
292my $vcscmd = pb_vcs_cmd($scheme);
293
294if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
295 if (! -f "$dest/ChangeLog") {
296 open(CL,"> $dest/ChangeLog") || confess "Unable to create $dest/ChangeLog";
297 # We need a minimal version for debian type of build
298 print CL "\n";
299 print CL "\n";
300 print CL "\n";
301 print CL "\n";
302 print CL "1990-01-01 none\n";
303 print CL "\n";
304 print CL " * test version\n";
305 print CL "\n";
306 close(CL);
307 pb_log(0,"Generating fake ChangeLog for test version\n");
308 open(CL,"> $dest/$ENV{'PBCMSLOGFILE'}") || die "Unable to create $dest/$ENV{'PBCMSLOGFILE'}";
309 close(CL);
310 }
311}
312
313if (! -f "$dest/ChangeLog") {
314 if ($scheme =~ /^svn/) {
315 # In case we have no network, just create an empty one before to allow correct build
316 open(CL,"> $dest/ChangeLog") || die "Unable to create $dest/ChangeLog";
317 close(CL);
318 my $command = pb_check_req("svn2cl",1);
319 if ((defined $command) && (-x $command)) {
320 pb_system("$command --group-by-day --authors=$authors -i -o $dest/ChangeLog $pkgdir","Generating ChangeLog from SVN with svn2cl");
321 } else {
322 # To be written from pbcl
323 pb_system("$vcscmd log -v $pkgdir > $dest/$ENV{'PBCMSLOGFILE'}","Extracting log info from SVN");
324 }
325 } elsif ($scheme =~ /^svk/) {
326 pb_system("$vcscmd log -v $pkgdir > $dest/$ENV{'PBCMSLOGFILE'}","Extracting log info from SVK");
327 } elsif ($scheme =~ /^hg/) {
328 # In case we have no network, just create an empty one before to allow correct build
329 open(CL,"> $dest/ChangeLog") || die "Unable to create $dest/ChangeLog";
330 close(CL);
331 pb_system("$vcscmd log -v $pkgdir > $dest/$ENV{'PBCMSLOGFILE'}","Extracting log info from Mercurial");
332 } elsif ($scheme =~ /^git/) {
333 # In case we have no network, just create an empty one before to allow correct build
334 open(CL,"> $dest/ChangeLog") || die "Unable to create $dest/ChangeLog";
335 close(CL);
336 pb_system("$vcscmd log -v $pkgdir > $dest/$ENV{'PBCMSLOGFILE'}","Extracting log info from GIT");
337 } elsif ($scheme =~ /^(flat)|(ftp)|(http)|(file)|(dir)\b/o) {
338 pb_system("echo ChangeLog for $pkgdir > $dest/ChangeLog","Empty ChangeLog file created");
339 } elsif ($scheme =~ /^cvs/) {
340 my $tmp=basename($pkgdir);
341 # CVS needs a relative path !
342 # In case we have no network, just create an empty one before to allow correct build
343 open(CL,"> $dest/ChangeLog") || die "Unable to create $dest/ChangeLog";
344 close(CL);
345 my $command = pb_check_req("cvs2cl",1);
346 if (-x $command) {
347 pb_system("$command --group-by-day -U $authors -f $dest/ChangeLog $pkgdir","Generating ChangeLog from CVS with cvs2cl");
348 } else {
349 # To be written from pbcl
350 pb_system("$vcscmd log $tmp > $dest/$ENV{'PBCMSLOGFILE'}","Extracting log info from CVS");
351 }
352 } else {
353 die "cms $scheme unknown";
354 }
355}
356if (! -f "$dest/ChangeLog") {
357 copy("$dest/$ENV{'PBCMSLOGFILE'}","$dest/ChangeLog");
358}
359}
360
361=back
362
363=head1 WEB SITES
364
365The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
366
367=head1 USER MAILING LIST
368
369None exists for the moment.
370
371=head1 AUTHORS
372
373The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
374
375=head1 COPYRIGHT
376
377Project-Builder.org is distributed under the GPL v2.0 license
378described in the file C<COPYING> included with the distribution.
379
380=cut
381
3821;
Note: See TracBrowser for help on using the repository browser.