2015-08-03 12:23:00 +00:00
|
|
|
// Copyright 2015 The go-python Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/gonuts/commander"
|
|
|
|
"github.com/gonuts/flag"
|
|
|
|
)
|
|
|
|
|
|
|
|
func gopyMakeCmdGen() *commander.Command {
|
|
|
|
cmd := &commander.Command{
|
|
|
|
Run: gopyRunCmdGen,
|
2019-02-22 10:33:05 +00:00
|
|
|
UsageLine: "gen <go-package-name> [other-go-package...]",
|
2015-07-24 14:16:31 +00:00
|
|
|
Short: "generate (C)Python language bindings for Go",
|
|
|
|
Long: `
|
2019-02-22 10:33:05 +00:00
|
|
|
gen generates (C)Python language bindings for Go package(s).
|
2015-07-24 14:16:31 +00:00
|
|
|
|
|
|
|
ex:
|
2019-02-22 10:33:05 +00:00
|
|
|
$ gopy gen [options] <go-package-name> [other-go-package...]
|
2015-07-24 14:16:31 +00:00
|
|
|
$ gopy gen github.com/go-python/gopy/_examples/hi
|
|
|
|
`,
|
|
|
|
Flag: *flag.NewFlagSet("gopy-gen", flag.ExitOnError),
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
cmd.Flag.String("vm", "python", "path to python interpreter")
|
2015-07-24 14:16:31 +00:00
|
|
|
cmd.Flag.String("output", "", "output directory for bindings")
|
2019-02-22 10:33:05 +00:00
|
|
|
cmd.Flag.String("name", "", "name of output package (otherwise name of first package is used)")
|
2015-07-24 14:16:31 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func gopyRunCmdGen(cmdr *commander.Command, args []string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
log.Printf("expect a fully qualified go package name as argument\n")
|
|
|
|
return fmt.Errorf(
|
|
|
|
"gopy-gen: expect a fully qualified go package name as argument",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
cmdstr := argStr()
|
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
var (
|
|
|
|
odir = cmdr.Flag.Lookup("output").Value.Get().(string)
|
|
|
|
vm = cmdr.Flag.Lookup("vm").Value.Get().(string)
|
2019-02-22 10:33:05 +00:00
|
|
|
name = cmdr.Flag.Lookup("name").Value.Get().(string)
|
2019-01-11 11:00:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if vm == "" {
|
|
|
|
vm = "python"
|
|
|
|
}
|
2015-07-24 14:16:31 +00:00
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
for _, path := range args {
|
|
|
|
pkg, err := newPackage(path)
|
|
|
|
if name == "" {
|
|
|
|
name = pkg.Name()
|
|
|
|
}
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
2019-02-22 10:33:05 +00:00
|
|
|
return fmt.Errorf("gopy-gen: go/build.Import failed with path=%q: %v", path, err)
|
2015-07-24 14:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
err = genPkg(odir, name, cmdstr, vm)
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|