commit 72832baa56dfbf0cff0980746db708c897017603 Author: Damian Johnson atagar@torproject.org Date: Thu Dec 13 22:50:34 2012 -0800
Script to republish stem's website content
Script for stem's webserver (staticiforme) to provide a daemon that periodically publishes our site's content. --- docs/republish | 7 ++++ docs/republish.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 0 deletions(-)
diff --git a/docs/republish b/docs/republish new file mode 100644 index 0000000..26ecfc9 --- /dev/null +++ b/docs/republish @@ -0,0 +1,7 @@ +#!/bin/sh + +export PATH=/home/stem/bin:$PATH +export PYTHONPATH=/home/stem/lib/python + +python /home/stem/stem/docs/republish.py + diff --git a/docs/republish.py b/docs/republish.py new file mode 100644 index 0000000..ccd7ae1 --- /dev/null +++ b/docs/republish.py @@ -0,0 +1,86 @@ +import getopt +import logging +import subprocess +import sys +import time + +LOGGER = logging.getLogger("republish") +LOGGER.setLevel(logging.INFO) + +handler = logging.FileHandler('/home/stem/site_republishing.log') +handler.setFormatter(logging.Formatter( + fmt = '%(asctime)s [%(levelname)s] %(message)s', + datefmt = '%m/%d/%Y %H:%M:%S', +)) +LOGGER.addHandler(handler) + +OPT = "r:h" +OPT_EXPANDED = ["repeat=", "help"] + +HELP_MSG = """\ +Republishes stem's website. This can either be done or on a reoccurring basis. +If stem's repository is unchanged then this is a no-op. + + -r, --repeat RATE tries to republish the site at a set rate, in minutes +""" + +def run(command): + # Runs the given command. This returns the stdout if successful, and raises + # an OSError if it fails. + + cmd = subprocess.Popen(command.split(' '), stdout = subprocess.PIPE, stderr = subprocess.PIPE) + + if cmd.wait() == 0: + return cmd.communicate()[0] + else: + stdout, stderr = cmd.communicate() + raise OSError("'%s' failed\n stdout: %s\n stderr: %s" % (command, stdout, stderr)) + +def republish_site(): + # Checks if stem's repository has changed, rebuilding the site if so. Ideally + # we'd use plumbing commands to check this but... meh. Patches welcome. + + clone_cmd = 'git --git-dir=/home/stem/stem/.git pull' + + if 'Already up-to-date.' not in run(clone_cmd): + LOGGER.log(logging.INFO, "Stem's repository has changed. Republishing...") + run('cd /home/stem/stem/docs') + run('make html') + run('sudo -u mirroradm static-master-update-component stem.torproject.org') + LOGGER.log(logging.INFO, " site republished") + +if __name__ == '__main__': + try: + opts = getopt.getopt(sys.argv[1:], OPT, OPT_EXPANDED)[0] + except getopt.GetoptError, exc: + print "%s (for usage provide --help)" % exc + sys.exit(1) + + LOGGER.log(logging.INFO, "Starting stem site republisher") + repeat_rate = None + + for opt, arg in opts: + if opt in ("-r", "--repeat"): + if arg.isdigit(): + repeat_rate = int(arg) + else: + print "The --repeat argument must be an integer, got '%s'" % arg + sys.exit(1) + elif opt in ("-h", "--help"): + print HELP_MSG + sys.exit() + + if repeat_rate: + latest_run = 0 # unix timestamp for when we last ran + + while time.time() < (latest_run + repeat_rate * 60): + time.sleep(15) + + try: + latest_run = time.time() + republish_site() + except OSError, exc: + LOGGER.log(logging.WARN, str(exc)) + else: + republish_site() +
tor-commits@lists.torproject.org