def check_api_module_imports(self, py_num): """ Checks if the integration imports an API module and if so pastes the module in the package. :param py_num: The python version - api modules are in python 3 """ if py_num > 3: unifier = Unifier(self.project_dir) code_file_path = unifier.get_code_file('.py') try: # Look for an import to an API module in the code. If there is such import, we need to copy the correct # module file to the package directory. with io.open(code_file_path, mode='r', encoding='utf-8') as script_file: _, module_name = unifier.check_api_module_imports( script_file.read()) if module_name: module_path = os.path.join(self.configuration.env_dir, 'Packs', 'ApiModules', 'Scripts', module_name, module_name + '.py') print_v('Copying ' + os.path.join( self.configuration.env_dir, 'Scripts', module_path)) if not os.path.exists(module_path): raise ValueError( 'API Module {} not found, you might be outside of the content repository' ' or this API module does not exist'.format( module_name)) shutil.copy(os.path.join(module_path), self.project_dir) except Exception as e: print_v('Unable to retrieve the module file {}: {}'.format( module_name, str(e)))
def test_check_api_module_imports(): from demisto_sdk.yaml_tools.unifier import Unifier module_import, module_name = Unifier.check_api_module_imports(DUMMY_SCRIPT) assert module_import == 'from MicrosoftApiModule import * # noqa: E402' assert module_name == 'MicrosoftApiModule'