Skip to content

Commit

Permalink
Merge pull request #68 from plesk/CPCLOUD-3156-imp-deprecated
Browse files Browse the repository at this point in the history
[CPCLOUD-3156] Replace deprecated `imp` library with `importlib` library
  • Loading branch information
Kubik-Rubik authored May 13, 2024
2 parents de51345 + 7f07af3 commit 365d7f9
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions agent360/agent360.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import StringIO
from Queue import Queue, Empty

if sys.version_info >= (3,4):
import importlib.util
else:
import imp

import glob
import imp
import certifi
import ssl

Expand Down Expand Up @@ -246,20 +250,28 @@ def test_plugins(plugins=[]):
print('%s:' % plugin_name)

try:
fp, pathname, description = imp.find_module(plugin_name)
if sys.version_info >= (3,4):
spec = importlib.util.find_spec(plugin_name)
else:
fp, pathname, description = imp.find_module(plugin_name)
except Exception as e:
print('Find error:', e)
continue

try:
module = imp.load_module(plugin_name, fp, pathname, description)
if sys.version_info >= (3,4):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
else:
module = imp.load_module(plugin_name, fp, pathname, description)
except Exception as e:
print('Load error:', e)
continue
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()
if sys.version_info < (3,4):
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()

try:
payload = module.Plugin().run(agent.config)
Expand Down Expand Up @@ -399,16 +411,26 @@ def _plugins_init(self):
if self.config.getboolean(name, 'subprocess'):
self.schedule[filename] = 0
else:
fp, pathname, description = imp.find_module(name)
if sys.version_info >= (3,4):
spec = importlib.util.find_spec(name)
else:
fp, pathname, description = imp.find_module(name)

try:
module = imp.load_module(name, fp, pathname, description)
if sys.version_info >= (3,4):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
else:
module = imp.load_module(name, fp, pathname, description)
except Exception:
module = None
logging.error('import_plugin_exception:%s', str(sys.exc_info()[0]))
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()
if sys.version_info < (3,4):
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()

if module:
self.schedule[module] = 0
else:
Expand Down

0 comments on commit 365d7f9

Please sign in to comment.