~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/mock-code-import

  • Committer: jml at canonical
  • Date: 2008-02-19 06:28:07 UTC
  • mto: This revision was merged to the branch mainline in revision 5742.
  • Revision ID: jml@canonical.com-20080219062807-nabgj07p3t0p94v6
Helpful utility for running a branch to be imported.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.4
 
2
# Copyright 2008 Canonical Ltd.  All rights reserved.
 
3
 
 
4
"""Make a Subversion repostiory and then make a CodeImportJob for it."""
 
5
 
 
6
# XXX: JonathanLange 2008-01-03: This is deliberately horrible.
 
7
# You can make it nicer if you want.
 
8
 
 
9
import _pythonpath
 
10
 
 
11
import os
 
12
import shutil
 
13
from subprocess import PIPE, Popen
 
14
import tempfile
 
15
import transaction
 
16
 
 
17
from zope.component import getUtility
 
18
 
 
19
from canonical.launchpad.interfaces import (
 
20
    BranchType, CodeImportReviewStatus, ICodeImportSet,
 
21
    ICodeImportJobWorkflow, ILaunchpadCelebrities, RevisionControlSystems)
 
22
from canonical.launchpad.scripts import execute_zcml_for_scripts
 
23
from canonical.launchpad.testing import LaunchpadObjectFactory
 
24
 
 
25
 
 
26
def shell(*args):
 
27
    print ' '.join(args)
 
28
    return Popen(args, stdout=PIPE).communicate()[0]
 
29
 
 
30
 
 
31
def make_temp_svn_repo():
 
32
    directory = tempfile.mkdtemp()
 
33
    repo = os.path.join(directory, 'repo')
 
34
    os.mkdir(repo)
 
35
    shell('svnadmin', 'create', repo)
 
36
    return os.path.abspath(repo)
 
37
 
 
38
 
 
39
def in_temp_dir(function):
 
40
    def new_function(*args, **kwargs):
 
41
        curr_dir = os.getcwd()
 
42
        temp_dir = tempfile.mkdtemp()
 
43
        os.chdir(temp_dir)
 
44
        try:
 
45
            return function(*args, **kwargs)
 
46
        finally:
 
47
            os.chdir(curr_dir)
 
48
            shutil.rmtree(temp_dir)
 
49
    return new_function
 
50
 
 
51
 
 
52
@in_temp_dir
 
53
def populate_repo(url):
 
54
    url = '/'.join([url, 'trunk'])
 
55
    shell('svn', 'mkdir', '-m', '"Make trunk"', url)
 
56
    shell('svn', 'co', url)
 
57
    os.chdir('trunk')
 
58
    file = open('README', 'w')
 
59
    file.write('No real content\n')
 
60
    file.close()
 
61
    shell('svn', 'add', 'README')
 
62
    shell('svn', 'commit', '-m', '"Add a file."')
 
63
    return os.path.abspath(os.getcwd())
 
64
 
 
65
 
 
66
def make_import_job(svn_url):
 
67
    factory = LaunchpadObjectFactory()
 
68
    vcs_imports = getUtility(ILaunchpadCelebrities).vcs_imports
 
69
    branch = factory.makeBranch(
 
70
        BranchType.IMPORTED, owner=vcs_imports)
 
71
    registrar = factory.makePerson()
 
72
    code_import = getUtility(ICodeImportSet).new(
 
73
        registrar, branch, RevisionControlSystems.SVN,
 
74
        svn_branch_url=svn_url)
 
75
    workflow = getUtility(ICodeImportJobWorkflow)
 
76
    code_import.updateFromData(
 
77
        {'review_status': CodeImportReviewStatus.REVIEWED}, registrar)
 
78
    job = workflow.newJob(code_import)
 
79
    return job
 
80
 
 
81
 
 
82
def main():
 
83
    execute_zcml_for_scripts(use_web_security=False)
 
84
    repo_path = make_temp_svn_repo()
 
85
    working_path = populate_repo('file://' + repo_path)
 
86
    job = make_import_job(
 
87
        'svn://localhost:8083/' + os.path.basename(repo_path) + '/'
 
88
        + os.path.basename(working_path))
 
89
    transaction.commit()
 
90
    print
 
91
    print "REPOSITORY:", repo_path
 
92
    print "ID:", job.id
 
93
    print "URL:", job.code_import.svn_branch_url
 
94
    print
 
95
    print "RUNNING Subversion server. Press Ctrl-C to terminate."
 
96
    shell('svnserve', '-d', '--foreground', '--listen-port', '8083', '--root',
 
97
          os.path.dirname(repo_path))
 
98
    print
 
99
 
 
100
 
 
101
if __name__ == '__main__':
 
102
    main()