Debian Codegraph @

texk/texlive/linked_scripts/svn-multi/svn-multi.pl

raw
1 #!/usr/bin/perl
2 # --------------------------------------------------------------
3 # svn-multi.pl for the svn-multi v2.0 LaTeX package
4 # version 0.1, Mar 1st, 2009
5 # --------------------------------------------------------------
6 #
7 # Copyright (C) 2006-2012 Martin Scharrer
8 # E-mail: [email protected]
9 # Code repository: https://bitbucket.org/martin_scharrer/svn-multi
10 #
11 # This program works only in combination with the LaTeX package 'svn-multi' and
12 # generates .svx files with '\svnidlong' macros holding Subversion keywords for
13 # files declared with '\svnexternal' inside the LaTeX document.
14 #
15 # This program is free software under the GPL v3 and LPPL v1.3c or later.
16 #
17 #
18 # GPL v3:
19 # This program is free software: you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation, either version 3 of the License, or
22 # (at your option) any later version.
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public License
30 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #
32 #
33 # LPPL v1.3c:
34 # This work may be distributed and/or modified under the
35 # conditions of the LaTeX Project Public License, either version 1.3c
36 # of this license or (at your option) any later version.
37 # The latest version of this license is in
38 # http://www.latex-project.org/lppl.txt
39 # and version 1.3c or later is part of all distributions of LaTeX
40 # version 2008/05/04 or later.
41 #
42 # This work has the LPPL maintenance status `maintained'.
43 #
44 # The Current Maintainer of this work is Martin Scharrer.
45 #
46 # This work consists of the files svn-multi.dtx, svn-multi-pl.dtx and
47 # svn-multi.ins and the derived files svn-multi.sty, svnkw.sty and svn-multi.pl.
48 #
49 # The file 'svn-multi.pl' may be renamed to 'svn-multi' for installation
50 # purposes.
51 #
52 #
53 use strict;
54 use warnings;
55 use File::Basename;
56 my $VERSION = "0.2";
57 my ($REV,$DATE) =
58 (split ' ','$Id$')[2,3];
59
60 my $dollar = '$';
61 my @PATH;
62 my %EXCLUDE = map { $_ => 1 } qw(sty tex aux log out toc fff ttt svn svx);
63
64 sub create_svxfile ($@);
65 sub usage;
66
67 if (!@ARGV or grep { $_ eq '--help' or $_ eq '-h' } @ARGV) {
68 usage();
69 }
70
71 print STDOUT "This is svn-multi.pl, Version $VERSION-$REV, $DATE\n";
72
73 my ($jobname, $dir, $suffix) = fileparse(shift @ARGV, qr/\.(tex|ltx|dtx|svn)$/);
74 if ($dir && $dir ne './') {
75 printf STDOUT "Main directory is '$dir'.\n";
76 chdir($dir);
77 }
78
79 if ($jobname =~ /^-/) {
80 usage();
81 }
82 my $outfile = "$jobname.svx";
83
84 my %external;
85
86 my $resvnexternalpath = qr/
87 ^ # at begin of line
88 \s* # allow for spaces
89 \\\@svnexternalpath # the macro name
90 \s*
91 { # begin token group
92 \s*
93 (?: # paths:
94 { # { of first path
95 (.*) # everything else, e.g: 'patha}{pathb}{pathc'
96 } # } of last path
97 | # or nothing
98 )
99 \s*
100 } # end token group
101 \s*
102 $ # end of line
103 /x;
104
105 my $resvnexternal = qr/
106 ^ # at begin of line
107 \s* # allow for spaces
108 \\\@svnexternal # the macro name
109 \s*
110 (?: # optional:
111 \[ # opening [
112 ([^\]]*) # group name (everything until ])
113 \] # closing ]
114 )?
115 \s*
116 { # begin token group
117 ([^}]+) # file name (everything until })
118 } # end token group
119 \s*
120 { # begin token group
121 \s*
122 (?: # paths:
123 { # { of first file
124 (.*) # everything else, e.g: 'filea}{fileb}{filec'
125 } # } of last file
126 | # or nothing
127 )
128 \s*
129 } # end token group
130 \s*
131 $ # end of line
132 /x;
133
134 if (-e "$jobname.aux" and open( my $svnfh, '<', "$jobname.aux")) {
135 print STDOUT "Reading '$jobname.aux'.\n";
136 while (<$svnfh>) {
137 chomp;
138 if (/$resvnexternalpath/) {
139 push @PATH, ( split /}\s*{/, $1 );
140 }
141 elsif (/$resvnexternal/) {
142 my ($group,$file,$list) = ($1||"",$2,$3||"");
143 $file =~ s/^\.\///;
144 push @{$external{$file}{$group} ||= []}, ( split /}\s*{/, $list );
145 }
146 }
147 close ($svnfh);
148 }
149 else {
150 warn "No .aux file found for '$jobname'!\n";
151 }
152
153 # Add TEXINPUTS to path
154 push @PATH, map { $_ =~ s/(?<!\/)$/\//; $_ } grep { $_ }
155 split(':', $ENV{'TEXINPUTS'}||"");
156
157 my @mainfilepairs;
158 my $maintex = "$jobname.tex";
159 if (exists $external{$maintex}) {
160 while ( my ($group,$list) = each %{$external{$maintex}} ) {
161 push @mainfilepairs, [ $group, [ @$list ] ];
162 }
163 delete $external{$maintex};
164 }
165
166 push @mainfilepairs, parse_args(@ARGV);
167 create_svxfile("$jobname.svx", @mainfilepairs )
168 if @mainfilepairs;
169
170 foreach my $file (keys %external) {
171 my @pairs;
172 my $svxfile = $file;
173 $svxfile =~ s/\.(tex|ltx)$/.svx/;
174 while ( my ($group,$list) = each %{$external{$file}} ) {
175 push @pairs, [ $group, [ @$list ] ];
176 }
177 create_svxfile($svxfile, @pairs);
178 }
179
180
181 sub parse_args {
182 my @args = @_;
183 my $group = '';
184 my @files;
185 my $readfg;
186 my @pairs;
187
188 foreach my $arg (@args) {
189 if ($readfg) {
190 $readfg = 0;
191 $group = $arg;
192 $group =~ s/^["']|["']$//; # '
193 }
194 elsif ($arg =~ /^--group|^-?-fg/) {
195 push @pairs, [ $group, [ @files ] ];
196 @files = ();
197 if ($arg =~ /^--group=(.*)/) {
198 $group = $1;
199 $group =~ s/^["']|["']$//; # '
200 }
201 else {
202 $readfg = 1;
203 }
204 }
205 elsif ($arg =~ /^--fls/) {
206 push @files, read_fls("$jobname.fls");
207 }
208 else {
209 push @files, $arg;
210 }
211 }
212 push @pairs, [ $group, [ @files ] ] if @files;
213 return @pairs;
214 }
215
216 sub path_search {
217 my $file = shift;
218 $file =~ s/##/#/g;
219 return $file if not $file or -e $file or not @PATH;
220
221 foreach my $dir (@PATH) {
222 if (-e "$dir$file") {
223 return "$dir$file";
224 }
225 }
226
227 return $file;
228 }
229
230 sub create_svxfile ($@) {
231 my ($svxfile, @fgpair) = @_;
232 my $lastgroup;
233 my $fgused = 0;
234 my %seen;
235 return if not @fgpair or not $svxfile;
236
237 open(my $svxfh, '>', $svxfile) or do {
238 warn "ERROR: Could not create SVX file '$svxfile'!\n";
239 return;
240 };
241 print STDOUT "Generating .svx file '$svxfile'.\n";
242 select $svxfh;
243 print "% Generated by svn-multi.pl v$VERSION\n\n";
244
245 while ( my ($group, $files) = @{shift @fgpair||[]}) {
246 no warnings 'uninitialized';
247 if ( (not defined $lastgroup and $group) or ($group ne $lastgroup) ) {
248 print "\\svngroup{$group}\n";
249 }
250 use warnings;
251 if ($group) {
252 $fgused = 1;
253 }
254
255 foreach my $file (@$files) {
256 $file = path_search($file);
257
258 # Only print the file once per group and .svx file
259 next if $seen{$group}{$file};
260 $seen{$group}{$file} = 1;
261
262 open(my $infoh, '-|', "svn info '$file' 2>/dev/null") or next;
263 my %info = map { chomp; split /\s*:\s*/, $_, 2 } <$infoh>;
264 close($infoh);
265 if (not keys %info) {
266 print "% Could not receive keywords for '$file'!\n\n";
267 next;
268 }
269 print "% Keywords for '$file'\n";
270 print svnidlong(\%info);
271 print "\\svnexternalfile";
272 print "[$group]" if $group;
273 print "{$file}\n";
274 print "\n"
275 }
276
277 $lastgroup = $group;
278 }
279 print "\n";
280 close ($svxfh);
281 }
282
283 sub svnid {
284 use Date::Parse;
285 use Date::Format;
286 my $href = shift;
287 return "" if (not defined $href->{Name});
288 my $date = time2str("%Y-%m-%d %XZ", str2time($href->{'Last Changed Date'}), 'Z');
289 return <<"EOT";
290 \\svnid{${dollar}Id: $href->{Name} $href->{'Last Changed Rev'} $date $href->{'Last Changed Author'} \$}
291 EOT
292 }
293
294 sub svnidlong {
295 my $href = shift;
296 return <<"EOT";
297 \\svnidlong
298 {${dollar}HeadURL: $href->{URL} \$}
299 {${dollar}LastChangedDate: $href->{'Last Changed Date'} \$}
300 {${dollar}LastChangedRevision: $href->{'Last Changed Rev'} \$}
301 {${dollar}LastChangedBy: $href->{'Last Changed Author'} \$}
302 EOT
303 }
304
305 sub read_fls {
306 my $fls = shift;
307 my %stack;
308 open (my $fh, '<', $fls) or return;
309 while (<$fh>) {
310 chomp;
311 if (/^INPUT ([^\/].*)$/) {
312 my $file = $1;
313 my $ext = substr($file, rindex($file,'.')+1);
314 $stack{$1} = 1 if not exists $EXCLUDE{$ext};
315 }
316 }
317 close($fh);
318 return keys %stack;
319 }
320
321 sub usage {
322 print STDOUT <<'EOT';
323 Usage:
324 svn-multi.pl jobname[.tex] [--fls] [--group|-g <group name>] [input_files] ...
325 ... [--group|-g <group name>] [input_files] ...
326
327 Description:
328 This LaTeX helper script collects Subversion keywords from non-(La)TeX files
329 and provides it to the 'svn-multi' package using '.svx' files. It will first
330 scan the file '<jobname>.aux' for files declared by the '\svnextern' macro but
331 also allows to provide additional files including the corresponding groups. The
332 keywords for the additional files will be written in the file '<jobname>.svx'.
333
334 Options:
335 jobname[.tex] : The LaTeX `jobname`, i.e. the basename of your main LaTeX file.
336 --group <GN> : Use given group name <GN> for all following files,
337 or -g <GN> including the one read by a '--fls' option, until the next
338 group is specified.
339 --fls : Read list of (additional) files from the file '<jobname>.fls'. This
340 file is produced by LaTeX when run with the '--recorder' option and
341 contains a list of all input and output files used by the LaTeX main
342 file. Only input files with a relative path will be used. A
343 previously selected group will be honoured.
344
345 Examples:
346 The main LaTeX file here is 'mymainlatexfile.tex'.
347
348 svn-multi.pl mymainlatexfile
349 Creates Subversion keywords for all files declared by '\svnextern' inside
350 the LaTeX code.
351
352 svn-multi.pl mymainlatexfile --group=FLS --fls
353 Creates Subversion keywords for all files declared by '\svnextern' inside
354 the LaTeX code. In addition it does the same for all relative input files
355 mentioned in the .fls file which are placed in the 'FLS' group.
356
357 svn-multi.pl mymainlatexfile a b c --group=B e d f
358 In addition to the '\svnextern' declared files the keywords for the files
359 'a', 'b' and 'c' will be added without a specific group, i.e. the last group
360 specified in the LaTeX file before the '\svnextern' macro will be used. The
361 keywords for 'e', 'd', 'f' will be part of group 'B'.
362
363 svn-multi.pl mymainlatexfile --group=A a --group=B b --group='' c
364 File 'a' is in group 'A', 'b' is in 'B' and 'c' is not in any group.
365
366 Further Information:
367 See the svn-multi package manual for more information about this script.
368 EOT
369 exit(0);
370 }
371
372 __END__