view bin/publishstaticweb.py @ 430:4aaf73a6225e treepkg-status

introduced after_copy_hook and made rsync upload to publishdir optional
author Bjoern Ricks <bricks@intevation.de>
date Thu, 29 Jul 2010 12:07:57 +0000
parents ef87d30468b6
children bccd69dedbe5
line wrap: on
line source
#! /usr/bin/python
# Copyright (C) 2007, 2008, 2009 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

"""Publishes a static web-site with a status report"""

import sys
import os
from optparse import OptionParser
from ConfigParser import SafeConfigParser

import treepkgcmd
from treepkg.readconfig import read_config_section
from treepkg.run import call
from treepkg.cmdexpand import cmdexpand
from treepkg.util import ensure_directory

def remove_trailing_slashes(s):
    return s.rstrip("/")

def expand_filename(filename):
    """
    Applies os.path.expanduser and os.path.expandvars to filename
    """
    return os.path.expandvars(os.path.expanduser(filename))

staticweb_desc = ["build_user", "build_host", "build_create", "build_template",
                  "build_logs",
                  ("build_dir", remove_trailing_slashes),
                  "publish_user", "publish_host",
                  ("publish_dir", remove_trailing_slashes),
                  ("cachedir",
                   lambda s: expand_filename(remove_trailing_slashes(s)))]

#Default values for the configuration options can be set here
staticweb_defaults = [("build_logs", "build_log.txt.gz")]

def read_config(filename):
    parser = SafeConfigParser()
    parser.add_section("staticweb")
    for value in staticweb_defaults:
        parser.set("staticweb", value[0], value[1])
    parser.read([filename])
    return read_config_section(parser, "staticweb", staticweb_desc)

def parse_commandline():
    parser = OptionParser()
    parser.set_defaults(config_file=os.path.join(treepkgcmd.topdir,
                                                 "staticweb.cfg"))
    parser.set_defaults(update_cache_only=False)
    parser.add_option("--config-file",
                      help=("The configuration file. Default is staticweb.cfg"))
    parser.add_option("--update-cache-only", action="store_true",
                      help=("Stop after updating the cache."
                            " This omits the last step of the publishing"
                            " process"))
    return parser.parse_args()

def publish_static_site(config_filename, update_cache_only=False):
    config = read_config(config_filename)

    # create web-page on build host
    call(cmdexpand("ssh $build_user$@$build_host $build_create"
                   " --show-logs=$build_logs"
                   " --status-template=$build_template $build_dir",
                   **config))

    # rsync the new web-pages to the local cache
    ensure_directory(config["cachedir"])
    call(cmdexpand("rsync -rL --delete $build_user$@$build_host:$build_dir/"
                   " $cachedir",
                   **config))

    if not update_cache_only:
        # rsync the web pages from the local cache to the publishing host
        call(cmdexpand("rsync -rL --delete $cachedir/"
                       " $publish_user$@$publish_host:$publish_dir",
                       **config))


def main():
    options, args = parse_commandline()
    publish_static_site(options.config_file, options.update_cache_only)

main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)