From c0e0f114c94689c7f615b02e80fb751e1420b310 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Tue, 6 Jun 2017 22:05:45 +0100 Subject: [PATCH] Add patchVersion function that I will use for version management --- fsl/version.py | 56 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/fsl/version.py b/fsl/version.py index 9acc9d7fd..68a81a1e9 100644 --- a/fsl/version.py +++ b/fsl/version.py @@ -5,7 +5,7 @@ # Author: Paul McCarthy <pauldmccarthy@gmail.com> # """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. __version__ parseVersionString compareVersions + patchVersion The ``fslpy`` version number consists of three numbers, separated by a period, -which roughly obeys the Semantic Versioning conventions (http://semver.org/): +roughly obeys the Semantic Versioning conventions (http://semver.org/), and +is compatible with PEP 440 (https://www.python.org/dev/peps/pep-0440/): + 1. The major release number. This gets updated for major/external releases. @@ -26,13 +29,19 @@ which roughly obeys the Semantic Versioning conventions (http://semver.org/): 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 string +import os.path as op +import re +import string -__version__ = 'dev' +__version__ = '1.0.2.dev' """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) + break + + with open(filename, 'wt') as f: + lines = f.writelines(lines) -- GitLab