~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/codehosting/codeimport/tests/servers.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-19 03:58:35 UTC
  • mfrom: (13604.2.16 proper-codeimport-servers)
  • Revision ID: launchpad@pqm.canonical.com-20110819035835-jzfzwb2a3mg7485y
[r=henninge][no-qa] Run actual git and mercurial servers when testing
        code imports rather than importing from local paths.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import subprocess
21
21
import tempfile
22
22
import time
 
23
import threading
23
24
 
24
25
from bzrlib.tests.treeshape import build_tree_contents
25
26
from bzrlib.transport import Server
31
32
import dulwich.index
32
33
from dulwich.objects import Blob
33
34
from dulwich.repo import Repo as GitRepo
 
35
from dulwich.server import (
 
36
    DictBackend,
 
37
    TCPGitServer,
 
38
    )
 
39
from mercurial.ui import (
 
40
    ui as hg_ui,
 
41
    )
 
42
from mercurial.hgweb import (
 
43
    hgweb,
 
44
    server as hgweb_server,
 
45
    )
 
46
from mercurial.localrepo import localrepository
34
47
import subvertpy.ra
35
48
import svn_oo
36
49
 
204
217
        self._repository = self.createRepository(self._repository_path)
205
218
 
206
219
 
 
220
class TCPGitServerThread(threading.Thread):
 
221
    """Thread that runs a TCP Git server."""
 
222
 
 
223
    def __init__(self, backend, address, port=None):
 
224
        super(TCPGitServerThread, self).__init__()
 
225
        self.setName("TCP Git server on %s:%s" % (address, port))
 
226
        self.server = TCPGitServer(backend, address, port)
 
227
 
 
228
    def run(self):
 
229
        self.server.serve_forever()
 
230
 
 
231
    def get_address(self):
 
232
        return self.server.server_address
 
233
 
 
234
    def stop(self):
 
235
        self.server.shutdown()
 
236
 
 
237
 
207
238
class GitServer(Server):
208
239
 
209
 
    def __init__(self, repo_url):
 
240
    def __init__(self, repository_path, use_server=False):
210
241
        super(GitServer, self).__init__()
211
 
        self.repo_url = repo_url
 
242
        self.repository_path = repository_path
 
243
        self._use_server = use_server
 
244
 
 
245
    def get_url(self):
 
246
        """Return a URL to the Git repository."""
 
247
        if self._use_server:
 
248
            return 'git://%s:%d/' % self._server.get_address()
 
249
        else:
 
250
            return local_path_to_url(self.repository_path)
 
251
 
 
252
    def createRepository(self, path):
 
253
        GitRepo.init(path)
 
254
 
 
255
    def start_server(self):
 
256
        super(GitServer, self).start_server()
 
257
        self.createRepository(self.repository_path)
 
258
        if self._use_server:
 
259
            repo = GitRepo(self.repository_path)
 
260
            self._server = TCPGitServerThread(
 
261
                DictBackend({"/": repo}), "localhost", 0)
 
262
            self._server.start()
 
263
 
 
264
    def stop_server(self):
 
265
        super(GitServer, self).stop_server()
 
266
        if self._use_server:
 
267
            self._server.stop()
212
268
 
213
269
    def makeRepo(self, tree_contents):
214
 
        wd = os.getcwd()
215
 
        try:
216
 
            os.chdir(self.repo_url)
217
 
            repo = GitRepo.init(".")
218
 
            blobs = [
219
 
                (Blob.from_string(contents), filename) for (filename, contents)
220
 
                in tree_contents]
221
 
            repo.object_store.add_objects(blobs)
222
 
            root_id = dulwich.index.commit_tree(repo.object_store, [
223
 
                (filename, b.id, stat.S_IFREG | 0644)
224
 
                for (b, filename) in blobs])
225
 
            repo.do_commit(committer='Joe Foo <joe@foo.com>',
226
 
                message=u'<The commit message>', tree=root_id)
227
 
        finally:
228
 
            os.chdir(wd)
 
270
        repo = GitRepo(self.repository_path)
 
271
        blobs = [
 
272
            (Blob.from_string(contents), filename) for (filename, contents)
 
273
            in tree_contents]
 
274
        repo.object_store.add_objects(blobs)
 
275
        root_id = dulwich.index.commit_tree(repo.object_store, [
 
276
            (filename, b.id, stat.S_IFREG | 0644)
 
277
            for (b, filename) in blobs])
 
278
        repo.do_commit(committer='Joe Foo <joe@foo.com>',
 
279
            message=u'<The commit message>', tree=root_id)
 
280
 
 
281
 
 
282
class MercurialServerThread(threading.Thread):
 
283
    """A thread which runs a Mercurial http server."""
 
284
 
 
285
    def __init__(self, path, address, port=0):
 
286
        super(MercurialServerThread, self).__init__()
 
287
        self.ui = hg_ui()
 
288
        self.ui.setconfig("web", "address", address)
 
289
        self.ui.setconfig("web", "port", port)
 
290
        self.app = hgweb(path, baseui=self.ui)
 
291
        self.httpd = hgweb_server.create_server(self.ui, self.app)
 
292
        # By default the Mercurial server output goes to stdout,
 
293
        # redirect it to prevent a lot of spurious output.
 
294
        self.httpd.errorlog = StringIO()
 
295
        self.httpd.accesslog = StringIO()
 
296
 
 
297
    def get_address(self):
 
298
        return (self.httpd.addr, self.httpd.port)
 
299
 
 
300
    def run(self):
 
301
        self.httpd.serve_forever()
 
302
 
 
303
    def stop(self):
 
304
        self.httpd.shutdown()
229
305
 
230
306
 
231
307
class MercurialServer(Server):
232
308
 
233
 
    def __init__(self, repo_url):
 
309
    def __init__(self, repository_path, use_server=False):
234
310
        super(MercurialServer, self).__init__()
235
 
        self.repo_url = repo_url
 
311
        self.repository_path = repository_path
 
312
        self._use_server = use_server
 
313
 
 
314
    def get_url(self):
 
315
        if self._use_server:
 
316
            return "http://%s:%d/" % self._hgserver.get_address()
 
317
        else:
 
318
            return local_path_to_url(self.repository_path)
 
319
 
 
320
    def start_server(self):
 
321
        super(MercurialServer, self).start_server()
 
322
        self.createRepository(self.repository_path)
 
323
        if self._use_server:
 
324
            self._hgserver = MercurialServerThread(self.repository_path,
 
325
                "localhost")
 
326
            self._hgserver.start()
 
327
 
 
328
    def stop_server(self):
 
329
        super(MercurialServer, self).stop_server()
 
330
        if self._use_server:
 
331
            self._hgserver.stop()
 
332
 
 
333
    def createRepository(self, path):
 
334
        localrepository(hg_ui(), self.repository_path, create=1)
236
335
 
237
336
    def makeRepo(self, tree_contents):
238
 
        from mercurial.ui import ui
239
 
        from mercurial.localrepo import localrepository
240
 
        repo = localrepository(ui(), self.repo_url, create=1)
 
337
        repo = localrepository(hg_ui(), self.repository_path)
241
338
        for filename, contents in tree_contents:
242
 
            f = open(os.path.join(self.repo_url, filename), 'w')
 
339
            f = open(os.path.join(self.repository_path, filename), 'w')
243
340
            try:
244
341
                f.write(contents)
245
342
            finally: