~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
#!/usr/bin/python -S
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Create dummy Bazaar branches for all HOSTED branches in the database.

The Launchpad sample data includes a number of HOSTED branches that users can
branch from, push to and view on the website. However, some of these things
will break if we are missing the actual Bazaar branches, so we have a script
to create them.

NOTE: This script will delete any existing sample data branches, so that the
sample data on the filesystem is consistent with the sample data in the
database.
"""

import _pythonpath

import os
import shutil
import sys
import tempfile

import transaction
from zope.component import getUtility

# Until we completely kill canonical.launchpad.database.__init__.py and remove
# all of the import *'s we need to import it first here.
import canonical.launchpad.database

from lp.code.enums import BranchType
from lp.code.model.branch import Branch
from lp.codehosting.tests.helpers import make_bazaar_branch_and_tree
from canonical.config import config
from canonical.database.sqlbase import sqlvalues
from lp.services.scripts import execute_zcml_for_scripts


def main(argv):
    os.environ['BZR_HOME'] = tempfile.mkdtemp()
    if os.path.exists(config.codehosting.mirrored_branches_root):
        shutil.rmtree(config.codehosting.mirrored_branches_root)
    execute_zcml_for_scripts()
    try:
        branches = Branch.select(
            "Branch.branch_type = %s" % sqlvalues(BranchType.HOSTED))
        for branch in branches:
            make_bazaar_branch_and_tree(branch)
    finally:
        transaction.abort()
    print "Created %d branches based on sample data." % len(list(branches))


if __name__ == '__main__':
    main(sys.argv)