Skip to content

Instantly share code, notes, and snippets.

@juanjux
Created June 13, 2017 12:13
Show Gist options
  • Save juanjux/57389ebad9db18bdd64fcc8d4ae1ba9c to your computer and use it in GitHub Desktop.
Save juanjux/57389ebad9db18bdd64fcc8d4ae1ba9c to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"os"
"time"
"path/filepath"
"io/ioutil"
"github.com/bblfsh/sdk/protocol"
"google.golang.org/grpc"
)
var pythonMatch = "/usr/lib/python3.6/*.py"
// Note: start the server with:
// docker run --privileged -p 9432:9432 --name bblfsh bblfsh/server
func main() {
// Connect to the running server
conn, err := grpc.Dial("0.0.0.0:9432", grpc.WithTimeout(time.Second*2),
grpc.WithInsecure())
if err != nil {
os.Exit(1)
}
client := protocol.NewProtocolServiceClient(conn)
pyfiles, err := filepath.Glob(pythonMatch)
if err != nil {
fmt.Println("filepath.Glob failed for ", pythonMatch)
return
}
for _, fname := range pyfiles {
fmt.Println(fname)
content, err := ioutil.ReadFile(fname)
if err != nil {
fmt.Println("Error: could not read file: ", fname)
continue
}
contentstr := string(content)
req := &protocol.ParseUASTRequest{Filename: fname,
Content: contentstr,
Language: "python"}
resp, err := client.ParseUAST(context.TODO(), req)
if err != nil {
fmt.Println("ParseUAST failed for: ", fname)
}
// FIXME: Crashes sometimes here, check resp.UAST? :(
data, err := resp.UAST.Marshal()
if err != nil {
fmt.Println("Error: failed to export file: ", fname)
continue
}
_, f := filepath.Split(fname)
err = ioutil.WriteFile(f + ".pb", data, 00644)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment