8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / scripts / hg-active.py
blob495cdfc0db36f71a7bdebb55156105b933cb9dcf
1 #!@PYTHON@
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.
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.
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.
18 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
21 '''
22 Create a wx-style active list on stdout based on a Mercurial
23 workspace in support of webrev's Mercurial support.
24 '''
27 # NB: This assumes the normal onbld directory structure
29 import sys, os
31 sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
32 "python%d.%d" % sys.version_info[:2]))
34 # Allow running from the source tree, using the modules in the source tree
35 sys.path.insert(2, os.path.join(os.path.dirname(__file__), ".."))
37 from onbld.Scm import Version
39 try:
40 Version.check_version()
41 except Version.VersionMismatch, versionerror:
42 sys.stderr.write("Error: %s\n" % versionerror)
43 sys.exit(1)
46 import getopt, binascii
47 from mercurial import error, hg, ui, util
48 from onbld.Scm.WorkSpace import WorkSpace
51 def usage():
52 sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
53 os.path.basename(__file__))
54 sys.exit(2)
57 def 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()
64 parentpath = None
65 wspath = None
66 outputfile = None
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
76 if not wspath:
77 usage()
79 try:
80 repository = hg.repository(ui.ui(), wspath)
81 except error.RepoError, e:
82 sys.stderr.write("failed to open repository: %s\n" % e)
83 sys.exit(1)
85 ws = WorkSpace(repository)
86 act = ws.active(parentpath)
88 node = act.parenttip.node()
89 parenttip = binascii.hexlify(node)
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
101 fh.write("HG_PARENT=%s\n" % parenttip)
103 entries = [i for i in act]
104 entries.sort()
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)
112 # Strip blank lines.
113 comments = filter(lambda x: x and not x.isspace(),
114 entry.comments)
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')
123 if __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)