~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
#
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
8687.15.4 by Karl Fogel
Add the copyright header block to more files; tweak format in a few files.
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.
11369.3.1 by Benji York
checkpoint
7
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
8
Example:
11369.3.1 by Benji York
checkpoint
9
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
10
    % LPCONFIG=development bin/py utilities/create-lp-wadl-and-apidoc.py \\
11
      "lib/canonical/launchpad/apidoc/wadl-development-%(version)s.xml"
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
12
"""
11604.1.6 by Benji York
more review tweaks
13
import _pythonpath # Not lint, actually needed.
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
14
11929.14.1 by Benji York
parallelize the WADL generation
15
from multiprocessing import Process
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
16
import optparse
10466.8.1 by Leonard Richardson
Initial implementation.
17
import os
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
18
import sys
10420.4.1 by Leonard Richardson
Initial implementation.
19
20
from zope.component import getUtility
10466.8.1 by Leonard Richardson
Initial implementation.
21
from zope.pagetemplate.pagetemplatefile import PageTemplateFile
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
22
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
23
from canonical.launchpad.rest.wadl import generate_wadl, generate_html
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
24
from canonical.launchpad.scripts import execute_zcml_for_scripts
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.
25
from canonical.launchpad.systemhomes import WebServiceApplication
10420.4.1 by Leonard Richardson
Initial implementation.
26
from lazr.restful.interfaces import IWebServiceConfiguration
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
27
10736.1.1 by Jonathan Lange
Use non-testing login in script required to build Launchpad
28
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
29
def write(filename, content):
30
    """Replace the named file with the given string."""
31
    f = open(filename, 'w')
32
    f.write(content)
33
    f.close()
34
35
11929.14.1 by Benji York
parallelize the WADL generation
36
def make_files(path_template, directory, version, force):
37
    wadl_filename = path_template % {'version': version}
38
    # If the WADL file doesn't exist or we're being forced to regenerate
39
    # it...
40
    if (not os.path.exists(wadl_filename) or force):
41
        print "Writing WADL for version %s to %s." % (
42
            version, wadl_filename)
43
        write(wadl_filename, generate_wadl(version))
44
    else:
45
        print "Skipping already present WADL file:", wadl_filename
46
47
    # Now, convert the WADL into an human-readable description and
48
    # put the HTML in the same directory as the WADL.
49
    html_filename = os.path.join(directory, version + ".html")
50
    # If the HTML file doesn't exist or we're being forced to regenerate
51
    # it...
52
    if (not os.path.exists(html_filename) or force):
53
        print "Writing apidoc for version %s to %s" % (
54
            version, html_filename)
55
        write(html_filename, generate_html(wadl_filename,
56
            suppress_stderr=False))
57
    else:
58
        print "Skipping already present HTML file:", html_filename
59
60
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
61
def main(path_template, force=False):
10420.4.2 by Leonard Richardson
Response to feedback.
62
    WebServiceApplication.cached_wadl = None # do not use cached file version
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
63
    execute_zcml_for_scripts()
10420.4.1 by Leonard Richardson
Initial implementation.
64
    config = getUtility(IWebServiceConfiguration)
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
65
    directory = os.path.dirname(path_template)
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
66
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
67
    # First, create an index.html with links to all the HTML
68
    # documentation files we're about to generate.
69
    template_file = 'apidoc-index.pt'
70
    template = PageTemplateFile(template_file)
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
71
    index_filename = os.path.join(directory, "index.html")
72
    print "Writing index:", index_filename
73
    f = open(index_filename, 'w')
10524.1.3 by Leonard Richardson
Simplified template code even more thanks to gary's code.
74
    f.write(template(config=config))
10524.1.2 by Leonard Richardson
Style the apidoc index and simplify its generation.
75
11929.14.1 by Benji York
parallelize the WADL generation
76
    # Start a process to build each set of WADL and HTML files.
77
    processes = []
10466.8.2 by Leonard Richardson
Minor cleanup.
78
    for version in config.active_versions:
11929.14.1 by Benji York
parallelize the WADL generation
79
        p = Process(target=make_files,
80
            args=(path_template, directory, version, force))
81
        p.start()
82
        processes.append(p)
10466.8.1 by Leonard Richardson
Initial implementation.
83
11929.14.1 by Benji York
parallelize the WADL generation
84
    # Wait for all the subprocesses to finish.
85
    for p in processes:
86
        p.join()
10466.8.1 by Leonard Richardson
Initial implementation.
87
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
88
    return 0
89
11604.1.3 by Benji York
fix lint
90
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
91
def parse_args(args):
92
    usage = "usage: %prog [options] PATH_TEMPLATE"
93
    parser = optparse.OptionParser(usage=usage)
94
    parser.add_option(
95
        "--force", action="store_true",
96
        help="Replace any already-existing files.")
97
    parser.set_defaults(force=False)
98
    options, args = parser.parse_args(args)
99
    if len(args) != 2:
100
        parser.error("A path template is required.")
101
102
    return options, args
103
11604.1.3 by Benji York
fix lint
104
6770.2.1 by Francis J. Lacoste
Add create-lp-wadl.py
105
if __name__ == '__main__':
11604.1.1 by Benji York
extract just the refactorings from my (now abandoned) check-in-wadl branch and
106
    options, args = parse_args(sys.argv)
107
    sys.exit(main(args[1], options.force))