From 9bf107a102b8e6e9e199a5a93390c2a9c5cf9d53 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 26 May 2019 17:55:14 +0200 Subject: Move deploy to external file --- cli/cli.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) (limited to 'cli') diff --git a/cli/cli.py b/cli/cli.py index 232bca1b..5a386ed6 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -153,3 +153,106 @@ def touch(filename): def touch_cli(args): """CLI wrapper around touch.""" touch(args.filename) + + +def deploy(args): + """Deploys build directory to origin/master without regenerating. + + Returns + ------- + 0 + On success. Exit early with nonzero status otherwise. + + """ + + # pylint: disable=unused-argument,too-many-statements + + # check whether root is dirty + os.chdir(ROOTDIR) + dirty = subprocess.check_output(["git", "status", "--porcelain"]) + if dirty: + sys.stderr.write(YELLOW) + sys.stderr.write("Project root is dirty.\n") + sys.stderr.write("You may want to commit in your changes " + "to the source branch, since the SHA and title " + "of the latest commit on the source branch will be " + "incorporated into the commit message on " + "the deployment branch. Type s[hell] on the " + "next prompt to open an interactive shell.\n") + sys.stderr.write(RESET) + while True: + sys.stderr.write("Continue? [yNs] ") + answer = input() + if not answer: + # default + abort = True + break + elif answer.startswith(('y', 'Y')): + abort = False + break + elif answer.startswith(('n', 'N')): + abort = True + break + elif answer.startswith(('s', 'S')): + shell = (os.environ['SHELL'] if 'SHELL' in os.environ and os.environ['SHELL'] + else 'zsh') + subprocess.call(shell) + stilldirty = subprocess.check_output(["git", "status", "--porcelain"]) + if stilldirty: + sys.stderr.write(YELLOW) + sys.stderr.write("Project root is still dirty.\n") + sys.stderr.write(RESET) + else: + sys.stderr.write("Please answer yes or no.\n") + if abort: + sys.stderr.write("%saborting deployment%s\n" % (RED, RESET)) + return 1 + + # extract latest commit on the source branch + source_commit = subprocess.check_output( + ["git", "log", "-1", "--pretty=oneline", "source", "--"]).decode('utf-8').strip() + + # cd into BUILDDIR and assemble commit message + sys.stderr.write("%scommand: cd '%s'%s\n" % (BLUE, BUILDDIR, RESET)) + os.chdir(BUILDDIR) + + # extract updated time from atom.xml + if not os.path.exists("atom.xml"): + sys.stderr.write("atom.xml not found, cannot deploy\naborting\n") + return 1 + atomxml = ET.parse("atom.xml").getroot() + updated = atomxml.find('{http://www.w3.org/2005/Atom}updated').text + + commit_message = ("Site updated at %s\n\nsource branch was at:\n%s\n" % + (updated, source_commit)) + + # commit changes in BUILDDIR + sys.stderr.write("%scommand: git add --all%s\n" % (BLUE, RESET)) + subprocess.check_call(["git", "add", "--all"]) + sys.stderr.write("%scommand: git commit --no-verify --gpg-sign --message='%s'%s\n" % + (BLUE, commit_message, RESET)) + try: + subprocess.check_call(["git", "commit", "--gpg-sign", + "--message=%s" % commit_message]) + except subprocess.CalledProcessError: + sys.stderr.write("\n%serror: git commit failed%s\n" % (RED, RESET)) + return 1 + + # check dirty status + dirty = subprocess.check_output(["git", "status", "--porcelain"]) + if dirty: + sys.stderr.write(RED) + sys.stderr.write("error: failed to commit all changes; " + "build directory still dirty\n") + sys.stderr.write("error: please manually inspect what was left out\n") + sys.stderr.write(RESET) + return 1 + + # push to origin/master + sys.stderr.write("%scommand: git push origin master%s\n" % (BLUE, RESET)) + try: + subprocess.check_call(["git", "push", "origin", "master"]) + except subprocess.CalledProcessError: + sys.stderr.write("\n%serror: git push failed%s\n" % (RED, RESET)) + return 1 + return 0 -- cgit v1.2.1