Last active
August 15, 2018 18:29
-
-
Save icholy/4eaff2bbeeb2704568019e59cd6ef980 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import errno | |
import os | |
import subprocess | |
import sys | |
import shutil | |
import glob | |
import tarfile | |
import os.path | |
def shell(command, cwd = None, env = None, verbose = True): | |
if verbose: | |
print("{} {}".format(command, env)) | |
environment = os.environ.copy() | |
if env: | |
environment.update(env) | |
cmd = subprocess.Popen( | |
command, | |
shell = True, | |
cwd = cwd, | |
env = environment, | |
stdin = subprocess.PIPE, | |
stdout = subprocess.PIPE, | |
stderr = subprocess.STDOUT | |
) | |
output = cmd.stdout.read().decode("utf-8") | |
if output: | |
print(output) | |
def mkdir(path): | |
try: | |
os.makedirs(path) | |
except OSError as exc: | |
if exc.errno == errno.EEXIST and os.path.isdir(path): | |
pass | |
else: | |
raise | |
def cp(src, dest): | |
if os.path.exists(dest): | |
rm(dest) | |
if os.path.isfile(src): | |
shutil.copyfile(src, dest) | |
else: | |
shutil.copytree(src, dest) | |
def rm(pattern): | |
for path in glob.glob(pattern): | |
print("rm {}".format(path)) | |
if os.path.isfile(path): | |
os.remove(path) | |
else: | |
shutil.rmtree(path, ignore_errors = True) | |
def tar(src, dest): | |
print("tar {} {}".format(src, dest)) | |
with tarfile.open(dest, "w:gz") as tar: | |
tar.add(src, arcname=os.path.basename(src)) | |
tar.close() | |
def echo(message): | |
print("\n{}\n".format(message)) | |
NAME = "thing" | |
VERSION = subprocess.check_output("git describe --tags", shell=True).strip().decode("utf-8") | |
TARGETS = [ | |
{ "name": "ARM7", "suffix": "arm7", "env": { "GOOS": "linux", "GOARCH": "arm", "GOARM": "7" } }, | |
{ "name": "Linux", "suffix": "amd64", "env": { "GOOS": "linux", "GOARCH": "amd64" } }, | |
{ "name": "Windows", "suffix": "amd64.exe", "env": { "GOOS": "windows", "GOARCH": "amd64" } }, | |
{ "name": "Mac", "suffix": "osx_amd64", "env": { "GOOS": "darwin", "GOARCH": "amd64" } } | |
] | |
def build(): | |
echo("BUILDING") | |
mkdir("build/") | |
for target in TARGETS: | |
file = "{}_{}".format(NAME, target["suffix"]) | |
cmd = 'go build -ldflags="-X main.version={}" -o build/{} ./cmd/{}'.format(VERSION, file, NAME) | |
print("{} -> build/{}".format(target["name"], file)) | |
shell(cmd, env=target["env"], verbose=False) | |
def dist(): | |
build() | |
echo("CREATING ARCHIVE") | |
cp("build/", "{}_{}/".format(NAME, VERSION)) | |
tar("{}_{}".format(NAME, VERSION), "{}_{}.tar.gz".format(NAME, VERSION)) | |
rm("{}_{}".format(NAME, VERSION)) | |
def clean(): | |
rm("./build/") | |
rm("{}_*.tar.gz".format(NAME)) | |
def rmrc(): | |
shell("git tag | grep -i 'rc[0-9]' | xargs -i git push origin :refs/tags/{}") | |
shell("git tag | grep -i 'rc[0-9]' | xargs -i git tag -d {}") | |
def test(): | |
shell("go test ./...") | |
def get_rule(): | |
if len(sys.argv) < 2: | |
return "dist" | |
return sys.argv[1] | |
rules = { | |
"build": build, | |
"dist": dist, | |
"rmrc": rmrc, | |
"clean": clean, | |
"test": test | |
} | |
if __name__ == "__main__": | |
r = get_rule() | |
if r not in rules: | |
print("{} is not a valid rule".format(r)) | |
else: | |
rules[r]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment