Created
May 4, 2021 08:53
-
-
Save bboozzoo/183ddaf2552a3a5274c49667b248ffab 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 python3 | |
import argparse | |
import subprocess | |
import os | |
import os.path | |
import logging | |
import shutil | |
import shlex | |
DESTDIR = os.getenv("DESTDIR", "") | |
STATIC_BUILD_FLAGS = "-buildmode=pie -ldflags='-extldflags=-static'" | |
BINDIR = "/usr/bin" | |
LIBEXECDIR = "/usr/lib/snapd" | |
DIRS = { | |
"snap": BINDIR | |
# the rest goes to libexecdir | |
} | |
FLAGS = { | |
"snap-exec": STATIC_BUILD_FLAGS, | |
"snap-bootstrap": STATIC_BUILD_FLAGS, | |
"snap-update-ns": STATIC_BUILD_FLAGS, | |
# the rest has no flags | |
} | |
TAGS = { | |
"snap": "-tags 'nomanagers nosecboot'" | |
# the rest has no tags | |
} | |
def pre_action(what): | |
if what == "snapd" and not DESTDIR: | |
logging.debug("stopping snapd service") | |
subprocess.run(shlex.split("sudo systemctl stop snapd"), check=True) | |
def post_action(what): | |
if what == "snapd" and not DESTDIR: | |
logging.debug("starting snapd service") | |
subprocess.run(shlex.split("sudo systemctl start snapd"), check=True) | |
def parse_arguments(): | |
parser = argparse.ArgumentParser(description="build helper") | |
parser.add_argument("-b", "--just-build", action="store_true", help="just build") | |
parser.add_argument("-t", "--tags", help="build tags to use (overrides defaults, if any)") | |
parser.add_argument("what", help="binary to build") | |
return parser.parse_known_args() | |
def main(opts, rest): | |
test_bin = "test-" + opts.what | |
cmd = "go build {flags} {tags} {rest} -o {output} github.com/snapcore/snapd/cmd/{cmd}".format( | |
flags=FLAGS.get(opts.what, ""), | |
tags="-tags '{}'".format(opts.tags) if opts.tags else TAGS.get(opts.what, ""), | |
output=test_bin, | |
cmd=opts.what, | |
rest=" ".join(rest), | |
) | |
logging.debug('running "%s"', cmd) | |
subprocess.run(shlex.split(cmd), check=True) | |
sz = os.path.getsize(test_bin) | |
logging.debug("binary size: %.2f kB", sz / 1024.0) | |
if opts.just_build: | |
logging.debug("skipping installation") | |
return | |
# install | |
pre_action(opts.what) | |
dst = os.path.join(DIRS.get(opts.what, LIBEXECDIR), opts.what) | |
if DESTDIR: | |
logging.debug('DESTDIR set to %s', DESTDIR) | |
dst = os.path.join(DESTDIR, os.path.relpath(dst, start='/')) | |
logging.debug('destination: %s', dst) | |
subprocess.run( | |
shlex.split("sudo cp -v {src} {dst}".format(src=test_bin, dst=dst)), check=True | |
) | |
post_action(opts.what) | |
if __name__ == "__main__": | |
opts, rest = parse_arguments() | |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s") | |
main(opts, rest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment