Skip to content

Commit

Permalink
[Plugin Manager] Fix for compatibility check in QgsPluginInstaller an…
Browse files Browse the repository at this point in the history
…d QgsPluginRegistry
  • Loading branch information
borysiasty committed Jun 10, 2013
1 parent 61e101d commit 9711506
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
6 changes: 3 additions & 3 deletions python/pyplugin_installer/installer_data.py
Expand Up @@ -33,7 +33,7 @@
import qgis.utils
from qgis.core import *
from qgis.utils import iface
from version_compare import compareVersions, normalizeVersion
from version_compare import compareVersions, normalizeVersion, isCompatible

"""
Data structure:
Expand Down Expand Up @@ -487,7 +487,7 @@ def xmlDownloaded(self,nr,state):
if not qgisMaximumVersion: qgisMaximumVersion = qgisMinimumVersion[0] + ".99"
#if compatible, add the plugin to the list
if not pluginNodes.item(i).firstChildElement("disabled").text().strip().upper() in ["TRUE","YES"]:
if compareVersions(QGis.QGIS_VERSION, qgisMinimumVersion) < 2 and compareVersions(qgisMaximumVersion, QGis.QGIS_VERSION) < 2:
if isCompatible(QGis.QGIS_VERSION, qgisMinimumVersion, qgisMaximumVersion):
#add the plugin to the cache
plugins.addFromRepository(plugin)
# set state=2, even if the repo is empty
Expand Down Expand Up @@ -620,7 +620,7 @@ def pluginMetadata(fct):
qgisMaximumVersion = pluginMetadata("qgisMaximumVersion").strip()
if not qgisMaximumVersion: qgisMaximumVersion = qgisMinimumVersion[0] + ".999"
#if compatible, add the plugin to the list
if compareVersions(QGis.QGIS_VERSION, qgisMinimumVersion) == 2 or compareVersions(qgisMaximumVersion, QGis.QGIS_VERSION) == 2:
if not isCompatible(QGis.QGIS_VERSION, qgisMinimumVersion, qgisMaximumVersion):
error = "incompatible"
errorDetails = "%s - %s" % (qgisMinimumVersion, qgisMaximumVersion)

Expand Down
51 changes: 50 additions & 1 deletion python/pyplugin_installer/version_compare.py
Expand Up @@ -114,7 +114,7 @@ def compareElements(s1,s2):

# ------------------------------------------------------------------------ #
def compareVersions(a,b):
""" Compare two version numbers. Return 0 if a==b or error, 1 if a<b and 2 if b>a """
""" Compare two version numbers. Return 0 if a==b or error, 1 if a>b and 2 if b>a """
if not a or not b:
return 0
a = normalizeVersion(a)
Expand Down Expand Up @@ -155,3 +155,52 @@ def compareVersions(a,b):
return 1
else:
return 2






## COMPARE CURRENT QGIS VERSION WITH qgisMinimumVersion AND qgisMaximumVersion """

def splitVersion(s):
""" split string into 2 or 3 numerical segments """
if not s or type(s) not in [str, unicode]:
return None
l = unicode(s).split('.')
for c in l:
if not c.isnumeric():
return None
if int(c)>999:
return None
if len(l) not in [2,3]:
return None
return l


def isCompatible(curVer, minVer, maxVer=None):
""" Compare current QGIS version with qgisMinVersion and qgisMaxVersion """
minVer = splitVersion(minVer)
maxVer = splitVersion(maxVer)
curVer = splitVersion( curVer.split("-")[0] )

if not minVer or not curVer:
return False

if not maxVer:
maxVer = [minVer[0], "999", "999"]

if len(minVer)<3:
minVer += ["0"]

if len(curVer)<3:
curVer += ["0"]

if len(maxVer)<3:
maxVer += ["999"]

minVer = "%03d%03d%03d" % ( int(minVer[0]), int(minVer[1]), int(minVer[2]) )
maxVer = "%03d%03d%03d" % ( int(maxVer[0]), int(maxVer[1]), int(maxVer[2]) )
curVer = "%03d%03d%03d" % ( int(curVer[0]), int(curVer[1]), int(curVer[2]) )

return ( minVer <= curVer and maxVer >= curVer)
33 changes: 14 additions & 19 deletions src/app/qgspluginregistry.cpp
Expand Up @@ -207,12 +207,11 @@ bool QgsPluginRegistry::checkQgisVersion( QString minVersion, QString maxVersion
}

// Parse qgisMaxVersion. Must be in form x.y.z or just x.y
int maxVerMajor, maxVerMinor, maxVerBugfix = 0;
int maxVerMajor, maxVerMinor, maxVerBugfix = 999;
if ( maxVersion.isEmpty() || maxVersion == "__error__" )
{
maxVerMajor = minVerMajor;
maxVerMinor = 999;
maxVerBugfix = 999;
}
else
{
Expand Down Expand Up @@ -253,23 +252,19 @@ bool QgsPluginRegistry::checkQgisVersion( QString minVersion, QString maxVersion
int qgisMinor = qgisVersionParts.at( 1 ).toInt();
int qgisBugfix = qgisVersionParts.at( 2 ).toInt();

// first check major version
if ( minVerMajor > qgisMajor || maxVerMajor < qgisMajor )
return false;
if ( minVerMajor < qgisMajor || maxVerMajor > qgisMajor )
return true;
// if same, check minor version
if ( minVerMinor > qgisMinor || maxVerMinor < qgisMinor )
return false;
if ( minVerMinor < qgisMinor || maxVerMinor > qgisMinor )
return true;

// if still same, check bugfix version (lower range only)
if ( minVerBugfix > qgisBugfix )
return false;

// looks like min or max version is the same as our version - that's fine
return true;
// build strings with trailing zeroes
QString minVer = QString( "%1%2%3" ).arg( minVerMajor, 3, 10, QChar( '0' ) )
.arg( minVerMinor, 3, 10, QChar( '0' ) )
.arg( minVerBugfix, 3, 10, QChar( '0' ) );
QString maxVer = QString( "%1%2%3" ).arg( maxVerMajor, 3, 10, QChar( '0' ) )
.arg( maxVerMinor, 3, 10, QChar( '0' ) )
.arg( maxVerBugfix, 3, 10, QChar( '0' ) );
QString curVer = QString( "%1%2%3" ).arg( qgisMajor, 3, 10, QChar( '0' ) )
.arg( qgisMinor, 3, 10, QChar( '0' ) )
.arg( qgisBugfix, 3, 10, QChar( '0' ) );

// compare
return ( minVer <= curVer && maxVer >= curVer);
}


Expand Down

0 comments on commit 9711506

Please sign in to comment.