~launchpad-pqm/launchpad/devel

2006 by Canonical.com Patch Queue Manager
[trivial] Add system documentation/test for execute_zcml_for_scripts.
1
Scripts and ZCML
2
----------------
3
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
4
The full Zope component architecture is available from scripts so long
5
as they call `execute_zcml_for_scripts()`.
6
7
Let's make a simple example script that uses getUtility and the database
8
to demonstrate this:
9
10
    >>> import os
11
    >>> import subprocess
12
    >>> import tempfile
13
    >>> from textwrap import dedent
14
    >>> script_file = tempfile.NamedTemporaryFile()
15
    >>> script_file.write(dedent("""\
14565.2.15 by Curtis Hovey
Moved canonical.launchpad.scripts __init__ to lp.services.scripts.
16
    ...     from lp.services.scripts import execute_zcml_for_scripts
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
17
    ...     from lp.registry.interfaces.person import IPersonSet
18
    ...     from zope.component import getUtility
19
    ... 
20
    ...     execute_zcml_for_scripts()
21
    ...     print getUtility(IPersonSet).get(1).displayname
22
    ...     """))
23
    >>> script_file.flush()
2006 by Canonical.com Patch Queue Manager
[trivial] Add system documentation/test for execute_zcml_for_scripts.
24
25
Run the script (making sure it uses the testrunner configuration).
26
14605.1.1 by Curtis Hovey
Moved canonical.config to lp.services.
27
    >>> from lp.services.config import config
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
28
    >>> bin_py = os.path.join(config.root, 'bin', 'py')
29
    >>> proc = subprocess.Popen(
30
    ...     [bin_py, script_file.name], stdout=subprocess.PIPE, stderr=None)
2006 by Canonical.com Patch Queue Manager
[trivial] Add system documentation/test for execute_zcml_for_scripts.
31
32
Check that we get the expected output.
33
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
34
    >>> print proc.stdout.read()
35
    Mark Shuttleworth
36
37
    >>> print proc.wait()
38
    0
2006 by Canonical.com Patch Queue Manager
[trivial] Add system documentation/test for execute_zcml_for_scripts.
39
40
Remove the temporary file.
41
14027.3.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
42
    >>> script_file.close()