xref: /titanic_41/usr/src/tools/scripts/hg-active.py (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
1#! /usr/bin/python2.4
2#
3#  This program is free software; you can redistribute it and/or modify
4#  it under the terms of the GNU General Public License version 2
5#  as published by the Free Software Foundation.
6#
7#  This program is distributed in the hope that it will be useful,
8#  but WITHOUT ANY WARRANTY; without even the implied warranty of
9#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10#  GNU General Public License for more details.
11#
12#  You should have received a copy of the GNU General Public License
13#  along with this program; if not, write to the Free Software
14#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15#
16
17#
18# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
19# Use is subject to license terms.
20#
21
22'''
23Create a wx-style active list on stdout based on a Mercurial
24workspace in support of webrev's Mercurial support.
25'''
26
27#
28# NB: This assumes the normal onbld directory structure
29#
30import sys, os
31sys.path.insert(1, "%s/../lib/python" % os.path.dirname(__file__))
32sys.path.insert(1, "%s/.." % os.path.dirname(__file__))
33
34from onbld.Scm import Version
35
36try:
37    Version.check_version()
38except Version.VersionMismatch, versionerror:
39    sys.stderr.write("Error: %s\n" % versionerror)
40    sys.exit(1)
41
42
43import getopt, binascii
44from mercurial import hg, ui, util
45from onbld.Scm.WorkSpace import WorkSpace, HgRepoError
46
47
48def usage():
49    sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
50                     os.path.basename(__file__))
51    sys.exit(2)
52
53
54def main(argv):
55    try:
56        opts = getopt.getopt(argv, 'w:o:p:')[0]
57    except getopt.GetoptError, e:
58        sys.stderr.write(str(e) + '\n')
59        usage()
60
61    parentpath = None
62    wspath = None
63    outputfile = None
64
65    for opt, arg in opts:
66        if opt == '-w':
67            wspath = arg
68        elif opt == '-o':
69            outputfile = arg
70        elif opt == '-p':
71            parentpath = arg
72
73    if not wspath:
74        usage()
75
76    try:
77        repository = hg.repository(ui.ui(), wspath)
78    except HgRepoError, e:
79        sys.stderr.write("failed to open repository: %s\n" % e)
80        sys.exit(1)
81
82    ws = WorkSpace(repository)
83    act = ws.active(parentpath)
84
85    node = act.parenttip.node()
86    parenttip = binascii.hexlify(node)
87
88    fh = None
89    if outputfile:
90        try:
91            fh = open(outputfile, 'w')
92        except EnvironmentError, e:
93            sys.stderr.write("could not open output file: %s\n" % e)
94            sys.exit(1)
95    else:
96        fh = sys.stdout
97
98    fh.write("HG_PARENT=%s\n" % parenttip)
99
100    entries = [i for i in act]
101    entries.sort()
102
103    for entry in entries:
104        if entry.is_renamed() or entry.is_copied():
105            fh.write("%s %s\n" % (entry.name, entry.parentname))
106        else:
107            fh.write("%s\n" % entry.name)
108
109        # Strip blank lines.
110        comments = filter(lambda x: x and not x.isspace(),
111                          entry.comments)
112
113        fh.write('\n')
114        if comments:
115            fh.write('%s\n' % '\n'.join(comments))
116        else:
117            fh.write("*** NO COMMENTS ***\n")
118        fh.write('\n')
119
120if __name__ == '__main__':
121    try:
122        main(sys.argv[1:])
123    except KeyboardInterrupt:
124        sys.exit(1)
125    except util.Abort, msg:
126        sys.stderr.write("Abort: %s\n" % msg)
127        sys.exit(1)
128