Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace imp module by importlib #11530

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions bin/inject-to-config-cache
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Add a config and it's meta data to the config cache.

import os
import sys
import imp
import subprocess
import importlib

from PSetTweaks.WMTweak import makeTweak
from WMCore.Cache.WMConfigCache import ConfigCache
Expand All @@ -23,15 +22,16 @@ def loadConfig(configPath):
sys.stdout.flush()
cfgBaseName = os.path.basename(configPath).replace(".py", "")
cfgDirName = os.path.dirname(configPath)
modPath = imp.find_module(cfgBaseName, [cfgDirName])

loadedConfig = imp.load_module(cfgBaseName, modPath[0],
modPath[1], modPath[2])
modSpecs = importlib.machinery.PathFinder().find_spec(cfgBaseName, [cfgDirName])
module = modSpecs.loader.load_module()

print("done.")
return loadedConfig
return module

if __name__ == "__main__":
def main():
"""
Main function of the module
"""
if len(sys.argv) != 8:
print("Usage: %s couchUrl database_name user_name group_name input_file label description" % sys.argv[0])
sys.exit(1)
Expand All @@ -54,3 +54,6 @@ if __name__ == "__main__":
print(" DocID: %s" % configCache.document["_id"])
print(" Revision: %s" % configCache.document["_rev"])
sys.exit(0)

if __name__ == "__main__":
main()
19 changes: 11 additions & 8 deletions bin/outputmodules-from-config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Pull output module metadata from a CMSSW config.
"""
import urllib.request

import imp
import importlib
import os
import sys
import tempfile
Expand All @@ -21,12 +21,9 @@ def loadConfig(configPath):
"""
cfgBaseName = os.path.basename(configPath).replace(".py", "")
cfgDirName = os.path.dirname(configPath)
modPath = imp.find_module(cfgBaseName, [cfgDirName])

loadedConfig = imp.load_module(cfgBaseName, modPath[0],
modPath[1], modPath[2])

return loadedConfig
modSpecs = importlib.machinery.PathFinder().find_spec(cfgBaseName, [cfgDirName])
module = modSpecs.loader.load_module()
return module

def outputModulesFromConfig(configHandle):
"""
Expand Down Expand Up @@ -55,7 +52,10 @@ def outputModulesFromConfig(configHandle):

return outputModules

if __name__ == "__main__":
def main():
"""
Main function of the module
"""
try:
jsonDecoder = JSONDecoder()
jsonEncoder = JSONEncoder()
Expand Down Expand Up @@ -102,3 +102,6 @@ if __name__ == "__main__":
except Exception as ex:
print(ex)
sys.exit(1)

if __name__ == "__main__":
main()
5 changes: 3 additions & 2 deletions bin/wmagent-mod-config
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ https://github.com/dmwm/deployment/blob/master/asyncstageout/manage#L246
"""

import getopt
import imp
import importlib
import os
import socket
import sys
Expand Down Expand Up @@ -43,7 +43,8 @@ def importConfigTemplate(filename):
Given filename, load it and grab the configuration object from it

"""
mod = imp.load_module("wmcore_config_input", open(filename, 'r'), filename, (".py", "r", imp.PY_SOURCE))
modSpecs = importlib.machinery.PathFinder().find_spec(filename)
mod = modSpecs.loader.load_module("wmcore_config_input")
config = getattr(mod, 'config', None)
if config is None:
msg = "No config attribute found in %s" % filename
Expand Down
5 changes: 3 additions & 2 deletions src/python/PSetTweaks/PSetTweak.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from past.builtins import basestring
from future.utils import viewitems, viewvalues

import imp
import importlib
import inspect
import json
import pickle
Expand Down Expand Up @@ -401,7 +401,8 @@ def unpersist(self, filename, formatting=None):
self.process.__dict__.update(unpickle.__dict__)

if formatting == "python":
modRef = imp.load_source('tempTweak', filename)
modSpecs = importlib.util.spec_from_file_location('tempTweak', filename)
modRef = modSpecs.loader.load_module()
lister = PSetLister()
lister(modRef.process)
for pset in lister.psets:
Expand Down
4 changes: 0 additions & 4 deletions src/python/WMCore/Agent/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

"""

import os
import imp
import types

from WMCore.Configuration import ConfigSection as BaseConfigSection
from WMCore.Configuration import Configuration as BaseConfiguration
from WMCore.Configuration import loadConfigurationFile as baseLoadConfigurationFile
Expand Down
9 changes: 4 additions & 5 deletions src/python/WMCore/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from Utils.PythonVersion import PY3

import imp
import importlib

_SimpleTypes = [
bool,
Expand Down Expand Up @@ -596,12 +596,11 @@ def loadConfigurationFile(filename):
cfgBaseName = os.path.basename(filename).replace(".py", "")
cfgDirName = os.path.dirname(filename)
if not cfgDirName:
modPath = imp.find_module(cfgBaseName)
modSpecs = importlib.machinery.PathFinder().find_spec(cfgBaseName)
else:
modPath = imp.find_module(cfgBaseName, [cfgDirName])
modSpecs = importlib.machinery.PathFinder().find_spec(cfgBaseName, [cfgDirName])
try:
modRef = imp.load_module(cfgBaseName, modPath[0],
modPath[1], modPath[2])
modRef = modSpecs.loader.load_module()
except Exception as ex:
msg = "Unable to load Configuration File:\n"
msg += "%s\n" % filename
Expand Down
6 changes: 3 additions & 3 deletions test/python/Integration_t/RequestLifeCycleBase_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from nose.plugins.attrib import attr
import time
import os
import imp
import importlib
from functools import wraps

# decorator around tests - record errors
Expand Down Expand Up @@ -70,8 +70,8 @@ def _configCacheId(self, label):
configCache.addConfig(os.path.join(configDir, label + '.py'))
configCache.setLabel(label)
configCache.setDescription(label)
modPath = imp.find_module(label, [configDir])
loadedConfig = imp.load_module(label, modPath[0], modPath[1], modPath[2])
modSpecs = importlib.machinery.PathFinder().find_spec(label, [configDir])
loadedConfig = modSpecs.loader.load_module()
configCache.setPSetTweaks(makeTweak(loadedConfig.process).jsondictionary())
configCache.save()
return configCache.getIDFromLabel(label)
Expand Down
1 change: 0 additions & 1 deletion test/python/WMCore_t/Services_t/PyCurlRESTModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import unittest
import threading
import cherrypy
import imp
import os
import uuid
import tempfile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from builtins import zip

import imp
import unittest
import os
import sys
Expand Down