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