|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Create Change Log Entry for a Mercurial repository. |
| 4 | +""" |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import traceback |
| 8 | +import subprocess |
| 9 | +import optparse |
| 10 | +import time |
| 11 | + |
| 12 | +def hg_command(options, *args): |
| 13 | + """Run a hg command in path and return the result. |
| 14 | + returns a tuple of strings, stdout and stderr |
| 15 | + Throws on error. |
| 16 | + """ |
| 17 | + arglist = ["hg", "--encoding", "UTF-8", "--noninteractive"] + list(args) |
| 18 | + cmd = " ".join(arglist) |
| 19 | + if options.verbose: |
| 20 | + print "Running: ", cmd |
| 21 | + |
| 22 | + proc = subprocess.Popen(arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 23 | + |
| 24 | + # proc.communicate returns a tuple with stdout and stderr as utf-8 strings. |
| 25 | + # convert this to a list of decoded to python native strings |
| 26 | + out, err = [utf8string.decode("utf-8") for utf8string in proc.communicate()] |
| 27 | + |
| 28 | + if proc.returncode: |
| 29 | + raise Exception("Error {0} running {1}:\n".format(proc.returncode, cmd), |
| 30 | + err,out,) |
| 31 | + return (out, err) |
| 32 | + |
| 33 | + |
| 34 | +def runEditor(options, fileName): |
| 35 | + if options.editor != "none": |
| 36 | + # start the editor. Do not wait for the results |
| 37 | + subprocess.Popen([options.editor, fileName]) |
| 38 | + |
| 39 | + |
| 40 | +def displaySet(options, title, paths): |
| 41 | + """Display the paths in the set |
| 42 | + options.verbose and options.quiet control the amount of data displayed |
| 43 | + title names the set of paths |
| 44 | + """ |
| 45 | + if(not options.quiet): |
| 46 | + if len(paths) > 0: |
| 47 | + print title,"files:",len(paths) |
| 48 | + if(options.verbose): |
| 49 | + for path in paths: |
| 50 | + print ' ',path |
| 51 | + else: |
| 52 | + if(options.verbose): |
| 53 | + print title,"files:",len(paths) |
| 54 | + |
| 55 | + |
| 56 | +def writeToChangeLog(file, paths, changeType): |
| 57 | + if len(paths) > 0: |
| 58 | + for path in paths: |
| 59 | + file.write(" * " + path + ":\n") |
| 60 | + file.write( " " + changeType + "\n") |
| 61 | + |
| 62 | +def genChangeLogTag(options): |
| 63 | + nowstr = time.strftime("%a %b %d %H:%M:%S UTC %Y", time.gmtime()) |
| 64 | + username = options.user |
| 65 | + replyto = options.email |
| 66 | + changeLogTag = nowstr + " " + username+ " <" + replyto + ">" |
| 67 | + return changeLogTag |
| 68 | + |
| 69 | + |
| 70 | +def run(options, args): |
| 71 | + changeLogFileName = "ChangeLog" |
| 72 | + if(len(args) > 0): |
| 73 | + changeLogFileName = args[0] |
| 74 | + if(len(args) > 1): |
| 75 | + print "Too many arguments: ", args[1:] |
| 76 | + return-1 |
| 77 | + if not os.path.exists(changeLogFileName): |
| 78 | + print "Change log,", changeLogFileName, "must be an existing file. Are you in the right directory. " |
| 79 | + return -1 |
| 80 | + |
| 81 | + statusText, errorText = hg_command(options, "status", "-A", "-C") |
| 82 | + |
| 83 | + # from hg help status: |
| 84 | + # M = modified |
| 85 | + # A = added |
| 86 | + # = origin of the previous file listed as A (added) |
| 87 | + # R = removed |
| 88 | + # C = clean |
| 89 | + # ! = missing (deleted by non-hg command, but still tracked) |
| 90 | + # ? = not tracked |
| 91 | + # I = ignored |
| 92 | + modified = [] |
| 93 | + added = [] |
| 94 | + removed = [] |
| 95 | + missing = [] |
| 96 | + clean = [] |
| 97 | + unknown = [] |
| 98 | + ignored = [] |
| 99 | + renamed = [] |
| 100 | + |
| 101 | + for line in statusText.splitlines(): |
| 102 | + (sts,space,path) = line.rstrip().partition(' ') |
| 103 | + #print '['+sts+']:'+path |
| 104 | + if sts == 'M': |
| 105 | + modified.append(path) |
| 106 | + elif sts == 'A': |
| 107 | + added.append(path) |
| 108 | + elif sts == '': |
| 109 | + path = path.strip() |
| 110 | + old = added[-1] |
| 111 | + added = added[:-1] |
| 112 | + renamed.append((path, old)) |
| 113 | + elif sts == 'R': |
| 114 | + removed.append(path) |
| 115 | + elif sts == 'C': |
| 116 | + clean.append(path) |
| 117 | + elif sts == '!': |
| 118 | + missing.append(path) |
| 119 | + elif sts == '?': |
| 120 | + unknown.append(path) |
| 121 | + elif sts == 'I': |
| 122 | + ignored.append(path) |
| 123 | + else: |
| 124 | + print "Unknown status: '{0}' {1}".format(sts, path) |
| 125 | + |
| 126 | + for rename in renamed: |
| 127 | + removed.remove(rename[0]) |
| 128 | + |
| 129 | + displaySet(options, "Modified", modified) |
| 130 | + displaySet(options, "Added", added) |
| 131 | + displaySet(options, "Removed", removed) |
| 132 | + displaySet(options, "Missing", missing) |
| 133 | + displaySet(options, "Unknown", unknown) |
| 134 | + displaySet(options, "Ignored", ignored) |
| 135 | + displaySet(options, "Clean", clean) |
| 136 | + displaySet(options, "Renamed", renamed) |
| 137 | + |
| 138 | + if len(modified) + len(added) + len(removed) + len(renamed) > 0 : |
| 139 | + changeLogTag = genChangeLogTag(options) |
| 140 | + tempChangeLogFileName = "..." + changeLogFileName |
| 141 | + if(not options.quiet): |
| 142 | + print "Change Log Tag: ",changeLogTag |
| 143 | + |
| 144 | + ok = False |
| 145 | + with open(tempChangeLogFileName, "w") as changeFile: |
| 146 | + changeFile.write(changeLogTag + '\n'); |
| 147 | + writeToChangeLog(changeFile, modified, "Modified") |
| 148 | + writeToChangeLog(changeFile, added, "Added") |
| 149 | + writeToChangeLog(changeFile, removed,"Removed") |
| 150 | + for rename in renamed: |
| 151 | + writeToChangeLog(changeFile, rename, "Renamed") |
| 152 | + changeFile.write("\n") |
| 153 | + with open(changeLogFileName, "r") as oldChangeLog: |
| 154 | + for line in oldChangeLog: |
| 155 | + changeFile.write(line) |
| 156 | + ok = True |
| 157 | + |
| 158 | + if ok: |
| 159 | + backupChangeLogFileName = changeLogFileName + ".bak" |
| 160 | + if os.path.exists(backupChangeLogFileName): |
| 161 | + os.remove(backupChangeLogFileName) |
| 162 | + |
| 163 | + os.rename(changeLogFileName, backupChangeLogFileName) |
| 164 | + os.rename(tempChangeLogFileName, changeLogFileName) |
| 165 | + runEditor(options, changeLogFileName) |
| 166 | + return 0 |
| 167 | + return -1 |
| 168 | + |
| 169 | +def defineCommandLineOptions(parser): |
| 170 | + try: |
| 171 | + userid = os.environ["USER"] |
| 172 | + except: |
| 173 | + try: |
| 174 | + userid = os.environ["USERNAME"] |
| 175 | + except: |
| 176 | + print "Who are you? Tried USER and USERNAME" |
| 177 | + return -1 |
| 178 | + |
| 179 | + try: |
| 180 | + user = os.environ["CL_USERNAME"] |
| 181 | + except: |
| 182 | + user = userid |
| 183 | + |
| 184 | + try: |
| 185 | + email = os.environ["REPLYTO"]; |
| 186 | + except: |
| 187 | + email = userid + "@ociweb.com" |
| 188 | + |
| 189 | + editor = "none" |
| 190 | + if os.environ.has_key("EDITOR"): |
| 191 | + editor = os.environ["EDITOR"] |
| 192 | + |
| 193 | + parser.add_option("-u", "--user", default=user, help="User name (default: %default)") |
| 194 | + parser.add_option("-m", "--email", default=email, help="Email address (default: %default)") |
| 195 | + parser.add_option("-e", "--editor", default=editor, help="Edit file after creating it using %default. -e \"none\" disables editing.") |
| 196 | + parser.add_option("-v", "--verbose", action="store_true", default=False, help="Print noise to standard output.") |
| 197 | + parser.add_option("-q", "--quiet", action="store_true", default=False, help="Suppress extra output.") |
| 198 | + return 0 |
| 199 | + |
| 200 | +def main (argv = None): |
| 201 | + try: |
| 202 | + if(argv == None): |
| 203 | + argv = sys.argv |
| 204 | + parser = optparse.OptionParser(usage = "usage: %prog [options] [changeLogname]\n Default changeLogname is ChangeLog") |
| 205 | + if defineCommandLineOptions(parser) < 0: |
| 206 | + return -1 |
| 207 | + (options, args) = parser.parse_args(argv[1:]) |
| 208 | + if(options.verbose): |
| 209 | + print "verbose: ", options.verbose |
| 210 | + print "user: ", options.user |
| 211 | + print "email: ", options.email |
| 212 | + print "editor: ", options.editor |
| 213 | + print "positional: ", args |
| 214 | + |
| 215 | + return run(options, args) |
| 216 | + except Exception as ex: |
| 217 | + print "caught exception" |
| 218 | + traceback.print_exc(ex) |
| 219 | + return -1 |
| 220 | + |
| 221 | +if __name__ == "__main__": |
| 222 | + sys.exit(main()) |
0 commit comments