1 Star 0 Fork 0

kaylee595 / go-wav2lipEx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
python.go 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
kaylee595 提交于 2023-12-15 15:12 . first
package python
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
)
const (
pyExt = ".py"
)
type Python struct {
BinPath string
Ext string
Envs []string
}
type Option func(*Python)
func WithExt(ext string) Option {
return func(p *Python) {
p.Ext = ext
}
}
func WithEnvs(envs ...string) Option {
return func(p *Python) {
p.Envs = envs
}
}
func New(pythonFile string, options ...Option) *Python {
binPathAbs, _ := filepath.Abs(pythonFile)
p := &Python{
BinPath: binPathAbs,
Ext: pyExt,
}
for _, option := range options {
option(p)
}
return p
}
func (p *Python) ExecCtx(ctx context.Context, codeDir, pyFile string, args ...string) error {
cmd := exec.CommandContext(ctx, p.BinPath, append([]string{fmt.Sprintf("%s%s", pyFile, p.Ext)}, args...)...)
cmd.Dir = codeDir
cmd.Env = p.Envs
errContext := new(bytes.Buffer)
errContext.WriteString(fmt.Sprintf("cmd: [ %s ]\n", cmd.String()))
cmd.Stderr = io.MultiWriter(errContext, os.Stderr)
cmd.Stdout = io.MultiWriter(errContext, os.Stdout)
err := cmd.Run()
if err != nil {
err = errors.New(errContext.String() + "\n\n" + err.Error())
}
return err
}
1
https://gitee.com/kaylee595/go-wav2lipEx.git
git@gitee.com:kaylee595/go-wav2lipEx.git
kaylee595
go-wav2lipEx
go-wav2lipEx
v1.2.0

搜索帮助