kombu/extra/release/bump_version.py

167 lines
4.3 KiB
Python
Raw Normal View History

2011-09-26 17:11:41 +00:00
#!/usr/bin/env python
from __future__ import absolute_import
import errno
import os
import re
import sys
import subprocess
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
rq = lambda s: s.strip("\"'")
2013-09-10 16:26:12 +00:00
str_t = str if sys.version_info[0] >= 3 else basestring
2011-09-26 17:11:41 +00:00
def cmd(*args):
return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
@contextmanager
def no_enoent():
try:
yield
2013-02-12 15:06:49 +00:00
except OSError as exc:
2011-09-26 17:11:41 +00:00
if exc.errno != errno.ENOENT:
raise
2011-09-26 17:13:53 +00:00
2011-09-26 17:11:41 +00:00
class StringVersion(object):
def decode(self, s):
2011-09-27 13:07:36 +00:00
s = rq(s)
2011-09-26 17:11:41 +00:00
text = ""
major, minor, release = s.split(".")
if not release.isdigit():
pos = release.index(re.split("\d+", release)[1][0])
release, text = release[:pos], release[pos:]
return int(major), int(minor), int(release), text
def encode(self, v):
return ".".join(map(str, v[:3])) + v[3]
to_str = StringVersion().encode
from_str = StringVersion().decode
class TupleVersion(object):
def decode(self, s):
v = list(map(rq, s.split(", ")))
return (tuple(map(int, v[0:3])) +
tuple(["".join(v[3:])]))
def encode(self, v):
v = list(v)
def quote(lit):
2013-09-10 16:26:12 +00:00
if isinstance(lit, str_t):
2011-09-26 17:11:41 +00:00
return '"%s"' % (lit, )
return str(lit)
if not v[-1]:
v.pop()
return ", ".join(map(quote, v))
2011-09-26 17:13:53 +00:00
2011-09-26 17:11:41 +00:00
class VersionFile(object):
def __init__(self, filename):
self.filename = filename
2011-09-27 13:07:36 +00:00
self._kept = None
2011-09-26 17:11:41 +00:00
def _as_orig(self, version):
2011-09-27 13:07:36 +00:00
return self.wb % {"version": self.type.encode(version),
"kept": self._kept}
2011-09-26 17:11:41 +00:00
def write(self, version):
pattern = self.regex
with no_enoent():
with NamedTemporaryFile() as dest:
with open(self.filename) as orig:
for line in orig:
if pattern.match(line):
dest.write(self._as_orig(version))
else:
dest.write(line)
os.rename(dest.name, self.filename)
def parse(self):
pattern = self.regex
2011-09-27 13:07:36 +00:00
gpos = 0
2011-09-26 17:11:41 +00:00
with open(self.filename) as fh:
for line in fh:
m = pattern.match(line)
if m:
2011-09-27 13:07:36 +00:00
if "?P<keep>" in pattern.pattern:
self._kept, gpos = m.groupdict()["keep"], 1
return self.type.decode(m.groups()[gpos])
2011-09-26 17:11:41 +00:00
2011-09-26 17:13:53 +00:00
2011-09-26 17:11:41 +00:00
class PyVersion(VersionFile):
regex = re.compile(r'^VERSION\s*=\s*\((.+?)\)')
2011-09-27 13:07:36 +00:00
wb = "VERSION = (%(version)s)\n"
2011-09-26 17:11:41 +00:00
type = TupleVersion()
class SphinxVersion(VersionFile):
regex = re.compile(r'^:[Vv]ersion:\s*(.+?)$')
2011-09-27 13:07:36 +00:00
wb = ':Version: %(version)s\n'
2011-09-26 17:11:41 +00:00
type = StringVersion()
2011-09-27 13:07:36 +00:00
class CPPVersion(VersionFile):
regex = re.compile(r'^\#\s*define\s*(?P<keep>\w*)VERSION\s+(.+)')
wb = '#define %(kept)sVERSION "%(version)s"\n'
type = StringVersion()
2011-09-26 17:11:41 +00:00
2011-09-27 13:07:36 +00:00
_filetype_to_type = {"py": PyVersion,
"rst": SphinxVersion,
"c": CPPVersion,
"h": CPPVersion}
2011-09-26 17:11:41 +00:00
2012-09-21 13:18:16 +00:00
2011-09-26 20:02:11 +00:00
def filetype_to_type(filename):
_, _, suffix = filename.rpartition(".")
return _filetype_to_type[suffix](filename)
def bump(*files, **kwargs):
version = kwargs.get("version")
files = [filetype_to_type(f) for f in files]
2011-09-26 17:11:41 +00:00
versions = [v.parse() for v in files]
current = list(reversed(sorted(versions)))[0] # find highest
2011-09-26 20:02:11 +00:00
if version:
next = from_str(version)
2011-09-26 17:11:41 +00:00
else:
major, minor, release, text = current
if text:
raise Exception("Can't bump alpha releases")
next = (major, minor, release + 1, text)
print("Bump version from %s -> %s" % (to_str(current), to_str(next)))
for v in files:
print(" writing %r..." % (v.filename, ))
v.write(next)
2011-09-26 17:12:10 +00:00
print(cmd("git", "commit", "-m", "Bumps version to %s" % (to_str(next), ),
2013-01-17 13:50:01 +00:00
*[f.filename for f in files]))
2011-09-26 17:12:10 +00:00
print(cmd("git", "tag", "v%s" % (to_str(next), )))
2011-09-26 17:11:41 +00:00
2011-09-26 20:02:11 +00:00
def main(argv=sys.argv, version=None):
2011-09-26 17:11:41 +00:00
if not len(argv) > 1:
print("Usage: distdir [docfile] -- <custom version>")
sys.exit(0)
if "--" in argv:
c = argv.index('--')
2011-09-26 20:02:11 +00:00
version = argv[c + 1]
2011-09-26 17:11:41 +00:00
argv = argv[:c]
2011-09-27 13:07:36 +00:00
bump(*argv[1:], version=version)
2011-09-26 17:11:41 +00:00
if __name__ == "__main__":
main()