~mthaddon/mojo/test-process-config-changes-from-diff

« back to all changes in this revision

Viewing changes to mojo/bzr.py

  • Committer: Matthew Wedgwood
  • Date: 2013-07-15 23:00:19 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: matthew.wedgwood@canonical.com-20130715230019-nb2n56zplj5snnq4
WIP: add bzr module, tests, and refactored actions for multi-phase deploy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from bzrlib import (
 
3
    errors,
 
4
    urlutils,
 
5
)
 
6
from bzrlib.branch import Branch
 
7
from bzrlib.bzrdir import BzrDir
 
8
from bzrlib.plugin import load_plugins
 
9
 
 
10
load_plugins()
 
11
 
 
12
 
 
13
class BzrBranch(object):
 
14
    "Simplified bazaar branch API"
 
15
    def __init__(self, location):
 
16
        if location.startswith("file://"):
 
17
            location = location[7:]
 
18
        self.location = location
 
19
        if urlutils.is_url(self.location):
 
20
            self.is_local = False
 
21
        else:
 
22
            if not os.path.isabs(self.location):
 
23
                cwd = os.getcwd()
 
24
                self.location = os.path.normpath(os.path.join(cwd, self.location))
 
25
            self.is_local = True
 
26
        self._branch = None
 
27
 
 
28
    @property
 
29
    def bzr_branch(self):
 
30
        if not self._branch:
 
31
            try:
 
32
                self._branch = Branch.open(self.location)
 
33
            except errors.NotBranchError:
 
34
                return None
 
35
        return self._branch
 
36
 
 
37
    @property
 
38
    def is_remote(self):
 
39
        return not self.is_local
 
40
 
 
41
    @property
 
42
    def is_branch(self):
 
43
        if self.bzr_branch is None:
 
44
            return False
 
45
        return True
 
46
 
 
47
    @property
 
48
    def revno(self):
 
49
        if self.bzr_branch:
 
50
            return self.bzr_branch.revno()
 
51
 
 
52
    @property
 
53
    def parent(self):
 
54
        if self.is_remote or self.bzr_branch is None:
 
55
            return None
 
56
        return self.bzr_branch.get_parent()
 
57
 
 
58
    def clone(self, dest_dir):
 
59
        if not self.bzr_branch:
 
60
            return None
 
61
        self.bzr_branch.bzrdir.sprout(dest_dir)
 
62
        return BzrBranch(dest_dir)
 
63
 
 
64
    @property
 
65
    def revno_tree(self):
 
66
        if self.is_remote:
 
67
            return None
 
68
        pass
 
69
 
 
70
    def create(self):
 
71
        "bzr init"
 
72
        if self.is_local:
 
73
            BzrDir.create_branch_convenience(self.location)
 
74
 
 
75
    def pull(self, src_location=None, overwrite=False, revno=None):
 
76
        if not self.is_branch:
 
77
            return False
 
78
        if src_location is None and self.is_local:
 
79
            src_location = self.parent
 
80
        other = BzrBranch(src_location)
 
81
        if other.is_branch:
 
82
            self.bzr_branch.pull(other.bzr_branch,
 
83
                                 overwrite=overwrite,
 
84
                                 stop_revision=revno)
 
85
            return True
 
86
        else:
 
87
            return False
 
88
 
 
89
    def push(self, dest_location, overwrite=False):
 
90
        dest_branch = BzrBranch(dest_location)
 
91
        if not dest_branch.is_branch:
 
92
            return None
 
93
        dest_branch.bzr_branch.push(self.bzr_branch, overwrite=overwrite)
 
94
        return dest_branch