Am Freitag, 29. Mai 2020 12:37:14 UTC+2 schrieb Harald Oehlmann:
I have following script in use, comments starting with #. are not removed, keeps original line numbers, destination directory has to exist.
Roland Frank
proc undebug {sourcedir destdir} {
set exclude_pattern {(pp|undebug|x|test)\.tcl$}
if {![file isdirectory $sourcedir]} {
error "$sourcedir is not a directory"
}
if {![file isdirectory $destdir]} {
error "$destdir is not a directory"
}
set sdir [file normalize $sourcedir]
#puts sdir=$sdir
set ddir [file normalize $destdir]
#puts ddir=$ddir
if {[string equal $sdir $ddir]} {
error "overwrite-protection"
}
set pattern [file join $sdir *.tcl]
set start [string length $sdir]
# exclude trailing /
incr start
foreach ff [glob -nocomplain $pattern] {
#puts \t$exclude_pattern
if {[regexp $exclude_pattern $ff]} {
#puts "skip $ff"
continue
}
#puts ff=$ff
set destfile [file join $ddir [string range $ff $start end]]
#puts "->$destfile"
unde $ff [file join $ddir $destfile]
}
}
proc unde {sfn dfn} {
if {[file exists $dfn]} {
file stat $dfn ds
file stat $sfn ss
if {$ds(mtime) > $ss(mtime)} {
#puts "$sfn unmodified: skip"
return
}
}
if {[string equal $sfn $dfn]} {
puts "unde $sfn $dfn >skipped"
return
}
puts "unde: using $sfn"
set fh [open $sfn r]
set cc [read $fh]
close $fh
if {[catch {set fh [open $dfn w]} err]} {
puts "error: $err"
puts "skip file $dfn"
} else {
set tmp0 [regsub -all -line {^\s*borg\s+log.*$} $cc {}]
#keep line numbering when removing comments
set tmp [regsub -all -line {^\s*#[^\.].*$} $tmp0 {}]
# remove comment lines
set tmp0 [regsub -all -line {^\s*#[^\.].*$} $tmp {}]
# remove puts without file
# but do not remove puts [time { ...}]
#
if {1} {
#set tmp [regsub -all -line {^\s*puts(\s+-nonewline){0,0}\s+[^\$\[].*$} $tmp0 {}]
set tmp [regsub -all -line {^\s*puts(\s+-nonewline){0,0}\s+[^\$\[].*$} $tmp0 {}]
puts $fh [regsub {\n+$} $tmp0 {}]
} else {
set tmp [regsub -all -line {^\s*puts(\s+-nonewline){0,0}\s+[^\$\[].*$} $tmp0 {}]
puts $fh [regsub {\n+$} $tmp {}]
}
# get vimlines
foreach line [split $cc \n] {
if {[regexp {^# vi\:} $line]} {
puts $fh $line
}
}
close $fh
}
}