From d75422c96f4677644bad24388d1275bf8be6c9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez?= Date: Wed, 5 Dec 2012 09:09:33 +0000 Subject: [PATCH] * Added a "needed" comment on USerServiceManager * Cached "modfinder" results * Added "early" loading of dispatchers, so they can register (for example) Configuration Parameters, etc.. --- .../org.eclipse.core.resources.prefs | 2 + server/src/uds/__init__.py | 1 + .../uds/core/managers/UserServiceManager.py | 5 +++ server/src/uds/core/util/modfinder.py | 30 ++++++++----- server/src/uds/dispatchers/__init__.py | 45 +++++++++++++++++++ server/src/uds/models.py | 28 +++++++----- 6 files changed, 87 insertions(+), 24 deletions(-) diff --git a/server/.settings/org.eclipse.core.resources.prefs b/server/.settings/org.eclipse.core.resources.prefs index 1d2c36cb8..902f99cf3 100644 --- a/server/.settings/org.eclipse.core.resources.prefs +++ b/server/.settings/org.eclipse.core.resources.prefs @@ -91,10 +91,12 @@ encoding//src/uds/core/workers/DeployedServiceCleaner.py=utf-8 encoding//src/uds/core/workers/PublicationCleaner.py=utf-8 encoding//src/uds/core/workers/ServiceCacheUpdater.py=utf-8 encoding//src/uds/core/workers/UserServiceCleaner.py=utf-8 +encoding//src/uds/dispatchers/__init__.py=utf-8 encoding//src/uds/dispatchers/pam/urls.py=utf-8 encoding//src/uds/dispatchers/pam/views.py=utf-8 encoding//src/uds/dispatchers/test/urls.py=utf-8 encoding//src/uds/dispatchers/test/views.py=utf-8 +encoding//src/uds/dispatchers/wyse_enterprise/__init__.py=utf-8 encoding//src/uds/dispatchers/wyse_enterprise/urls.py=utf-8 encoding//src/uds/dispatchers/wyse_enterprise/views.py=utf-8 encoding//src/uds/management/commands/config.py=utf-8 diff --git a/server/src/uds/__init__.py b/server/src/uds/__init__.py index 7febe31e3..4d75d3d97 100644 --- a/server/src/uds/__init__.py +++ b/server/src/uds/__init__.py @@ -41,6 +41,7 @@ import services # to make sure that the packages are initialized at this point import auths # To make sure that the packages are initialized at this point import osmanagers # To make sure that packages are initialized at this point import transports # To make sure that packages are initialized at this point +import dispatchers import models diff --git a/server/src/uds/core/managers/UserServiceManager.py b/server/src/uds/core/managers/UserServiceManager.py index e3b6d62a9..6f1dc14cf 100644 --- a/server/src/uds/core/managers/UserServiceManager.py +++ b/server/src/uds/core/managers/UserServiceManager.py @@ -216,6 +216,11 @@ class UserServiceManager(object): state_date=now, creation_date=now, data='', deployed_service=deployedServicePublication.deployed_service, user=user, in_use=False) def __createAssignedAtDbForNoPublication(self, deployedService, user): + ''' + __createCacheAtDb and __createAssignedAtDb uses a publication for create the UserService. + There is cases where deployed services do not have publications (do not need them), so we need this method to create + an UserService with no publications, and create them from an DeployedService + ''' self.__checkMaxDeployedReached(deployedService) now = getSqlDatetime() return deployedService.userServices.create(cache_level=0, state=State.PREPARING, os_state=State.PREPARING, diff --git a/server/src/uds/core/util/modfinder.py b/server/src/uds/core/util/modfinder.py index c6b02b1d6..eb4773f59 100644 --- a/server/src/uds/core/util/modfinder.py +++ b/server/src/uds/core/util/modfinder.py @@ -39,16 +39,22 @@ import uds.dispatchers logger = logging.getLogger(__name__) +patterns = [] + def loadModulesUrls(): - patterns = [] - try: - modName = 'uds.dispatchers' - pkgpath = os.path.dirname(sys.modules[modName].__file__) - for _, name, _ in pkgutil.iter_modules([pkgpath]): - fullModName = '%s.%s.urls' % (modName, name) - mod = __import__(fullModName, globals(), locals(), ['urlpatterns'], -1) - patterns += mod.urlpatterns - except Exception, e: - logger.debug(e) - pass - return patterns \ No newline at end of file + logger.debug('Looking for dispatching modules') + global patterns + if len(patterns) == 0: + try: + modName = 'uds.dispatchers' + pkgpath = os.path.dirname(sys.modules[modName].__file__) + for _, name, _ in pkgutil.iter_modules([pkgpath]): + fullModName = '%s.%s.urls' % (modName, name) + mod = __import__(fullModName, globals(), locals(), ['urlpatterns'], -1) + patterns += mod.urlpatterns + except Exception, e: + logger.debug(e) + pass + + return patterns + \ No newline at end of file diff --git a/server/src/uds/dispatchers/__init__.py b/server/src/uds/dispatchers/__init__.py index e69de29bb..3c1749825 100644 --- a/server/src/uds/dispatchers/__init__.py +++ b/server/src/uds/dispatchers/__init__.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2012 Virtual Cable S.L. +# All rights reserved. +# + +''' +@author: Adolfo Gómez, dkmaster at dkmon dot com +''' + +import logging + +logger = logging.getLogger(__name__) +''' +Service modules for uds are contained inside this package. +To create a new service module, you will need to follow this steps: + 1.- Create the service module, probably based on an existing one + 2.- Insert the module package as child of this package + 3.- Import the class of your service module at __init__. For example:: + from Service import SimpleService + 4.- Done. At Server restart, the module will be recognized, loaded and treated + +The registration of modules is done locating subclases of :py:class:`uds.core.auths.Authentication` + +.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com +''' + +def __init__(): + ''' + This imports all packages that are descendant of this package, and, after that, + it register all subclases of service provider as + ''' + import os.path, pkgutil + import sys + + # Dinamycally import children of this package. The __init__.py files must register, if needed, inside ServiceProviderFactory + pkgpath = os.path.dirname(sys.modules[__name__].__file__) + for _, name, _ in pkgutil.iter_modules([pkgpath]): + __import__(name, globals(), locals(), [], -1) + + + logger.debug('Dispatchers initialized') + +__init__() diff --git a/server/src/uds/models.py b/server/src/uds/models.py index fd253d154..a0a30e338 100644 --- a/server/src/uds/models.py +++ b/server/src/uds/models.py @@ -1678,24 +1678,28 @@ class Network(models.Model): ''' convert decimal dotted quad string to long integer ''' - - hexn = ''.join(["%02X" % long(i) for i in ip.split('.')]) - return long(hexn, 16) + try: + hexn = ''.join(["%02X" % long(i) for i in ip.split('.')]) + return long(hexn, 16) + except: + return 0 # Invalid values will map to "0.0.0.0" --> 0 @staticmethod def longToIp(n): ''' convert long int to dotted quad string ''' - - d = 256 * 256 * 256 - q = [] - while d > 0: - m,n = divmod(n,d) - q.append(str(m)) - d = d/256 - - return '.'.join(q) + try: + d = 256 * 256 * 256 + q = [] + while d > 0: + m,n = divmod(n,d) + q.append(str(m)) + d = d/256 + + return '.'.join(q) + except: + return '0.0.0.0' # Invalid values will map to "0.0.0.0" @staticmethod def networksFor(ip):