Commit c0e0f114 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Add patchVersion function that I will use for version management

parent c74de29c
......@@ -5,7 +5,7 @@
# Author: Paul McCarthy <>
"""The primary purpose of this module is as a container for the ``fslpy``
version number. A couple of conveniense functions for comparing version
version number. A handful of convenience functions for managing version
numbers are also defined here.
.. autosummary::
......@@ -14,10 +14,13 @@ numbers are also defined here.
The ``fslpy`` version number consists of three numbers, separated by a period,
which roughly obeys the Semantic Versioning conventions (
roughly obeys the Semantic Versioning conventions (, and
is compatible with PEP 440 (
1. The major release number. This gets updated for major/external releases.
......@@ -26,13 +29,19 @@ which roughly obeys the Semantic Versioning conventions (
3. The point release number. This gets updated for minor/internal releases,
which primarily involve bug-fixes and minor changes.
The sole exception to the above convention are evelopment versions, which end
in ``'.dev'``.
import os.path as op
import re
import string
__version__ = 'dev'
__version__ = ''
"""Current version number, as a string. """
......@@ -44,21 +53,26 @@ def parseVersionString(versionString):
An error is raised if the ``versionString`` is invalid.
if versionString == 'dev':
return 9999, 9999, 9999
components = versionString.split('.')
# Truncate after three elements -
# a development (unreleased0 version
# number will end with '.dev', but
# we ignore this for the purposes of
# comparison.
if len(components) == 4 and components[3] == 'dev':
components = components[:3]
# Major, minor, and point
# version are always numeric
major, minor, point = [c for c in components]
# Early versions of FSLeyes
# But early versions of FSLeyes
# used a letter at the end
# to denote a hotfix release.
# Don't break if we get one
# of these old version numbers.
point = point.strip(string.ascii_letters)
point = point.rstrip(string.ascii_letters)
return [int(c) for c in [major, minor, point]]
......@@ -91,3 +105,27 @@ def compareVersions(v1, v2, ignorePoint=False):
if p1 < p2: return -1
return 0
def patchVersion(filename, newversion):
"""Patches the given ``filename``, in place, with the given
``newversion``. Searches for a line of the form::
__version__ = '<oldversion>'
and replaces ``<oldversion>`` with ``newversion``.
filename = op.abspath(filename)
with open(filename, 'rt') as f:
lines = f.readlines()
pattern = re.compile('^__version__ *= *\'.*\' *$')
for i, line in enumerate(lines):
if pattern.match(line):
lines[i] = '__version__ = \'{0}\'\n'.format(newversion)
with open(filename, 'wt') as f:
lines = f.writelines(lines)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment