Rev 1: Initial framework of a command to push and spawn ssh bzr update in http://bzr.arbash-meinel.com/plugins/push_and_update

John Arbash Meinel john at arbash-meinel.com
Mon Feb 12 21:29:56 GMT 2007


At http://bzr.arbash-meinel.com/plugins/push_and_update

------------------------------------------------------------
revno: 1
revision-id: john at arbash-meinel.com-20070212212954-webb6mimejfcq7ug
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: push_and_update
timestamp: Mon 2007-02-12 15:29:54 -0600
message:
  Initial framework of a command to push and spawn ssh bzr update
added:
  __init__.py                    __init__.py-20070212212941-xifrmnv3jrbcv28z-1
  push_and_update.py             push_and_update.py-20070212212941-xifrmnv3jrbcv28z-2
-------------- next part --------------
=== added file '__init__.py'
--- a/__init__.py	1970-01-01 00:00:00 +0000
+++ b/__init__.py	2007-02-12 21:29:54 +0000
@@ -0,0 +1,48 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""A plugin for pushing and updating a remote repository.
+
+This assumes that you are pushing to an sftp:// or bzr+ssh:// location, and
+that bzr is installed on the remote machine.
+
+All it really does is:
+
+bzr push REMOTE
+ssh REMOTE
+bzr update
+"""
+
+from bzrlib import (
+    commands,
+    )
+
+
+class cmd_push_and_update(commands.Command):
+    """Push to a remote location, and update the working tree.
+
+    This assumes that you have ssh access to the remote location, and that
+    'bzr' is installed remotely.
+    """
+
+    takes_args = ['location?']
+
+    def run(self, location=None):
+        from push_and_update import push_and_update_helper
+        return push_and_update_helper(location=location)
+
+
+commands.register_command(cmd_push_and_update)

=== added file 'push_and_update.py'
--- a/push_and_update.py	1970-01-01 00:00:00 +0000
+++ b/push_and_update.py	2007-02-12 21:29:54 +0000
@@ -0,0 +1,81 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Helper functions for pushing and updating a remote tree."""
+
+import subprocess
+
+from bzrlib import (
+    branch,
+    bzrdir,
+    errors,
+    transport,
+    )
+
+
+def push_and_update_helper(location=None):
+    """Push to a remote location, and update it as well"""
+    local_branch, relpath = branch.Branch.open_containing('.')
+
+    if location is None:
+        stored_loc = local_branch.get_push_location()
+        if stored_loc is None:
+            raise errors.BzrCommandError('no push location known or specified.')
+        else:
+            print 'Using saved location: %s\n' % (stored_loc,)
+        location = stored_loc
+
+    try:
+        target_branch = branch.Branch.open(location)
+    except errors.NotBranchError:
+        target_bzrdir = bzrdir.BzrDir.create(location)
+        target_branch = local_branch.sprout(target_bzrdir)
+
+    push_func = getattr(local_branch, 'push', None)
+    if push_func is not None:
+        # bzr-0.15 introduces Branch.push() instead of only having
+        # Branch.pull()
+        push_func(target_branch)
+    else:
+        target_branch.pull(local_branch)
+
+    try:
+        target_wt = target_branch.bzrdir.open_workingtree()
+    except errors.NoWorkingTree:
+        pass #No remote working tree to update
+    except errors.NotLocalUrl:
+        # we have something to update
+        update_remote_branch(target_branch)
+    else:
+        # Push will update a local url
+        return
+
+
+def update_remote_branch(target_branch):
+    """Run 'bzr update' in a remote branch."""
+    (scheme, user, password, host,
+     port, path) = transport.split_url(branch.base)
+
+    if username:
+        user = '%s@' % (username,)
+    else:
+        user = ''
+
+    if port:
+        port = ':%s' % (port,)
+    cmd = ['ssh', user+host+port, 'bzr', 'update', path]
+
+    subprocess.call(cmd)



More information about the bazaar-commits mailing list