-
Notifications
You must be signed in to change notification settings - Fork 94
/
ttree
executable file
·643 lines (531 loc) · 20.6 KB
/
ttree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/perl -w
#========================================================================
#
# ttree
#
# DESCRIPTION
# Script for processing all directory trees containing templates.
# Template files are processed and the output directed to the
# relvant file in an output tree. The timestamps of the source and
# destination files can then be examined for future invocations
# to process only those files that have changed. In other words,
# it's a lot like 'make' for templates.
#
# AUTHOR
# Andy Wardley <abw@kfs.org>
#
# COPYRIGHT
# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
#
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
#------------------------------------------------------------------------
#
# $Id$
#
#========================================================================
use strict;
use Template;
use AppConfig qw( :expand );
use File::Copy;
use File::Path;
use File::Basename;
#------------------------------------------------------------------------
# config
#------------------------------------------------------------------------
my $NAME = "ttree";
my $VERSION = sprintf("%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/);
my $HOME = $ENV{ HOME } || '';
my $RCFILE = $ENV{"\U${NAME}rc"} || "$HOME/.${NAME}rc";
# offer create a sample config file if it doesn't exist, unless a '-f'
# has been specified on the command line
unless (-f $RCFILE or grep(/^-f$/, @ARGV) ) {
print("Do you want me to create a sample '.ttreerc' file for you?\n",
"(file: $RCFILE) [y/n]: ");
my $y = <STDIN>;
if ($y =~ /^y(es)?/i) {
write_config($RCFILE);
exit(0);
}
}
# read configuration file and command line arguments - I need to remember
# to fix varlist() and varhash() in AppConfig to make this nicer...
my $config = read_config($RCFILE);
my $dryrun = $config->nothing;
my $verbose = $config->verbose || $dryrun;
my $recurse = $config->recurse;
my $preserve = $config->preserve;
my $debug = $config->debug;
my $all = $config->all;
my $libdir = $config->lib;
my $ignore = $config->ignore;
my $copy = $config->copy;
my $accept = $config->accept;
my $srcdir = $config->src
|| die "Source directory not set (-s)\n";
my $destdir = $config->dest
|| die "Destination directory not set (-d)\n";
die "Source and destination directories may not be the same:\n $srcdir\n"
if $srcdir eq $destdir;
# unshift any perl5lib directories onto front of INC
unshift(@INC, @{ $config->perl5lib });
# get all template_* options from the config and fold keys to UPPER CASE
my %ttopts = $config->varlist('^template_', 1);
my %ucttopts;
@ucttopts{ map { uc } keys %ttopts } = values %ttopts;
#print "TT config: ", join(', ', map { "$_ => $ucttopts{ $_ }"}
# keys %ucttopts), "\n";
# get all template variable definitions
my $replace = $config->get('define');
#print "replace hash: ", join(', ', map { "$_ => $replace->{ $_ }"}
# keys %$replace), "\n";
# now create complete parameter hash for creating template processor
my $ttopts = {
%ucttopts,
RELATIVE => 1,
# INCLUDE_PATH => [ @$libdir, '.' ],
INCLUDE_PATH => [ $srcdir, @$libdir ],
OUTPUT_PATH => $destdir,
};
#------------------------------------------------------------------------
# pre-amble
#------------------------------------------------------------------------
print "$NAME $VERSION (Template Toolkit version $Template::VERSION)\n\n"
if $verbose;
if ($verbose) {
local $" = ', ';
print(STDERR
" Source: $srcdir\n",
" Destination: $destdir\n",
"Include Path: [ @$libdir ]\n",
" Ignore: [ @$ignore ]\n",
" Copy: [ @$copy ]\n",
" Accept: [ ", @$accept ? "@$accept" : "*", " ]\n\n");
print(STDERR "NOTE: dry run, doing nothing...\n")
if $dryrun;
}
if ($debug) {
local $" = ', ';
print STDERR "Template Toolkit configuration:\n";
foreach (keys %ucttopts) {
my $val = $ucttopts{$_};
next unless $val;
if (ref($val) eq 'ARRAY') {
next unless @$val;
$val = "[ @$val ]";
}
printf STDERR " %-12s => $val\n", $_;
}
print STDERR "\n";
}
#------------------------------------------------------------------------
# main-amble
#------------------------------------------------------------------------
#chdir($srcdir) || die "$srcdir: $!\n";
my $template = Template->new($ttopts);
if (@ARGV) {
# explicitly process files specified on command lines
foreach my $file (@ARGV) {
print " + $file\n" if $verbose;
$template->process($file, $replace, $file)
|| print " ! ", $template->error(), "\n";
}
}
else {
# implicitly process all file in source directory
process_tree();
}
#------------------------------------------------------------------------
# process_tree($dir)
#
# Walks the directory tree starting at $dir or the current directory
# if unspecified, processing files as found.
#------------------------------------------------------------------------
sub process_tree {
my $dir = shift;
my ($file, $path, $abspath, $check);
my $target;
local *DIR;
my $absdir = join('/', $srcdir ? $srcdir : (), $dir ? $dir : ());
$absdir ||= '.';
print STDERR " * processing tree: $absdir\n" if $debug;
opendir(DIR, $absdir) || do { warn "$absdir: $!\n"; return undef; };
FILE: while (defined ($file = readdir(DIR))) {
next if $file eq '.' || $file eq '..';
$path = $dir ? "$dir/$file" : $file;
$abspath = "$absdir/$file";
next unless -e $abspath;
# check against ignore list
foreach $check (@$ignore) {
if ($path =~ /$check/) {
printf " - %-32s (ignored, matches /$check/)\n", $path
if $verbose;
next FILE;
}
}
if (-d $abspath) {
if ($recurse) {
my ($uid, $gid, $mode);
(undef, undef, $mode, undef, $uid, $gid, undef, undef,
undef, undef, undef, undef, undef) = stat($abspath);
# create target directory if required
$target = "$destdir/$path";
unless (-d $target || $dryrun) {
mkdir $target, $mode || do {
warn "mkdir ($target): $!\n";
next;
};
# commented out by abw on 2000/12/04 - seems to raise a warning?
# chown($uid, $gid, $target) || warn "chown($target): $!\n";
printf " + %-32s (created target directory)\n", $path
if $verbose;
}
# recurse into directory
process_tree($path);
}
else {
printf " - %-32s (directory, not recursing)\n", $path
if $verbose;
}
}
else {
process_file($path, $abspath);
}
}
closedir(DIR);
}
#------------------------------------------------------------------------
# process_file()
#
# File filtering and processing sub-routine called by process_tree()
#------------------------------------------------------------------------
sub process_file {
my ($file, $absfile) = @_;
my ($dest, $base, $check, $srctime, $desttime, $mode, $uid, $gid);
$absfile ||= $file;
$dest = $destdir ? "$destdir/$file" : $file;
$base = basename($file);
# print "proc $file => $dest\n";
# stat the source file unconditionally, so we can preserve
# mode and ownership
(undef, undef, $mode, undef, $uid, $gid, undef, undef, undef, $srctime,
undef, undef, undef) = stat($absfile);
# test modification time of existing destination file
if (-f $dest && ! $all) {
$desttime = ( stat($dest) )[9];
if ($desttime > $srctime) {
printf " - %-32s (not modified)\n", $file
if $verbose;
return;
}
}
# check against copy list
foreach $check (@$copy) {
if ($base =~ /$check/) {
printf " > %-32s (copied, matches /$check/)\n", $file
if $verbose;
unless ($dryrun) {
copy($absfile, $dest);
if ($preserve) {
chown($uid, $gid, $dest) || warn "chown($dest): $!\n";
chmod($mode, $dest) || warn "chmod($dest): $!\n";
}
}
return;
}
}
# check against acceptance list
if (@$accept) {
unless (grep { $base =~ /$_/ } @$accept) {
printf " - %-32s (not accepted)\n", $file
if $verbose;
return;
}
}
print " + $file\n" if $verbose;
# process file
unless ($dryrun) {
$template->process($file, $replace, $file)
|| print(" ! ", $template->error(), "\n");
if ($preserve) {
chown($uid, $gid, $dest) || warn "chown($dest): $!\n";
chmod($mode, $dest) || warn "chmod($dest): $!\n";
}
}
}
#------------------------------------------------------------------------
# read_config($file)
#
# Handles reading of config file and/or command line arguments.
#------------------------------------------------------------------------
sub read_config {
my $file = shift;
my $config = AppConfig->new({
ERROR => sub { die @_, "\ntry `$NAME --help'\n" } },
'help|h' => { ACTION => \&help },
'src|s=s' => { EXPAND => EXPAND_ALL },
'dest|d=s' => { EXPAND => EXPAND_ALL },
'lib|l=s@' => { EXPAND => EXPAND_ALL },
'cfg|c=s' => { EXPAND => EXPAND_ALL, DEFAULT => '.' },
'verbose|v' => { DEFAULT => 0 },
'recurse|r' => { DEFAULT => 0 },
'nothing|n' => { DEFAULT => 0 },
'preserve|p' => { DEFAULT => 0 },
'all|a' => { DEFAULT => 0 },
'debug|dbg' => { DEFAULT => 0 },
'define=s%',
'ignore=s@',
'copy=s@',
'accept=s@',
'template_anycase|anycase',
'template_eval_perl|eval_perl',
'template_load_perl|load_perl',
'template_interpolate|interpolate',
'template_pre_chomp|pre_chomp|prechomp',
'template_post_chomp|post_chomp|postchomp',
'template_trim|trim',
'template_pre_process|pre_process|preprocess=s@',
'template_post_process|post_process|postprocess=s@',
'template_process|process=s',
'template_recursion|recursion',
'template_default|default=s',
'template_error|error=s',
'template_start_tag|start_tag|starttag=s',
'template_end_tag|end_tag|endtag=s',
'template_tag_style|tag_style|tagstyle=s',
'template_compile_ext|compile_ext=s',
'template_compile_dir|compile_dir=s',
'template_plugin_base|plugin_base|pluginbase=s@',
'perl5lib|perllib=s@'
);
# add the 'file' option now that we have a $config object that we
# can reference in a closure
$config->define(
'file|f=s@' => { EXPAND => EXPAND_ALL,
ACTION => sub {
my ($state, $item, $file) = @_;
$file = $state->cfg . "/$file"
unless $file =~ /^[\.\/]|(?:\w:)/;
$config->file($file) }
}
);
# process main config file, then command line args
$config->file($file) if -f $file;
$config->args();
$config;
}
#------------------------------------------------------------------------
# write_config($file)
#
# Writes a sample configuration file to the filename specified.
#------------------------------------------------------------------------
sub write_config {
my $file = shift;
open(CONFIG, ">$file") || die "failed to create $file: $!\n";
print(CONFIG <<END_OF_CONFIG);
#------------------------------------------------------------------------
# sample .ttreerc file created automatically by $NAME version $VERSION
#
# This file originally written to $file
#
# For more information on the contents of this configuration file, see
#
# perldoc ttree
# ttree -h
#
# NOTE: The directories specified below adopt the UNIX convention of
# specifying a user's home directory with the '~' character. This
# feature may not be available on other platforms in which case you
# should specify the directory in entirety.
#------------------------------------------------------------------------
#------------------------------------------------------------------------
# General options
# print summary of what's going on (-v)
verbose
# recurse into any sub-directories and process files (-r)
recurse
#------------------------------------------------------------------------
# The 'cfg' option defines a directory in which other ttree configuration
# files can be found; you can specify a file using the '-f' option,
# 'ttree -f myconfig' and the script will look for the file in this
# directory. Alteratively, provide an absolute path as an argument,
# 'ttree -f /tmp/foo'.
#
# By default, this option is commented out. You will need to create a
# directory, uncomment the following line and set the value appropriately.
# Having done that, you can then create files exactly like this in that
# location.
#cfg = ~/.ttree
#------------------------------------------------------------------------
# The remaining options define the default behaviour when you run ttree.
# This file is always processed before any file specified by '-f'. If
# you define the 'src' and 'dest' options then these will be used by
# default. Values for these options defined in files loaded with '-f'
# will override these default. Other options such as 'lib', 'ignore',
# 'copy' and 'accept' are accumulative.
# The 'src' option defines the location of the template files that
# you want to process
src = ~/websrc/public_html
# The 'dest' option specifies where the output should go. The script
# compares the modification dates of files in the 'src' and 'dest'
# directories to work out which need to be processed.
dest = ~/public_html
# 'lib' tells the processor (via INCLUDE_PATH) where to find any
# template files that may be INCLUDE'd. You can specify many.
lib = ~/websrc/templates
lib = /usr/local/templates/lib
# Things that aren't templates and should be ignored, specified as Perl
# regexen.
ignore = \\b(CVS|RCS)\\b
ignore = ^#
# Things that should be copied rather than processed.
copy = \\.png\$
copy = \\.gif\$
# By default, everything not ignored or copied is accepted; add 'accept'
# lines if you want to filter further. e.g.
# accept = \\.html\$
# accept = \\.atml\$
END_OF_CONFIG
close(CONFIG);
print "$file created. Please edit accordingly and re-run $NAME\n";
}
#------------------------------------------------------------------------
# help()
#
# Prints help message and exits.
#------------------------------------------------------------------------
sub help {
print<<END_OF_HELP;
$NAME $VERSION (Template Toolkit version $Template::VERSION)
usage: $NAME [options] [files]
Options:
-a (--all) Process all files, regardless of modification
-r (--recurse) Recurse into sub-directories
-p (--preserve) Preserve file ownership and permission
-n (--nothing) Do nothing, just print summary (enables -v)
-v (--verbose) Verbose mode
-h (--help) This help
-dbg (--debug) Debug mode
-s DIR (--src=DIR) Source directory
-d DIR (--dest=DIR) Destination directory
-c DIR (--cfg=DIR) Location of configuration files
-l DIR (--lib=DIR) Library directory (INCLUDE_PATH) (multiple)
-f FILE (--file=FILE) Read named configuration file (multiple)
File search specifications (all may appear multiple times):
--ignore=REGEX Ignore files matching REGEX
--copy=REGEX Copy files matching REGEX
--accept=REGEX Process only files matching REGEX
Additional options to set Template Toolkit configuration items:
--define var=value Define template variable
--interpolate Interpolate '\$var' references in text
--anycase Accept directive keywords in any case.
--pre_chomp Chomp leading whitespace
--post_chomp Chomp trailing whitespace
--trim Trim blank lines around template blocks
--eval_perl Evaluate [% PERL %] ... [% END %] code blocks
--load_perl Load regular Perl modules via USE directive
--pre_process=TEMPLATE Add TEMPLATE as header for each file
--post_process=TEMPLATE Add TEMPLATE as footer for each file
--process=TEMPLATE Use TEMPLATE as wrapper around each file
--default=TEMPLATE Use TEMPLATE as default
--error=TEMPLATE Use TEMPLATE to handle errors
--start_tag=STRING STRING defines start of directive tag
--end_tag=STRING STRING defined end of directive tag
--tag_style=STYLE Use pre-defined tag STYLE
--plugin_base=PACKAGE Base PACKAGE for plugins
--compile_ext=STRING File extension for compiled template files
--compile_dir=DIR Directory for compiled template files
--perl5lib=DIR Specify additional Perl library directories
See 'perldoc ttree' for further information. Note that earlier versions
of AppConfig (<1.53) may require options of the form '--name=opt' to be
specified as '-name opt'.
END_OF_HELP
exit(0);
}
__END__
#------------------------------------------------------------------------
# IMPORTANT NOTE
# This documentation is generated automatically from source
# templates. Any changes you make here may be lost.
#
# The 'docsrc' documentation source bundle is available for download
# from http://www.template-toolkit.org/docs.html and contains all
# the source templates, XML files, scripts, etc., from which the
# documentation for the Template Toolkit is built.
#------------------------------------------------------------------------
=head1 NAME
Template::Tools::ttree - Process entire directory trees of templates
=head1 SYNOPSIS
ttree [options] [files]
=head1 DESCRIPTION
The F<ttree> script is used to process entire directory trees containing
template files. The resulting output from processing each file is then
written to a corresponding file in a destination directory. The script
compares the modification times of source and destination files (where
they already exist) and processes only those files that have been modified.
In other words, it is the equivalent of 'make' for the Template Toolkit.
It supports a number of options which can be used to configure
behaviour, define locations and set Template Toolkit options. The
script first reads the F<.ttreerc> configuration file in the HOME
directory, or an alternative file specified in the TTREERC environment
variable. Then, it processes any command line arguments, including
any additional configuration files specified via the B<-f> (file) option.
A typical F<.ttreerc> file might look like this:
src = /home/abw/websrc/doc
dest = /home/abw/public_html
lib = /home/abw/websrc/lib
lib = /usr/local/templates/lib
cfg = /home/abw/.ttree
ignore = \b(CVS|RCS)\b
ignore = ^#
copy = \.(gif|png)$
accept = \.[ah]tml$
The B<src> option indicates a directory containing the template files
to be processed. A list of files may be specified on the command line
and each will be processed in turn, writing the generated output to a
corresponding file in the B<dest> directory. If no files are
explicitly named then all files in the B<src> directory will be
processed. The B<-r> (recurse) option will also cause sub-directories
to be searched for files. A source file is only processed if it has a
later modification time than any corresponding destination file.
Files will always be processed, regardless of modification times, if
they are named explicitly on the command line, or the B<-a> (all)
option is used.
The B<lib> option may be specified any number of times to indicate
directories in which the Template Toolkit should look for other
template files (INCLUDE_PATH) that it may need to INCLUDE or PROCESS,
but don't represent complete documents that should be processed in
their own right (e.g. headers, footers, menu). The B<cfg> directory
specifies the location of additional configuration files that may be
loaded via the B<-f> option.
The B<ignore>, B<copy> and B<accept> options are used to specify Perl
regexen to filter file names. Files that match any of the B<ignore>
options will not be processed. Remaining files that match any of the
B<copy> regexen will be copied to the destination directory. Remaining
files that then match any of the B<accept> criteria are then processed
via the Template Toolkit. If no B<accept> parameter is specified then
all files will be accepted for processing if not already copied or
ignored.
Additional options may be used to set Template Toolkit parameters.
For example:
interpolate
post_chomp
pre_process = header
post_process = footer
perl5lib = /home/abw/lib/perl5
See B<ttree --help> for a summary of options.
=head1 AUTHOR
Andy Wardley E<lt>abw@kfs.orgE<gt>
L<http://www.andywardley.com/|http://www.andywardley.com/>
=head1 VERSION
2.18, distributed as part of the
Template Toolkit version 2.04d, released on 29 August 2001.
=head1 COPYRIGHT
Copyright (C) 1996-2001 Andy Wardley. All Rights Reserved.
Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<tpage|Template::Tools::tpage>