17078Smjnelson#! /usr/bin/python
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#
187078Smjnelson# Copyright 2008 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()
387078Smjnelsonexcept Version.VersionMismatch, e:
397078Smjnelson    sys.stderr.write("Error: %s\n" % e)
407078Smjnelson    sys.exit(1)
417078Smjnelson
427078Smjnelsonimport getopt, binascii
43*7298SMark.J.Nelson@Sun.COMfrom mercurial import hg, repo, util
447078Smjnelsonfrom onbld.Scm.WorkSpace import WorkSpace
457078Smjnelson
467078Smjnelsondef usage():
477078Smjnelson    sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
487078Smjnelson                     os.path.basename(__file__))
497078Smjnelson    sys.exit(2)
507078Smjnelson
517078Smjnelsondef main(argv):
527078Smjnelson    try:
53*7298SMark.J.Nelson@Sun.COM        opts = getopt.getopt(argv, 'w:o:p:')[0]
547078Smjnelson    except getopt.GetoptError, e:
557078Smjnelson        sys.stderr.write(str(e) + '\n')
567078Smjnelson        usage()
577078Smjnelson
587078Smjnelson    parentpath = None
597078Smjnelson    wspath = None
60*7298SMark.J.Nelson@Sun.COM    outputfile = None
617078Smjnelson
627078Smjnelson    for opt, arg in opts:
637078Smjnelson        if opt == '-w':
647078Smjnelson            wspath = arg
65*7298SMark.J.Nelson@Sun.COM        elif opt == '-o':
66*7298SMark.J.Nelson@Sun.COM            outputfile = arg
677078Smjnelson        elif opt == '-p':
687078Smjnelson            parentpath = arg
697078Smjnelson
707078Smjnelson    if not wspath:
717078Smjnelson        usage()
727078Smjnelson
737078Smjnelson    try:
747078Smjnelson        repository = hg.repository(None, wspath)
757078Smjnelson    except repo.RepoError, e:
767078Smjnelson        sys.stderr.write("failed to open repository: %s\n" % e)
777078Smjnelson        sys.exit(1)
78*7298SMark.J.Nelson@Sun.COM
797078Smjnelson    ws = WorkSpace(repository)
807078Smjnelson    act = ws.active(parentpath)
817078Smjnelson
827078Smjnelson    node = act.parenttip.node()
837078Smjnelson    parenttip = binascii.hexlify(node)
84*7298SMark.J.Nelson@Sun.COM
85*7298SMark.J.Nelson@Sun.COM    fh = None
86*7298SMark.J.Nelson@Sun.COM    if outputfile:
87*7298SMark.J.Nelson@Sun.COM        try:
88*7298SMark.J.Nelson@Sun.COM            fh = open(outputfile, 'w')
89*7298SMark.J.Nelson@Sun.COM        except EnvironmentError, e:
90*7298SMark.J.Nelson@Sun.COM            sys.stderr.write("could not open output file: %s\n" % e)
91*7298SMark.J.Nelson@Sun.COM            sys.exit(1)
92*7298SMark.J.Nelson@Sun.COM    else:
93*7298SMark.J.Nelson@Sun.COM        fh = sys.stdout
94*7298SMark.J.Nelson@Sun.COM
95*7298SMark.J.Nelson@Sun.COM    fh.write("HG_PARENT=%s\n" % parenttip)
967078Smjnelson
977078Smjnelson    entries = [i for i in act]
987078Smjnelson    entries.sort()
997078Smjnelson
1007078Smjnelson    for entry in entries:
1017078Smjnelson        if entry.is_renamed():
102*7298SMark.J.Nelson@Sun.COM            fh.write("%s %s\n" % (entry.name, entry.parentname))
1037078Smjnelson        else:
104*7298SMark.J.Nelson@Sun.COM            fh.write("%s\n" % entry.name)
1057078Smjnelson
1067078Smjnelson        # Strip blank lines.
1077078Smjnelson        comments = filter(lambda x: x and not x.isspace(),
1087078Smjnelson                          entry.comments)
1097078Smjnelson
110*7298SMark.J.Nelson@Sun.COM        fh.write('\n')
1117078Smjnelson        if comments:
112*7298SMark.J.Nelson@Sun.COM            fh.write('%s\n' % '\n'.join(comments))
1137078Smjnelson        else:
114*7298SMark.J.Nelson@Sun.COM            fh.write("*** NO COMMENTS ***\n")
115*7298SMark.J.Nelson@Sun.COM        fh.write('\n')
1167078Smjnelson
1177078Smjnelsonif __name__ == '__main__':
1187078Smjnelson    try:
1197078Smjnelson        main(sys.argv[1:])
1207078Smjnelson    except KeyboardInterrupt:
1217078Smjnelson        sys.exit(1)
122*7298SMark.J.Nelson@Sun.COM    except util.Abort, msg:
123*7298SMark.J.Nelson@Sun.COM        sys.stderr.write("Abort: %s\n" % msg)
124*7298SMark.J.Nelson@Sun.COM        sys.exit(1)
125