~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#! /usr/bin/python -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
import os
16
import pkg_resources
17
import subprocess
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
18
import sys
10420.4.1 by Leonard Richardson
Initial implementation.
19
import urlparse
20
21
from zope.component import getUtility
10466.8.1 by Leonard Richardson
Initial implementation.
22
from zope.pagetemplate.pagetemplatefile import PageTemplateFile
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
23
24
from canonical.launchpad.scripts import execute_zcml_for_scripts
10736.1.1 by Jonathan Lange
Use non-testing login in script required to build Launchpad
25
from canonical.launchpad.webapp.interaction import (
26
    ANONYMOUS, setupInteractionByEmail)
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
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
10736.1.1 by Jonathan Lange
Use non-testing login in script required to build Launchpad
33
10556.2.5 by Leonard Richardson
Reverted changes to create-lp-wadl-and-apidoc.py.
34
def main(path_template):
10420.4.2 by Leonard Richardson
Response to feedback.
35
    WebServiceApplication.cached_wadl = None # do not use cached file version
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
36
    execute_zcml_for_scripts()
10420.4.1 by Leonard Richardson
Initial implementation.
37
    config = getUtility(IWebServiceConfiguration)
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
38
    directory, ignore = os.path.split(path_template)
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
39
10466.8.1 by Leonard Richardson
Initial implementation.
40
    stylesheet = pkg_resources.resource_filename(
41
        'launchpadlib', 'wadl-to-refhtml.xsl')
42
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
43
    # First, create an index.html with links to all the HTML
44
    # documentation files we're about to generate.
45
    template_file = 'apidoc-index.pt'
46
    template = PageTemplateFile(template_file)
47
    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.
48
    f.write(template(config=config))
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
49
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
50
    # Request the WADL from the root resource.
6770.4.5 by Francis J. Lacoste
Add comment for the uninitiated.
51
    # We do this by creating a request object asking for a WADL
52
    # representation.
10466.8.2 by Leonard Richardson
Minor cleanup.
53
    for version in config.active_versions:
10420.4.1 by Leonard Richardson
Initial implementation.
54
        url = urlparse.urljoin(allvhosts.configs['api'].rooturl, version)
55
        request = WebServiceTestRequest(version=version, environ={
56
            'SERVER_URL': url,
57
            'HTTP_HOST': allvhosts.configs['api'].hostname,
58
            'HTTP_ACCEPT': 'application/vd.sun.wadl+xml'
59
            })
60
        # We then bypass the usual publisher processing by associating
61
        # the request with the WebServicePublication (usually done  by the
62
        # publisher) and then calling the root resource - retrieved through
63
        # getApplication().
64
        request.setPublication(WebServicePublication(None))
10736.1.1 by Jonathan Lange
Use non-testing login in script required to build Launchpad
65
        setupInteractionByEmail(ANONYMOUS, request)
10420.4.1 by Leonard Richardson
Initial implementation.
66
        filename = path_template % {'version' : version}
67
        print "Writing WADL for version %s to %s." % (version, filename)
68
        f = open(filename, 'w')
69
        content = request.publication.getApplication(request)(request)
70
        f.write(content)
71
        f.close()
10466.8.1 by Leonard Richardson
Initial implementation.
72
73
        # Now, convert the WADL into an human-readable description and
74
        # put the HTML in the same directory as the WADL.
75
        html_filename = os.path.join(directory, version + ".html")
76
        print "Writing apidoc for version %s to %s" % (
77
            version, html_filename)
78
        stdout = open(html_filename, "w")
79
        subprocess.Popen(['xsltproc', stylesheet, filename], stdout=stdout)
80
        stdout.close()
81
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
82
    return 0
83
84
if __name__ == '__main__':
10556.2.5 by Leonard Richardson
Reverted changes to create-lp-wadl-and-apidoc.py.
85
    if len(sys.argv) != 2:
86
        print "Usage: %s [WADL path template]" % sys.argv[0]
87
        print " Example: %s path/to/wadl/wadl-%%(version).xml" % (
88
            sys.argv[0])
10420.4.1 by Leonard Richardson
Initial implementation.
89
        sys.exit(-1)
10556.2.5 by Leonard Richardson
Reverted changes to create-lp-wadl-and-apidoc.py.
90
    sys.exit(main(sys.argv[1]))