Roland McGrath 11487c
#!/usr/bin/python
Roland McGrath 11487c
#
Chuck Ebbert 5ae797
# Uses git config options user.name and user.email, falls
Chuck Ebbert 5ae797
# back to env vars $GIT_COMMITTER_NAME and $GIT_COMMITTER_EMAIL
Roland McGrath 11487c
#
Roland McGrath 11487c
import re
Roland McGrath 11487c
import sys
Roland McGrath 11487c
import time
Roland McGrath 11487c
import os
Roland McGrath 11487c
import string
Roland McGrath 11487c
Roland McGrath 11487c
class Specfile:
Roland McGrath 11487c
    def __init__(self,filename):
Roland McGrath 11487c
        file=open(filename,"r")
Roland McGrath 11487c
        self.lines=file.readlines()
Roland McGrath 11487c
        self.vr=""
Roland McGrath 11487c
Roland McGrath 11487c
    def getNextVR(self,aspec):
Roland McGrath 11487c
         # Get VR for changelog entry.
Roland McGrath 11487c
        (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 11487c
	pos = 0
Roland McGrath 11487c
        # general released kernel case, bump 1st field
Roland McGrath 11487c
        fedora_build = rel.split('.')[pos]
Roland McGrath 11487c
        if fedora_build == "0":
Roland McGrath 11487c
            # this is a devel kernel, bump 2nd field
Roland McGrath 11487c
            pos = 1
Roland McGrath 11487c
        elif rel.split('.')[-1] != fedora_build:
Roland McGrath 11487c
            # this is a branch, must bump 3rd field
Roland McGrath 11487c
            pos = 2
Roland McGrath 11487c
        fedora_build = rel.split('.')[pos]
Roland McGrath 11487c
        if pos == 1 and len(rel.split('.')) > 4:
Roland McGrath 11487c
            # uh... what? devel kernel in a branch? private build? just do no VR in clog...
Roland McGrath 11487c
            print "Warning: not adding any VR to changelog, couldn't tell for sure which field to bump"
Roland McGrath 11487c
            pos = -1
Roland McGrath 11487c
        next_fedora_build = int(fedora_build) + 1
Roland McGrath 11487c
        if pos == 0:
Roland McGrath 11487c
            nextrel = str(next_fedora_build)
Roland McGrath 11487c
        elif pos == 1:
Roland McGrath 11487c
            nextrel = "0." + str(next_fedora_build)
Roland McGrath 11487c
        elif pos == 2:
Roland McGrath 11487c
            nextrel = rel.split('.')[0] + "." + rel.split('.')[1] + "." + str(next_fedora_build)
Roland McGrath 11487c
        if pos >= 0:
Roland McGrath 11487c
            for s in rel.split('.')[pos + 1:]:
Roland McGrath 11487c
                nextrel = nextrel + "." + s
Roland McGrath 11487c
            self.vr = " "+ver+'-'+nextrel
Roland McGrath 11487c
Roland McGrath 11487c
    def addChangelogEntry(self,entry):
Chuck Ebbert 5ae797
        user = os.popen("git config --get user.name").read().rstrip()
Chuck Ebbert 5ae797
        if (user == ""):
Chuck Ebbert 5ae797
            user = os.environ.get("GIT_COMMITTER_NAME","Unknown")
Chuck Ebbert 5ae797
        email = os.popen("git config --get user.email").read().rstrip()
Chuck Ebbert 5ae797
        if (email == ""):
Chuck Ebbert 5ae797
            email = os.environ.get("GIT_COMMITTER_EMAIL","unknown")
Roland McGrath 11487c
        if (email == "unknown"):
Roland McGrath 11487c
            email = os.environ.get("USER","unknown")+"@fedoraproject.org"
Roland McGrath 11487c
        changematch=re.compile(r"^%changelog")
Roland McGrath 11487c
        date=time.strftime("%a %b %d %Y",   time.localtime(time.time()))
Roland McGrath 11487c
        newchangelogentry="%changelog\n* "+date+" "+user+" <"+email+">"+self.vr+"\n"+entry+"\n\n"
Roland McGrath 11487c
        for i in range(len(self.lines)):
Roland McGrath 11487c
            if(changematch.match(self.lines[i])):
Roland McGrath 11487c
                self.lines[i]=newchangelogentry
Roland McGrath 11487c
                break
Roland McGrath 11487c
Roland McGrath 11487c
    def writeFile(self,filename):
Roland McGrath 11487c
        file=open(filename,"w")
Roland McGrath 11487c
        file.writelines(self.lines)
Roland McGrath 11487c
        file.close()
Roland McGrath 11487c
Roland McGrath 11487c
if __name__=="__main__":
Roland McGrath 11487c
  aspec=(sys.argv[1])
Roland McGrath 11487c
  s=Specfile(aspec)
Roland McGrath 11487c
  entry=(sys.argv[2])
Roland McGrath 11487c
  s.getNextVR(aspec)
Roland McGrath 11487c
  s.addChangelogEntry(entry)
Roland McGrath 11487c
  s.writeFile(aspec)
Roland McGrath 11487c