xref: /titanic_44/usr/src/tools/scripts/hg-active.py (revision 5679c89fcd2facbb4334df8870d3d7a4d2b11673)
1#! /usr/bin/python
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 2008 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, e:
39    sys.stderr.write("Error: %s\n" % e)
40    sys.exit(1)
41
42import getopt, binascii
43from mercurial import hg, repo, util
44from onbld.Scm.WorkSpace import WorkSpace
45
46def usage():
47    sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
48                     os.path.basename(__file__))
49    sys.exit(2)
50
51def main(argv):
52    try:
53        opts = getopt.getopt(argv, 'w:o:p:')[0]
54    except getopt.GetoptError, e:
55        sys.stderr.write(str(e) + '\n')
56        usage()
57
58    parentpath = None
59    wspath = None
60    outputfile = None
61
62    for opt, arg in opts:
63        if opt == '-w':
64            wspath = arg
65        elif opt == '-o':
66            outputfile = arg
67        elif opt == '-p':
68            parentpath = arg
69
70    if not wspath:
71        usage()
72
73    try:
74        repository = hg.repository(None, wspath)
75    except repo.RepoError, e:
76        sys.stderr.write("failed to open repository: %s\n" % e)
77        sys.exit(1)
78
79    ws = WorkSpace(repository)
80    act = ws.active(parentpath)
81
82    node = act.parenttip.node()
83    parenttip = binascii.hexlify(node)
84
85    fh = None
86    if outputfile:
87        try:
88            fh = open(outputfile, 'w')
89        except EnvironmentError, e:
90            sys.stderr.write("could not open output file: %s\n" % e)
91            sys.exit(1)
92    else:
93        fh = sys.stdout
94
95    fh.write("HG_PARENT=%s\n" % parenttip)
96
97    entries = [i for i in act]
98    entries.sort()
99
100    for entry in entries:
101        if entry.is_renamed():
102            fh.write("%s %s\n" % (entry.name, entry.parentname))
103        else:
104            fh.write("%s\n" % entry.name)
105
106        # Strip blank lines.
107        comments = filter(lambda x: x and not x.isspace(),
108                          entry.comments)
109
110        fh.write('\n')
111        if comments:
112            fh.write('%s\n' % '\n'.join(comments))
113        else:
114            fh.write("*** NO COMMENTS ***\n")
115        fh.write('\n')
116
117if __name__ == '__main__':
118    try:
119        main(sys.argv[1:])
120    except KeyboardInterrupt:
121        sys.exit(1)
122    except util.Abort, msg:
123        sys.stderr.write("Abort: %s\n" % msg)
124        sys.exit(1)
125