5
from bzrlib.workingtree import WorkingTree
7
from juju_deployer.exception import ErrorExit
12
err_update = "Could not update branch %(path)s from %(branch_url)s\n\n "\
14
err_branch = "Could not branch %(branch_url)s to %(path)s\n\n %(output)s"
15
err_is_mod = "Couldn't determine if %(path)s was modified\n\n %(output)s"
16
err_pull = "Could not pull branch @ %(branch_url)s to %(path)s\n\n "\
18
err_cur_rev = "Could not determine current revision %(path)s\n\n "\
21
def __init__(self, path, origin, log):
26
def _call(self, args, error_msg, cwd=None, stderr=()):
27
#print " ".join(args), "in", cwd or self.path
29
stderr = stderr is None and stderr or subprocess.STDOUT
30
output = subprocess.check_output(
31
args, cwd=cwd or self.path, stderr=subprocess.STDOUT)
32
except subprocess.CalledProcessError, e:
33
#print "vcs err", " ".join(args), "[dir: %s]" % cwd
34
self.log.error(error_msg % self.get_err_msg_ctx(e))
38
def get_err_msg_ctx(self, e):
41
'branch_url': self.origin,
42
'exit_code': e.returncode,
44
'vcs': self.__class__.__name__.lower()}
46
def get_cur_rev(self):
47
raise NotImplementedError()
49
def update(self, rev=None):
50
raise NotImplementedError()
53
raise NotImplementedError()
56
raise NotImplementedError()
58
def is_modified(self):
59
raise NotImplementedError()
61
# upstream missing revisions?
66
def get_cur_rev(self):
67
params = ["bzr", "revno", "--tree"]
68
return self._call(params, self.err_cur_rev)
70
def update(self, rev=None):
71
params = ["bzr", "up"]
73
params.extend(["-r", str(rev)])
74
self._call(params, self.err_update)
77
params = ["bzr", "pull", "--remember", self.origin]
78
self._call(params, self.err_pull)
81
params = ["bzr", "branch", self.origin, self.path]
82
cwd = os.path.dirname(os.path.dirname(self.path))
85
self._call(params, self.err_branch, cwd)
87
def is_modified(self):
88
# To replace with bzr cli, we need to be able to detect changes
89
# to a wc @ a rev or @ trunk.
90
tree = WorkingTree.open(self.path)
91
return tree.has_changes()
96
def get_cur_rev(self):
97
params = ["git", "rev-parse", "HEAD"]
98
return self._call(params, self.err_cur_rev)
100
def update(self, rev=None):
101
params = ["git", "reset", "--merge"]
104
self._call(params, self.err_update)
107
params = ["git", "pull", "master"]
108
self._call(params, self.err_pull)
111
params = ["git", "clone", self.branch]
112
self._call(params, self.err_branch, os.path.dirname(self.path))
114
def is_modified(self):
115
params = ["git", "stat", "-s"]
116
return bool(self._call(params, self.err_is_mod).strip())
118
def get_origin(self):
119
params = ["git", "config", "--get", "remote.origin.url"]
120
return self._call(params, "")