Last active
November 24, 2015 17:57
-
-
Save matipan/cdc19d854110c98a4dbd to your computer and use it in GitHub Desktop.
File analyzer escrito en Go. Esta hecho a lo "bruto", necesita mejoras para incrementar legibilidad y no usar muchas estructuras. De todas maneras, gracias a las goroutines es bastante performante.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Este fue el output desde mi Home, tengan en cuenta que tengo descargado el codigo fuente de Golang, Ruby y Rails | |
asique los resultados de codigo escrito por mi no es este. El programa ignora todo lo que empieza con un "." sea | |
directorio o archivo. | |
Analizando | |
total: 13454 | |
Listo | |
-------------------- | |
Archivos analizados: 78279 | |
Se analizaron 602 distintos tipos de archivos | |
Tiempo tardado: 0.719806418 | |
------------------------ | |
Archivos de interes: | |
Go: 4966 | |
Ruby: 6245 | |
Javascript: 1099 | |
PHP: 520 | |
C: 474 | |
sh/bash: 47 | |
Java: 101 | |
*/ | |
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"strings" | |
"sync" | |
"time" | |
) | |
func RunAnalyzer(files chan string, wg *sync.WaitGroup, stats map[string]int) { | |
for { | |
fn := <-files | |
stats[fn] += 1 | |
wg.Done() | |
} | |
} | |
func interested(langs []string, l string) bool { | |
for _, v := range langs { | |
if v == l { | |
return true | |
} | |
} | |
return false | |
} | |
func main() { | |
lang_interest := []string{"go", "java", "js", "javascript", "ruby", "rb", "sh", "bash", "php", "c"} | |
c, an := 0, 0 | |
fls, stats, files, types := make(map[int]string), make(map[string]int), make(chan string), make(map[string]int) | |
start := time.Now() | |
var wg sync.WaitGroup | |
fmt.Println("Analizando") | |
filepath.Walk(os.Getenv("HOME"), func(path string, info os.FileInfo, err error) error { | |
where := strings.IndexAny(info.Name(), ".") | |
an += 1 | |
if where > 0 && !info.IsDir() { | |
t := strings.Split(info.Name(), ".") | |
l := t[len(t)-1] | |
types[l] += 1 | |
if interested(lang_interest, l) { | |
fls[c] = l | |
c += 1 | |
} | |
} else if where == 0 && info.IsDir() { | |
return filepath.SkipDir | |
} | |
return nil | |
}) | |
fmt.Println("total: ", c) | |
wg.Add(c) | |
for i := 0; i < 1; i++ { | |
go RunAnalyzer(files, &wg, stats) | |
} | |
for _, v := range fls { | |
files <- v | |
} | |
wg.Wait() | |
fmt.Println("Listo") | |
fmt.Println("\n\n\n--------------------") | |
fmt.Println("Archivos analizados:", an) | |
fmt.Println("Se analizaron", len(types), "distintos tipos de archivos") | |
fmt.Println("Tiempo tardado: ", time.Since(start).Seconds()) | |
fmt.Println("------------------------") | |
fmt.Println("Archivos de interes:") | |
fmt.Println( | |
"Go:", stats["go"], | |
"\nRuby:", stats["rb"], | |
"\nJavascript:", stats["js"]+stats["javascript"], | |
"\nPHP:", stats["php"], | |
"\nC:", stats["c"], | |
"\nsh/bash:", stats["bash"]+stats["sh"], | |
"\nJava:", stats["java"], | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment