func parsePatterns(pkgPatterns ...string) (packages []string) { for _, pkgPattern := range pkgPatterns { if !strings.HasSuffix(pkgPattern, "...") { packages = append(packages, pkgPattern) continue } parent := strings.TrimSuffix(pkgPattern, "...") parentPkg, err := build.Import(parent, cwd, build.AllowBinary) if err != nil { panic(err) } filepath.Walk(parentPkg.Dir, func(path string, info os.FileInfo, err error) error { if !info.IsDir() { return nil } path = strings.Replace(path, parentPkg.Dir, parent, 1) if _, err := build.Import(path, cwd, build.AllowBinary); err != nil { // This directory doesn't appear to be a go package return nil } packages = append(packages, path) return nil }) } return }
func newApp(args []string) { if len(args) == 0 { errorf("No path given.\nRun 'revel help new' for usage.\n") } importPath := args[0] _, err := build.Import(importPath, "", build.FindOnly) if err == nil { fmt.Fprintf(os.Stderr, "Abort: Import path %s already exists.\n", importPath) return } revelPkg, err := build.Import("github.com/robfig/revel", "", build.FindOnly) if err != nil { fmt.Fprintln(os.Stderr, "Failed to find revel code.") return } appDir := path.Join(revelPkg.SrcRoot, filepath.FromSlash(importPath)) err = os.MkdirAll(appDir, 0777) panicOnError(err, "Failed to create directory "+appDir) skeletonBase = path.Join(revelPkg.Dir, "skeleton") mustCopyDir(appDir, skeletonBase, map[string]interface{}{ // app.conf "AppName": filepath.Base(appDir), "Secret": genSecret(), }) fmt.Fprintln(os.Stdout, "Your application is ready:\n ", appDir) fmt.Fprintln(os.Stdout, "\nYou can run it with:\n revel run", importPath) }
func setApplicationPath(args []string) { var err error importPath = args[0] if filepath.IsAbs(importPath) { errorf("Abort: '%s' looks like a directory. Please provide a Go import path instead.", importPath) } _, err = build.Import(importPath, "", build.FindOnly) if err == nil { errorf("Abort: Import path %s already exists.\n", importPath) } revelPkg, err = build.Import(revel.REVEL_IMPORT_PATH, "", build.FindOnly) if err != nil { errorf("Abort: Could not find Revel source code: %s\n", err) } appPath = filepath.Join(srcRoot, filepath.FromSlash(importPath)) appName = filepath.Base(appPath) basePath = filepath.ToSlash(filepath.Dir(importPath)) if basePath == "." { // we need to remove the a single '.' when // the app is in the $GOROOT/src directory basePath = "" } else { // we need to append a '/' when the app is // is a subdirectory such as $GOROOT/src/path/to/revelapp basePath += "/" } }
// genMath generates the standard math package. func genMath() error { log.Println("Generating math package") // XXX currently just copying the already-generated Go files from the // standard library. We'll need to either translate the assembly files // (manually or automatically), or perhaps use compiler-rt. gopkg, err := build.Import("math", "", 0) if err != nil { return err } const llgopkgpath = "github.com/axw/llgo/pkg/math" llgopkg, err := build.Import(llgopkgpath, "", build.FindOnly) if err != nil { return err } for _, filename := range gopkg.GoFiles { srcfile := filepath.Join(gopkg.Dir, filename) data, err := ioutil.ReadFile(srcfile) if err != nil { return err } dstfile := filepath.Join(llgopkg.Dir, filename) err = ioutil.WriteFile(dstfile, data, os.FileMode(0644)) if err != nil { return err } log.Printf("- %s", filename) } return nil }
// genSyscall generates the standard syscall package. func genSyscall() error { log.Println("Generating syscall package") // XXX currently just copying the already-generated Go files from the // standard library. In the future, we'll just copy the handwritten // bits and generate the other stuff using a pure-Go package. gopkg, err := build.Import("syscall", "", 0) if err != nil { return err } const llgopkgpath = "github.com/axw/llgo/pkg/syscall" llgopkg, err := build.Import(llgopkgpath, "", build.FindOnly) if err != nil { return err } for _, filename := range gopkg.GoFiles { srcfile := filepath.Join(gopkg.Dir, filename) data, err := ioutil.ReadFile(srcfile) if err != nil { return err } dstfile := filepath.Join(llgopkg.Dir, filename) err = ioutil.WriteFile(dstfile, data, os.FileMode(0644)) if err != nil { return err } log.Printf("- %s", filename) } return nil }
// findSrcPaths uses the "go/build" package to find the source root for Revel // and the app. func findSrcPaths(importPath string) (revelSourcePath, appSourcePath string) { var ( gopaths = filepath.SplitList(build.Default.GOPATH) goroot = build.Default.GOROOT ) if len(gopaths) == 0 { ERROR.Fatalln("GOPATH environment variable is not set. ", "Please refer to http://golang.org/doc/code.html to configure your Go environment.") } if ContainsString(gopaths, goroot) { ERROR.Fatalf("GOPATH (%s) must not include your GOROOT (%s). "+ "Please refer to http://golang.org/doc/code.html to configure your Go environment.", gopaths, goroot) } appPkg, err := build.Import(importPath, "", build.FindOnly) if err != nil { ERROR.Fatalln("Failed to import", importPath, "with error:", err) } revelPkg, err := build.Import(REVEL_IMPORT_PATH, "", build.FindOnly) if err != nil { ERROR.Fatalln("Failed to find Revel with error:", err) } return revelPkg.SrcRoot, appPkg.SrcRoot }
func setAppPaths(args []string) { var err error importPath := args[0] if filepath.IsAbs(importPath) { errorf("Abort: '%s' looks like a directory. Please provide a Go import path instead.", importPath) } _, err = build.Import(importPath, "", build.FindOnly) if err == nil { errorf("Abort: Import path %s already exists.\n", importPath) } armadilloPkg, err2 := build.Import(IMPORT_PATH, "", build.FindOnly) if err2 != nil { errorf("Abort: Could not find Armadillo source code: %s\n", err) } appPath = filepath.Join(srcRoot, filepath.FromSlash(importPath)) appName = filepath.Base(appPath) skeletonPath = filepath.Join(armadilloPkg.Dir, "skeleton") templateData = map[string]interface{}{ "AppName": appName, } }
func buildLlgo() error { log.Println("Building llgo") pkg, err := build.Import(gollvmpkgpath, "", build.FindOnly) if err != nil { return err } cgoCflags := fmt.Sprintf("%s -I %s/../include", llvmcflags, pkg.Dir) cgoLdflags := fmt.Sprintf("%s -Wl,-L%s -lLLVM-%s", llvmldflags, llvmlibdir, llvmversion) var output []byte ldflags := "-r " + llvmlibdir args := []string{"get", "-ldflags", ldflags} llvmtag := "llvm" + llvmversion if strings.HasSuffix(llvmversion, "svn") { llvmtag = "llvmsvn" } args = append(args, []string{"-tags", llvmtag}...) args = append(args, llgopkgpath) cmd := exec.Command("go", args...) cmd.Env = os.Environ() cmd.Env = append(cmd.Env, "CGO_CFLAGS="+cgoCflags) cmd.Env = append(cmd.Env, "CGO_LDFLAGS="+cgoLdflags) output, err = cmd.CombinedOutput() if err != nil { fmt.Fprintf(os.Stderr, "%s\n", string(output)) return err } pkg, err = build.Import(llgopkgpath, "", build.FindOnly) if err != nil { return err } llgobin = path.Join(pkg.BinDir, "llgo") // If the user did not specify -triple on the command // line, ask llgo for it now. if triple == "" { output, err = exec.Command(llgobin, "-print-triple").CombinedOutput() if err != nil { fmt.Fprintf(os.Stderr, "%s\n", string(output)) return err } triple = strings.TrimSpace(string(output)) } if err = initGOVARS(triple); err != nil { return err } log.Printf("GOARCH = %s, GOOS = %s", GOARCH, GOOS) log.Printf("Built %s", llgobin) return nil }
func init() { var err error govdep1, err = build.Import("github.com/brianm/govdep1", ".", 0) if err != nil { panic(err) } govdep2, err = build.Import("bitbucket.org/xnio/govdep2", ".", 0) if err != nil { panic(err) } }
func newapp(args []string) { if len(args) == 0 { eprintf(newShortDesc) } filepath.Join(peony.GetPeonyPath(), "templates") importPath := args[0] gopath := build.Default.GOPATH if gopath == "" { eprintf("please set the GOPATH\n", importPath) } if filepath.IsAbs(importPath) { eprintf("importpath[%s] looks like the file path.\n", importPath) } if _, err := build.Import(importPath, "", build.FindOnly); err == nil { eprintf("importpath[%s] already exist.\n", importPath) } if _, err := build.Import(peony.PEONY_IMPORTPATH, "", build.FindOnly); err != nil { eprintf("peony source is required.\n") } tmplatesPath := filepath.Join(peony.GetPeonyPath(), "templates") errorsPath := filepath.Join(peony.GetPeonyPath(), "views", "errors") srcPath := filepath.Join(filepath.SplitList(gopath)[0], "src") appPath := filepath.Join(srcPath, filepath.FromSlash(importPath)) if err := os.Mkdir(appPath, 0777); err != nil { eprintf("mdir app dir error, %s\n", err.Error()) } if err := copyDir(tmplatesPath, appPath); err != nil { eprintf("copy dir error, %s\n", err.Error()) } if err := copyDir(errorsPath, filepath.Join(appPath, "app", "views", "errors")); err != nil { eprintf("copy dir error, %s\n", err.Error()) } appName := filepath.Base(filepath.FromSlash(importPath)) param := map[string]string{ "AppName": appName, "ImportPath": importPath, "SecKey": peony.GenSecKey(), } if err := genConfig(appPath, param); err != nil { eprintf("generator configure error, %s\n", err.Error()) } fmt.Println("app already is ready, please execute command: peony run", importPath) }
func Init(importPath string, mode string) { RunMode = mode // Find the user's app path. importPath = strings.TrimRight(importPath, "/") pkg, err := build.Import(importPath, "", build.FindOnly) if err != nil { log.Fatalln("Failed to import", importPath, "with error:", err) } BasePath = pkg.Dir if BasePath == "" { log.Fatalf("Failed to find code. Did you pass the import path?") } AppName = filepath.Base(BasePath) AppPath = path.Join(BasePath, "app") ViewsPath = path.Join(AppPath, "views") ImportPath = importPath // Find the provided resources. revelPkg, err := build.Import("github.com/robfig/revel", "", build.FindOnly) if err != nil { log.Fatalf("Failed to find revel code.") } RevelPath = revelPkg.Dir RevelTemplatePath = path.Join(RevelPath, "templates") // Load application.conf Config, err = LoadConfig(path.Join(BasePath, "conf", "app.conf")) if err != nil { log.Fatalln("Failed to load app.conf:", err) } Config.SetSection(mode) secretStr, err := Config.String("app.secret") if err != nil { log.Fatalln("No app.secret provided.") } secretKey = []byte(secretStr) // Configure logging. TRACE = getLogger("trace") INFO = getLogger("info") WARN = getLogger("warn") ERROR = getLogger("error") for _, hook := range InitHooks { hook() } revelInit = true }
func newApp(args []string) { if len(args) == 0 { errorf("No import path given.\nRun 'revel help new' for usage.\n") } gopath := build.Default.GOPATH if gopath == "" { errorf("Abort: GOPATH environment variable is not set. " + "Please refer to http://golang.org/doc/code.html to configure your Go environment.") } importPath := args[0] if path.IsAbs(importPath) { errorf("Abort: '%s' looks like a directory. Please provide a Go import path instead.", importPath) } _, err := build.Import(importPath, "", build.FindOnly) if err == nil { fmt.Fprintf(os.Stderr, "Abort: Import path %s already exists.\n", importPath) return } revelPkg, err := build.Import(revel.REVEL_IMPORT_PATH, "", build.FindOnly) if err != nil { fmt.Fprintf(os.Stderr, "Abort: Could not find Revel source code: %s\n", err) return } srcRoot := path.Join(filepath.SplitList(gopath)[0], "src") appDir := path.Join(srcRoot, filepath.FromSlash(importPath)) err = os.MkdirAll(appDir, 0777) panicOnError(err, "Failed to create directory "+appDir) skeletonBase = path.Join(revelPkg.Dir, "skeleton") mustCopyDir(appDir, skeletonBase, map[string]interface{}{ // app.conf "AppName": filepath.Base(appDir), "Secret": genSecret(), }) // Dotfiles are skipped by mustCopyDir, so we have to explicitly copy the .gitignore. gitignore := ".gitignore" mustCopyFile(path.Join(appDir, gitignore), path.Join(skeletonBase, gitignore)) fmt.Fprintln(os.Stdout, "Your application is ready:\n ", appDir) fmt.Fprintln(os.Stdout, "\nYou can run it with:\n revel run", importPath) }
func doParseReflect() { buildpkg, err := build.Import("reflect", "", 0) if err != nil { parseReflectResult, parseReflectError = nil, err return } filenames := make([]string, len(buildpkg.GoFiles)) for i, f := range buildpkg.GoFiles { filenames[i] = path.Join(buildpkg.Dir, f) } fset := token.NewFileSet() files, err := parseFiles(fset, filenames) if err != nil { parseReflectResult, parseReflectError = nil, err return } pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe) if err != nil { parseReflectResult, parseReflectError = nil, err return } _, err = types.Check(fset, pkg) if err != nil { parseReflectResult, parseReflectError = nil, err return } parseReflectResult, parseReflectError = pkg, nil return }
func scanDirectory(path, srcDir string) (ret []string, err error) { pkg, err := build.Import(path, srcDir, build.AllowBinary) if err != nil { return ret, err } for _, imp := range pkg.Imports { switch { case isStandardImport(imp): // Ignore standard packages case !build.IsLocalImport(imp): // Add the external package ret = appendPkg(ret, imp) fallthrough default: // Does the recursive walk pkgs, err := scanDirectory(imp, pkg.Dir) if err != nil { return ret, err } ret = appendPkgs(ret, pkgs) } } return ret, err }
// restore checks out the given revision. func restore(dep Dependency) error { rev, ok := restored[dep.root] debugln(rev) debugln(ok) debugln(dep.root) if ok { if rev != dep.Rev { return errors.New("Wanted to restore rev " + dep.Rev + ", already restored rev " + rev + " for another package in the repo") } verboseln("Skipping already restored repo") return nil } debugln("Restoring:", dep.ImportPath, dep.Rev) pkg, err := build.Import(dep.ImportPath, ".", build.FindOnly) if err != nil { // This should never happen debugln("Error finding package "+dep.ImportPath+" on restore:", err) return err } err = dep.vcs.RevSync(pkg.Dir, dep.Rev) if err == nil { restored[dep.root] = dep.Rev } return err }
func NewSubService(log skynet.Logger, servicePath, args, uuid string) (ss *SubService, err error) { ss = &SubService{ ServicePath: servicePath, Args: args, // TODO: proper argument splitting } ss.argv, err = shellquote.Split(args) if err != nil { return } ss.argv = append([]string{"-uuid", uuid}, ss.argv...) //verify that it exists on the local system pkg, err := build.Import(ss.ServicePath, "", 0) if err != nil { return } if pkg.Name != "main" { return } _, binName := path.Split(ss.ServicePath) binPath := filepath.Join(pkg.BinDir, binName) ss.binPath = binPath return }
func defaultPaths() string { pkg, err := build.Import(testPkg, "", build.FindOnly) if err != nil { return "" } return pkg.Dir }
func main() { flag.Parse() if build.Default.GOARCH == "amd64" { ptrKind = "Q" ptrSize = 8 if *useInt64 { intSize = 8 newIntSize = intSize intKind = "Q" } if *fixInt64 { newIntSize = 8 *fixOffset = true } } args := flag.Args() if len(args) == 0 { p, err := build.ImportDir(".", 0) if err != nil { log.Fatal(err) } check(p) } else { for _, arg := range args { p, err := build.Import(arg, "", 0) if err != nil { log.Print(err) continue } check(p) } } }
func init() { var err error example, err = build.Import("github.com/brianm/gov/example", ".", 0) if err != nil { panic(err) } }
func init() { unchecked = make(map[marker]bool) blank = make(map[marker]bool) pkg, err := build.Import(testPackage, "", 0) if err != nil { panic("failed to import test package") } fset := token.NewFileSet() astPkg, err := parser.ParseDir(fset, pkg.Dir, nil, parser.ParseComments) if err != nil { panic("failed to parse test package") } for _, file := range astPkg["main"].Files { for _, comment := range file.Comments { text := comment.Text() pos := fset.Position(comment.Pos()) switch text { case "UNCHECKED\n": unchecked[marker{pos.Filename, pos.Line}] = true case "BLANK\n": blank[marker{pos.Filename, pos.Line}] = true } } } }
func generate(serverPkg, serverType, clientType string) error { cwd, err := os.Getwd() if err != nil { return err } currentDir = cwd methods, imports, err := serverMethods(serverPkg, serverType) if err != nil { return errgo.Mask(err) } localPkg, err := build.Import(".", currentDir, 0) if err != nil { return errgo.Notef(err, "cannot open package in current directory") } arg := templateArg{ CommandLine: strings.Join(flag.Args(), " "), Imports: imports, Methods: methods, PkgName: localPkg.Name, ClientType: clientType, } var buf bytes.Buffer if err := code.Execute(&buf, arg); err != nil { return errgo.Mask(err) } data, err := format.Source(buf.Bytes()) if err != nil { return errgo.Notef(err, "cannot format source") } if err := writeOutput(data, clientType); err != nil { return errgo.Mask(err) } return nil }
func getExportedDeclerationsForPackage(pkgPath string, filename string, blacklist ...string) ([]string, error) { pkg, err := build.Import(pkgPath, ".", 0) if err != nil { return []string{}, err } declarations, err := getExportedDeclarationsForFile(filepath.Join(pkg.Dir, filename)) if err != nil { return []string{}, err } blacklistLookup := map[string]bool{} for _, declaration := range blacklist { blacklistLookup[declaration] = true } filteredDeclarations := []string{} for _, declaration := range declarations { if blacklistLookup[declaration] { continue } filteredDeclarations = append(filteredDeclarations, declaration) } return filteredDeclarations, nil }
func (e *Engine) Run(command []string, filename string, memlimit, timeout int) (crash.Info, error) { pkg, err := build.Import("github.com/bnagy/francis", ".", build.FindOnly) if err != nil { return crash.Info{}, fmt.Errorf("Couldn't find import path: %s", err) } tool := filepath.Join(pkg.Dir, "exploitaben/exploitaben.py") // Construct the command array cmdSlice := []string{tool, "-e", "MallocScribble=1", "-e", "MallocGuardEdges=1"} if e.Timeout > 0 { cmdSlice = append(cmdSlice, []string{"-t", strconv.Itoa(e.Timeout)}...) } cmdSlice = append(cmdSlice, "--") cmdSlice = append(cmdSlice, command...) cmdStr := strings.Join(cmdSlice, " ") cmd := exec.Command(cmdSlice[0], cmdSlice[1:]...) stdout, err := cmd.StdoutPipe() if err != nil { return crash.Info{}, fmt.Errorf("Error creating pipe: %s", err) } if err := cmd.Start(); err != nil { return crash.Info{}, fmt.Errorf("Error launching tool: %s", err) } out, _ := ioutil.ReadAll(stdout) cmd.Wait() return getCrashInfo(out, cmdStr) }
// walk resolves the package import graph from p to its roots. func walk(p string) *Package { pkgs := make(map[string]*Package) var f func(*Package, string) *Package f = func(parent *Package, p string) *Package { if pk, found := pkgs[p]; found { return pk } pk := Package{ Name: p, Parent: parent, } switch p { case "C", "unsafe", "runtime": // don't resolve dependencies, these // packages don't have any, or don't exist. default: pkg, err := build.Import(p, "", 0) if err != nil { log.Fatal(err) } for _, i := range pkg.Imports { pk.Children = append(pk.Children, f(&pk, i)) } } pkgs[p] = &pk return &pk } return f(nil, p) }
func setSkeletonPath(args []string) { var err error if len(args) == 2 { // user specified skeletonName := args[1] _, err = build.Import(skeletonName, "", build.FindOnly) if err != nil { // Execute "go get <pkg>" getCmd := exec.Command(gocmd, "get", "-d", skeletonName) fmt.Println("Exec:", getCmd.Args) getOutput, err := getCmd.CombinedOutput() // check getOutput for no buildible string bpos := bytes.Index(getOutput, []byte("no buildable Go source files in")) if err != nil && bpos == -1 { errorf("Abort: Could not find or 'go get' Skeleton source code: %s\n%s\n", getOutput, skeletonName) } } // use the skeletonPath = filepath.Join(srcRoot, skeletonName) } else { // use the revel default skeletonPath = filepath.Join(revelPkg.Dir, "skeleton") } }
func (c *buildCommand) Package(importPath string) (*build.Package, error) { pkg, err := build.Import(importPath, "", build.FindOnly) if err != nil { return nil, err } return pkg, nil }
func findImport(pkgs map[string][]Node, p string, size float64) { if p == "C" { return } //n := Node{Id:p, Label:p} if _, ok := pkgs[p]; ok { // seen this package before, skip it return } pkg, err := build.Import(p, "", 0) if err != nil { log.Fatal(err) } filter := func(imports []string) []Node { var n []Node for _, p := range imports { n = append(n, Node{Id: p, Label: p}) } return n } pkgs[p] = filter(pkg.Imports) for _, pkg := range pkgs[p] { findImport(pkgs, pkg.Id, size/2) } }
// OpenPackagePath returns a FSBundle which accesses files // in the source directory of the package named by the given // import path. func OpenPackage(import_path string) (Bundle, error) { pkg, err := build.Import(import_path, "", build.FindOnly) if err != nil { return nil, err } return OpenFS(pkg.Dir), nil }
// importPathToNameGoPath finds out the actual package name, as declared in its .go files. // If there's a problem, it falls back to using importPathToNameBasic. func importPathToNameGoPath(importPath string) (packageName string) { if buildPkg, err := build.Import(importPath, "", 0); err == nil { return buildPkg.Name } else { return importPathToNameBasic(importPath) } }
func pkgDir(path string) string { pkg, err := build.Import(path, "", build.FindOnly) if err != nil { return "" } return pkg.Dir }