Dashboard > Pulse v2.4 > ... > Remote API Examples > Python Example - Create Project With Property Values
  Pulse v2.4 Log In | Sign Up   View a printable version of the current page.  
  Python Example - Create Project With Property Values
Added by Jason Sankey, last edited by Jason Sankey on Dec 06, 2011
Labels: 
(None)

Pulse Manual Index

Overview

Shows how to create a new project from a template, with optional updates to properties (added properties or edited property values). This is useful if you can factor your projects such that all the configuration is in the template barring a handful of parameters expressed as properties.

Code

#! /usr/bin/env python

#
# This sample shows how to create a new project that matches an existing
# template with optional updates to properties.  Properties may be added,
# or inherited property values overridden.
#

from xmlrpclib import ServerProxy, Error
import sys

#
# These values must be edited to match your installation.
# You may also want to prompt for the password to avoid saving it in this file.
#
PULSE_URL = "http://localhost:8080/xmlrpc"
PULSE_USER = "admin"
PULSE_PASSWORD = "admin"

KEY_SYMBOLIC_NAME = "meta.symbolicName"
KEY_NAME = "name"
KEY_VALUE = "value"

TYPE_PROJECT = "zutubi.projectConfig"
TYPE_PROPERTY = "zutubi.resourceProperty"


def fatal(message):
    print >> sys.stderr, message
    sys.exit(1)


def add_project(server, token, template, name):
    project = {KEY_SYMBOLIC_NAME: TYPE_PROJECT, KEY_NAME: name}
    return server.RemoteApi.insertTemplatedConfig(token, "projects/" + template, project, False)


def parse_property(arg):
    parts = arg.split("=")
    if len(parts) != 2:
        raise Error("Invalid property spec '" + arg + "'")
    return parts


def parse_properties(args):
    return [parse_property(arg) for arg in args];


def set_properties(server, token, project_path, specs):
    for spec in specs:
        properties_path = project_path + "/properties"
        property_path = properties_path + "/" + spec[0]
        if server.RemoteApi.configPathExists(token, property_path):
            existing_property = server.RemoteApi.getConfig(token, property_path)
            if existing_property[KEY_VALUE] != spec[1]:
                existing_property[KEY_VALUE] = spec[1]
                server.RemoteApi.saveConfig(token, property_path, existing_property, True)
        else:
            property = {KEY_SYMBOLIC_NAME: TYPE_PROPERTY, KEY_NAME: spec[0], KEY_VALUE: spec[1]}
            server.RemoteApi.insertConfig(token, properties_path, property)


if __name__ == "__main__":
    if len(sys.argv) < 3:
        fatal("Usage: %s <template project> <new project> [<property name>=<value> ...]" % sys.argv[0])

    specs = parse_properties(sys.argv[3:])
    
    server = ServerProxy(PULSE_URL)
    token = server.RemoteApi.login(PULSE_USER, PULSE_PASSWORD)
    try:
        project_path = add_project(server, token, sys.argv[1], sys.argv[2])
        set_properties(server, token, project_path, specs)
    except Error, e:
        fatal("Error: %s" % e)
    finally:
        server.RemoteApi.logout(token)

Zutubi wiki is Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators