From 69400611b2940c2b5cd8bb587b359562eeb95abb Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Wed, 17 Aug 2022 00:11:57 -0600 Subject: [PATCH] utils/scanpypi: restore modules search path in case of error We extend the modules search path to be able to load the package metadata. Currently, it is only restored when loading those succeeded, not when it failed. Restore it to its previous state also in case of error, to avoid leaking the path further. Signed-off-by: James Hilliard Signed-off-by: Yann E. MORIN --- utils/scanpypi | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/utils/scanpypi b/utils/scanpypi index cc13701c0e..452b4a3fc3 100755 --- a/utils/scanpypi +++ b/utils/scanpypi @@ -296,23 +296,25 @@ class BuildrootPackage(): current_dir = os.getcwd() os.chdir(self.tmp_extract) sys.path.insert(0, self.tmp_extract) - s_file, s_path, s_desc = imp.find_module('setup', [self.tmp_extract]) - imp.load_module('__main__', s_file, s_path, s_desc) - if self.metadata_name in self.setup_args: - pass - elif self.metadata_name.replace('_', '-') in self.setup_args: - self.metadata_name = self.metadata_name.replace('_', '-') - elif self.metadata_name.replace('-', '_') in self.setup_args: - self.metadata_name = self.metadata_name.replace('-', '_') try: - self.setup_metadata = self.setup_args[self.metadata_name] - except KeyError: - # This means setup was not called - print('ERROR: Could not determine package metadata for {pkg}.\n' - .format(pkg=self.real_name)) - raise - os.chdir(current_dir) - sys.path.remove(self.tmp_extract) + s_file, s_path, s_desc = imp.find_module('setup', [self.tmp_extract]) + imp.load_module('__main__', s_file, s_path, s_desc) + if self.metadata_name in self.setup_args: + pass + elif self.metadata_name.replace('_', '-') in self.setup_args: + self.metadata_name = self.metadata_name.replace('_', '-') + elif self.metadata_name.replace('-', '_') in self.setup_args: + self.metadata_name = self.metadata_name.replace('-', '_') + try: + self.setup_metadata = self.setup_args[self.metadata_name] + except KeyError: + # This means setup was not called + print('ERROR: Could not determine package metadata for {pkg}.\n' + .format(pkg=self.real_name)) + raise + finally: + os.chdir(current_dir) + sys.path.remove(self.tmp_extract) def get_requirements(self, pkg_folder): """