/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to plan_to_base.py

  • Committer: John Arbash Meinel
  • Date: 2009-12-10 17:16:19 UTC
  • mfrom: (4884 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4889.
  • Revision ID: john@arbash-meinel.com-20091210171619-ehdcxjbl8afhq9g1
Bring in bzr.dev 4884

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Copyright (C) 2009 Canonical Ltd
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""A script that converts a bzr merge .plan file into a .BASE file."""
 
19
 
 
20
import sys
 
21
 
 
22
 
 
23
def plan_lines_to_base_lines(plan_lines):
 
24
    _ghost_warning = False
 
25
    base_lines = []
 
26
    for line in plan_lines:
 
27
        action, content = line.split('|', 1)
 
28
        action = action.strip()
 
29
        if action in ('killed-a', 'killed-b', 'killed-both', 'unchanged'):
 
30
            # If lines were removed by a or b or both, then they must have been
 
31
            # in the base. if unchanged, then they are copied from the base
 
32
            base_lines.append(content)
 
33
        elif action in ('killed-base', 'irrelevant', 'ghost-a', 'ghost-b',
 
34
                        'new-a', 'new-b'):
 
35
            # The first 4 are ones that are always suppressed
 
36
            # the last 2 are lines that are in A or B, but *not* in BASE, so we
 
37
            # ignore them
 
38
            continue
 
39
        else:
 
40
            sys.stderr.write('Unknown action: %s\n' % (action,))
 
41
    return base_lines
 
42
 
 
43
 
 
44
def plan_file_to_base_file(plan_filename):
 
45
    if not plan_filename.endswith('.plan'):
 
46
        sys.stderr.write('"%s" does not look like a .plan file\n'
 
47
                         % (plan_filename,))
 
48
        return
 
49
    plan_file = open(plan_filename, 'rb')
 
50
    try:
 
51
        plan_lines = plan_file.readlines()
 
52
    finally:
 
53
        plan_file.close()
 
54
    base_filename = plan_filename[:-4] + 'BASE'
 
55
    base_lines = plan_lines_to_base_lines(plan_lines)
 
56
    f = open(base_filename, 'wb')
 
57
    try:
 
58
        f.writelines(base_lines)
 
59
    finally:
 
60
        f.close()
 
61
 
 
62
 
 
63
def main(args):
 
64
    import optparse
 
65
    p = optparse.OptionParser('%prog foo.plan*')
 
66
 
 
67
    opts, args = p.parse_args(args)
 
68
    if len(args) < 1:
 
69
        sys.stderr.write('You must supply exactly a .plan file.\n')
 
70
        return 1
 
71
    for arg in args:
 
72
        plan_file_to_base_file(arg)
 
73
 
 
74
 
 
75
if __name__ == '__main__':
 
76
    sys.exit(main(sys.argv[1:]))