2007-07-27 10:33:19 +02:00
|
|
|
#!/usr/bin/python
|
2007-11-07 20:05:37 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-08-10 21:57:25 +01:00
|
|
|
#
|
2008-08-11 20:06:35 +02:00
|
|
|
# Copyright (C) 2007 Canonical Ltd.
|
2007-08-04 13:52:30 +02:00
|
|
|
# Created by Daniel Holbach <daniel.holbach@ubuntu.com>
|
2008-08-10 21:57:25 +01:00
|
|
|
# Modified by Jonathan Patrick Davies <jpds@ubuntu.com>
|
|
|
|
#
|
2008-08-11 20:06:35 +02:00
|
|
|
# ##################################################################
|
2008-08-10 21:57:25 +01:00
|
|
|
#
|
2008-08-11 20:06:35 +02:00
|
|
|
# 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; version 3.
|
|
|
|
#
|
|
|
|
# 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.
|
2008-08-10 21:57:25 +01:00
|
|
|
#
|
2008-08-11 20:06:35 +02:00
|
|
|
# See file /usr/share/common-licenses/GPL-3 for more details.
|
2008-08-10 21:57:25 +01:00
|
|
|
#
|
2008-08-11 20:06:35 +02:00
|
|
|
# ##################################################################
|
2007-08-04 13:52:30 +02:00
|
|
|
#
|
|
|
|
# This script is used to checkout or branch all the Bazaar branches
|
2008-08-14 10:43:20 +01:00
|
|
|
# of a Launchpad team.
|
|
|
|
#
|
2007-07-27 10:33:19 +02:00
|
|
|
|
|
|
|
import os
|
2008-08-10 21:53:43 +01:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import urllib2
|
|
|
|
from optparse import OptionParser
|
2007-07-27 10:33:19 +02:00
|
|
|
|
|
|
|
def main():
|
2008-08-10 21:53:43 +01:00
|
|
|
usage = "Usage: %prog [-d <directory>] -t <team> [-o <operation>]"
|
|
|
|
usage += "\nUsage: %prog <team>"
|
|
|
|
optParser = OptionParser(usage)
|
|
|
|
|
|
|
|
# Our options.
|
2008-08-14 10:43:20 +01:00
|
|
|
optParser.add_option("-d", "--directory", action = "store", type = "string",
|
|
|
|
dest = "directory", default = ".",
|
|
|
|
help = "Directory to download branches to.")
|
|
|
|
optParser.add_option("-t", "--team", action = "store", type = "string",
|
|
|
|
dest = "lpteam", help = "Launchpad team to download branches from.")
|
|
|
|
optParser.add_option("-o", "--operation", action = "store", type = "string",
|
|
|
|
dest = "operation", default = "branch",
|
|
|
|
help = "Whether to branch or checkout the Bazaar branches. May be " \
|
|
|
|
"either 'branch' or 'checkout'.")
|
2008-08-10 21:53:43 +01:00
|
|
|
|
|
|
|
(options, args) = optParser.parse_args()
|
|
|
|
|
2008-08-14 10:43:20 +01:00
|
|
|
# Parse our options aka Russian roulette time.
|
|
|
|
if len(args) != 1 and options.lpteam == None:
|
|
|
|
optParser.error("No team has been specified.")
|
2008-08-11 23:45:07 +01:00
|
|
|
|
2008-08-10 21:53:43 +01:00
|
|
|
# Launchpad team setting.
|
|
|
|
if options.lpteam:
|
2008-08-14 10:43:20 +01:00
|
|
|
team = str(options.lpteam).lower()
|
|
|
|
if args:
|
|
|
|
team = str(args[0]).lower()
|
2008-08-10 21:53:43 +01:00
|
|
|
|
2008-08-14 10:43:20 +01:00
|
|
|
directory = options.directory
|
|
|
|
|
|
|
|
# Dictionary settings.
|
|
|
|
if not os.path.isdir(directory): # Check that it is a directory.
|
|
|
|
optParser.error("%s is not a valid directory." % directory)
|
|
|
|
|
2008-08-10 21:53:43 +01:00
|
|
|
# Type of Bazaar operation to perform.
|
2008-08-14 10:43:20 +01:00
|
|
|
operation_type = str(options.operation).lower()
|
2008-08-10 21:53:43 +01:00
|
|
|
|
2008-08-14 10:43:20 +01:00
|
|
|
# Got an argument, check if it is valid.
|
2008-08-14 10:50:04 +01:00
|
|
|
if operation_type not in ("branch", "checkout"):
|
|
|
|
optParser.error("Invalid operation '%s' for '-o' flag." % \
|
|
|
|
operation_type)
|
2008-08-14 10:43:20 +01:00
|
|
|
|
2008-08-10 21:53:43 +01:00
|
|
|
|
2008-08-14 10:43:20 +01:00
|
|
|
# Fetch our current directory to return to later.
|
2007-07-27 10:33:19 +02:00
|
|
|
pwd = os.getcwd()
|
2008-08-14 10:43:20 +01:00
|
|
|
|
|
|
|
# Change to the specified directory.
|
2008-08-10 21:53:43 +01:00
|
|
|
os.chdir(directory)
|
2007-07-27 10:33:19 +02:00
|
|
|
|
2008-08-10 21:53:43 +01:00
|
|
|
# Try to open the teams code page.
|
|
|
|
try:
|
|
|
|
sock = urllib2.urlopen("https://code.launchpad.net/~%s" % team)
|
|
|
|
except urllib2.HTTPError:
|
2008-08-11 07:38:59 +01:00
|
|
|
print >> sys.stderr, "The page https://code.launchpad.net/~%s does " \
|
2008-08-10 21:53:43 +01:00
|
|
|
"not exist." % team
|
|
|
|
print >> sys.stderr, "Perhaps invalid team name?"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2007-07-27 10:33:19 +02:00
|
|
|
branch_list_page = sock.read()
|
|
|
|
sock.close()
|
2008-08-10 21:53:43 +01:00
|
|
|
|
2007-07-27 10:33:19 +02:00
|
|
|
branch_page_urls_regex = r'.*<a href="/(~%s/.*)".*' % team
|
|
|
|
branch_page_urls = re.findall(branch_page_urls_regex, branch_list_page)
|
2008-08-10 21:53:43 +01:00
|
|
|
|
2008-08-12 00:09:01 +01:00
|
|
|
# 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)
|
|
|
|
|
2008-08-14 10:43:20 +01:00
|
|
|
print "Downloading all branches for the team '%s'. This may take some " \
|
|
|
|
"time." % team
|
|
|
|
|
2008-08-12 00:09:01 +01:00
|
|
|
try:
|
|
|
|
os.makedirs(team)
|
|
|
|
except:
|
|
|
|
pass
|
2008-08-14 10:43:20 +01:00
|
|
|
|
2008-08-12 00:09:01 +01:00
|
|
|
os.chdir(team)
|
2008-08-10 21:53:43 +01:00
|
|
|
|
2007-07-27 10:33:19 +02:00
|
|
|
for url in branch_page_urls:
|
2008-08-10 21:53:43 +01:00
|
|
|
sock = urllib2.urlopen("https://code.launchpad.net/%s" % url)
|
|
|
|
branch_page = sock.read()
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
branch_url_regex = r'<tt>bzr branch lp:~(.*)</tt>'
|
2007-07-27 10:33:19 +02:00
|
|
|
branch_url = re.findall(branch_url_regex, branch_page)
|
2008-08-11 07:57:58 +01:00
|
|
|
print "Downloading branch: lp:~%s." % branch_url[0]
|
2008-08-10 21:53:43 +01:00
|
|
|
|
2007-07-27 10:33:19 +02:00
|
|
|
if branch_url[0]:
|
|
|
|
product = branch_url[0].split("/")[-2]
|
|
|
|
branch_nick = branch_url[0].split("/")[-1]
|
2008-08-10 21:53:43 +01:00
|
|
|
|
|
|
|
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" % branch_url[0]])
|
|
|
|
else:
|
|
|
|
os.chdir(branch_nick)
|
|
|
|
subprocess.call(["bzr", "merge", "--pull", "--remember"])
|
|
|
|
os.chdir(os.path.join(directory, team))
|
2007-07-27 10:33:19 +02:00
|
|
|
|
|
|
|
os.chdir(pwd)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|