-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgobuild
executable file
·105 lines (85 loc) · 2.55 KB
/
gobuild
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
#!/usr/bin/env python3
import click
import contextlib
import os
import shutil
import subprocess
NAME = os.path.basename(os.getcwd())
DSTDIR = "./tmp"
# (os, architecture, suffix, archive)
ARCHS = [
("darwin", "amd64", "osx64", "tgz"),
("windows", "amd64", "windows64", "zip"),
("windows", "386", "windows32", "zip"),
("linux", "amd64", "linux64", "tgz"),
("linux", "arm", "linuxARM", "tgz"),
("netbsd", "amd64", "netbsd64", "tgz"),
("netbsd", "arm", "netbsdARM", "tgz"),
("openbsd", "amd64", "openbsd64", "tgz"),
("freebsd", "amd64", "freebsd64", "tgz"),
]
@contextlib.contextmanager
def chdir(newdir):
curdir = os.getcwd()
try:
os.chdir(newdir)
yield
finally:
os.chdir(curdir)
def version():
print("Installing locally")
subprocess.call(["go", "install", "./cmd/%s"%NAME])
p = subprocess.Popen([NAME, "--version"], stderr=subprocess.PIPE)
return p.communicate()[1].strip().decode("ascii")
def build(vers, goos, goarch, suffix, archive):
basename = "%s-%s-%s"%(NAME, vers, suffix)
fname = basename + "." + archive
dstdir = os.path.join(DSTDIR, basename)
pkgdst = os.path.abspath(os.path.join(DSTDIR, "packages", fname))
binname = NAME
if goos == "windows":
binname = binname + ".exe"
print("building to ", dstdir)
if os.path.exists(dstdir):
shutil.rmtree(dstdir)
os.makedirs(dstdir)
env = os.environ.copy()
env["GOOS"] = goos
env["GOARCH"] = goarch
subprocess.call(
[
"go", "build",
"-o", os.path.join(dstdir, binname),
"./cmd/%s"%NAME
],
env = env
)
with chdir(DSTDIR):
if archive == "tgz":
print("\tmaking .tgz")
subprocess.Popen(
["tar", "-czvf", pkgdst, basename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
elif archive == "zip":
print("\tmaking .zip")
subprocess.Popen(
["zip", "-r", pkgdst, basename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
@click.command()
@click.option("--arch", default=None, help="Build just this architecture")
def main(arch):
v = version()
print("Version is: ", v)
subprocess.call(["mkdir", "-p", os.path.join(DSTDIR, "packages")])
print
archs = ARCHS
if arch:
archs = [i for i in ARCHS if arch in i]
for i in archs:
build(v, *i)
if __name__ == "__main__":
main()