Eric Paris 109846f
#!/usr/bin/python -t
Eric Paris 109846f
# -*- mode: Python; indent-tabs-mode: nil; coding: utf-8 -*-
Eric Paris 109846f
#
Eric Paris 109846f
# Copyright (c) 2005-2013 Fedora Project
Eric Paris 109846f
#
Eric Paris 109846f
# This program is free software; you can redistribute it and/or modify
Eric Paris 109846f
# it under the terms of the GNU General Public License as published by
Eric Paris 109846f
# the Free Software Foundation; either version 2 of the License, or
Eric Paris 109846f
# (at your option) any later version.
Eric Paris 109846f
#
Eric Paris 109846f
# This program is distributed in the hope that it will be useful,
Eric Paris 109846f
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Eric Paris 109846f
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Eric Paris 109846f
# GNU General Public License for more details.
Eric Paris 109846f
#
Eric Paris 109846f
# You should have received a copy of the GNU General Public License
Eric Paris 109846f
# along with this program; if not, write to the Free Software
Eric Paris 109846f
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Eric Paris 109846f
Eric Paris 109846f
import re
Eric Paris 109846f
import subprocess
Eric Paris 109846f
import sys
Eric Paris 109846f
import textwrap
Eric Paris 109846f
import time
Eric Paris 109846f
from optparse import OptionParser
Eric Paris 109846f
Eric Paris 109846f
Eric Paris 109846f
__version__ = "1.0.12"
Eric Paris 109846f
Eric Paris 109846f
class SpecFile:
Eric Paris 109846f
    def __init__(self, filename):
Eric Paris 109846f
        self.filename = filename
Eric Paris 109846f
        f = None
Eric Paris 109846f
        try:
Eric Paris 109846f
            f = open(filename,"r")
Eric Paris 109846f
            self.lines = f.readlines()
Eric Paris 109846f
        finally:
Eric Paris 109846f
            f and f.close()
Eric Paris 109846f
Eric Paris 109846f
    _changelog_pattern = re.compile(r"^%changelog(\s|$)", re.I)
Eric Paris 109846f
Eric Paris 109846f
    def addChangelogEntry(self, evr, entry, email):
Eric Paris 109846f
        for i in range(len(self.lines)):
Eric Paris 109846f
            if SpecFile._changelog_pattern.match(self.lines[i]):
Eric Paris 109846f
                if len(evr):
Eric Paris 109846f
                    evrstring = ' - %s' % evr
Eric Paris 109846f
                else:
Eric Paris 109846f
                    evrstring = ''
Eric Paris 109846f
                date = time.strftime("%a %b %d %Y", time.gmtime())
Eric Paris 109846f
                newchangelogentry = "* %s %s%s\n%s\n\n" % \
Eric Paris 109846f
                    (date, email, evrstring, entry)
Eric Paris 109846f
                self.lines[i] += newchangelogentry
Eric Paris 109846f
                return
Eric Paris 109846f
Eric Paris 109846f
    def writeFile(self, filename):
Eric Paris 109846f
        f = open(filename, "w")
Eric Paris 109846f
        f.writelines(self.lines)
Eric Paris 109846f
        f.close()
Eric Paris 109846f
Eric Paris 109846f
    def debugdiff(self, old, new):
Eric Paris 109846f
        print ('%s\n-%s\n+%s\n' % (self.filename, old, new))
Eric Paris 109846f
Eric Paris 109846f
if __name__ == "__main__":
Eric Paris 109846f
    usage = '''Usage: %prog [OPTION]... SPECFILE...'''
Eric Paris 109846f
Eric Paris 109846f
    userstring = subprocess.Popen("rpmdev-packager 2>/dev/null", shell = True,
Eric Paris 109846f
                                  stdout = subprocess.PIPE).communicate()[0]
Eric Paris 109846f
    userstring = userstring.strip() or None
Eric Paris 109846f
Eric Paris 109846f
    parser = OptionParser(usage=usage)
Eric Paris 109846f
    parser.add_option("-c", "--comment", default='- rebuilt',
Eric Paris 109846f
                      help="changelog comment (default: \"- rebuilt\")")
Eric Paris 109846f
    parser.add_option("-u", "--userstring", default=userstring,
Eric Paris 109846f
                      help="user name+email string (default: output from "+
Eric Paris 109846f
                      "rpmdev-packager(1))")
Eric Paris 109846f
    (opts, args) = parser.parse_args()
Eric Paris 109846f
Eric Paris 109846f
    if not args:
Eric Paris 109846f
        parser.error('No specfiles specified')
Eric Paris 109846f
Eric Paris 109846f
    if not opts.userstring:
Eric Paris 109846f
        parser.error('Userstring required, see option -u')
Eric Paris 109846f
Eric Paris 109846f
    # Grab bullet, insert one if not found.
Eric Paris 109846f
    bullet_re = re.compile(r'^([^\s\w])\s', re.UNICODE)
Eric Paris 109846f
    bullet = "-"
Eric Paris 109846f
    match = bullet_re.search(opts.comment)
Eric Paris 109846f
    if match:
Eric Paris 109846f
        bullet = match.group(1)
Eric Paris 109846f
    else:
Eric Paris 109846f
        opts.comment = bullet + " " + opts.comment
Eric Paris 109846f
Eric Paris 109846f
    # Format comment.
Eric Paris 109846f
    if opts.comment.find("\n") == -1:
Eric Paris 109846f
        wrapopts = { "subsequent_indent": (len(bullet)+1) * " ",
Eric Paris 109846f
                     "break_long_words":  False }
Eric Paris 109846f
        if sys.version_info[:2] > (2, 5):
Eric Paris 109846f
            wrapopts["break_on_hyphens"] = False
Eric Paris 109846f
        opts.comment = textwrap.fill(opts.comment, 80, **wrapopts)
Eric Paris 109846f
Eric Paris 109846f
    for aspec in args:
Eric Paris 109846f
        try:
Eric Paris 109846f
            s = SpecFile(aspec)
Eric Paris 109846f
        except:
Eric Paris 109846f
            # Not actually a parser error, but... meh.
Eric Paris 109846f
            parser.error(sys.exc_info()[1])
Eric Paris 109846f
Eric Paris 109846f
        # Get EVR for changelog entry.
Eric Paris 109846f
        cmd = ("rpm", "-q", "--specfile", "--define", "dist %{nil}",
Eric Paris 109846f
               "--qf=%|epoch?{%{epoch}:}:{}|%{version}-%{release}\n", aspec)
Eric Paris 109846f
        popen = subprocess.Popen(cmd, stdout = subprocess.PIPE)
Eric Paris 109846f
        evr = str(popen.communicate()[0]).split("\n")[0]
Eric Paris 109846f
Eric Paris 109846f
        s.addChangelogEntry(evr, opts.comment, opts.userstring)
Eric Paris 109846f
        s.writeFile(aspec)
Eric Paris 109846f
Eric Paris 109846f
sys.exit(0)