Debian Codegraph @

bin/lp-get-branches

raw
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2007 Canonical Ltd.
5 # Created by Daniel Holbach <[email protected]>
6 # Modified by Jonathan Patrick Davies <[email protected]>
7 #
8 # ##################################################################
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; version 3.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # See file /usr/share/common-licenses/GPL-3 for more details.
20 #
21 # ##################################################################
22 #
23 # This script is used to checkout or branch all the Bazaar branches
24 # of a Launchpad team.
25 #
26
27 import os
28 import subprocess
29 import sys
30 from lptools import config
31 from optparse import OptionParser
32
33 def main():
34 usage = "Usage: %prog [-d <directory>] -t <team> [-o <operation>]"
35 usage += "\nUsage: %prog <team>"
36 opt_parser = OptionParser(usage)
37
38 # Our options.
39 opt_parser.add_option("-d", "--directory", action="store", type="string",
40 dest="directory", default=os.getcwd(),
41 help="Directory to download branches to.")
42 opt_parser.add_option("-t", "--team", action="store", type="string",
43 dest="lpteam",
44 help="Launchpad team to download branches from.")
45 opt_parser.add_option("-o", "--operation", action="store", type="string",
46 dest="operation", default="branch",
47 help="Whether to branch or checkout the Bazaar "
48 "branches. May be either 'branch' or "
49 "'checkout'.")
50
51 (options, args) = opt_parser.parse_args()
52
53 launchpad = config.get_launchpad("get-branches")
54
55 # Fetch our current directory to return to later.
56 pwd = os.getcwd()
57
58 # Parse our options.
59 if len(args) != 1 and options.lpteam == None:
60 opt_parser.error("No team has been specified.")
61
62 # Dictionary settings.
63 directory = options.directory
64 if not os.path.isdir(directory): # Check that it is a directory.
65 opt_parser.error("%s is not a valid directory." % directory)
66 os.chdir(directory)
67
68 # Type of Bazaar operation to perform.
69 operation_type = options.operation.lower()
70 if operation_type not in ("branch", "checkout"):
71 opt_parser.error("Invalid operation '%s' for '-o' flag." % \
72 operation_type)
73
74 # Launchpad team setting.
75 if options.lpteam:
76 team = options.lpteam.lower()
77 if args:
78 team = args[0].lower()
79 try:
80 team = launchpad.people[team]
81 except KeyError:
82 print("E: The team '%s' doesn't exist." % team, file=sys.stderr)
83
84 # Get a list of branches
85 branches = team.getBranches()
86
87 print("Downloading all branches for the '%s' team. This may take some " \
88 "time." % team.display_name)
89
90 try:
91 os.makedirs(team.name)
92 except:
93 pass
94
95 os.chdir(team.name)
96
97 for branch in branches:
98 project_name = branch.project.name
99 if not os.path.exists(project_name):
100 os.makedirs(project_name)
101 os.chdir(project_name)
102
103 if not os.path.exists(branch.name):
104 print("Branching %s ..." % branch.display_name)
105 cmd = ["bzr", operation_type, branch.bzr_identity, branch.name]
106 subprocess.call(cmd)
107 else:
108 print("Merging %s ..." % branch.display_name)
109 os.chdir(branch.name)
110 subprocess.call(["bzr", "merge", "--pull", "--remember"])
111 os.chdir(os.path.join(directory, team.name))
112
113 os.chdir(pwd)
114 sys.exit(0)
115
116 if __name__ == "__main__":
117 try:
118 main()
119 except KeyboardInterrupt:
120 print("Operation was interrupted by user.")