xref: /titanic_44/usr/src/tools/scripts/hg-active.py (revision 20e6d5c536ad5b300e7fafb6a92e13040f492977)
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# ident	"%Z%%M%	%I%	%E% SMI"
22#
23
24'''
25Create a wx-style active list on stdout based on a Mercurial
26workspace in support of webrev's Mercurial support.
27'''
28
29#
30# NB: This assumes the normal onbld directory structure
31#
32import sys, os
33sys.path.insert(1, "%s/../lib/python" % os.path.dirname(__file__))
34sys.path.insert(1, "%s/.." % os.path.dirname(__file__))
35
36from onbld.Scm import Version
37
38try:
39    Version.check_version()
40except Version.VersionMismatch, e:
41    sys.stderr.write("Error: %s\n" % e)
42    sys.exit(1)
43
44import getopt, binascii
45from mercurial import hg, repo
46from onbld.Scm.WorkSpace import WorkSpace
47
48def usage():
49    sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
50                     os.path.basename(__file__))
51    sys.exit(2)
52
53def main(argv):
54    try:
55        opts, args = getopt.getopt(argv, 'w:p:')
56    except getopt.GetoptError, e:
57        sys.stderr.write(str(e) + '\n')
58        usage()
59
60    parentpath = None
61    wspath = None
62
63    for opt, arg in opts:
64        if opt == '-w':
65            wspath = arg
66        elif opt == '-p':
67            parentpath = arg
68
69    if not wspath:
70        usage()
71
72    try:
73        repository = hg.repository(None, wspath)
74    except repo.RepoError, e:
75        sys.stderr.write("failed to open repository: %s\n" % e)
76        sys.exit(1)
77
78    ws = WorkSpace(repository)
79    act = ws.active(parentpath)
80
81    node = act.parenttip.node()
82    parenttip = binascii.hexlify(node)
83    print "HG_PARENT=" + parenttip
84
85    entries = [i for i in act]
86    entries.sort()
87
88    for entry in entries:
89        if entry.is_renamed():
90            print "%s %s" % (entry.name, entry.parentname)
91        else:
92            print entry.name
93
94        # Strip blank lines.
95        comments = filter(lambda x: x and not x.isspace(),
96                          entry.comments)
97
98        print
99        if comments:
100            print '\n'.join(comments)
101        else:
102            print "*** NO COMMENTS ***"
103        print
104
105if __name__ == '__main__':
106    try:
107        main(sys.argv[1:])
108    except KeyboardInterrupt:
109        sys.exit(1)
110