From: <ch...@us...> - 2010-01-08 23:02:35
|
Revision: 333 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=333&view=rev Author: chozone Date: 2010-01-08 23:02:27 +0000 (Fri, 08 Jan 2010) Log Message: ----------- [VPS-core] Added functions.py (global functions). Added check_version function, to that file. Modified Paths: -------------- trunk/server/core/parser.py trunk/server/core/server.py Added Paths: ----------- trunk/server/core/functions.py Added: trunk/server/core/functions.py =================================================================== --- trunk/server/core/functions.py (rev 0) +++ trunk/server/core/functions.py 2010-01-08 23:02:27 UTC (rev 333) @@ -0,0 +1,129 @@ +## This file is part of Virtual Playground +## Copyright (c) 2009 Jos Ratsma + Koen Koning + +## 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., 675 Mass Ave, Cambridge, MA 02139, USA. + + +def check_version(version, expr): + """ + Checks whether a version meets certain conditions. + The first argument, `version`, contains the version you want to check. + The second argument, `expr`, contains the condition (expression). + + Examples of some expression are: + + Exact match: + '1.0.3' match ONLY that version (True if version equal given) + + Greater than: + '>0.9.48' match all versions above version '0.9.48', AND '0.9.48' + itself (True if same or above given) + + Lower than: + '<10.4.0' match all versions below version '10.4.0', AND '10.4.0' + itself (True if same or below given) + + Range: + '0.9.3-0.9.11' match all versions between '0.9.3' and '0.9.11', + including those values themselves (True if same or + between two values) + + + If all conditions are met, this function will return True, otherwise + False. This function will raise a ValueError in case that there were + errors in the provided version/expression. + + As an extra note, remember that 1.0.2 is lower than 1.0.10 (10 > 2). In + other words, there is no preceding zero. + This is because it's not a decimal number. + """ + + # Filter out invalid versions/expressions, so this function works + # correctly later on. + if not expr: return True + if not version: return False + version = str(version) + expr = str(expr) + + try: [int(i) for i in version.split('.')] + except ValueError: raise ValueError, "No valid version: '%s'"%version + + try: [[int(i) for i in p.split('.')] \ + for p in expr.strip('<>').split('-')] + except ValueError: raise ValueError, "No valid expression: '%s'"%expr + + + # Our first real tests. If version matches expression, we return True. + # Also, if the expression, stripped of '<' and '>' characters at the + # beginning (or end...) matched our version, we return True. + # This is for the 'Greater/Lower OR EQUAL than' behaviour of those + # characters. + if version == expr: return True + if version == expr.strip('<>'): return True + + if expr[0] == '>': + # Greater than + + # Get rid of the signs, so we have normal version to work with. + expr = expr.strip('<>') + + # Save the three int that make up the version into an array, so + # we can access them easily to do our check, a few lines below. + c1 = [int(i) for i in version.split('.')] + c2 = [int(i) for i in expr.split('.')] + + # Loops through major, minor, revision, in that order, and + # it tests whether the version is higher/lower. Note that if the + # version are the same, it has already been filtered out, before + # this if block. + for p1, p2 in zip(c1, c2): + if p1 > p2: return True + if p1 < p2: return False + elif expr[0] == '<': + # Less than + + # Mostly same as above + + # Get rid of the signs, so we have normal version to work with. + expr = expr.strip('<>') + + # Save the three int that make up the version into an array, so + # we can access them easily to do our check, a few lines below. + c1 = [int(i) for i in version.split('.')] + c2 = [int(i) for i in expr.split('.')] + + # Loops through major, minor, revision, in that order, and + # it tests whether the version is higher/lower. Note that if the + # version are the same, it has already been filtered out, before + # this if block. + for p1, p2 in zip(c1, c2): + if p1 < p2: return True + if p1 > p2: return False + elif expr.find('-') > -1: + # Range + + # Get the two versions + r1, r2 = expr.split('-') + + # Use this function to see if it's higher than the first, and + # lower than the second value :-) + if _check_version(version, '>' + r1) \ + and _check_version(version, '<' + r2): + return True + + # Better luck next time :-( + return False + + Modified: trunk/server/core/parser.py =================================================================== --- trunk/server/core/parser.py 2010-01-03 16:04:22 UTC (rev 332) +++ trunk/server/core/parser.py 2010-01-08 23:02:27 UTC (rev 333) @@ -15,8 +15,10 @@ ## along with this program; if not, write to the Free Software ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +from functions import * import rsa, hashlib + class Parser(): def __init__(self, sh): """ Modified: trunk/server/core/server.py =================================================================== --- trunk/server/core/server.py 2010-01-03 16:04:22 UTC (rev 332) +++ trunk/server/core/server.py 2010-01-08 23:02:27 UTC (rev 333) @@ -16,9 +16,12 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import simplejson, socket, threading, time, random, sys, hashlib + +from functions import * from parser import Parser import rsa + class Server(threading.Thread): """ This is the server-core for Virtual Playground. This will handle all This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |