• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kshard / thinker / 15084025492

17 May 2025 09:48AM UTC coverage: 54.037% (-4.2%) from 58.222%
15084025492

Pull #20

github

fogfish
use echo for bash test
Pull Request #20: establish agent taxonomy - prompter, manifold, automata

331 of 612 new or added lines in 28 files covered. (54.08%)

8 existing lines in 4 files now uncovered.

609 of 1127 relevant lines covered (54.04%)

0.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.33
/command/python.go
1
//
2
// Copyright (C) 2025 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/kshard/thinker
7
//
8

9
package command
10

11
import (
12
        "bytes"
13
        "encoding/json"
14
        "fmt"
15
        "os"
16
        "os/exec"
17
        "path/filepath"
18

19
        "github.com/kshard/thinker"
20
)
21

22
// A unique name for bash (the shell)
23
const PYTHON = "python"
24

25
// Create new python command, defining working dir
26
func Python(dir string) thinker.Cmd {
1✔
27
        return thinker.Cmd{
1✔
28
                Cmd:   PYTHON,
1✔
29
                About: "Executed python scripts.",
1✔
30
                Args: []thinker.Arg{
1✔
31
                        {
1✔
32
                                Name:  "script",
1✔
33
                                Type:  "string",
1✔
34
                                About: `script is python program.`,
1✔
35
                        },
1✔
36
                },
1✔
37
                Run: python(dir),
1✔
38
        }
1✔
39
}
1✔
40

41
func pySetup(dir string) error {
1✔
42
        pyenv := filepath.Join(dir, ".venv")
1✔
43
        if _, err := os.Stat(pyenv); err != nil {
2✔
44
                setup := exec.Command("python3", "-m", "venv", ".venv")
1✔
45
                setup.Dir = dir
1✔
46

1✔
47
                _, err := setup.Output()
1✔
48
                if err != nil {
1✔
49
                        return err
×
50
                }
×
51

52
                pipreqs := exec.Command(filepath.Join(pyenv, "bin/python"), "-m", "pip", "install", "pigar")
1✔
53
                pipreqs.Dir = dir
1✔
54

1✔
55
                _, err = pipreqs.Output()
1✔
56
                if err != nil {
1✔
57
                        return err
×
58
                }
×
59
        }
60

61
        return nil
1✔
62
}
63

64
func pyDeps(dir string) error {
1✔
65
        pyenv := filepath.Join(dir, ".venv")
1✔
66

1✔
67
        deps := exec.Command(filepath.Join(pyenv, "bin/pigar"), "generate", "--question-answer", "yes", "--auto-select")
1✔
68
        deps.Dir = dir
1✔
69

1✔
70
        _, err := deps.Output()
1✔
71
        if err != nil {
1✔
72
                return err
×
73
        }
×
74

75
        pip := exec.Command(filepath.Join(pyenv, "bin/python"), "-m", "pip", "install", "-r", "requirements.txt")
1✔
76
        pip.Dir = dir
1✔
77

1✔
78
        _, err = pip.Output()
1✔
79
        if err != nil {
1✔
80
                return err
×
81
        }
×
82

83
        return nil
1✔
84
}
85

86
func pyfile(dir, code string) (string, error) {
1✔
87
        fd, err := os.CreateTemp(dir, "job-*.py")
1✔
88
        if err != nil {
1✔
89
                return "", err
×
90
        }
×
91
        defer fd.Close()
1✔
92

1✔
93
        _, err = fd.WriteString(code)
1✔
94
        if err != nil {
1✔
95
                return "", err
×
96
        }
×
97

98
        return fd.Name(), nil
1✔
99
}
100

101
func python(dir string) func(json.RawMessage) ([]byte, error) {
1✔
102
        return func(command json.RawMessage) ([]byte, error) {
2✔
103
                if err := pySetup(dir); err != nil {
1✔
NEW
104
                        return nil, err
×
105
                }
×
106

107
                var code script
1✔
108
                if err := json.Unmarshal(command, &code); err != nil {
1✔
NEW
109
                        err := thinker.Feedback(
×
NEW
110
                                "The input does not contain valid JSON object",
×
NEW
111
                                "JSON parsing has failed with an error "+err.Error(),
×
NEW
112
                        )
×
NEW
113
                        return nil, err
×
UNCOV
114
                }
×
115

116
                file, err := pyfile(dir, code.Script)
1✔
117
                if err != nil {
1✔
NEW
118
                        return nil, err
×
119
                }
×
120

121
                if err := pyDeps(dir); err != nil {
1✔
NEW
122
                        return nil, err
×
123
                }
×
124

125
                pyenv := filepath.Join(dir, ".venv")
1✔
126
                cmd := exec.Command(filepath.Join(pyenv, "bin/python"), file)
1✔
127
                var stdout bytes.Buffer
1✔
128
                var stderr bytes.Buffer
1✔
129
                cmd.Dir = dir
1✔
130
                cmd.Stdout = &stdout
1✔
131
                cmd.Stderr = &stderr
1✔
132

1✔
133
                if err := cmd.Run(); err != nil {
1✔
134
                        err = thinker.Feedback(
×
NEW
135
                                fmt.Sprintf("The tool %s has failed, improve the response based on feedback:", PYTHON),
×
136

×
137
                                `Strictly adhere python code formatting, use \t, \n where is needed`,
×
138
                                "Execution of python script is failed with the error: "+err.Error(),
×
139
                                "The error output is "+stderr.String(),
×
140
                        )
×
NEW
141
                        return nil, err
×
142
                }
×
143

144
                return stdout.Bytes(), nil
1✔
145
        }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc