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

kshard / thinker / 15083532464

17 May 2025 08:42AM UTC coverage: 33.807% (-24.4%) from 58.222%
15083532464

Pull #20

github

fogfish
update license
Pull Request #20: establish agent taxonomy - prompter, manifold, automata

241 of 612 new or added lines in 28 files covered. (39.38%)

144 existing lines in 5 files now uncovered.

381 of 1127 relevant lines covered (33.81%)

0.36 hits per line

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

0.0
/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
UNCOV
26
func Python(dir string) thinker.Cmd {
×
UNCOV
27
        return thinker.Cmd{
×
NEW
28
                Cmd:   PYTHON,
×
NEW
29
                About: "Executed python scripts.",
×
NEW
30
                Args: []thinker.Arg{
×
NEW
31
                        {
×
NEW
32
                                Name:  "script",
×
NEW
33
                                Type:  "string",
×
NEW
34
                                About: `script is python program.`,
×
NEW
35
                        },
×
NEW
36
                },
×
NEW
37
                Run: python(dir),
×
UNCOV
38
        }
×
UNCOV
39
}
×
40

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

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

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

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

UNCOV
61
        return nil
×
62
}
63

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

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

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

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

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

UNCOV
83
        return nil
×
84
}
85

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

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

UNCOV
98
        return fd.Name(), nil
×
99
}
100

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

NEW
107
                var code script
×
NEW
108
                if err := json.Unmarshal(command, &code); err != nil {
×
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

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

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

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

×
UNCOV
133
                if err := cmd.Run(); err != nil {
×
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

NEW
144
                return stdout.Bytes(), nil
×
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