-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbuild.py
44 lines (32 loc) · 1.18 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
#!/usr/bin/env python3
import os, time, subprocess
def runCmd(cmd):
p = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
stdout = p.communicate()[0].decode('utf-8').strip()
return stdout
# Get last tag.
def lastTag():
return runCmd('git describe --abbrev=0 --tags')
# Get current branch name.
def branch():
return runCmd('git rev-parse --abbrev-ref HEAD')
# Get last git commit id.
def lastCommitId():
return runCmd('git log --pretty=format:"%h" -1')
# Assemble build command.
def buildCmd():
buildFlag = []
version = lastTag()
if version != "":
buildFlag.append("-X main._version_ '{}'".format(version))
branchName = branch()
if branchName != "":
buildFlag.append("-X main._branch_ '{}'".format(branchName))
commitId = lastCommitId()
if commitId != "":
buildFlag.append("-X main._commitId_ '{}'".format(commitId))
# current time
buildFlag.append("-X main._buildTime_ '{}'".format(time.strftime("%Y-%m-%d %H:%M %z")))
return 'go build -ldflags "{}"'.format(" ".join(buildFlag))
if subprocess.call(buildCmd(), shell = True) == 0:
print("build finished.")