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 |
|
---|
13 | package ProjectBuilder::CMS;
|
---|
14 |
|
---|
15 | use strict 'vars';
|
---|
16 | use Carp 'confess';
|
---|
17 | use Data::Dumper;
|
---|
18 | use English;
|
---|
19 | use File::Basename;
|
---|
20 | use File::Copy;
|
---|
21 | use POSIX qw(strftime);
|
---|
22 | use lib qw (lib);
|
---|
23 | use ProjectBuilder::Version;
|
---|
24 | use ProjectBuilder::Base;
|
---|
25 | use ProjectBuilder::Conf;
|
---|
26 | use ProjectBuilder::VCS;
|
---|
27 |
|
---|
28 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
29 |
|
---|
30 | use vars qw($VERSION $REVISION @ISA @EXPORT);
|
---|
31 | use Exporter;
|
---|
32 |
|
---|
33 | # Export, by default, all the functions into the namespace of
|
---|
34 | # any code which uses this module.
|
---|
35 |
|
---|
36 | our @ISA = qw(Exporter);
|
---|
37 | our @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 |
|
---|
44 | ProjectBuilder::CMS, part of the project-builder.org
|
---|
45 |
|
---|
46 | =head1 DESCRIPTION
|
---|
47 |
|
---|
48 | This 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 |
|
---|
56 | This function setup the environment for the CMS system related to the URL given by the pburl configuration parameter.
|
---|
57 | The potential parameter indicates whether we should inititate the context or not.
|
---|
58 | It sets up environement variables (PBPROJDIR, PBDIR, PBREVISION, PBCMSLOGFILE)
|
---|
59 |
|
---|
60 | =cut
|
---|
61 |
|
---|
62 | sub pb_cms_init {
|
---|
63 |
|
---|
64 | my $pbinit = shift;
|
---|
65 | my $param = shift;
|
---|
66 |
|
---|
67 | my ($pburl) = pb_conf_get("pburl");
|
---|
68 | pb_log(2,"DEBUG: Project URL of $ENV{'PBPROJ'}: $pburl->{$ENV{'PBPROJ'}}\n");
|
---|
69 | my ($scheme, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
|
---|
70 | my $vcscmd = pb_vcs_cmd($scheme);
|
---|
71 |
|
---|
72 | my ($pbprojdir) = pb_conf_get_if("pbprojdir");
|
---|
73 |
|
---|
74 | if ((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
|
---|
80 | eval { $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 |
|
---|
88 | my $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 | #
|
---|
96 | my $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);
|
---|
100 | pb_vcs_compliant(undef,'PBDIR',"$ENV{'PBPROJDIR'}/$tmp",$turl,$pbinit);
|
---|
101 |
|
---|
102 |
|
---|
103 | if ($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 |
|
---|
151 | pb_log(1,"pb_cms_init returns $scheme,$pburl->{$ENV{'PBPROJ'}}\n");
|
---|
152 | return($scheme,$pburl->{$ENV{'PBPROJ'}});
|
---|
153 | }
|
---|
154 |
|
---|
155 | =item B<pb_cms_get_pkg>
|
---|
156 |
|
---|
157 | This function returns the list of packages we are working on in a CMS action.
|
---|
158 | The first parameter is the default list of packages from the configuration file.
|
---|
159 | The second parameter is the optional list of packages from the configuration file.
|
---|
160 |
|
---|
161 | =cut
|
---|
162 |
|
---|
163 | sub pb_cms_get_pkg {
|
---|
164 |
|
---|
165 | my @pkgs = ();
|
---|
166 | my $defpkgdir = shift;
|
---|
167 | my $extpkgdir = shift;
|
---|
168 |
|
---|
169 | # Get packages list
|
---|
170 | if (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 | }
|
---|
178 | pb_log(0,"Packages: ".join(',',@pkgs)."\n");
|
---|
179 | return(\@pkgs);
|
---|
180 | }
|
---|
181 |
|
---|
182 | =item B<pb_cms_get_real_pkg>
|
---|
183 |
|
---|
184 | This function returns the real name of a virtual package we are working on in a CMS action.
|
---|
185 | It supports the following types: perl.
|
---|
186 | The first parameter is the virtual package name
|
---|
187 |
|
---|
188 | =cut
|
---|
189 |
|
---|
190 | sub pb_cms_get_real_pkg {
|
---|
191 |
|
---|
192 | my $pbpkg = shift;
|
---|
193 | my $dtype = shift;
|
---|
194 | my $pbpkgreal = $pbpkg;
|
---|
195 |
|
---|
196 | my @nametype = pb_conf_get_if("namingtype");
|
---|
197 | my $type = $nametype[0]->{$pbpkg};
|
---|
198 | if (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 |
|
---|
220 | pb_log(2,"pb_cms_get_real_pkg returns $pbpkgreal\n");
|
---|
221 | return($pbpkgreal);
|
---|
222 | }
|
---|
223 |
|
---|
224 | =item B<pb_cms_create_authors>
|
---|
225 |
|
---|
226 | This function creates a AUTHORS files for the project. It call it AUTHORS.pb if an AUTHORS file already exists.
|
---|
227 | The first parameter is the source file for authors information.
|
---|
228 | The second parameter is the directory where to create the final AUTHORS file.
|
---|
229 | The third parameter is the schema of the CMS systems (svn, cvs, svn+ssh, ...)
|
---|
230 |
|
---|
231 | =cut
|
---|
232 |
|
---|
233 | sub pb_cms_create_authors {
|
---|
234 |
|
---|
235 | my $authors=shift;
|
---|
236 | my $dest=shift;
|
---|
237 | my $scheme=shift;
|
---|
238 |
|
---|
239 | return if ($authors eq "/dev/null");
|
---|
240 | open(SAUTH,$authors) || die "Unable to open $authors";
|
---|
241 | # Save a potentially existing AUTHORS file and write instead to AUTHORS.pb
|
---|
242 | my $ext = "";
|
---|
243 | if (-f "$dest/AUTHORS") {
|
---|
244 | $ext = ".pb";
|
---|
245 | }
|
---|
246 | open(DAUTH,"> $dest/AUTHORS$ext") || die "Unable to create $dest/AUTHORS$ext";
|
---|
247 | print DAUTH "Authors of the project are:\n";
|
---|
248 | print DAUTH "===========================\n";
|
---|
249 | while (<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 | }
|
---|
264 | close(DAUTH);
|
---|
265 | close(SAUTH);
|
---|
266 | }
|
---|
267 |
|
---|
268 | =item B<pb_cms_log>
|
---|
269 |
|
---|
270 | This function creates a ChangeLog file for the project.
|
---|
271 | The first parameter is the schema of the CMS systems (svn, cvs, svn+ssh, ...)
|
---|
272 | The second parameter is the directory where the CMS content was checked out.
|
---|
273 | The third parameter is the directory where to create the final ChangeLog file.
|
---|
274 | The fourth parameter is unused.
|
---|
275 | The fifth parameter is the source file for authors information.
|
---|
276 |
|
---|
277 | It 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 |
|
---|
282 | sub pb_cms_log {
|
---|
283 |
|
---|
284 | my $scheme = shift;
|
---|
285 | my $pkgdir = shift;
|
---|
286 | my $dest = shift;
|
---|
287 | my $chglog = shift;
|
---|
288 | my $authors = shift;
|
---|
289 | my $testver = shift;
|
---|
290 |
|
---|
291 | pb_cms_create_authors($authors,$dest,$scheme);
|
---|
292 | my $vcscmd = pb_vcs_cmd($scheme);
|
---|
293 |
|
---|
294 | if ((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 |
|
---|
313 | if (! -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 | }
|
---|
356 | if (! -f "$dest/ChangeLog") {
|
---|
357 | copy("$dest/$ENV{'PBCMSLOGFILE'}","$dest/ChangeLog");
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | =back
|
---|
362 |
|
---|
363 | =head1 WEB SITES
|
---|
364 |
|
---|
365 | The 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 |
|
---|
369 | None exists for the moment.
|
---|
370 |
|
---|
371 | =head1 AUTHORS
|
---|
372 |
|
---|
373 | The 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 |
|
---|
377 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
378 | described in the file C<COPYING> included with the distribution.
|
---|
379 |
|
---|
380 | =cut
|
---|
381 |
|
---|
382 | 1;
|
---|