Roland McGrath 11487c5
#!/usr/bin/python
Roland McGrath 11487c5
#
Roland McGrath 11487c5
# Needs $GIT_COMMITTER_NAME and $GIT_COMMITTER_EMAIL set.
Roland McGrath 11487c5
#
Roland McGrath 11487c5
import re
Roland McGrath 11487c5
import sys
Roland McGrath 11487c5
import time
Roland McGrath 11487c5
import os
Roland McGrath 11487c5
import string
Roland McGrath 11487c5
Roland McGrath 11487c5
class Specfile:
Roland McGrath 11487c5
    def __init__(self,filename):
Roland McGrath 11487c5
        file=open(filename,"r")
Roland McGrath 11487c5
        self.lines=file.readlines()
Roland McGrath 11487c5
        self.vr=""
Roland McGrath 11487c5
Roland McGrath 11487c5
    def getNextVR(self,aspec):
Roland McGrath 11487c5
         # Get VR for changelog entry.
Roland McGrath 11487c5
        (ver,rel) = os.popen("LC_ALL=C rpm --specfile -q --qf '%%{version} %%{release}\n' --define 'dist %%{nil}' %s | head -1" % aspec).read().strip().split(' ')
Roland McGrath 11487c5
	pos = 0
Roland McGrath 11487c5
        # general released kernel case, bump 1st field
Roland McGrath 11487c5
        fedora_build = rel.split('.')[pos]
Roland McGrath 11487c5
        if fedora_build == "0":
Roland McGrath 11487c5
            # this is a devel kernel, bump 2nd field
Roland McGrath 11487c5
            pos = 1
Roland McGrath 11487c5
        elif rel.split('.')[-1] != fedora_build:
Roland McGrath 11487c5
            # this is a branch, must bump 3rd field
Roland McGrath 11487c5
            pos = 2
Roland McGrath 11487c5
        fedora_build = rel.split('.')[pos]
Roland McGrath 11487c5
        if pos == 1 and len(rel.split('.')) > 4:
Roland McGrath 11487c5
            # uh... what? devel kernel in a branch? private build? just do no VR in clog...
Roland McGrath 11487c5
            print "Warning: not adding any VR to changelog, couldn't tell for sure which field to bump"
Roland McGrath 11487c5
            pos = -1
Roland McGrath 11487c5
        next_fedora_build = int(fedora_build) + 1
Roland McGrath 11487c5
        if pos == 0:
Roland McGrath 11487c5
            nextrel = str(next_fedora_build)
Roland McGrath 11487c5
        elif pos == 1:
Roland McGrath 11487c5
            nextrel = "0." + str(next_fedora_build)
Roland McGrath 11487c5
        elif pos == 2:
Roland McGrath 11487c5
            nextrel = rel.split('.')[0] + "." + rel.split('.')[1] + "." + str(next_fedora_build)
Roland McGrath 11487c5
        if pos >= 0:
Roland McGrath 11487c5
            for s in rel.split('.')[pos + 1:]:
Roland McGrath 11487c5
                nextrel = nextrel + "." + s
Roland McGrath 11487c5
            self.vr = " "+ver+'-'+nextrel
Roland McGrath 11487c5
Roland McGrath 11487c5
    def addChangelogEntry(self,entry):
Roland McGrath 11487c5
        user = os.environ.get("GIT_COMMITTER_NAME","unknown")
Roland McGrath 11487c5
        email = os.environ.get("GIT_COMMITTER_EMAIL","unknown")
Roland McGrath 11487c5
        if (email == "unknown"):
Roland McGrath 11487c5
            email = os.environ.get("USER","unknown")+"@fedoraproject.org"
Roland McGrath 11487c5
        changematch=re.compile(r"^%changelog")
Roland McGrath 11487c5
        date=time.strftime("%a %b %d %Y",   time.localtime(time.time()))
Roland McGrath 11487c5
        newchangelogentry="%changelog\n* "+date+" "+user+" <"+email+">"+self.vr+"\n"+entry+"\n\n"
Roland McGrath 11487c5
        for i in range(len(self.lines)):
Roland McGrath 11487c5
            if(changematch.match(self.lines[i])):
Roland McGrath 11487c5
                self.lines[i]=newchangelogentry
Roland McGrath 11487c5
                break
Roland McGrath 11487c5
Roland McGrath 11487c5
    def writeFile(self,filename):
Roland McGrath 11487c5
        file=open(filename,"w")
Roland McGrath 11487c5
        file.writelines(self.lines)
Roland McGrath 11487c5
        file.close()
Roland McGrath 11487c5
Roland McGrath 11487c5
if __name__=="__main__":
Roland McGrath 11487c5
  aspec=(sys.argv[1])
Roland McGrath 11487c5
  s=Specfile(aspec)
Roland McGrath 11487c5
  entry=(sys.argv[2])
Roland McGrath 11487c5
  s.getNextVR(aspec)
Roland McGrath 11487c5
  s.addChangelogEntry(entry)
Roland McGrath 11487c5
  s.writeFile(aspec)
Roland McGrath 11487c5