utils/scanpypi: allow installation of commands without 'main' method
In case the setup.py file of a python package does not directly call the 'setup' method, utils/scanpypi was hoping there be a 'main' function which would do the work, normally called via a construct like: if __name__ == '__main__': main() However, this construct is nonstandard, and there are packages in PyPI which call 'setup()' directly from the 'if' statement, without a main() method. But scanpypi does not actually need to make such assumption: when loading the module, it can decide the name to be '__main__', just as if setup.py would be loaded interactively. Additionally, remove some logic seemingly related to the previous trick of calling 'main'. There should not be a problem in keeping already loaded modules in sys.modules, as this is the purpose of sys.modules. Signed-off-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
This commit is contained in:
parent
ad042904f4
commit
e43c050944
|
@ -299,7 +299,7 @@ class BuildrootPackage():
|
||||||
os.chdir(self.tmp_extract)
|
os.chdir(self.tmp_extract)
|
||||||
sys.path.insert(0,self.tmp_extract)
|
sys.path.insert(0,self.tmp_extract)
|
||||||
s_file, s_path, s_desc = imp.find_module('setup', [self.tmp_extract])
|
s_file, s_path, s_desc = imp.find_module('setup', [self.tmp_extract])
|
||||||
setup = imp.load_module('setup', s_file, s_path, s_desc)
|
setup = imp.load_module('__main__', s_file, s_path, s_desc)
|
||||||
if self.metadata_name in self.setup_args:
|
if self.metadata_name in self.setup_args:
|
||||||
pass
|
pass
|
||||||
elif self.metadata_name.replace('_', '-') in self.setup_args:
|
elif self.metadata_name.replace('_', '-') in self.setup_args:
|
||||||
|
@ -309,19 +309,10 @@ class BuildrootPackage():
|
||||||
try:
|
try:
|
||||||
self.setup_metadata = self.setup_args[self.metadata_name]
|
self.setup_metadata = self.setup_args[self.metadata_name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# This means setup was not called which most likely mean that it is
|
# This means setup was not called
|
||||||
# called through the if __name__ == '__main__' directive.
|
print('ERROR: Could not determine package metadata for {pkg}.\n'
|
||||||
# In this case, we can only pray that it is called through a
|
.format(pkg=self.real_name))
|
||||||
# function called main() in setup.py.
|
raise
|
||||||
setup.main() # Will raise AttributeError if not found
|
|
||||||
self.setup_metadata = self.setup_args[self.metadata_name]
|
|
||||||
# Here we must remove the module the hard way.
|
|
||||||
# We must do this because of a very specific case: if a package calls
|
|
||||||
# setup from the __main__ but does not come with a 'main()' function,
|
|
||||||
# for some reason setup.main() will successfully call the main
|
|
||||||
# function of a previous package...
|
|
||||||
sys.modules.pop('setup', None)
|
|
||||||
del setup
|
|
||||||
os.chdir(current_dir)
|
os.chdir(current_dir)
|
||||||
sys.path.remove(self.tmp_extract)
|
sys.path.remove(self.tmp_extract)
|
||||||
|
|
||||||
|
@ -706,7 +697,7 @@ def main():
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
continue
|
continue
|
||||||
except AttributeError as error:
|
except (AttributeError, KeyError) as error:
|
||||||
print('Error: Could not install package {pkg}: {error}'.format(
|
print('Error: Could not install package {pkg}: {error}'.format(
|
||||||
pkg=package.real_name, error=error))
|
pkg=package.real_name, error=error))
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in a new issue