From 265dc5f857964ac06866a097aa5425c6cb692e45 Mon Sep 17 00:00:00 2001 From: Avidan Hessing <46294017+avidan-H@users.noreply.github.com> Date: Sun, 1 Dec 2019 17:01:31 +0200 Subject: [PATCH] Fix file exists check (#39) * update isfile check in copy_dir_json to check the correct file path * update get_code_file to use the correct package name * update passed arg to Unifier in copy_content_yml * revert get_code_file function * remove 'test_.*\.py' from ignore regex --- demisto_sdk/yaml_tools/content_creator.py | 8 +++++--- demisto_sdk/yaml_tools/unifier.py | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/demisto_sdk/yaml_tools/content_creator.py b/demisto_sdk/yaml_tools/content_creator.py index 728a23392f4..22ba5440488 100644 --- a/demisto_sdk/yaml_tools/content_creator.py +++ b/demisto_sdk/yaml_tools/content_creator.py @@ -122,7 +122,7 @@ def copy_content_yml(self, path, out_path, yml_info): script_obj = yml_info['script'] with io.open(path, mode='r', encoding='utf-8') as file_: yml_text = file_.read() - unifier = Unifier(path, parent_dir_name, out_path) + unifier = Unifier(os.path.dirname(path), parent_dir_name, out_path) out_map = unifier.write_yaml_with_docker(yml_text, yml_info, script_obj) if len(out_map.keys()) > 1: print(" - yaml generated multiple files: {}".format(out_map.keys())) @@ -166,8 +166,10 @@ def copy_dir_json(self, dir_path, bundle): new_path = dpath if dir_name == 'IndicatorFields' and not dpath.startswith('incidentfield-indicatorfield-'): new_path = dpath.replace('incidentfield-', 'incidentfield-indicatorfield-') - if os.path.isfile(new_path): - raise NameError('Failed while trying to create {}. File already exists.'.format(new_path)) + if os.path.isfile(os.path.join(bundle, new_path)): + raise NameError( + f'Failed while trying to create {os.path.join(bundle, new_path)}. File already exists.' + ) dpath = new_path if len(dpath) >= self.file_name_max_size: diff --git a/demisto_sdk/yaml_tools/unifier.py b/demisto_sdk/yaml_tools/unifier.py index e044a80dc68..8be588d69e0 100644 --- a/demisto_sdk/yaml_tools/unifier.py +++ b/demisto_sdk/yaml_tools/unifier.py @@ -177,20 +177,22 @@ def get_data(self, extension): def get_code_file(self, script_type): """Return the first code file in the specified directory path - :param script_type: script type: .py or .js :type script_type: str :return: path to found code file :rtype: str """ - # We assume here the code file has the same name as the integration/script name, plus the addition of the type - package_name = os.path.basename(os.path.dirname(self.package_path)) - code_file_path = self.package_path + package_name + script_type - if not os.path.isfile(code_file_path): - raise Exception('Code file does not exists or has different name than {}' - .format(package_name + script_type)) - return code_file_path + ignore_regex = (r'CommonServerPython\.py|CommonServerUserPython\.py|demistomock\.py|_test\.py' + r'|conftest\.py') + if not self.package_path.endswith('/'): + self.package_path += '/' + if self.package_path.endswith('Scripts/CommonServerPython/'): + return self.package_path + 'CommonServerPython.py' + + script_path = list(filter(lambda x: not re.search(ignore_regex, x), + glob.glob(os.path.join(self.package_path, '*' + script_type))))[0] + return script_path def insert_script_to_yml(self, script_type, yml_text, yml_data): script_path = self.get_code_file(script_type)