~launchpad-pqm/launchpad/devel

10303.1.1 by Gary Poster
use newest version of zc.buildout
1
#! /usr/bin/python2.5 -S
8687.15.4 by Karl Fogel
Add the copyright header block to more files; tweak format in a few files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
5
6
"""Create a static WADL file describing the current webservice.
7
8
Usage hint:
9
10420.4.1 by Leonard Richardson
Initial implementation.
10
% LPCONFIG="edge" utilities/create-lp-wadl.py launchpad-%(version)s.wadl
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
11
"""
12
13
import _pythonpath
14
10466.8.1 by Leonard Richardson
Initial implementation.
15
from cStringIO import StringIO
16
import os
17
import pkg_resources
18
import subprocess
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
19
import sys
10420.4.1 by Leonard Richardson
Initial implementation.
20
import urlparse
21
22
from zope.component import getUtility
10466.8.1 by Leonard Richardson
Initial implementation.
23
from zope.pagetemplate.pagetemplatefile import PageTemplateFile
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
24
25
from canonical.launchpad.ftests import login, ANONYMOUS
26
from canonical.launchpad.scripts import execute_zcml_for_scripts
27
from canonical.launchpad.webapp.servers import (
28
    WebServicePublication, WebServiceTestRequest)
29
from canonical.launchpad.webapp.vhosts import allvhosts
7182.3.2 by Gary Poster
change code to keep launchpad code in launchpad, not lazr; add tests. Once (or if) lint is happy, this should be ready for review.
30
from canonical.launchpad.systemhomes import WebServiceApplication
10420.4.1 by Leonard Richardson
Initial implementation.
31
from lazr.restful.interfaces import IWebServiceConfiguration
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
32
10556.2.5 by Leonard Richardson
Reverted changes to create-lp-wadl-and-apidoc.py.
33
def main(path_template):
10420.4.2 by Leonard Richardson
Response to feedback.
34
    WebServiceApplication.cached_wadl = None # do not use cached file version
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
35
    execute_zcml_for_scripts()
10420.4.1 by Leonard Richardson
Initial implementation.
36
    config = getUtility(IWebServiceConfiguration)
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
37
    directory, ignore = os.path.split(path_template)
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
38
10466.8.1 by Leonard Richardson
Initial implementation.
39
    stylesheet = pkg_resources.resource_filename(
40
        'launchpadlib', 'wadl-to-refhtml.xsl')
41
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
42
    # First, create an index.html with links to all the HTML
43
    # documentation files we're about to generate.
44
    template_file = 'apidoc-index.pt'
45
    template = PageTemplateFile(template_file)
46
    f = open(os.path.join(directory, "index.html"), 'w')
10524.1.3 by Leonard Richardson
Simplified template code even more thanks to gary's code.
47
    f.write(template(config=config))
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
48
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
49
    # Request the WADL from the root resource.
6770.4.5 by Francis J. Lacoste
Add comment for the uninitiated.
50
    # We do this by creating a request object asking for a WADL
51
    # representation.
10466.8.2 by Leonard Richardson
Minor cleanup.
52
    for version in config.active_versions:
10420.4.1 by Leonard Richardson
Initial implementation.
53
        url = urlparse.urljoin(allvhosts.configs['api'].rooturl, version)
54
        request = WebServiceTestRequest(version=version, environ={
55
            'SERVER_URL': url,
56
            'HTTP_HOST': allvhosts.configs['api'].hostname,
57
            'HTTP_ACCEPT': 'application/vd.sun.wadl+xml'
58
            })
59
        # We then bypass the usual publisher processing by associating
60
        # the request with the WebServicePublication (usually done  by the
61
        # publisher) and then calling the root resource - retrieved through
62
        # getApplication().
63
        request.setPublication(WebServicePublication(None))
64
        login(ANONYMOUS, request)
65
        filename = path_template % {'version' : version}
66
        print "Writing WADL for version %s to %s." % (version, filename)
67
        f = open(filename, 'w')
68
        content = request.publication.getApplication(request)(request)
69
        f.write(content)
70
        f.close()
10466.8.1 by Leonard Richardson
Initial implementation.
71
72
        # Now, convert the WADL into an human-readable description and
73
        # put the HTML in the same directory as the WADL.
74
        html_filename = os.path.join(directory, version + ".html")
75
        print "Writing apidoc for version %s to %s" % (
76
            version, html_filename)
77
        stdout = open(html_filename, "w")
78
        subprocess.Popen(['xsltproc', stylesheet, filename], stdout=stdout)
79
        stdout.close()
80
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
81
    return 0
82
83
if __name__ == '__main__':
10556.2.5 by Leonard Richardson
Reverted changes to create-lp-wadl-and-apidoc.py.
84
    if len(sys.argv) != 2:
85
        print "Usage: %s [WADL path template]" % sys.argv[0]
86
        print " Example: %s path/to/wadl/wadl-%%(version).xml" % (
87
            sys.argv[0])
10420.4.1 by Leonard Richardson
Initial implementation.
88
        sys.exit(-1)
10556.2.5 by Leonard Richardson
Reverted changes to create-lp-wadl-and-apidoc.py.
89
    sys.exit(main(sys.argv[1]))