Merge lp:~stefanor/ibid/update-branches into lp:ibid

Proposed by Stefano Rivera
Status: Merged
Approved by: Stefano Rivera
Approved revision: 1046
Merged at revision: 1049
Proposed branch: lp:~stefanor/ibid/update-branches
Merge into: lp:ibid
Diff against target: 128 lines (+124/-0)
1 file modified
contrib/update-branches.py (+124/-0)
To merge this branch: bzr merge lp:~stefanor/ibid/update-branches
Reviewer Review Type Date Requested Status
Keegan Carruthers-Smith Approve
Review via email: mp+82793@code.launchpad.net

Commit message

A little contrib script for managing checkouts

Description of the change

This is probably useful to others too...

To post a comment you must log in.
lp:~stefanor/ibid/update-branches updated
1046. By Stefano Rivera

Explain the purpose of update-branches

Revision history for this message
Keegan Carruthers-Smith (keegan-csmith) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'contrib/update-branches.py'
2--- contrib/update-branches.py 1970-01-01 00:00:00 +0000
3+++ contrib/update-branches.py 2011-11-19 20:00:29 +0000
4@@ -0,0 +1,124 @@
5+#!/usr/bin/env python
6+# Copyright (c) 2011, Stefano Rivera
7+# Released under terms of the MIT/X/Expat Licence. See COPYING for details.
8+#
9+# This script keeps local checkouts of all branches currently involved in
10+# merge-requests, or all active branches.
11+# It should be run inside the root of a bzr repository, e.g.:
12+# $ bzr init-repo ibid; cd ibid; update-branches -a
13+
14+import optparse
15+import os
16+import subprocess
17+import sys
18+
19+from launchpadlib.launchpad import Launchpad
20+
21+
22+def main():
23+ parser = optparse.OptionParser()
24+ parser.add_option('-a', '--acive-branches',
25+ dest='active_branches',
26+ default=False, action='store_true',
27+ help='Pull all active branches, not just ones involved '
28+ 'in merge proposals')
29+ parser.add_option('-p', '--project', metavar='PROJECT',
30+ default=os.path.basename(os.getcwd()),
31+ help='LP Project (default: current directory name)')
32+ parser.add_option('-r', '--root', metavar='DIR',
33+ default='.',
34+ help='Target directory (default: .)')
35+ parser.add_option('-s', '--shallow',
36+ default=False, action='store_true',
37+ help='Store branches directly in the root '
38+ '(default: user/branch)')
39+ parser.add_option('--anonymous',
40+ default=False, action='store_true',
41+ help='Log in to LP anonymously (may be confused by '
42+ 'identical commit counts')
43+ options, args = parser.parse_args()
44+
45+ if options.anonymous:
46+ lp = Launchpad.login_anonymously('update-branches', 'production')
47+ else:
48+ lp = Launchpad.login_with('update-branches', 'production')
49+
50+ project = lp.projects[options.project]
51+
52+ if options.active_branches:
53+ branches = project.getBranches()
54+ else:
55+ branches = []
56+ for mp in project.getMergeProposals():
57+ branches.append(mp.source_branch)
58+ branches.append(mp.target_branch)
59+
60+ update_branches(options.root, options.project, branches,
61+ options.shallow)
62+
63+
64+def update_branches(root, project_name, branches, shallow):
65+ devnull = open('/dev/null', 'w')
66+ seen = set()
67+ for branch in branches:
68+ unique_name = branch.unique_name
69+ print 'lp:' + unique_name
70+ split_unique_name = unique_name.split('/')
71+ if len(split_unique_name) != 3:
72+ print >> sys.stderr, ('Unexpected branch name: lp:%s, skipping '
73+ % unique_name)
74+ continue
75+ user, project, branch_name = split_unique_name
76+ user = user[1:]
77+ if project != project_name:
78+ print >> sys.stderr, ('Unexpected project: lp:%s, skipping '
79+ % unique_name)
80+ continue
81+
82+ if shallow:
83+ user_dir = root
84+ else:
85+ user_dir = os.path.join(root, user)
86+ if not os.path.isdir(user_dir):
87+ os.mkdir(user_dir)
88+ seen.add((user, branch_name))
89+
90+ branch_dir = os.path.join(user_dir, branch_name)
91+ if os.path.isdir(branch_dir):
92+ id_ = branch.last_scanned_id
93+ if id_ == '<email address hidden>':
94+ id_ = str(branch.revision_count)
95+ if subprocess.call(('bzr', 'log', '-r', id_),
96+ stdout=devnull, stderr=devnull,
97+ cwd=branch_dir) == 0:
98+ print "Up to date"
99+ else:
100+ subprocess.call(('bzr', 'pull', '--remember',
101+ 'lp:%s' % unique_name),
102+ cwd=branch_dir)
103+ else:
104+ subprocess.call(('bzr', 'branch', 'lp:%s' % unique_name),
105+ cwd=user_dir)
106+
107+ if not shallow:
108+ for user in os.listdir(root):
109+ if user.startswith('.'):
110+ continue
111+ if not os.path.isdir(user):
112+ continue
113+ user_dir = os.path.join(root, user)
114+ for branch_name in os.listdir(user_dir):
115+ branch_dir = os.path.join(user_dir, branch_name)
116+ if not os.path.isdir(branch_dir):
117+ continue
118+ if not os.path.isdir(os.path.join(branch_dir, '.bzr')):
119+ continue
120+ if (user, branch_name) not in seen:
121+ print ("Inactive local branch: %s/%s"
122+ % (user, branch_name))
123+
124+
125+if __name__ == '__main__':
126+ main()
127+
128+# vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches

to all changes: