File: lp-get-branches

package info (click to toggle)
lptools 0.3.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: python: 1,550; makefile: 6
file content (120 lines) | stat: -rwxr-xr-x 4,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 Canonical Ltd.
# Created by Daniel Holbach <daniel.holbach@ubuntu.com>
# Modified by Jonathan Patrick Davies <jpds@ubuntu.com>
#
# ##################################################################
#
# 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.
#
# See file /usr/share/common-licenses/GPL-3 for more details.
#
# ##################################################################
#
# This script is used to checkout or branch all the Bazaar branches
# of a Launchpad team.
#

import os
import subprocess
import sys
from lptools import config
from optparse import OptionParser

def main():
    usage = "Usage: %prog [-d <directory>] -t <team> [-o <operation>]"
    usage += "\nUsage: %prog <team>"
    opt_parser = OptionParser(usage)

    # Our options.
    opt_parser.add_option("-d", "--directory", action="store", type="string",
                          dest="directory", default=os.getcwd(),
                          help="Directory to download branches to.")
    opt_parser.add_option("-t", "--team", action="store", type="string",
                          dest="lpteam",
                          help="Launchpad team to download branches from.")
    opt_parser.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'.")

    (options, args) = opt_parser.parse_args()

    launchpad = config.get_launchpad("get-branches")

    # Fetch our current directory to return to later.
    pwd = os.getcwd()

    # Parse our options.
    if len(args) != 1 and options.lpteam == None:
        opt_parser.error("No team has been specified.")

    # Dictionary settings.
    directory = options.directory
    if not os.path.isdir(directory): # Check that it is a directory.
        opt_parser.error("%s is not a valid directory." % directory)
    os.chdir(directory)

    # Type of Bazaar operation to perform.
    operation_type = options.operation.lower()
    if operation_type not in ("branch", "checkout"):
        opt_parser.error("Invalid operation '%s' for '-o' flag." % \
            operation_type)

    # Launchpad team setting.
    if options.lpteam:
        team = options.lpteam.lower()
    if args:
        team = args[0].lower()
    try:
        team = launchpad.people[team]
    except KeyError:
        print("E: The team '%s' doesn't exist." % team, file=sys.stderr)

    # Get a list of branches
    branches = team.getBranches()

    print("Downloading all branches for the '%s' team. This may take some " \
        "time." % team.display_name)

    try:
        os.makedirs(team.name)
    except:
        pass

    os.chdir(team.name)

    for branch in branches:
        project_name = branch.project.name
        if not os.path.exists(project_name):
            os.makedirs(project_name)
        os.chdir(project_name)

        if not os.path.exists(branch.name):
            print("Branching %s ..." % branch.display_name)
            cmd = ["bzr", operation_type, branch.bzr_identity, branch.name]
            subprocess.call(cmd)
        else:
            print("Merging %s ..." % branch.display_name)
            os.chdir(branch.name)
            subprocess.call(["bzr", "merge", "--pull", "--remember"])
        os.chdir(os.path.join(directory, team.name))

    os.chdir(pwd)
    sys.exit(0)

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("Operation was interrupted by user.")