This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
python.go
128 lines (100 loc) · 3.3 KB
/
python.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package templates
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/abdfnx/botway/constants"
"github.com/abdfnx/looker"
)
func MainPyContent(platform string) string {
return Content("main.py", platform+"-python", "", "")
}
func RequirementsContent(platform string) string {
return Content("requirements.txt", platform+"-python", "", "")
}
func PyProjectContent(botName string) string {
return Content("pyproject.toml", "discord-python", botName, "")
}
func PythonTemplate(botName, platform, pm string) {
pip := "pip3"
pythonPath := "python3"
if runtime.GOOS == "windows" {
pip = "pip"
pythonPath = "python"
}
_, err := looker.LookPath(pythonPath)
pipPath, perr := looker.LookPath(pip)
if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" python is not installed"))
} else if perr != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(fmt.Sprintf(" %s is not installed", pip)))
} else {
mainFile := os.WriteFile(filepath.Join(botName, "src", "main.py"), []byte(MainPyContent(platform)), 0644)
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(DockerfileContent(botName, pm+".dockerfile", platform)), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(Resources(platform, "python.md")), 0644)
if resourcesFile != nil {
log.Fatal(resourcesFile)
}
if mainFile != nil {
log.Fatal(mainFile)
}
if dockerFile != nil {
log.Fatal(dockerFile)
}
installCmd := ""
pkgs := ""
shared_pkgs := "botway.py pyyaml"
if platform == "discord" {
pkgs = "discord.py pynacl " + shared_pkgs
} else if platform == "telegram" {
pkgs = "python-telegram-bot cryptography PySocks ujson " + shared_pkgs
} else if platform == "slack" {
pkgs = "slack-bolt " + shared_pkgs
} else if platform == "twitch" {
pkgs = "twitchio " + shared_pkgs
}
if pm == "pip" {
requirementsFile := os.WriteFile(filepath.Join(botName, "requirements.txt"), []byte(RequirementsContent(platform)), 0644)
if requirementsFile != nil {
log.Fatal(requirementsFile)
}
installCmd = pipPath + " install -r requirements.txt"
} else if pm == "pipenv" {
pipenvPath, err := looker.LookPath("pipenv")
if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" pipenv is not installed"))
}
installCmd = pipenvPath + " install " + pkgs
} else if pm == "poetry" {
poetryPath, err := looker.LookPath("poetry")
if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" poetry is not installed"))
}
pyprojectFile := os.WriteFile(filepath.Join(botName, "pyproject.toml"), []byte(PyProjectContent(botName)), 0644)
if pyprojectFile != nil {
log.Fatal(pyprojectFile)
}
installCmd = poetryPath + " add " + pkgs
}
cmd := exec.Command("bash", "-c", installCmd)
if runtime.GOOS == "windows" {
cmd = exec.Command("powershell.exe", installCmd)
}
cmd.Dir = botName
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Printf("error: %v\n", err)
}
CheckProject(botName, platform)
}
}