#!/usr/bin/env python3 # # 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., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """importVersionControl: Useful to test that a module meets version needs This module is very simple, idea only and handles numbers only You will still need to handle seperating out the version number ! Python does provide a version handler in distutils.version ! If you want maxVersion, or anything simular, just define a function yourself. comparableVersion() is provided just for that reason. Please note that you must handle ValueError yourself For Example, since I know that I place the number first in my version line: >>> import CodeLIB >>> minVersion(CodeLIB.__version__.split()[0], "0.1", CodeLIB.__name__) True """ __all__ = ['comparableVersion', 'exactVersion', 'minVersion', 'VersionError'] __author__ = "Cody A. Taylor ( codemister99@yahoo.com )" __version__ = "1.4" class VersionError(ImportError): pass def comparableVersion(versionString, sep="."): """Return a list that is easy for python to compare >>> comparableVersion("1.4.67") [1, 4, 67] """ return [int(x) for x in versionString.split(sep)] def exactVersion(importVersion, compare, moduleName="", sep="."): """Raises VersionError for you if not correct version >>> exactVersion("0.3", "0.3") True >>> exactVersion("0.2.5", "0.2.1", "foo") Traceback (most recent call last): ... VersionError: foo (0.2.5): must be version 0.2.1 """ if comparableVersion(importVersion, sep) != comparableVersion(compare, sep): raise VersionError("{} ({}): must be version {}".format(moduleName, importVersion, compare)) return True def minVersion(importVersion, compare, moduleName="", sep="."): """Raise VersionError for you if version not met >>> minVersion("0.3.3", "0.3.2") True >>> minVersion("0.2.4.1", "0.3.9", "foo") Traceback (most recent call last): ... VersionError: foo (0.2.4.1): must be version 0.3.9 or higher """ if comparableVersion(importVersion, sep) < comparableVersion(compare, sep): raise VersionError("{} ({}): must be version {} or higher".format(moduleName, importVersion, compare)) return True if __name__ == "__main__": import doctest doctest.testmod()