Tcl Module creation script

Script below converts many installed packages into the Tcl Module format (TIP 189 [L1 ]) built on the trofs package.

I ran it on one of my systems and produced 178 *.tm files.

 package require Tcl 8.5
 package require trofs 0.4
 eval [package unknown] Tcl [package provide Tcl]
 foreach p [package names] {
    if {[string match *::* $p]} {
        # Don't handle this case yet;  TM file names can't contain ::
        continue
    }
    foreach v [package versions $p] {
        set loadScript [package ifneeded $p $v]
        if {[catch {llength $loadScript} l]} {continue}
        if {[lindex $loadScript 0] ne "source"} {continue}
        set file [lindex $loadScript 1]
        if {![file readable $file]} {continue}
        set dir [file dirname $file]
        set tail [file tail $file]
        if {[string match *.tm $tail]} {continue}
        set numFiles [llength [glob -directory $dir *]]
        if {($numFiles == 2)
                && ([catch {glob -directory $dir pkgIndex.tcl}] == 0)} {
            # Installed package is just script + pkgIndex.
            # It's very likely already in suitable TM format
            file copy -force $file $p-$v.tm
            continue
        }
        set f [open $p-$v.tm w]
        puts $f {package require Tcl 8.5}
        puts $f {package require trofs 0.4}
        puts $f [subst -nocommands {
                source [file join [trofs::mount [info script]] $tail]
        }]
        close $f
        trofs::archive $dir $p-$v.tm
    }
 }

See also: