#!/usr/sfw/bin/tclsh8.3 proc status {} { foreach barra {| / - \\ |} { puts -nonewline \033\[1m$barra\033\[0m flush stdout after 2 puts -nonewline \b flush stdout } } proc DistroAnalyzer_error { mensagem } { puts "DistroAnalyzer.tcl MSG: $mensagem" exit 1 } proc DistroAnalyzer_help {} { global dirtmp puts "+---------------------------------------------------------------------------+ | Copyright(C) 2004 Marcelo Leal. | | This program is free software; you can redistribute it and/or modify | | it under the terms of the GNU General Public License as published by | | the Free Software Foundation; either version 2 of the License, or | | (at your option) any later version. | | | | This program is distributed in the hope that it will be useful, | | but WITHOUT ANY WARRANTY; without even the implied warranty of | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | GNU General Public License for more details. | | | | You should have received a copy of the GNU General Public License | | along with this program; if not, write to the Free Software | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | | | |---------------------------------------------------------------------------| | DistroAnalyzer.TCL MSG: | | Usage: | | (1) To generate a XML File: | | DistroAnalyzer.tcl --distro | | --version | | --releasename | | --maturity | | --text-file | | (2) To generate a PAJEK File: | | DistroAnalyzer.tcl --xml-file | | (3) To generate a DAMAGE File: | | DistroAnalyzer.tcl --pajek-file | +---------------------------------------------------------------------------+" if [ catch { file delete -force /tmp/$dirtmp } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } exit 0 } proc get_fhw { arquivo } { if [ catch { set fh [ open $arquivo { RDWR CREAT } ] } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } return $fh } proc get_fhr { arquivo } { if [ catch { set fh [ open $arquivo RDONLY ] } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } return $fh } proc header_pjk {} { global argv fhw1 fhw2 egrep wc broken_depends arquivo1 arquivo2 cat script_awk set id [ set id_arcs 1 ] if [ catch { exec $egrep "\\" [string tolower [ lindex $argv 1 ] ] | $wc -l } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } set fhr2 [ get_fhr [string tolower [ lindex $argv 1 ] ] ] set firstline [ gets $fhr2 ] puts $fhw1 "*vertices [ expr $cod_retorno - 1 ]" foreach line [ split [ read $fhr2 ] \n ] { if { ![ string first " " $line ] } { regsub -- {(.*)} $line "" cod_retorno regsub -- {(.*)} $cod_retorno "" package puts $fhw1 "$id \"$package\" 0 0 0 x_fact 2.0 y_fact 2.0 ic Goldenrod bc Black" incr id } } puts "The follow packages: ( $broken_depends ) the names do not match..." close $fhr2 close $fhw1 if [ catch { exec $cat $arquivo2 >> $arquivo1 } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } if [ catch { file rename -force $arquivo1 ./packages.net } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } if [ catch { file delete $script_awk } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } } proc body_pjk {} { global argv cat fhw2 arquivo1 egrep sed script_awk tail bd_arcs broken_depends arquivo2 set id 0 set broken_depends "" puts $fhw2 *Arcs if [ catch { exec $egrep "\\" [string tolower [ lindex $argv 1 ] ] | $sed 1d } resultado ] { DistroAnalyzer_error "Error! $resultado" } foreach line [ split $resultado \n ] { regsub -- {(.*)} $line "" cod_retorno regsub -- {(.*)} $cod_retorno "" indice lappend bd $indice } set fhr2 [ get_fhr [string tolower [ lindex $argv 1 ] ] ] foreach line [ split [ read $fhr2 ] \n ] { if { [ string first " " $line ] >= 0 } { incr id } if { [ string first " " $line ] >= 0 } { set lista_dependencias [ split $line ] foreach package $lista_dependencias { if { ![ string equal "" $package ] } { if { ![ string equal "" $package ] } { if { ![ string is space $package ] } { set nr_package [lsearch $bd "$package"] if [ string equal $nr_package "-1" ] { set nr_package $package lappend broken_depends $package } else { incr nr_package } puts $fhw2 "$id $nr_package 1 c Green" } } } } } status } close $fhw2 } proc gera_script {} { global script_awk chmod set script_awk /tmp/script-[expr rand()] set fhw [ get_fhw $script_awk ] puts $fhw "#!/bin/bash" puts $fhw "awk -F\":\" '\{print \$1\}' \$1" close $fhw if [ catch { exec $chmod 700 $script_awk } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } } proc body_norm { fhr2 } { global argv fhw2 arquivo1 arquivo2 egrep if [ catch { exec $egrep "Package: " [ string tolower [ lindex $argv 9 ] ] } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } foreach line [ split [ read $fhr2 ] \n ] { if { [ string first "Package:" $line ] < 0 } { if { [ string first "Version:" $line ] < 0 } { set lista_dependencias [ split $line ] set lista_aux "" foreach package $lista_dependencias { if { [ lsearch $lista_aux $package ] < 0 } { lappend lista_aux $package if { ![ string equal $package "|" ] } { if { [ string first "Depends:" $package ] < 0 } { if { [ string first "(" $package ] < 0 } { if { [ string first ")" $package ] < 0 } { regsub -- {,(.*)} $package "" package_norm if { [ string first $package_norm $cod_retorno ] >= 0 } { puts -nonewline $fhw2 "$package_norm " flush stdout } } } } else { puts -nonewline $fhw2 "$package " flush stdout } } } } } else { puts $fhw2 "$line" } } else { puts $fhw2 "" puts $fhw2 "$line" } status } puts $fhw2 "" close $fhw2 close $fhr2 } proc header_xml { name version releasename maturity } { global fhr1 fhw1 puts $fhw1 "" puts $fhw1 "" puts $fhw1 "" puts $fhw1 " $name" puts $fhw1 " $version" puts $fhw1 " $releasename" puts $fhw1 " $maturity" } proc first_package {} { global id fhr1 fhw1 # twice... set line [ gets $fhr1 ] set line [ gets $fhr1 ] regsub -- {(.*): } $line "" package puts $fhw1 " " puts $fhw1 " $package" incr id } proc body_xml {} { global argv id fhr1 fhw1 arquivo1 arquivo2 foreach line [ split [ read $fhr1 ] \n ] { if { [ string first "Package:" $line ] >= 0 } { regsub -- {(.*): } $line "" package puts $fhw1 " " puts $fhw1 " " puts $fhw1 " $package" incr id } else { if { [ string first "Version:" $line ] >= 0 } { regsub -- {(.*)Version:} $line "" version puts $fhw1 " $version" puts $fhw1 " $id" } else { if { [ string first "Depends:" $line ] >= 0 } { regsub -- {(.*)Depends:} $line "" depends puts $fhw1 " $depends" } } } status } puts $fhw1 " " puts $fhw1 "" puts "GIX.TCL MSG: $id pacotes processados" close $fhr1 close $fhw1 if [ catch { file rename -force $arquivo1 ./packages-[string tolower [ lindex $argv 1 ] ].xml } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } } proc gera_xml {} { global argv argc header_xml [ string tolower [ lindex $argv 1 ] ] [ string tolower [ lindex $argv 3 ] ] [ string tolower [ lindex $argv 5 ] ] [ string tolower [ lindex $argv 7 ] ] puts "STEP 1: Normalizing..." set fhr2 [ get_fhr [ string tolower [ lindex $argv 9 ] ] ] body_norm $fhr2 # Hack to the first keyword. first_package puts "STEP 2: Generating the XML file..." body_xml } proc gera_pjk {} { puts "-> Wait, " gera_script puts "STEP 1: Generating the PAJEK file (body section)..." body_pjk puts "STEP 2: Generating the PAJEK file (header section)..." header_pjk } proc gera_dam {} { global argc argv contador pkg_names nr_pkgs flag qtd bd bd_cycle bd_cycle_temp ciclos fhw1 fhw2 fhw3 fhw4 arquivo1 arquivo4 sed puts "STEP 1: Preparing ..." set fhr2 [ get_fhr [string tolower [ lindex $argv 1 ] ] ] gera_parts $fhr2 puts "STEP 2: Generating the file ..." for {set x 1 } { $x < $nr_pkgs } { incr x } { set flag "primeiro_ciclo" set qtd 0 set bd "" set bd_cycle "" set ciclos "" set bd_cycle_temp "" lappend bd $x seek $fhw3 0 start puts $x qtd_depends $x $fhw3 puts $fhw1 "Damage for the package: [ lindex $pkg_names $x ] = $qtd" puts $fhw4 "" status } puts $fhw4 "---> $contador cycles" close $fhw1 close $fhw2 close $fhw3 close $fhw4 if [ catch { file rename -force $arquivo1 ./damage.txt } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } if [ catch { exec $sed /^\$/d $arquivo4 > ./cycles.txt } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } } proc args_wrapper {} { global argv argc fhw1 fhw2 fhr1 if [ string equal "" [ lindex $argv 0 ] ] { DistroAnalyzer_help } switch -exact -- [ string tolower [ lindex $argv 0 ] ] { --distro { switch -exact -- [ string tolower [ lindex $argv 2 ] ] { --version { switch -exact -- [ string tolower [ lindex $argv 4 ] ] { --releasename { switch -exact -- [ string tolower [ lindex $argv 6 ] ] { --maturity { switch -exact -- [ string tolower [ lindex $argv 8 ] ] { --text-file { gera_xml } default { DistroAnalyzer_help } } } default { DistroAnalyzer_help } } } default { DistroAnalyzer_help } } } default { DistroAnalyzer_help } } } --xml-file { gera_pjk } --pajek-file { gera_dam } default { DistroAnalyzer_help } } } proc gera_parts { fhr2 } { global nr_pkgs fhw2 fhw3 pkg_names set firstline [ gets $fhr2 ] if ![ string equal "*vertices" [ lindex [ split $firstline ] 0 ] ] { DistroAnalyzer_error "Error! Bad file..." } set line "" while { ![string equal "*Arcs" $line ] } { set line [ gets $fhr2 ] puts $fhw2 $line lappend pkg_names [ lindex [ split $line \" ] 1 ] incr nr_pkgs } foreach line [ split [ read $fhr2 ] \n ] { puts $fhw3 $line } close $fhr2 } proc qtd_depends { pkg fhw3 } { global argv qtd arquivo1 pkg_names bd bd_cycle bd_cycle_temp ciclos contador fhw4 flag foreach line [ split [ read $fhw3 ] \n ] { if [ string equal $pkg [ lindex [ split $line ] 1 ] ] { if [ string equal [ lindex $bd 0 ] [ lindex [ split $line ] 0 ] ] { if [ string equal $pkg [ lindex [ split $line ] 0 ] ] { lappend bd_cycle [ lindex $bd 0 ] } if [ string equal $flag "primeiro_ciclo" ] { incr contador set name_package [ string toupper [ lindex $pkg_names [ lindex $bd 0 ] ] ] puts -nonewline $fhw4 "$contador. The package $name_package have dependence cycle with the packages: " set flag "" } for {set i 0 } { $i < [ llength $bd_cycle ] } { incr i } { set indice [ lsearch $ciclos [ lindex $bd_cycle $i ] ] if [ string equal $indice "-1" ] { puts -nonewline $fhw4 " [ lindex $pkg_names [ lindex $bd_cycle $i ] ]," lappend ciclos [ lindex $bd_cycle $i ] } } } set indice [lsearch $bd [ lindex [ split $line ] 0 ] ] if [ string equal $indice "-1" ] { lappend bd [ lindex [ split $line ] 0 ] lappend bd_cycle [ lindex [ split $line ] 0 ] incr qtd if { [ string equal "recursive" [ string tolower [ lindex $argv 2 ] ] ] } { seek $fhw3 0 start qtd_depends [ lindex [ split $line ] 0 ] $fhw3 set bd_cycle_temp [ lreplace $bd_cycle [ expr [ llength $bd_cycle ] -1 ] [ expr [ llength $bd_cycle ] -1 ] ] set bd_cycle $bd_cycle_temp } } } } } # ---------------------------- # Main # ---------------------------- # Global variables lappend pkg_names "0" set id 0 set nr_pkgs 0 set contador 0 set qtd 0 set flag "primeiro_ciclo" set bd "" set bd_cycle "" set bd_cycle_temp "" set ciclos "" # External commands set sed "/usr/local/bin/sed" set egrep "/usr/local/bin/egrep" set cat "/usr/local/bin/cat" set chmod "/usr/bin/chmod" set tail "/usr/local/bin/tail" set wc "/usr/local/bin/wc" set sed "/usr/bin/sed" set dirtmp .da[expr rand()] if [ catch { file mkdir /tmp/$dirtmp } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } set arquivo1 /tmp/$dirtmp/DistroAnalyzer[expr rand()] set arquivo2 /tmp/$dirtmp/DistroAnalyzer[expr rand()] set arquivo3 /tmp/$dirtmp/DistroAnalyzer[expr rand()] set arquivo4 /tmp/$dirtmp/DistroAnalyzer[expr rand()] set fhw1 [ get_fhw $arquivo1 ] set fhw2 [ get_fhw $arquivo2 ] set fhw3 [ get_fhw $arquivo3 ] set fhw4 [ get_fhw $arquivo4 ] set fhr1 [ get_fhr $arquivo2 ] # The game... args_wrapper if [ catch { file delete -force /tmp/$dirtmp } cod_retorno ] { DistroAnalyzer_error "Error! $cod_retorno" } puts "done."