-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.tcl
993 lines (816 loc) · 23 KB
/
path.tcl
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
# -*- tcl -*- Copyright (c) 2012-2024 Andreas Kupries
# # ## ### ##### ######## ############# #####################
## Path utility commands.
namespace eval ::kettle::path {
namespace export {[a-z]*}
namespace ensemble create
# unable to import kettle::option, circular dependency
namespace import ::kettle::io
namespace import ::kettle::status
}
# # ## ### ##### ######## ############# #####################
## API commands.
proc ::kettle::path::norm {path} {
# full path normalization
return [file dirname [file normalize $path/__]]
}
proc ::kettle::path::strip {path prefix} {
#io trace {STRIP ($path) PREFIX ($prefix) -- [relativesrc $path] -- [string match {../*} [relativesrc $path]]}
if {[string match {../*} [relativesrc $path]]} {
# Ignore a path symlinked to something outside of the project
# directory.
#
# __ATTENTION__: Note that we are accepting symlinks outside
# of the current root, as long as they reference something
# still in the project.
#
# Note how the `io trace` does a strip operation on the
# unnormalized paths to get the proper relative path of the
# borken link for display.
set link [lrange \
[file split $path] \
[llength [file split $prefix]] \
end]
if {[llength $link]} {
io trace { Ignored: [file join {*}$link] links to outside the project}
} else {
io trace { Ignored: $path is outside the project}
}
return -code continue
}
return [file join \
{*}[lrange \
[file split [norm $path]] \
[llength [file split [norm $prefix]]] \
end]]
}
proc ::kettle::path::relativecwd {dst} {
relative [pwd] $dst
}
proc ::kettle::path::relativesrc {dst} {
relative [sourcedir] $dst
}
proc ::kettle::path::relative {base dst} {
# Modified copy of ::fileutil::relative (tcllib)
# Adapted to 8.5 ({*}).
#
# Taking two _directory_ paths, a base and a destination, computes the path
# of the destination relative to the base.
#
# Arguments:
# base The path to make the destination relative to.
# dst The destination path
#
# Results:
# The path of the destination, relative to the base.
# Ensure that the link to directory 'dst' is properly done relative to
# the directory 'base'.
if {[file pathtype $base] ne [file pathtype $dst]} {
return -code error "Unable to compute relation for paths of different pathtypes: [file pathtype $base] vs. [file pathtype $dst], ($base vs. $dst)"
}
set base [norm $base]
set dst [norm $dst]
set save $dst
set base [file split $base]
set dst [file split $dst]
while {[lindex $dst 0] eq [lindex $base 0]} {
set dst [lrange $dst 1 end]
set base [lrange $base 1 end]
if {![llength $dst]} {break}
}
set dstlen [llength $dst]
set baselen [llength $base]
if {($dstlen == 0) && ($baselen == 0)} {
# Cases:
# (a) base == dst
set dst .
} else {
# Cases:
# (b) base is: base/sub = sub
# dst is: base = {}
# (c) base is: base = {}
# dst is: base/sub = sub
while {$baselen > 0} {
set dst [linsert $dst 0 ..]
incr baselen -1
}
set dst [file join {*}$dst]
}
return $dst
}
proc ::kettle::path::sourcedir {{path {}}} {
return [norm [file join [kettle option get @srcdir] $path]]
}
proc ::kettle::path::script {} {
return [norm [kettle option get @srcscript]]
}
proc ::kettle::path::libdir {{path {}}} {
return [norm [file join [kettle option get --lib-dir] $path]]
}
proc ::kettle::path::bindir {{path {}}} {
return [norm [file join [kettle option get --bin-dir] $path]]
}
proc ::kettle::path::incdir {{path {}}} {
return [norm [file join [kettle option get --include-dir] $path]]
}
proc ::kettle::path::mandir {{path {}}} {
return [norm [file join [kettle option get --man-dir] $path]]
}
proc ::kettle::path::htmldir {{path {}}} {
return [norm [file join [kettle option get --html-dir] $path]]
}
proc ::kettle::path::markdowndir {{path {}}} {
return [norm [file join [kettle option get --markdown-dir] $path]]
}
proc ::kettle::path::set-executable {path} {
io trace { !chmod ugo+x $path}
dry-barrier
catch {
file attributes $path -permissions ugo+x
}
return
}
proc ::kettle::path::grep {pattern data} {
return [lsearch -all -inline -glob [split $data \n] $pattern]
}
proc ::kettle::path::rgrep {pattern data} {
return [lsearch -all -inline -regexp [split $data \n] $pattern]
}
proc ::kettle::path::fixhashbang {file shell} {
dry-barrier
set in [open $file rb]
gets $in line
if {![string match "#!*tclsh*" $line]} {
return -code error "No tclsh #! in $file"
}
io trace { !fix hash-bang $shell}
set out [open ${file}.[pid] wb]
io puts $out "#!/usr/bin/env [norm $shell]"
fcopy $in $out
close $in
close $out
file rename -force ${file}.[pid] $file
return
}
proc ::kettle::path::add-top-comment {comment contents} {
set r {}
set done no
foreach line [split $contents \n] {
if {$done || [regexp "^\\s*\#.*$" $line]} {
lappend r $line
continue
}
lappend r $comment
lappend r $line
set done yes
}
join $r \n
}
proc ::kettle::path::tcl-package-file {file} {
# Test files may contain fake provide statements for mocks and
# such. We must not detect them as installable packages.
if {[tcltest-file $file]} {
return 0
}
set contents [c/z [cat $file]]
if {[llength [grep "# @kettle: no packages" $contents]]} {
return 0
}
set provisions [grep {*package provide *} $contents]
if {![llength $provisions]} {
return 0
}
set rfile [relativesrc $file]
io trace { Testing: $rfile}
foreach line $provisions {
io trace { Candidate |$line|}
if {[catch {
lassign $line cmd method pn pv
}]} {
io trace { * Not a list}
continue
}
if {$cmd ne "package"} {
io trace { * $cmd: Not a 'package' command}
continue
}
if {$method ne "provide"} {
io trace { * $method: Not a 'package provide' command}
continue
}
if {[catch {package vcompare $pv 0}]} {
io trace { * $pkgver: Not a version number}
continue
}
if {[llength [rgrep {^\s*package\s+require\s+critcl} $contents]]} {
io trace { * critcl required: Not pure Tcl}
continue
}
io trace { Accepted: $pn $pv @ $rfile}
lappend files $rfile
set here [file dirname $file]
# Look for referenced dependent files.
foreach line [grep {* @owns: *} $contents] {
if {![regexp {#\s+@owns:\s+(.*)$} $line -> path]} continue
lappend files \
[relativesrc \
[file normalize \
[file join $here [string trim $path]]]]
}
# For 'scan'.
kettle option set @predicate [list $files $pn $pv]
return 1
}
# No candidate satisfactory.
return 0
}
proc ::kettle::path::critcl3-package-file {file} {
set contents [c/z [cat $file]]
set provisions [grep {*package provide *} $contents]
if {![llength $provisions]} {
return 0
}
set rfile [relativesrc $file]
io trace { Testing: $rfile}
foreach line $provisions {
io trace { Candidate |$line|}
if {[catch {
lassign $line cmd method pn pv
}]} {
io trace { * Not a list}
continue
}
if {$cmd ne "package"} {
io trace { * $cmd: Not a 'package' command}
continue
}
if {$method ne "provide"} {
io trace { * $method: Not a 'package provide' command}
continue
}
if {[catch {package vcompare $pv 0}]} {
io trace { * $pkgver: Not a version number}
continue
}
# Nearly accepted. Now check if this file asks for critcl.
if {![llength [rgrep {package\s+require\s+critcl\s+3} $contents]]} {
io trace { * critcl 3: Not required}
continue
}
io trace { Accepted: $pn $pv @ $rfile}
# For 'scan'.
kettle option set @predicate [list $file $pn $pv]
return 1
}
# No candidate satisfactory.
return 0
}
proc ::kettle::path::doctools-file {path} {
set test [c/z [cathead $path 1024 -translation binary]]
# anti marker
if {[regexp -- {--- !doctools ---} $test]} { return 0 }
if {[regexp -- "!tcl\.tk//DSL doctools//EN//" $test]} { return 0 }
# marker
if {[regexp "\\\[manpage_begin " $test]} { return 1 }
if {[regexp -- {--- doctools ---} $test]} { return 1 }
if {[regexp -- "tcl\.tk//DSL doctools//EN//" $test]} { return 1 }
# no usable marker
return 0
}
proc ::kettle::path::diagram-file {path} {
set test [c/z [cathead $path 1024 -translation binary]]
# marker
if {[regexp {tcl.tk//DSL diagram//EN//1.0} $test]} { return 1 }
# no usable marker
return 0
}
proc ::kettle::path::tcltest-file {path} {
set test [c/z [cathead $path 1024 -translation binary]]
# marker
if {[regexp {tcl.tk//DSL tcltest//EN//} $test]} { return 1 }
# no usable marker
return 0
}
proc ::kettle::path::teapot-file {path} {
set test [c/z [cathead $path 1024 -translation binary]]
# marker
if {[regexp {tcl.tk//DSL teapot//EN//} $test]} { return 1 }
# no usable marker
return 0
}
proc ::kettle::path::bench-file {path} {
set test [c/z [cathead $path 1024 -translation binary]]
# marker
if {[regexp {tcl.tk//DSL tclbench//EN//} $test]} { return 1 }
# no usable marker
return 0
}
proc ::kettle::path::kettle-build-file {path} {
set test [c/z [cathead $path 100 -translation binary]]
# marker (no anti-markers)
if {[regexp {kettle -f} $test]} { return 1 }
return 0
}
proc ::kettle::path::foreach-file {path pv script} {
upvar 1 $pv thepath
set ex [kettle option get --ignore-glob]
set known {}
lappend waiting $path
while {[llength $waiting]} {
set pending $waiting
set waiting {}
set at 0
while {$at < [llength $pending]} {
set current [lindex $pending $at]
incr at
# Do not follow into parent.
if {[string match *.. $current]} continue
# Ignore what we have visited already.
set c [file dirname [file normalize $current/___]]
if {[dict exists $known $c]} continue
dict set known $c .
# Ignore non-development files.
if {[Ignore $ex $c]} continue
# Expand directories.
if {[file isdirectory $c]} {
lappend waiting {*}[lsort -unique [glob -nocomplain -directory $c * .*]]
continue
}
# Handle files as per the user's will.
set thepath $current
switch -exact -- [catch { uplevel 1 $script } result] {
0 - 4 {
# ok, continue - nothing
}
2 {
# return, abort, rethrow
return -code return
}
3 {
# break, abort
return
}
1 - default {
# error, any thing else - rethrow
return -code error $result
}
}
}
}
return
}
proc ::kettle::path::scan {label root predicate} {
set nroot [sourcedir $root]
io trace {}
io trace {SCAN $label @ [relativesrc $nroot]}
if {![file exists $nroot]} {
io trace { NOT FOUND}
return -code return
}
set result {}
foreach-file $nroot path {
set spath [strip $path $nroot]
#io trace {CHECK $spath}
# General checking, outside of the custom predicates.
# Skip core files: core, and core.\d+
set n [file tail $spath]
if {$n eq "core" || [regexp {^core\.\d+$} $n]} {
io trace { SKIP core dump: $spath}
continue
}
try {
kettle option unset @predicate
if {![uplevel 1 [list {*}$predicate $path]]} continue
io trace { Accepted: $spath}
if {[kettle option exists @predicate]} {
lappend result {*}[kettle option get @predicate]
} else {
lappend result $spath
}
} on error {e o} {
io err { io puts " Skipped: [relativesrc $path] @ $e" }
} finally {
kettle option unset @predicate
}
}
if {![llength $result]} { return -code return }
return [list $nroot $result]
}
proc ::kettle::path::tmpfile {{prefix tmp_}} {
global tcl_platform
return .kettle_$prefix[pid]_[clock seconds]_[clock milliseconds]_[info hostname]_$tcl_platform(user)
}
proc ::kettle::path::tmpdir {} {
# Taken from tcllib fileutil.
global tcl_platform env
set attempdirs [list]
set problems {}
foreach tmp {TMPDIR TEMP TMP} {
if { [info exists env($tmp)] } {
lappend attempdirs $env($tmp)
} else {
lappend problems "No environment variable $tmp"
}
}
switch $tcl_platform(platform) {
windows {
lappend attempdirs "C:\\TEMP" "C:\\TMP" "\\TEMP" "\\TMP"
}
macintosh {
lappend attempdirs $env(TRASH_FOLDER) ;# a better place?
}
default {
lappend attempdirs \
[file join / tmp] \
[file join / var tmp] \
[file join / usr tmp]
}
}
lappend attempdirs [pwd]
foreach tmp $attempdirs {
if { [file isdirectory $tmp] &&
[file writable $tmp] } {
return [file normalize $tmp]
} elseif { ![file isdirectory $tmp] } {
lappend problems "Not a directory: $tmp"
} else {
lappend problems "Not writable: $tmp"
}
}
# Fail if nothing worked.
return -code error "Unable to determine a proper directory for temporary files\n[join $problems \n]"
}
proc ::kettle::path::ensure-cleanup {path} {
::atexit [lambda {path} {
file delete -force $path
} [norm $path]]
}
proc ::kettle::path::c/z {content} {
return [lindex [split $content \x1A] 0]
}
proc ::kettle::path::cat {path args} {
set c [open $path r]
if {[llength $args]} { fconfigure $c {*}$args }
set contents [read $c]
close $c
return $contents
}
proc ::kettle::path::cathead {path n args} {
set c [open $path r]
if {[llength $args]} { fconfigure $c {*}$args }
set contents [read $c $n]
close $c
return $contents
}
proc ::kettle::path::write {path contents args} {
set c [open $path w]
if {[llength $args]} { fconfigure $c {*}$args }
::puts -nonewline $c $contents
close $c
return
}
proc ::kettle::path::write-append {path contents args} {
set c [open $path a]
if {[llength $args]} { fconfigure $c {*}$args }
::puts -nonewline $c $contents
close $c
return
}
proc ::kettle::path::write-prepend {path contents args} {
set new [tmpfile tmp_prepend_]
write-append $new $contents {*}$args
write-append $new [cat $path {*}$args] {*}$args
file rename -force $new $path
return
}
proc ::kettle::path::write-modify {path cmdprefix args} {
set new [tmpfile tmp_modify_]
write $new [{*}$cmdprefix [cat $path {*}$args]] {*}$args
file rename -force $new $path
return
}
proc ::kettle::path::copy-file {src dstdir} {
# Copy single file into destination _directory_
# Fails goal on an existing file.
io puts -nonewline "\tInstalling file \"$src\": "
dry-barrier
if {[catch {
file mkdir $dstdir
file copy $src $dstdir/[file tail $src]
} msg]} {
io err { io puts "FAIL ($msg)" }
status fail "FAIL ($msg)"
} else {
io ok { io puts OK }
}
}
proc ::kettle::path::copy-files {dstdir args} {
# Copy multiple files into a destination _directory_
# Fails goal on an existing file.
foreach src $args {
copy-file $src $dstdir
}
return
}
proc ::kettle::path::remove-path {base path} {
# General uninstallation of a file or directory.
io puts -nonewline "\tUninstalling \"[relative $base ${path}]\": "
dry-barrier
if {[catch {
file delete -force $path
} msg]} {
io err { io puts "FAIL ($msg)" }
status fail
} else {
io ok { io puts OK }
}
}
proc ::kettle::path::remove-paths {base args} {
# General uninstallation of multiple files.
foreach path $args {
remove-path $base $path
}
return
}
proc ::kettle::path::install-application {src dstdir} {
# Install single-file application into destination _directory_.
# a previously existing file is moved out of the way.
set fname [file tail $src]
io puts "Installing application \"$fname\""
io puts " Into [relativesrc $dstdir]"
dry-barrier {
# Simulated run, has its own dry-barrier.
copy-file $src $dstdir
}
# Save existing file, if any.
file delete -force $dstdir/${fname}.old
catch {
file rename $dstdir/${fname} $dstdir/${fname}.old
}
try {
copy-file $src $dstdir
} trap {KETTLE STATUS FAIL} {e o} {
# Failed, restore previous, if any.
catch {
file rename $dstdir/${fname}.old $dstdir/${fname}
}
return {*}$o $e
}
set-executable $dstdir/$fname
return
}
proc ::kettle::path::install-script {src dstdir shell {cmd {}}} {
# Install single-file script application into destination _directory_.
# a previously existing file is moved out of the way.
set fname [file tail $src]
io puts "Installing script \"$fname\""
io puts " Into [relativesrc $dstdir]"
dry-barrier {
# Simulated run, has its own dry-barrier.
copy-file $src $dstdir
}
# Save existing file, if any.
file mkdir $dstdir
file delete -force $dstdir/${fname}.old
catch {
file rename $dstdir/${fname} $dstdir/${fname}.old
}
try {
copy-file $src $dstdir
} trap {KETTLE STATUS FAIL} {e o} {
# Failed, restore previous, if any.
catch {
file rename $dstdir/${fname}.old $dstdir/${fname}
}
return {*}$o $e
}
if {[llength $cmd]} {
{*}$cmd $dstdir/$fname
}
fixhashbang $dstdir/$fname $shell
set-executable $dstdir/$fname
return
}
proc ::kettle::path::install-file-group {label dstdir args} {
# Install multiple files into a destination directory.
# The destination is created to hold the files. The files
# are strongly coupled, i.e. belong together.
io puts "Installing $label"
io puts " Into [relativesrc $dstdir]"
dry-barrier {
# Simulated installation (has its own dry-barrier).
copy-files $dstdir {*}$args
}
set new ${dstdir}-new
set old ${dstdir}-old
# Clean temporary destination. Remove left-overs from previous runs.
file delete -force $new
file mkdir $new
try {
copy-files $new {*}$args
} trap {KETTLE STATUS FAIL} {e o} {
file delete -force $new
return {*}$o $e
}
# Now shuffle old and new things around to put the new into place.
io puts -nonewline { Commmit: }
if {[catch {
file delete -force $old
catch { file rename $dstdir $old }
file rename -force $new $dstdir
file delete -force $old
} msg]} {
io err { io puts "FAIL ($msg)" }
status fail
} else {
io ok { io puts OK }
}
return
}
proc ::kettle::path::install-file-set {label dstdir args} {
# Install multiple files into a destination directory.
# The destination has to exist. The files in the set
# are only loosely coupled. Example: manpages.
io puts "Installing $label"
io puts " Into [relativesrc $dstdir]"
## Consider removal of existing files ...
## Except, for manpages we want to be informed of clashes.
## for others it might make sense ...
copy-files $dstdir {*}$args
return
}
proc ::kettle::path::uninstall-application {src dstdir} {
set fname [file tail $src]
io puts "Uninstall application \"$fname\""
io puts " From [relativesrc $dstdir]"
remove-path $dstdir $dstdir/$fname
return
}
proc ::kettle::path::uninstall-file-group {label dstdir} {
io puts "Uninstalling $label"
io puts " From [relativesrc [file dirname $dstdir]]"
remove-path [file dirname $dstdir] $dstdir
return
}
proc ::kettle::path::uninstall-file-set {label dstdir args} {
# Install multiple files into a destination directory.
# The destination has to exist. The files in the set
# are only loosely coupled. Example: manpages.
io puts "Uninstalling $label"
io puts " From [relativesrc $dstdir]"
## Consider removal of existing files ...
## Except, for manpages we want to be informed of clashes.
## for others it might make sense ...
foreach f $args {
remove-path $dstdir $dstdir/$f
}
return
}
proc ::kettle::path::exec {args} {
pipe line {
# line ends in \n, except possibly at eof.
io puts -nonewline $line
} {*}$args
return
}
proc ::kettle::path::pipe {lv script args} {
upvar 1 $lv line
set stderr [tmpfile pipe_stderr_]
ensure-cleanup $stderr
io trace { PIPE: [T $args]}
if {[kettle option get --dry]} return
set err {}
set pipe [open "|$args 2> $stderr" r]
fconfigure $pipe -translation lf
try {
while {![eof $pipe]} {
if {[gets $pipe line] < 0} continue
if {![eof $pipe]} {
append line \n
}
try {
uplevel 1 $script
} trap {KETTLE} {e o} {
# Rethrow internal signals.
# No report, not a true error.
return {*}$o $e
} on error {e o} {
io err { io puts $e }
break
}
}
} finally {
try {
close $pipe
} on error {e o} {
io err { io puts $e }
}
set err [cat $stderr]
file delete $stderr
}
if {$err eq {}} return
io err { io puts $err }
return
}
proc ::kettle::path::in {path script} {
set here [pwd]
try {
cd $path
uplevel 1 $script
} finally {
cd $here
}
}
proc ::kettle::path::scanup {path cmd} {
io trace {scan up $path ($cmd)}
set path [file normalize $path]
while {1} {
io trace { testing $path}
# Found the proper directory, per the predicate.
if {[{*}$cmd $path]} { return $path }
# Not found, walk to parent
set new [file dirname $path]
# Stop when reaching the root.
if {$new eq $path} { return {} }
if {$new eq {}} { return {} }
# Ok, truly walk up.
set path $new
}
return {}
}
# # ## ### ##### ######## ############# #####################
## Repository type detection, extraction of current revision, ...
proc ::kettle::path::find.git {path} {
scanup $path ::kettle::path::is.git
}
proc ::kettle::path::find.fossil {path} {
scanup $path ::kettle::path::is.fossil
}
proc ::kettle::path::revision.git {path} {
in $path {
try {
set v [::exec {*}[auto_execok git] describe]
} on error {e o} {
set v [lindex [split [dict get $o -errorinfo] \n] 0]
}
}
return [string trim $v]
}
proc ::kettle::path::revision.fossil {path} {
set fossilcmd [auto_execok fossil]
if {[llength $fossilcmd]} {
in $path {
set info [::exec {*}$fossilcmd info]
}
return [lindex [grep {checkout:*} $info] 0 1]
} else {
return Unknown
}
}
proc ::kettle::path::is.git {path} {
set control $path/.git
expr {[file exists $control] && [file isdirectory $control]}
}
proc ::kettle::path::is.fossil {path} {
foreach control {
_FOSSIL_
.fslckout
.fos
} {
set control $path/$control
if {[file exists $control] && [file isfile $control]} {return 1}
}
return 0
}
# # ## ### ##### ######## ############# #####################
## Internal
proc ::kettle::path::dry-barrier {{dryscript {}}} {
if {![kettle option get --dry]} return
# dry run: notify, ...
if {$dryscript eq {}} {
io cyan { io puts {!dry run!} }
} else {
uplevel 1 $dryscript
}
# ... and abort caller.
return -code return
}
proc ::kettle::path::T {words} {
set r {}
foreach w $words {
if {[file exists $w]} {
set w [relativecwd [norm $w]]
}
lappend r $w
}
return $r
}
proc ::kettle::path::Ignore {patterns path} {
set path [file tail $path]
foreach p $patterns {
if {[string match $p $path]} { return 1 }
}
return 0
}
# # ## ### ##### ######## ############# #####################
return