-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
255 lines (215 loc) · 6.99 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"archive/zip"
"bufio"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
fmt.Println(`
_____ ______ _____
| __ \ | ____| / ____|
| |__) | _| |__ __ _____ ___| | __ ___
| ___/ | | | __| \ \/ / _ \/ __| | |_ |/ _ \
| | | |_| | |____ > < __/ (__| |__| | (_) |
|_| \__, |______/_/\_\___|\___|\_____|\___/
__/ |
|___/
`)
fmt.Println("© 2024 PyExecGo Contributors - Builder Version: v1.1.2")
fmt.Println("PyExecGo is released under the MIT License")
fmt.Println()
fmt.Println("The 'template' folder and 'template.zip' file will be deleted if they exist.")
fmt.Print("Press 'Enter' to confirm and continue: ")
bufio.NewReader(os.Stdin).ReadString('\n')
fmt.Println("Removing existing 'template.zip' and 'template' folder if they exist...")
removeFile("template.zip")
removeDir("template")
fmt.Println("Downloading the repository...")
zipURL := "https://github.com/PyExecGo-Project/Template-Windows/archive/refs/heads/main.zip"
if err := downloadFile("template.zip", zipURL); err != nil {
fmt.Println("Error downloading zip file:", err)
return
}
fmt.Println("Extracting the repository...")
if err := unzip("template.zip", "template"); err != nil {
fmt.Println("Error extracting zip file:", err)
return
}
if err := os.Chdir("template/Template-Windows-main"); err != nil {
fmt.Println("Error changing directory:", err)
return
}
fmt.Println("Setting up portable-python-bin directory...")
removeDir("portable-python-bin")
if err := os.MkdirAll("portable-python-bin", os.ModePerm); err != nil {
fmt.Println("Error creating directory:", err)
return
}
if err := os.Chdir("portable-python-bin"); err != nil {
fmt.Println("Error changing directory:", err)
return
}
fmt.Println("Downloading Powershell script...")
psScriptURL := "https://raw.githubusercontent.com/PyExecGo-Project/pyportable/refs/heads/main/portablepy.ps1"
if err := downloadFile("portablepy.ps1", psScriptURL); err != nil {
fmt.Println("Error downloading PowerShell script:", err)
return
}
fmt.Println("Temporarily changing the ExecutionPolicy to Unrestricted...")
if err := exec.Command("powershell", "Set-ExecutionPolicy", "-ExecutionPolicy", "Unrestricted", "-Scope", "CurrentUser").Run(); err != nil {
fmt.Println("Error setting execution policy:", err)
return
}
fmt.Println("Setting up Python with the Powershell script...")
if err := exec.Command("powershell", "-ExecutionPolicy", "Bypass", "-File", "portablepy.ps1").Run(); err != nil {
fmt.Println("Error running portable-python-bin script:", err)
return
}
fmt.Println("Changing the ExecutionPolicy back to Restricted...")
if err := exec.Command("powershell", "Set-ExecutionPolicy", "-ExecutionPolicy", "Restricted", "-Scope", "CurrentUser").Run(); err != nil {
fmt.Println("Error resetting execution policy:", err)
return
}
fmt.Println("Changing directory back again...")
if err := os.Chdir(".."); err != nil {
fmt.Println("Error changing directory:", err)
return
}
fmt.Println("Please place all your Python files in the 'template/Template-Windows-main' folder.")
fmt.Print("Press 'Enter' when done: ")
bufio.NewReader(os.Stdin).ReadString('\n')
pythonFile := getInput("Enter the Python script you want to run (default is 'main.py'): ", "main.py")
projectName := getInput("Enter your project name (this will be added to the executable): ", "")
fmt.Println("Updating main.go with the project name and Python script filename...")
if err := updateMainGoWithProjectInfo(projectName, pythonFile); err != nil {
fmt.Println("Error updating main.go:", err)
return
}
fmt.Println("Inserting special sauce...")
if err := insertSpecialSauce(pythonFile); err != nil {
fmt.Println("Error inserting special sauce:", err)
return
}
fmt.Println("Building the Go executable...")
if err := exec.Command("..\\..\\portable-go-bin\\bin\\go.exe", "build", ".\\main.go").Run(); err != nil {
fmt.Println("Error building Go executable:", err)
return
}
fmt.Println("Cleaning up...")
cleanupFiles()
fmt.Print("Build complete! You can now run the executable in template/Template-Windows-main.")
bufio.NewReader(os.Stdin).ReadString('\n')
}
func getInput(prompt, defaultValue string) string {
fmt.Print(prompt)
input, _ := bufio.NewReader(os.Stdin).ReadString('\n')
input = strings.TrimSpace(input)
if input == "" {
return defaultValue
}
return input
}
func removeFile(filepath string) {
if err := os.RemoveAll(filepath); err != nil {
fmt.Printf("Error removing %s: %v\n", filepath, err)
}
}
func removeDir(dir string) {
if err := os.RemoveAll(dir); err != nil {
fmt.Printf("Error removing directory %s: %v\n", dir, err)
}
}
func insertSpecialSauce(pythonFile string) error {
specialSauce, err := os.ReadFile("special-sauce.py")
if err != nil {
return err
}
pythonContent, err := os.ReadFile(pythonFile)
if err != nil {
return err
}
combinedContent := append(specialSauce, pythonContent...)
return os.WriteFile(pythonFile, combinedContent, 0644)
}
func updateMainGoWithProjectInfo(projectName, pythonFile string) error {
mainGoContent, err := os.ReadFile("main.go")
if err != nil {
return err
}
lines := strings.Split(string(mainGoContent), "\n")
for i, line := range lines {
if strings.Contains(line, "This executable was built for the project:") {
lines[i] = fmt.Sprintf(" fmt.Println(\"This executable was built for the project: %s\")", projectName)
}
if strings.Contains(line, "main.py") {
lines[i] = strings.ReplaceAll(line, "main.py", pythonFile)
}
}
updatedMainGoContent := strings.Join(lines, "\n")
return os.WriteFile("main.go", []byte(updatedMainGoContent), 0644)
}
func downloadFile(filepath, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
fPath := filepath.Join(dest, f.Name)
if !strings.HasPrefix(fPath, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("invalid file path: %s", fPath)
}
if f.FileInfo().IsDir() {
os.MkdirAll(fPath, os.ModePerm)
continue
}
if err = os.MkdirAll(filepath.Dir(fPath), os.ModePerm); err != nil {
return err
}
outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
rc, err := f.Open()
if err != nil {
outFile.Close()
return err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return err
}
}
return nil
}
func cleanupFiles() {
removeFile("main.go")
removeFile("go.mod")
removeFile("special-sauce.py")
removeFile("README.md")
removeFile("LICENSE")
removeFile(".gitignore")
removeFile("..\\..\\template.zip")
}