-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.py
64 lines (56 loc) · 2.17 KB
/
build.py
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
#!/usr/bin/env python
import os
import sys
import subprocess
import time
from subprocess import call
try:
# GOPATH = "/Users/johnlindsay/Documents/programming/GoCode/" # change this as needed
GOPATH = "/Users/johnlindsay/go/"
GOOS = 'darwin' # darwin, windows, or linux
GOARCH = 'amd64' # 386, amd64, or arm
cleanCode = False
mode = 'install' # install, build, or test; cross-compilation requires build
# Change the current directory
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
# Find the GOHOSTOS and GOHOSTARCH
GOHOSTOS = ""
GOHOSTARCH = ""
cmd = "go env"
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
while True:
line = ps.stdout.readline()
if line != '':
if 'GOHOSTOS' in line:
GOHOSTOS = line.split("=")[1].replace("\"", "").strip()
elif 'GOHOSTARCH' in line:
GOHOSTARCH = line.split("=")[1].replace("\"", "").strip()
else:
break
# set the GOPATH, GOOS, and GOARCH environment variables
os.environ['GOPATH'] = GOPATH
if GOOS != GOHOSTOS:
os.environ['GOOS'] = GOOS
mode = 'build' # cross-compilation requires build
if GOARCH != GOHOSTARCH:
os.environ['GOARCH'] = GOARCH
mode = 'build' # cross-compilation requires build
if cleanCode:
retcode = call(['go', 'clean'], shell=False)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
if mode == "build":
retcode = call(['go', 'build', '-v'], shell=False)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
elif mode == "install":
retcode = call(['go', 'install'], shell=False)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
elif mode == "test":
retcode = call(['go', 'test', '-v'], shell=False)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
except OSError as e:
print >>sys.stderr, "Execution failed:", e