bh@287: #! /usr/bin/python bh@287: # Copyright (C) 2007, 2008, 2009 by Intevation GmbH thomas@78: # Authors: thomas@78: # Bernhard Herzog thomas@78: # thomas@78: # This program is free software under the GPL (>=v2) thomas@78: # Read the file COPYING coming with the software for details. thomas@78: thomas@78: """Publishes a static web-site with a status report""" thomas@78: thomas@78: import sys bricks@578: import shlex thomas@78: import os thomas@78: from optparse import OptionParser thomas@78: from ConfigParser import SafeConfigParser thomas@78: thomas@78: import treepkgcmd thomas@78: from treepkg.readconfig import read_config_section thomas@78: from treepkg.run import call thomas@78: from treepkg.cmdexpand import cmdexpand thomas@78: from treepkg.util import ensure_directory thomas@78: thomas@78: def remove_trailing_slashes(s): thomas@78: return s.rstrip("/") thomas@78: thomas@78: def expand_filename(filename): thomas@78: """ thomas@78: Applies os.path.expanduser and os.path.expandvars to filename thomas@78: """ thomas@78: return os.path.expandvars(os.path.expanduser(filename)) thomas@78: bricks@578: staticweb_desc = ["build_user", "build_host", bricks@578: ("build_create", shlex.split), "build_template", aheinecke@372: "build_logs", thomas@78: ("build_dir", remove_trailing_slashes), thomas@78: "publish_user", "publish_host", thomas@78: ("publish_dir", remove_trailing_slashes), thomas@78: ("cachedir", thomas@78: lambda s: expand_filename(remove_trailing_slashes(s)))] thomas@78: aheinecke@372: #Default values for the configuration options can be set here aheinecke@372: staticweb_defaults = [("build_logs", "build_log.txt.gz")] aheinecke@372: thomas@78: def read_config(filename): thomas@78: parser = SafeConfigParser() aheinecke@372: parser.add_section("staticweb") aheinecke@372: for value in staticweb_defaults: aheinecke@372: parser.set("staticweb", value[0], value[1]) thomas@78: parser.read([filename]) thomas@78: return read_config_section(parser, "staticweb", staticweb_desc) thomas@78: thomas@78: def parse_commandline(): thomas@78: parser = OptionParser() thomas@78: parser.set_defaults(config_file=os.path.join(treepkgcmd.topdir, thomas@78: "staticweb.cfg")) aheinecke@338: parser.set_defaults(update_cache_only=False) thomas@78: parser.add_option("--config-file", bh@319: help=("The configuration file. Default is staticweb.cfg")) aheinecke@338: parser.add_option("--update-cache-only", action="store_true", aheinecke@338: help=("Stop after updating the cache." aheinecke@338: " This omits the last step of the publishing" aheinecke@338: " process")) thomas@78: return parser.parse_args() thomas@78: aheinecke@338: def publish_static_site(config_filename, update_cache_only=False): thomas@78: config = read_config(config_filename) thomas@78: thomas@78: # create web-page on build host bricks@578: call(cmdexpand("ssh $build_user$@$build_host @build_create" aheinecke@372: " --show-logs=$build_logs" bh@158: " --status-template=$build_template $build_dir", thomas@78: **config)) thomas@78: thomas@78: # rsync the new web-pages to the local cache thomas@78: ensure_directory(config["cachedir"]) thomas@78: call(cmdexpand("rsync -rL --delete $build_user$@$build_host:$build_dir/" thomas@78: " $cachedir", thomas@78: **config)) thomas@78: aheinecke@338: if not update_cache_only: aheinecke@338: # rsync the web pages from the local cache to the publishing host aheinecke@338: call(cmdexpand("rsync -rL --delete $cachedir/" aheinecke@338: " $publish_user$@$publish_host:$publish_dir", aheinecke@338: **config)) thomas@78: thomas@78: thomas@78: def main(): thomas@78: options, args = parse_commandline() aheinecke@338: publish_static_site(options.config_file, options.update_cache_only) thomas@78: thomas@78: main()