1*11838SLiane.Praza@Sun.COM#!/usr/bin/python2.4
27078Smjnelson#
37078Smjnelson#  This program is free software; you can redistribute it and/or modify
47078Smjnelson#  it under the terms of the GNU General Public License version 2
57078Smjnelson#  as published by the Free Software Foundation.
67078Smjnelson#
77078Smjnelson#  This program is distributed in the hope that it will be useful,
87078Smjnelson#  but WITHOUT ANY WARRANTY; without even the implied warranty of
97078Smjnelson#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
107078Smjnelson#  GNU General Public License for more details.
117078Smjnelson#
127078Smjnelson#  You should have received a copy of the GNU General Public License
137078Smjnelson#  along with this program; if not, write to the Free Software
147078Smjnelson#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
157078Smjnelson#
167078Smjnelson
177078Smjnelson#
1810399Srichlowe@richlowe.net# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
197078Smjnelson# Use is subject to license terms.
207078Smjnelson#
217078Smjnelson
227078Smjnelson'''
237078SmjnelsonCreate a wx-style active list on stdout based on a Mercurial
247078Smjnelsonworkspace in support of webrev's Mercurial support.
257078Smjnelson'''
267078Smjnelson
277078Smjnelson#
287078Smjnelson# NB: This assumes the normal onbld directory structure
297078Smjnelson#
307078Smjnelsonimport sys, os
317078Smjnelsonsys.path.insert(1, "%s/../lib/python" % os.path.dirname(__file__))
327078Smjnelsonsys.path.insert(1, "%s/.." % os.path.dirname(__file__))
337078Smjnelson
347078Smjnelsonfrom onbld.Scm import Version
357078Smjnelson
367078Smjnelsontry:
377078Smjnelson    Version.check_version()
3810399Srichlowe@richlowe.netexcept Version.VersionMismatch, versionerror:
3910399Srichlowe@richlowe.net    sys.stderr.write("Error: %s\n" % versionerror)
407078Smjnelson    sys.exit(1)
417078Smjnelson
4210399Srichlowe@richlowe.net
437078Smjnelsonimport getopt, binascii
4410399Srichlowe@richlowe.netfrom mercurial import hg, ui, util
4510399Srichlowe@richlowe.netfrom onbld.Scm.WorkSpace import WorkSpace, HgRepoError
4610399Srichlowe@richlowe.net
477078Smjnelson
487078Smjnelsondef usage():
497078Smjnelson    sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
507078Smjnelson                     os.path.basename(__file__))
517078Smjnelson    sys.exit(2)
527078Smjnelson
5310399Srichlowe@richlowe.net
547078Smjnelsondef main(argv):
557078Smjnelson    try:
567298SMark.J.Nelson@Sun.COM        opts = getopt.getopt(argv, 'w:o:p:')[0]
577078Smjnelson    except getopt.GetoptError, e:
587078Smjnelson        sys.stderr.write(str(e) + '\n')
597078Smjnelson        usage()
607078Smjnelson
617078Smjnelson    parentpath = None
627078Smjnelson    wspath = None
637298SMark.J.Nelson@Sun.COM    outputfile = None
647078Smjnelson
657078Smjnelson    for opt, arg in opts:
667078Smjnelson        if opt == '-w':
677078Smjnelson            wspath = arg
687298SMark.J.Nelson@Sun.COM        elif opt == '-o':
697298SMark.J.Nelson@Sun.COM            outputfile = arg
707078Smjnelson        elif opt == '-p':
717078Smjnelson            parentpath = arg
727078Smjnelson
737078Smjnelson    if not wspath:
747078Smjnelson        usage()
757078Smjnelson
767078Smjnelson    try:
7710399Srichlowe@richlowe.net        repository = hg.repository(ui.ui(), wspath)
7810399Srichlowe@richlowe.net    except HgRepoError, e:
797078Smjnelson        sys.stderr.write("failed to open repository: %s\n" % e)
807078Smjnelson        sys.exit(1)
8110399Srichlowe@richlowe.net
827078Smjnelson    ws = WorkSpace(repository)
837078Smjnelson    act = ws.active(parentpath)
847078Smjnelson
857078Smjnelson    node = act.parenttip.node()
867078Smjnelson    parenttip = binascii.hexlify(node)
877298SMark.J.Nelson@Sun.COM
887298SMark.J.Nelson@Sun.COM    fh = None
897298SMark.J.Nelson@Sun.COM    if outputfile:
907298SMark.J.Nelson@Sun.COM        try:
917298SMark.J.Nelson@Sun.COM            fh = open(outputfile, 'w')
927298SMark.J.Nelson@Sun.COM        except EnvironmentError, e:
937298SMark.J.Nelson@Sun.COM            sys.stderr.write("could not open output file: %s\n" % e)
947298SMark.J.Nelson@Sun.COM            sys.exit(1)
957298SMark.J.Nelson@Sun.COM    else:
967298SMark.J.Nelson@Sun.COM        fh = sys.stdout
977298SMark.J.Nelson@Sun.COM
987298SMark.J.Nelson@Sun.COM    fh.write("HG_PARENT=%s\n" % parenttip)
997078Smjnelson
1007078Smjnelson    entries = [i for i in act]
1017078Smjnelson    entries.sort()
1027078Smjnelson
1037078Smjnelson    for entry in entries:
10410789SEdward.Pilatowicz@Sun.COM        if entry.is_renamed() or entry.is_copied():
1057298SMark.J.Nelson@Sun.COM            fh.write("%s %s\n" % (entry.name, entry.parentname))
1067078Smjnelson        else:
1077298SMark.J.Nelson@Sun.COM            fh.write("%s\n" % entry.name)
1087078Smjnelson
1097078Smjnelson        # Strip blank lines.
1107078Smjnelson        comments = filter(lambda x: x and not x.isspace(),
1117078Smjnelson                          entry.comments)
1127078Smjnelson
1137298SMark.J.Nelson@Sun.COM        fh.write('\n')
1147078Smjnelson        if comments:
1157298SMark.J.Nelson@Sun.COM            fh.write('%s\n' % '\n'.join(comments))
1167078Smjnelson        else:
1177298SMark.J.Nelson@Sun.COM            fh.write("*** NO COMMENTS ***\n")
1187298SMark.J.Nelson@Sun.COM        fh.write('\n')
1197078Smjnelson
1207078Smjnelsonif __name__ == '__main__':
1217078Smjnelson    try:
1227078Smjnelson        main(sys.argv[1:])
1237078Smjnelson    except KeyboardInterrupt:
1247078Smjnelson        sys.exit(1)
1257298SMark.J.Nelson@Sun.COM    except util.Abort, msg:
1267298SMark.J.Nelson@Sun.COM        sys.stderr.write("Abort: %s\n" % msg)
1277298SMark.J.Nelson@Sun.COM        sys.exit(1)
128