Policy change for NDA/third party modules loading ("addons.coriolis").

* Change: In cumulus/plugins/__init__.py, new behavior for loading
    plugins supplied as third party. In order to avoid messing up
    with the "site-packages/coriolis/" main package tree (with
    modules named "coriolis.<MODULE>", they have to be under
    "site-packages/addons/coriolis/" (so modules will be named
    "addons.coriolis.<MODULE>"). This should prevent *overwritting*
    standard modules by third party ones.
      Now uses pathlib for module loading.
      Had a conflict between Hurricane.Path and pathlib.Path, now do
    not import the Hurricane one into the module globals...
This commit is contained in:
Jean-Paul Chaput 2023-02-28 13:32:11 +01:00
parent 800305abc3
commit c68932c813
1 changed files with 31 additions and 25 deletions

View File

@ -16,10 +16,13 @@
import os import os
import sys import sys
import traceback import traceback
from pathlib import Path
from .. import Cfg from .. import Cfg
from .. import helpers from .. import helpers
from ..helpers.io import vprint, ErrorMessage, WarningMessage from ..helpers.io import vprint, ErrorMessage, WarningMessage, \
from ..Hurricane import Contact, Path, Occurrence, Instance showStackTrace, showPythonTrace
from .. import Hurricane
from ..Hurricane import Contact, Occurrence, Instance
from .. import Viewer from .. import Viewer
from ..CRL import AllianceFramework, RoutingLayerGauge from ..CRL import AllianceFramework, RoutingLayerGauge
@ -83,13 +86,13 @@ class CheckUnplaced ( object ):
if instance.getPlacementStatus() == Instance.PlacementStatus.UNPLACED: if instance.getPlacementStatus() == Instance.PlacementStatus.UNPLACED:
self.unplaceds.append( Occurrence(instance,path) ) self.unplaceds.append( Occurrence(instance,path) )
rpath = Path( path, instance ) rpath = Hurricane.Path( path, instance )
self._rcheckUnplaced( rpath, instance.getMasterCell() ) self._rcheckUnplaced( rpath, instance.getMasterCell() )
return return
def check ( self ): def check ( self ):
self._rcheckUnplaced( Path(), self.cell ) self._rcheckUnplaced( Hurricane.Path(), self.cell )
if self.unplaceds: if self.unplaceds:
if self.flags & (ShowWarnings | WarningsAreErrors): if self.flags & (ShowWarnings | WarningsAreErrors):
@ -180,7 +183,7 @@ class StackedVia ( object ):
return return
def loadPlugins ( pluginsDir ): def loadPlugins ( pluginsDir, prefix=None ):
""" """
Forced import of all the modules that resides in the directory ``pluginsDir``. Forced import of all the modules that resides in the directory ``pluginsDir``.
Works in three stages: Works in three stages:
@ -197,20 +200,21 @@ def loadPlugins ( pluginsDir ):
.. note:: Those modules will be searched later (in ``unicornInit.py``) for any .. note:: Those modules will be searched later (in ``unicornInit.py``) for any
potential ``unicornHook()`` function. potential ``unicornHook()`` function.
""" """
sys.modules['coriolis.plugins'].__path__.append( pluginsDir ) sys.modules['coriolis.plugins'].__path__.append( str(pluginsDir) )
if not os.path.isdir(pluginsDir): if not pluginsDir.is_dir():
print( ErrorMessage( 3, 'plugins.__init__.py: Cannot find <plugins> directory:' \ print( ErrorMessage( 3, 'plugins.__init__.py: Cannot find <plugins> directory:' \
, '"{}"'.format(pluginsDir) )) , '"{}"'.format(pluginsDir) ))
return return
moduleNames = [] moduleNames = []
for entry in os.listdir( pluginsDir ): for entry in pluginsDir.glob('*'):
if entry == '__init__.py': continue if entry.name == '__init__.py': continue
if entry == '__pycache__': continue if entry.name == '__pycache__': continue
packageName = 'coriolis.plugins.' + entry packageName = 'coriolis.plugins.' + entry.name
if not entry.endswith('.py'): if prefix is not None:
path = os.path.join(pluginsDir,entry) packageName = prefix + '.' + packageName
if os.path.isdir(path): if not entry.suffix == '.py':
if entry.is_dir():
if not packageName in sys.modules: if not packageName in sys.modules:
vprint( 2, ' - "{}" (module)'.format( packageName )) vprint( 2, ' - "{}" (module)'.format( packageName ))
module = __import__( packageName, globals(), locals() ) module = __import__( packageName, globals(), locals() )
@ -226,10 +230,10 @@ def loadPlugins ( pluginsDir ):
module = __import__( moduleName, globals(), locals() ) module = __import__( moduleName, globals(), locals() )
except ErrorMessage as e: except ErrorMessage as e:
print( e ) print( e )
helpers.io.showStackTrace( e.trace ) showStackTrace( e.trace )
except Exception as e: except Exception as e:
print( e ) print( e )
helpers.io.showPythonTrace( __file__, e ) showPythonTrace( __file__, e )
return return
@ -239,23 +243,25 @@ def staticInitialization ():
if loaded: return if loaded: return
try: try:
vprint( 1, ' o Preload standard plugins.' ) vprint( 1, ' o Preload standard plugins.' )
pluginsDir = os.path.dirname(__file__) pluginsDir = Path( __file__ ).parent.resolve()
loadPlugins( pluginsDir ) loadPlugins( pluginsDir )
if helpers.ndaTopDir: if helpers.ndaTopDir:
vprint( 1, ' o Preload NDA protected plugins.' ) vprint( 1, ' o Preload NDA/addons protected plugins.' )
pythonDir = os.path.join( helpers.ndaTopDir, 'python{}.{}'.format( sys.version_info.major pythonDir = Path(helpers.ndaTopDir) / Path('python{}.{}'.format( sys.version_info.major
, sys.version_info.minor )) , sys.version_info.minor ))
if os.path.isdir(pythonDir): if pythonDir.is_dir():
pluginsDir = os.path.join( pythonDir, 'site-packages/plugins' ) coriolisDir = pythonDir / 'site-packages'
loadPlugins( pluginsDir ) pluginsDir = coriolisDir / 'addons' / 'coriolis' / 'plugins'
sys.path.append( str(coriolisDir) )
loadPlugins( pluginsDir, prefix='addons' )
else: else:
vprint( 1, ' - No NDA protected plugins directory.' ) vprint( 1, ' - No NDA/addons protected plugins directory.' )
vprint( 1, ' ({}).'.format( pythonDir )) vprint( 1, ' ({}).'.format( pythonDir ))
else: else:
vprint( 1, ' - No NDA protected plugins.' ) vprint( 1, ' - No NDA protected plugins.' )
except Exception as e: except Exception as e:
helpers.io.showPythonTrace( __file__, e ) showPythonTrace( __file__, e )
loaded = True loaded = True
return return