From 03452d0fbe2f5a4279aceaeb7ba8dd2e3ab5976c Mon Sep 17 00:00:00 2001 From: Adrien Bruneton Date: Thu, 13 Feb 2014 11:01:28 +0100 Subject: [PATCH] New Python's logic: deleting un-necessary "shared_modules" logic. --- bin/virtual_salome.py | 5 +- src/KERNEL_PY/CMakeLists.txt | 3 - src/KERNEL_PY/batchmode_salome.py | 3 +- src/KERNEL_PY/import_hook.py | 220 ------------------------- src/KERNEL_PY/kernel_shared_modules.py | 54 ------ src/KERNEL_PY/salome_shared_modules.py | 107 ------------ 6 files changed, 3 insertions(+), 389 deletions(-) delete mode 100755 src/KERNEL_PY/import_hook.py delete mode 100755 src/KERNEL_PY/kernel_shared_modules.py delete mode 100755 src/KERNEL_PY/salome_shared_modules.py diff --git a/bin/virtual_salome.py b/bin/virtual_salome.py index 954d7c004..c5d31f748 100644 --- a/bin/virtual_salome.py +++ b/bin/virtual_salome.py @@ -135,7 +135,7 @@ def link_module(options): module_lib_dir=os.path.join(module_dir,get_lib_dir(),'salome') module_lib_py_dir=os.path.join(module_dir,get_lib_dir(),pyversio,'site-packages','salome') module_lib_py_shared_dir=os.path.join(module_dir,get_lib_dir(),pyversio, - 'site-packages','salome','shared_modules') + 'site-packages','salome') module_share_dir=os.path.join(module_dir,'share','salome') module_doc_gui_dir=os.path.join(module_dir,'doc','salome','gui') module_doc_tui_dir=os.path.join(module_dir,'doc','salome','tui') @@ -150,7 +150,7 @@ def link_module(options): lib_dir=os.path.join(home_dir,'lib','salome') lib_py_dir=os.path.join(home_dir,'lib',pyversio,'site-packages','salome') lib_py_shared_dir=os.path.join(home_dir,'lib',pyversio, - 'site-packages','salome','shared_modules') + 'site-packages','salome') share_dir=os.path.join(home_dir,'share','salome') doc_gui_dir=os.path.join(home_dir,'doc','salome','gui') doc_tui_dir=os.path.join(home_dir,'doc','salome','tui') @@ -213,7 +213,6 @@ def link_module(options): mkdir(lib_pypkg_dir) mkdir(lib_py_shared_dir) for fn in os.listdir(module_lib_py_dir): - if fn == "shared_modules": continue if fn == "salome": continue symlink(os.path.join(module_lib_py_dir, fn), os.path.join(lib_py_dir, fn)) pass diff --git a/src/KERNEL_PY/CMakeLists.txt b/src/KERNEL_PY/CMakeLists.txt index 760e32203..2864e9cd1 100755 --- a/src/KERNEL_PY/CMakeLists.txt +++ b/src/KERNEL_PY/CMakeLists.txt @@ -20,8 +20,6 @@ ADD_SUBDIRECTORY(kernel) SET(salomepython_PYTHON - salome_shared_modules.py - import_hook.py omnipatch.py ) @@ -43,5 +41,4 @@ SET(salomepython_PYTHON ) SALOME_INSTALL_SCRIPTS("${salomepython_PYTHON}" ${SALOME_INSTALL_PYTHON}) -SALOME_INSTALL_SCRIPTS(kernel_shared_modules.py ${SALOME_INSTALL_PYTHON_SHARED}) SALOME_INSTALL_SCRIPTS(__init__.py ${SALOME_INSTALL_PYTHON}/salome) diff --git a/src/KERNEL_PY/batchmode_salome.py b/src/KERNEL_PY/batchmode_salome.py index 834c884f2..a7292c08a 100755 --- a/src/KERNEL_PY/batchmode_salome.py +++ b/src/KERNEL_PY/batchmode_salome.py @@ -23,8 +23,7 @@ # File : batchmode_salome.py # Module : SALOME -#import salome_shared_modules -# + from omniORB import CORBA from LifeCycleCORBA import * import SALOME diff --git a/src/KERNEL_PY/import_hook.py b/src/KERNEL_PY/import_hook.py deleted file mode 100755 index d57441173..000000000 --- a/src/KERNEL_PY/import_hook.py +++ /dev/null @@ -1,220 +0,0 @@ -# -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE -# -# Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, -# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License. -# -# This library 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 -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -# - -""" -This module replaces the standard import mechanism with one -that filters some imports that can't be done more than once. - -This is related to the multi study feature that is implemented -by using the Python multi interpreter feature. -Some modules register objects or classes by calling modules -implemented in C. These operations can't be done multiple times. -So it's very important to control these imports. - -Examples: - - PyQt : import qt calls a C module to register classes - - OmniORB : import *_idl calls a C module to register CORBA interfaces - -Usage: - - First import the module : import import_hook. This module will - replace the original importer mechanism - - - Next register the module names or pattern names to filter out:: - import_hook.register_name("a") - import_hook.register_pattern(pattern) - - where pattern is a function with one parameter, the module name - to be imported, that returns true or false depending if this module is - to be filtered or not. - - - Then it's done - -IMPORTANT : Every subinterpretor has its own import_hook module. import_hook is not shared among subinterpretors. -The mechanism only works if shared_imported and pattern are shared between all subinterpretors. -This is done by calling init_shared_modules(). - -""" -import sys, imp, __builtin__ - -# Keep in shared_imported a copy of dictionnary modules -# that need to be imported only once in multi-study context -shared_imported={} - -# patterns contains functions that returns 1 or 0 depending if -# the module name (argument) must be filtered out or not -# These functions are added by calling register_pattern -patterns=[] - -original_import=__builtin__.__import__ - -def register_name(name): - if shared_imported.has_key(name):return - shared_imported[name]=None - -def register_pattern(pattern): - patterns.append(pattern) - -def is_shared(name): - """ Indicate if module name is a shared module - among multiple interpreters (return value=1) - """ - if shared_imported.has_key(name):return 1 - for pattern in patterns: - if pattern(name) : return 1 - return 0 - -def get_shared_imported(name,fromlist): - """ If the module is registered in shared_imported - update the sys.modules dict - Let the real import be done by original_import - """ - module= shared_imported.get(name) - if module is None : - #module name is not shared or not already imported - #let original_import do the job - return None - - # module is already imported and shared. Put it in sys.modules and - # let original_import finish the job - sys.modules[name]=module - -def get_real_module(mod,name): - """Return effective module on import - Standard import returns module A on import A.B - To get module A.B use get_real_module with name "A.B" - """ - components = name.split('.') - for comp in components[1:]: - mod = getattr(mod, comp) - return mod - -def set_shared_imported(name,module): - """ Register a shared module - Name can be a dotted name : package - """ - shared_imported[name]=module - #print "Module %s shared registered" % name,module - -def import_module(partname, fqname, parent): - """ Try to import module fqname - It's parent is module parent and has name partname - """ - #print "import_module",partname, fqname, parent - try: - m = sys.modules[fqname] - except KeyError: - pass - else: - return m - -def ensure_fromlist(m, fromlist, recursive=0): - """ Return the real modules list to be imported - """ - #print "ensure_fromlist",m, fromlist, recursive - l=[] - for sub in fromlist: - if sub == "*": - if not recursive: - try: - all = m.__all__ - except AttributeError: - pass - else: - l.extend(ensure_fromlist(m, all, 1)) - else: - #try to find if sub is an attribute (eventually dotted) of m - components=sub.split('.') - has_submod=True - submod=m - for comp in components: - if hasattr(submod,comp): - submod=getattr(submod, comp) - else: - has_submod=False - break - - if has_submod: - #the attribute has been found - if type(submod) == type(sys): - l.append(("%s.%s" % (m.__name__, sub),submod)) - else: - subname="%s.%s" % (m.__name__, sub) - submod = import_module(sub, subname, m) - #if not found ignore it - if submod: - l.append((subname,submod)) - return l - -def import_hook(name, globals=None, locals=None, fromlist=None, *args, **kwds): - """ Import replacement for sharing modules among multiple interpreters - Mostly update sys.modules before doing real import - """ - #print "import_hook",name,fromlist - m=get_shared_imported(name,fromlist) - - module= original_import(name, globals, locals, fromlist, *args, **kwds) - - if fromlist: - #when fromlist is specified, module is the real module - #fromlist is a list of possibly dotted name - m=module - for nam,mod in ensure_fromlist(m, fromlist): - if is_shared(nam): - set_shared_imported(nam,mod) - else: - #when fromlist is not specified and name is a dotted name, - # module is the root package not the real module - #so we need to retrieve it - # note: some modules like xml.dom do not play the rule - # (import xml: no attribute dom, but import xml.dom OK) - try: - m=get_real_module(module,name) - except AttributeError: - m=None - - if type(m) == type(sys) and is_shared(m.__name__): - set_shared_imported(m.__name__,m) - - return module - -original_reload=__builtin__.reload - -def reload_hook(module): - if is_shared(module.__name__): - return module - return original_reload(module) - -__builtin__.__import__=import_hook -# Reload is not replaced -#__builtin__.reload=reload_hook - -def init_shared_modules(shared_module): - global shared_imported, patterns - shared_imported=shared_module.shared_imported - patterns= shared_module.patterns - for k,v in shared_imported.items(): - if v is not None:sys.modules[k]=v - shared_imported["salome_shared_modules"]=shared_module - import salome_shared_modules - for m in salome_shared_modules.list_modules: - m.init_shared_modules() diff --git a/src/KERNEL_PY/kernel_shared_modules.py b/src/KERNEL_PY/kernel_shared_modules.py deleted file mode 100755 index 855ff4d3a..000000000 --- a/src/KERNEL_PY/kernel_shared_modules.py +++ /dev/null @@ -1,54 +0,0 @@ -# -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE -# -# Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, -# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License. -# -# This library 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 -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -# - -""" - -""" -import import_hook - -import glob,os,sys,string,imp - -from import_hook import register_name -from import_hook import register_pattern - -register_name("qt") -register_name("libSALOME_LifeCycleCORBA") -register_pattern(lambda(x):x.endswith("_idl")) -register_pattern(lambda(x):x.endswith("_Swig")) - -register_name("omniORB") -register_name("CORBA") -register_name("CosNaming") -register_name("CosNaming__POA") -register_name("omnipatch") - -import omniORB -from omniORB import CORBA -import CosNaming -import CosNaming__POA -import omnipatch - -def init_shared_modules(): - """ - This function initializes shared modules that need to be - """ diff --git a/src/KERNEL_PY/salome_shared_modules.py b/src/KERNEL_PY/salome_shared_modules.py deleted file mode 100755 index fc0f569ef..000000000 --- a/src/KERNEL_PY/salome_shared_modules.py +++ /dev/null @@ -1,107 +0,0 @@ -# -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE -# -# Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, -# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License. -# -# This library 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 -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -# - -# File : salome_shared_modules.py -# Module : SALOME -# -""" -This module with help of import_hook and *_shared_modules -filters imports when using the embedded Python interpretor. - -Some imports can't be done more than once. -This is related to the multi study feature that is implemented -by using the Python multi interpreter feature. -Some modules register objects or classes by calling modules -implemented in C. These operations can't be done multiple times. -So it's very important to control these imports. - -Examples: - - PyQt : import qt calls a C module to register classes - - OmniORB : import *_idl calls a C module to register CORBA interfaces - -Usage: - - First : the module salome_shared_modules is imported by main Python interpretor. - It will keep a dictionnary and a list that are shared among all - the subinterpretors and imports import_hook module that replaces - the standard import mechanism par one that prevents more than one import - for some modules identified by name (call register_name) or by a - validator (call register_pattern). - - Calls to register_name and register_pattern are done in modules named *_shared_modules - that could be found in the path SALOMEPATH - -""" -import glob,os,sys - -import import_hook -# shared_imported, patterns, register_name, register_pattern -# will be shared by all Python sub interpretors -from import_hook import patterns -from import_hook import register_name -from import_hook import register_pattern - -register_name("salome_shared_modules") -register_name("omniORB") -register_name("omnipatch") -register_pattern(lambda(x):x.endswith("_idl")) -register_pattern(lambda(x):x.startswith("omniORB.")) - -from omnipatch import shared_imported -shared_imported.update(import_hook.shared_imported) -import_hook.shared_imported=shared_imported - -# Get the SALOMEPATH if set or else use KERNEL_ROOT_DIR that should be set. -salome_path=os.environ.get("SALOMEPATH",os.getenv("KERNEL_ROOT_DIR")) - -list_modules=[] - -# Import all *_shared_modules in the path and store them in list_modules -splitter = ":" -if sys.platform == "win32": - splitter = ";" -path=salome_path.split(splitter) -import platform -libdir = "lib" -for rep in path: - # Import all *_shared_modules in rep - for f in glob.glob(os.path.join(rep,libdir,"python"+sys.version[:3],"site-packages","salome","shared_modules","*_shared_modules.py")): - try: - name=os.path.splitext(os.path.basename(f))[0] - register_name(name) - #print name + " REGISTERED" - m=__import__(name) - #print name + " IMPORTED" - list_modules.append(m) - except: - print "Exception during register and import shared module" - pass - -# -# If shared modules have been imported before installing import mechanism -# we add them to shared_imported -# -for name,module in sys.modules.items(): - if module and import_hook.is_shared(name) and not shared_imported.has_key(name): - #print "Module shared added to shared_imported: ",name - shared_imported[name]=module - -- 2.39.2