Last active
December 15, 2015 07:49
-
-
Save nevir/5226483 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"go/build" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func main() { | |
processSequential() // 0.23s user 0.12s system 98% cpu 0.356 total | |
//processParallel() // 0.29s user 0.20s system 112% cpu 0.432 total | |
} | |
func processParallel() { | |
sem := make(chan bool, 1) | |
count := 0 | |
for _, path := range packagePaths() { | |
go func(path string) { | |
processPackage(path) | |
sem <- true | |
}(path) | |
count++ | |
} | |
for i := 0; i < count; i++ { | |
<-sem | |
} | |
} | |
func processSequential() { | |
for _, path := range packagePaths() { | |
processPackage(path) | |
} | |
} | |
func isPathDir(path string) bool { | |
f, err := os.Open(path) | |
defer f.Close() | |
if err != nil { | |
fmt.Printf("Error when opening path %v: %v\n", path, err) | |
return false | |
} | |
info, err := f.Stat() | |
if err != nil { | |
fmt.Printf("Error when statting path %v: %v\n", path, err) | |
return false | |
} | |
return info.IsDir() | |
} | |
func fullGlob(basePath string) []string { | |
result := []string{} | |
for { | |
basePath = basePath + "/[^.]*" | |
paths, err := filepath.Glob(basePath) | |
if err != nil { | |
fmt.Printf("Error when globbing path %v: %v\n", basePath, err) | |
break | |
} | |
if len(paths) == 0 { | |
break | |
} | |
for _, path := range paths { | |
if isPathDir(path) { | |
result = append(result, path) | |
} | |
} | |
} | |
return result | |
} | |
func packagePaths() []string { | |
result := []string{} | |
for _, srcDir := range build.Default.SrcDirs() { | |
for _, path := range fullGlob(srcDir) { | |
if isPathDir(path) { | |
result = append(result, path) | |
} | |
} | |
} | |
return result | |
} | |
func packageInfo(path string) *build.Package { | |
result, err := build.Default.ImportDir(path, 0) | |
if err != nil { | |
fmt.Printf("Failed to import package %v: %v\n", path, err) | |
return nil | |
} | |
return result | |
} | |
func processPackage(packagePath string) { | |
// fmt.Printf("processPackage(%v)\n", packagePath) | |
info, err := build.Default.ImportDir(packagePath, 0) | |
if err != nil { | |
fmt.Printf("Failed to import package %v: %v\n", packagePath, err) | |
return | |
} | |
targetPath := "out/" + strings.Replace(packagePath, "/", "-", -1) + ".txt" | |
infoFile, err := os.Create(targetPath) | |
if err != nil { | |
fmt.Printf("Failed to open file for writing %v: %v\n", targetPath, err) | |
return | |
} | |
data, _ := json.MarshalIndent(info, "", " ") | |
fmt.Fprint(infoFile, string(data)) | |
infoFile.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment