xref: /titanic_41/usr/src/tools/scripts/hg-active.py (revision 5203bc321053fb87d7073c7640548fab73634793)
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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
19#
20
21'''
22Create a wx-style active list on stdout based on a Mercurial
23workspace in support of webrev's Mercurial support.
24'''
25
26#
27# NB: This assumes the normal onbld directory structure
28#
29import sys, os
30
31sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
32                                "python%d.%d" % sys.version_info[:2]))
33
34# Allow running from the source tree, using the modules in the source tree
35sys.path.insert(2, os.path.join(os.path.dirname(__file__), ".."))
36
37from onbld.Scm import Version
38
39try:
40    Version.check_version()
41except Version.VersionMismatch, versionerror:
42    sys.stderr.write("Error: %s\n" % versionerror)
43    sys.exit(1)
44
45
46import getopt, binascii
47from mercurial import hg, ui, util
48from onbld.Scm.WorkSpace import WorkSpace, HgRepoError
49
50
51def usage():
52    sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
53                     os.path.basename(__file__))
54    sys.exit(2)
55
56
57def main(argv):
58    try:
59        opts = getopt.getopt(argv, 'w:o:p:')[0]
60    except getopt.GetoptError, e:
61        sys.stderr.write(str(e) + '\n')
62        usage()
63
64    parentpath = None
65    wspath = None
66    outputfile = None
67
68    for opt, arg in opts:
69        if opt == '-w':
70            wspath = arg
71        elif opt == '-o':
72            outputfile = arg
73        elif opt == '-p':
74            parentpath = arg
75
76    if not wspath:
77        usage()
78
79    try:
80        repository = hg.repository(ui.ui(), wspath)
81    except HgRepoError, e:
82        sys.stderr.write("failed to open repository: %s\n" % e)
83        sys.exit(1)
84
85    ws = WorkSpace(repository)
86    act = ws.active(parentpath)
87
88    node = act.parenttip.node()
89    parenttip = binascii.hexlify(node)
90
91    fh = None
92    if outputfile:
93        try:
94            fh = open(outputfile, 'w')
95        except EnvironmentError, e:
96            sys.stderr.write("could not open output file: %s\n" % e)
97            sys.exit(1)
98    else:
99        fh = sys.stdout
100
101    fh.write("HG_PARENT=%s\n" % parenttip)
102
103    entries = [i for i in act]
104    entries.sort()
105
106    for entry in entries:
107        if entry.is_renamed() or entry.is_copied():
108            fh.write("%s %s\n" % (entry.name, entry.parentname))
109        else:
110            fh.write("%s\n" % entry.name)
111
112        # Strip blank lines.
113        comments = filter(lambda x: x and not x.isspace(),
114                          entry.comments)
115
116        fh.write('\n')
117        if comments:
118            fh.write('%s\n' % '\n'.join(comments))
119        else:
120            fh.write("*** NO COMMENTS ***\n")
121        fh.write('\n')
122
123if __name__ == '__main__':
124    try:
125        main(sys.argv[1:])
126    except KeyboardInterrupt:
127        sys.exit(1)
128    except util.Abort, msg:
129        sys.stderr.write("Abort: %s\n" % msg)
130        sys.exit(1)
131