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