~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
PO import script
================

    >>> from lp.translations.model.potemplate import POTemplate
    >>> from lp.app.interfaces.launchpad import ILaunchpadCelebrities
    >>> from lp.registry.interfaces.person import IPersonSet
    >>> from lp.translations.enums import RosettaImportStatus
    >>> from lp.translations.interfaces.translationimportqueue import (
    ...     ITranslationImportQueue,
    ...     )
    >>> from canonical.config import config
    >>> import datetime
    >>> import pytz
    >>> UTC = pytz.timezone('UTC')
    >>> rosetta_experts = getUtility(ILaunchpadCelebrities).rosetta_experts

Login as an admin to be able to do changes to the import queue.

    >>> login('carlos@canonical.com')

    >>> translation_import_queue = getUtility(ITranslationImportQueue)
    >>> import transaction

We don't care about a POTemplate we are working with, so just pick any.

    >>> potemplate = POTemplate.get(1)

Provide a POFile with Last-Translator set to a user not existing in
the sampledata.

    >>> print getUtility(IPersonSet).getByEmail('danilo@canonical.com')
    None

    >>> pofile = potemplate.newPOFile('sr')
    >>> pofile_content = r'''
    ... msgid ""
    ... msgstr ""
    ... "PO-Revision-Date: 2005-06-04 20:41+0100\n"
    ... "Last-Translator: Danilo Ĺ egan <danilo@canonical.com>\n"
    ... "Content-Type: text/plain; charset=UTF-8\n"
    ... "X-Rosetta-Export-Date: %s\n"
    ...
    ... msgid "Foo %%s"
    ... msgstr "Bar"
    ... ''' % datetime.datetime.now(UTC).isoformat()

We clean the import queue.

    >>> for entry in translation_import_queue:
    ...     translation_import_queue.remove(entry)
    >>> translation_import_queue.countEntries()
    0

Product must have privileges which allow suggestions from those
without special privileges, like "cprov": structured privileges are
enough (but we could go with "open" as well, which would allow not
only suggestions but full translations as well).

    >>> from lp.translations.enums import TranslationPermission
    >>> product = pofile.potemplate.productseries.product
    >>> product.translationpermission = TranslationPermission.STRUCTURED

We add a PO file to the import queue, approving it along the way.

    >>> cprov = getUtility(IPersonSet).getByName('cprov')
    >>> entry = translation_import_queue.addOrUpdateEntry(
    ...     pofile.path, pofile_content, False, cprov,
    ...     sourcepackagename=pofile.potemplate.sourcepackagename,
    ...     distroseries=pofile.potemplate.distroseries,
    ...     productseries=pofile.potemplate.productseries)
    >>> entry.pofile = pofile
    >>> entry.setStatus(RosettaImportStatus.APPROVED, rosetta_experts)
    >>> transaction.commit()
    >>> translation_import_queue.countEntries()
    1

Now we run the import script, making sure that the DB user it runs under
has enough privileges for the script to run to completion.

    >>> import os
    >>> import sys
    >>> import subprocess
    >>> script = os.path.join(config.root, "cronscripts",
    ...                       "rosetta-poimport.py")
    >>> process = subprocess.Popen([sys.executable, script],
    ...                            stdout=subprocess.PIPE,
    ...                            stderr=subprocess.PIPE,)
    >>> stdout, stderr = process.communicate()
    >>> process.returncode
    0
    >>> print stderr
    INFO    Creating lockfile: /var/lock/launchpad-rosetta-poimport.lock
    INFO    Importing: Serbian (sr) ... of evolution-2.2 in Evolution trunk
    INFO    Import requests completed.
    <BLANKLINE>
    >>> transaction.commit()

A new Account for 'danilo@canonical.com' is created.

    >>> danilo = getUtility(IPersonSet).getByEmail('danilo@canonical.com')
    >>> danilo.displayname
    u'Danilo \u0160egan'