view bin/publishpackages.py @ 287:1fcdffbeb9de

Make the #! line in the commands more portable. Use /usr/bin/python instead of /usr/bin/python2.4.
author Bernhard Herzog <bh@intevation.de>
date Tue, 01 Sep 2009 13:35:23 +0000
parents acf6c0ce2014
children f70cee73a7ec
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 selected packages created by treepkg"""

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

import treepkgcmd
from treepkg.readconfig import read_config_section, convert_bool
from treepkg.run import call, capture_output
from treepkg.cmdexpand import cmdexpand
from treepkg.util import ensure_directory, listdir_abs


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))

config_desc = ["distribution", "section", "num_newest",
               "build_user", "build_host", "build_listpackages",
               "publish_user", "publish_host",
               ("after_upload_hook", shlex.split),
               ("publish_remove_old_packages", convert_bool),
               ("publish_dir", remove_trailing_slashes),
               ("cachedir",
                lambda s: expand_filename(remove_trailing_slashes(s)))]

def read_config(filename):
    parser = SafeConfigParser()
    parser.read([filename])
    return read_config_section(parser, "publishpackages", config_desc)

def parse_commandline():
    parser = OptionParser()
    parser.set_defaults(config_file=os.path.join(treepkgcmd.topdir,
                                                 "publishpackages.cfg"),
                        quiet=False)
    parser.add_option("--config-file",
                      help=("The configuration file."
                            " Default is publishpackages.cfg"))
    parser.add_option("--revision",
                      help=("The revision whose files are to be published."
                            " If not given, the latest revision is used"
                            " The value may be given as REV-RULESREV to specify"
                            " both the main source revision and the revision of"
                            " the packaging rules"))
    parser.add_option("--dist",
                      help=("The debian distribution name to use on"
                            " the publishing system"))
    parser.add_option("--section",
                      help=("The debian distribution section name to use on"
                            " the publishing system"))
    parser.add_option("--track",
                      help=("The package track whose files are to be"
                            " published. If not given, files of all tracks"
                            " will be published"))
    parser.add_option("--quiet", action="store_true",
                      help=("Do not print progress meters or other"
                            " informational output"))
    return parser.parse_args()

def prefix_for_remote_command(user, host):
    """Returns the ssh call needed to run a command on a remote host.
    If host is empty, the function assumes the command is to be run on
    the local host as the same user that exectutes this function, so
    that no ssh or other call is needed.
    """
    prefix = []
    if host:
        prefix.extend(["ssh", "%s@%s" % (user, host)])
    return prefix


def copy_to_cache(variables, track, revision, arch, quiet):
    listpackages_vars = variables.copy()

    if arch == "source":
        listpackages_vars["pkgtype"] = "--source"
    else:
        listpackages_vars["pkgtype"] = "--binary"

    if track:
        listpackages_vars["track"] = ["--track", track]
    else:
        listpackages_vars["track"] = []

    if revision:
        listpackages_vars["revision"] = ["--revision", revision]
    else:
        listpackages_vars["revision"] = []

    runremote = prefix_for_remote_command(variables["build_user"],
                                          variables["build_host"])
    files = capture_output(cmdexpand("@runremote $build_listpackages"
                                     " @track @revision $pkgtype"
                                     " --newest=$num_newest",
                                     runremote=runremote,
                                     **listpackages_vars)).strip().split("\n")

    # scp the packages to the cache dir
    cachedir = variables["cachedir"]
    shutil.rmtree(cachedir, ignore_errors=True)
    ensure_directory(cachedir)
    if variables["build_host"]:
        userhost = "%(build_user)s@%(build_host)s:" % variables
        files = [userhost + filename for filename in files]
    scp_flags = []
    if quiet:
        scp_flags.append("-q")
    call(cmdexpand("scp -p @scp_flags @files $cachedir/", files=files,
                   scp_flags=scp_flags, **variables))


def copy_to_publishdir(variables, dist, section, arch, quiet):
    destdir = os.path.join(variables["publish_dir"], dist, section, arch)
    remote_destdir = destdir
    if variables["publish_host"]:
        remote_destdir = (("%(publish_user)s@%(publish_host)s:" % variables)
                          + remote_destdir)
    runremote = prefix_for_remote_command(variables["publish_user"],
                                          variables["publish_host"])

    call(cmdexpand("@runremote mkdir --parents $destdir",
                   runremote=runremote, destdir=destdir, **variables))
    rsync_flags = []
    if variables["publish_remove_old_packages"]:
        rsync_flags.append("--delete")
    if quiet:
        rsync_flags.append("--quiet")
    call(cmdexpand("rsync @rsync_flags -rpt $cachedir/ $remote_destdir/",
                   rsync_flags=rsync_flags, remote_destdir=remote_destdir,
                   **variables))


def publish_packages_arch(variables, track, revision, dist, section, arch,
                          quiet):
    copy_to_cache(variables, track, revision, arch, quiet)
    copy_to_publishdir(variables, dist, section, arch, quiet)


def publish_packages(config_filename, track, revision, dist, section, quiet):
    config = read_config(config_filename)

    if dist is None:
        dist = config["distribution"]
    if section is None:
        section = config["section"]

    for arch in ["binary-i386", "source"]:
        publish_packages_arch(config, track, revision, dist, section, arch,
                              quiet)

    # update apt archive
    call(config["after_upload_hook"])

def main():
    options, args = parse_commandline()
    publish_packages(options.config_file, options.track, options.revision,
                     options.dist, options.section, options.quiet)

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