Ticket #27244: port.tcl-space.patch

File port.tcl-space.patch, 5.0 KB (added by outis, 13 years ago)

adds "space" action

  • port.

    old new  
    152152proc _const value {
    153153    return $value
    154154}
    155155
    156156
     157# Format an integer representing bytes using binary units (KiB, MiB, GiB)
     158proc bytesize {siz {unit {}}} {
     159    set scale 0
     160    switch -- $unit {
     161        K {
     162            set unit "KiB"
     163            #set scale 1024.0
     164            set siz [expr $siz / 1024.0]
     165        }
     166        M {
     167            set unit "MiB"
     168            #set scale 1048576.0
     169            set siz [expr $siz / 1048576.0]
     170        }
     171        B { }
     172        default {
     173            if {$siz > 0x400} {
     174                if {$siz > 0x100000} {
     175                    if {$siz > 0x40000000} {
     176                        set unit "GiB"
     177                        set siz [expr $siz / 1073741824.0]                   
     178                    } else {
     179                        set unit "MiB"
     180                        set siz [expr $siz / 1048576.0]
     181                    }                   
     182                } else {
     183                    set unit "KiB"
     184                    #set scale 1024.0
     185                    set siz [expr $siz / 1024.0]
     186                }
     187            } else {
     188                set unit "B  "
     189            }
     190        }
     191    }
     192    if {[expr round($siz)] != $siz} {
     193        set siz [format {%.3f} $siz]
     194    }
     195    return "$siz $unit"
     196}
     197
     198
    157199
    158200# Produce an error message, and exit, unless
    159201# we're handling errors in a soft fashion, in which
    160202# case we continue
    161203proc fatal_softcontinue s {
     
    30963138    registry::close_file_map
    30973139
    30983140    return $status
    30993141}
    31003142
     3143# Alternative: calc space from $portdbpath/software/$port/$portversion
     3144proc action_space {action portlist {opts {}}} {
     3145    global private_options global_options
     3146    set status 0
     3147    set spaceall 0.0
     3148    require_portlist portlist
     3149    if {[array names private_options -exact size_unit] == {}} {
     3150        array set  private_options {size_unit {}}
     3151    }
     3152    foreachport $portlist {
     3153        set space 0.0
     3154        set files [registry::port_registered $portname]
     3155        if { $files != 0 } {
     3156            if { [llength $files] > 0 } {
     3157                foreach file $files {
     3158                    catch {
     3159                        set space [expr $space + [file size $file] ]
     3160                    }
     3161                }
     3162                #puts [array get variations]
     3163                set msg "[bytesize $space $private_options(size_unit)] $portname"
     3164                if { $portversion != {} } {
     3165                    set msg "$msg @$portversion"
     3166                }
     3167                #set msg "[bytesize $space $private_options(size_unit)] [regsub {/} $portspec(fullname) { @}]"
     3168                puts $msg
     3169                set spaceall [expr $space + $spaceall]
     3170            } else {
     3171                puts "Port $portname does not contain any file or is not active."
     3172            }
     3173        } else {
     3174            puts "Port $portname is not installed."
     3175        }
     3176    }
     3177    if {[llength $portlist] > 1} {
     3178        puts "[bytesize $spaceall] total"
     3179    }
     3180    return $status
     3181}
     3182
    31013183proc action_variants { action portlist opts } {
    31023184    global global_variations
    31033185    set status 0
    31043186    if {[require_portlist portlist]} {
    31053187        return 1
     
    37713853    uninstall   [list action_uninstall      [ACTION_ARGS_PORTS]] \
    37723854    \
    37733855    installed   [list action_installed      [ACTION_ARGS_PORTS]] \
    37743856    outdated    [list action_outdated       [ACTION_ARGS_PORTS]] \
    37753857    contents    [list action_contents       [ACTION_ARGS_PORTS]] \
     3858    space       [list action_space          [ACTION_ARGS_PORTS]] \
    37763859    dependents  [list action_dependents     [ACTION_ARGS_PORTS]] \
    37773860    rdependents [list action_dependents     [ACTION_ARGS_PORTS]] \
    37783861    deps        [list action_deps           [ACTION_ARGS_PORTS]] \
    37793862    rdeps       [list action_deps           [ACTION_ARGS_PORTS]] \
    37803863    variants    [list action_variants       [ACTION_ARGS_PORTS]] \
     
    40284111                        set global_options(ports_autoclean) yes
    40294112                    }
    40304113                    k {
    40314114                        set global_options(ports_autoclean) no
    40324115                    }
     4116                    B {
     4117                        set private_options(size_unit) B
     4118                    }
     4119                    K {
     4120                        set private_options(size_unit) K
     4121                    }
     4122                    M {
     4123                        set private_options(size_unit) M
     4124                    }
    40334125                    t {
    40344126                        set global_options(ports_trace) yes
    40354127                    }
    40364128                    y {
    40374129                        set global_options(ports_dryrun) yes
     
    43794471array set ui_options        {}
    43804472array set global_options    {}
    43814473array set global_variations {}
    43824474
    43834475# Global options private to this script
    4384 array set private_options {}
     4476array set private_options {size_unit {}}
    43854477
    43864478# Make sure we get the size of the terminal
    43874479# We do this here to save it in the boot_env, in case we determined it manually
    43884480term_init_size
    43894481