[ Michael Bienia ]

get-branches: Use the LP API to obtain a list of branches of a team.
This commit is contained in:
Michael Bienia 2010-04-16 12:29:02 +02:00
parent 299c397d63
commit a2a92f02d3
2 changed files with 51 additions and 76 deletions

7
debian/changelog vendored
View File

@ -1,3 +1,10 @@
ubuntu-dev-tools (0.99) UNRELEASED; urgency=low
[ Michael Bienia ]
* get-branches: Use the LP API to obtain a list of branches of a team.
-- Michael Bienia <geser@ubuntu.com> Fri, 16 Apr 2010 12:25:35 +0200
ubuntu-dev-tools (0.98) lucid; urgency=low ubuntu-dev-tools (0.98) lucid; urgency=low
[ Ryan Kavanagh ] [ Ryan Kavanagh ]

View File

@ -25,11 +25,10 @@
# #
import os import os
import re
import subprocess import subprocess
import sys import sys
import urllib2
from optparse import OptionParser from optparse import OptionParser
from ubuntutools.lp.lpapicache import PersonTeam
def main(): def main():
usage = "Usage: %prog [-d <directory>] -t <team> [-o <operation>]" usage = "Usage: %prog [-d <directory>] -t <team> [-o <operation>]"
@ -49,93 +48,62 @@ def main():
(options, args) = optParser.parse_args() (options, args) = optParser.parse_args()
# Fetch our current directory to return to later.
pwd = os.getcwd()
# Parse our options. # Parse our options.
if len(args) != 1 and options.lpteam == None: if len(args) != 1 and options.lpteam == None:
optParser.error("No team has been specified.") optParser.error("No team has been specified.")
# Launchpad team setting.
if options.lpteam:
team = str(options.lpteam).lower()
if args:
team = str(args[0]).lower()
directory = options.directory
# Dictionary settings. # Dictionary settings.
directory = options.directory
if not os.path.isdir(directory): # Check that it is a directory. if not os.path.isdir(directory): # Check that it is a directory.
optParser.error("%s is not a valid directory." % directory) optParser.error("%s is not a valid directory." % directory)
os.chdir(directory)
# Type of Bazaar operation to perform. # Type of Bazaar operation to perform.
operation_type = str(options.operation).lower() operation_type = options.operation.lower()
# Got an argument, check if it is valid.
if operation_type not in ("branch", "checkout"): if operation_type not in ("branch", "checkout"):
optParser.error("Invalid operation '%s' for '-o' flag." % \ optParser.error("Invalid operation '%s' for '-o' flag." % \
operation_type) operation_type)
# Launchpad team setting.
# Fetch our current directory to return to later. if options.lpteam:
pwd = os.getcwd() team = options.lpteam.lower()
if args:
# Change to the specified directory. team = args[0].lower()
os.chdir(directory)
# Try to open the teams code page.
try: try:
sock = urllib2.urlopen("https://code.launchpad.net/~%s" % team) team = PersonTeam(team)
except urllib2.HTTPError: except KeyError:
print >> sys.stderr, "The page https://code.launchpad.net/~%s does " \ print >> sys.stderr, "E: The team '%s' doesn't exist." % team
"not exist." % team
print >> sys.stderr, "Perhaps invalid team name?"
sys.exit(1)
branch_list_page = sock.read() # Get a list of branches
sock.close() branches = team.getBranches()
branch_page_urls_regex = r'.*href="/(~%s/.*)">lp:.*' % team print "Downloading all branches for the '%s' team. This may take some " \
branch_page_urls = re.findall(branch_page_urls_regex, branch_list_page) "time." % team.display_name
# Check the team actually has branches.
if len(branch_page_urls) == 0:
print "The team '%s' does not have any branches on Launchpad." % team
sys.exit(0)
print "Downloading all branches for the team '%s'. This may take some " \
"time." % team
try: try:
os.makedirs(team) os.makedirs(team.name)
except: except:
pass pass
os.chdir(team) os.chdir(team.name)
for url in branch_page_urls: for branch in branches:
sock = urllib2.urlopen("https://code.launchpad.net/%s" % url) project_name = branch.project.name
branch_page = sock.read() if not os.path.exists(project_name):
sock.close() os.makedirs(project_name)
os.chdir(project_name)
branch_url_regex = r'<dd>bzr branch <span.*>lp:(.*)</span></dd>' if not os.path.exists(branch.name):
branch_url = re.findall(branch_url_regex, branch_page) print "Branching %s ..." % branch.display_name
subprocess.call(["bzr", operation_type, branch.bzr_identity, branch.name])
if branch_url[0]:
print "Downloading branch: lp:%s (%s)." % (branch_url[0], url)
product = url.split("/")[-2]
branch_nick = url.split("/")[-1]
else: else:
continue print "Merging %s ..." % branch.display_name
os.chdir(branch.name)
print branch_nick, product, os.getcwd()
if not os.path.exists(product):
os.makedirs(product)
os.chdir(product)
if not os.path.exists(branch_nick):
subprocess.call(["bzr", operation_type, "lp:%s" % url])
else:
os.chdir(branch_nick)
subprocess.call(["bzr", "merge", "--pull", "--remember"]) subprocess.call(["bzr", "merge", "--pull", "--remember"])
os.chdir(os.path.join(directory, team)) os.chdir(os.path.join(directory, team.name))
os.chdir(pwd) os.chdir(pwd)
sys.exit(0) sys.exit(0)