def format_file(self, file_path: str, file_ending: str) -> None: """ Runs demisto-sdk format on the file :param file_path: The file path :param file_ending: The file ending :return: None """ if self.run_format and file_ending in ('yml', 'json'): format_manager(input=os.path.abspath(file_path), no_validate=False)
def format_converted_pack(self) -> None: """Runs the demisto-sdk's format command on the pack converted from the contribution zipfile""" click.echo( f'Executing \'format\' on the restructured contribution zip files at "{self.pack_dir_path}"' ) from_version = '6.0.0' if self.create_new else '' format_manager(input=self.pack_dir_path, from_version=from_version, no_validate=True, update_docker=True, assume_yes=True)
def format_converted_pack(self) -> None: """Runs the demisto-sdk's format command on the pack converted from the contribution zipfile""" click.echo( f'Executing \'format\' on the restructured contribution zip new/modified files at {self.pack_dir_path}' ) from_version = '6.0.0' if self.create_new else '' format_manager( from_version=from_version, no_validate=True, update_docker=True, verbose=True, assume_yes=True, include_untracked=True, interactive=False, )
def test_output_file(self, invalid_output): try: res_invalid = format_manager(input=invalid_output, output=invalid_output) assert res_invalid except Exception as e: assert str(e) == "The given output path is not a specific file path.\nOnly file path can be a output path." \ " Please specify a correct output."
def test_set_feed_params_in_config(source, target, path, answer): """ Given - Integration yml with feed field labeled as true and all necessary params exist. - Integration yml with feed field labeled as true and without the necessary feed params. - destination_path to write the formatted integration to. When - Running the format command. Then - Ensure the file was created. - Ensure that the feedBypassExclusionList, Fetch indicators , feedReputation, feedReliability , feedExpirationPolicy, feedExpirationInterval ,feedFetchInterval params were added to the yml of the integration. """ os.makedirs(path, exist_ok=True) shutil.copyfile(source, target) res = format_manager(input=target) with open(target, 'r') as f: content = f.read() yaml_content = yaml.load(content) params = yaml_content['configuration'] for counter, param in enumerate(params): if 'defaultvalue' in param: params[counter].pop('defaultvalue') for param in FEED_REQUIRED_PARAMS: assert param in params os.remove(target) os.rmdir(path) assert res is answer
def test_set_fetch_params_in_config(source, target, path, answer): """ Given - Integration yml with isfetch field labeled as true and correct fetch params. - Integration yml with isfetch field labeled as true and without the fetch params. - destination_path to write the formatted integration to. When - Running the format command. Then - Ensure the file was created. - Ensure that the isfetch and incidenttype params were added to the yml of the integration. """ os.makedirs(path, exist_ok=True) shutil.copyfile(source, target) res = format_manager(input=target, verbose=True) with open(target, 'r') as f: content = f.read() yaml_content = yaml.load(content) params = yaml_content['configuration'] for param in params: if 'defaultvalue' in param and param['name'] != 'feed': param.pop('defaultvalue') for param in FETCH_REQUIRED_PARAMS: assert param in yaml_content['configuration'] os.remove(target) os.rmdir(path) assert res is answer
def test_format_file(self, source, target, path, answer): os.makedirs(path, exist_ok=True) shutil.copyfile(source, target) res = format_manager(input=target, output=target, verbose=True) shutil.rmtree(target, ignore_errors=True) shutil.rmtree(path, ignore_errors=True) assert res is answer
def test_format_file(self, source, target, path, answer): os.makedirs(path) shutil.copyfile(source, target) res = format_manager(input=target, output=target) os.remove(target) os.rmdir(path) assert res is answer
def test_eqaul_value_in_file(input, output, path): os.makedirs(path, exist_ok=True) shutil.copyfile(input, output) format = format_manager(input=output) check = True with open(output, 'r') as f: if 'simple: =' in f: check = False os.remove(output) os.rmdir(path) assert check assert not format
def test_format_file(user_input, source, target, path, answer): user_responses = [Mock(), Mock(), Mock()] user_responses[0] = 'y' # answer to update fromVersion choice user_responses[1] = '5.0.0' # version that should be added user_responses[2] = 'n' # answer to adding description question user_input.side_effect = user_responses os.makedirs(path, exist_ok=True) shutil.copyfile(source, target) res = format_manager(input=target, output=target, verbose=True) os.remove(target) os.rmdir(path) assert res is answer
def convert_contribution_to_pack(self): """Create a Pack in the content repo from the contents of a contribution zipfile""" try: packs_dir = os.path.join(get_content_path(), 'Packs') metadata_dict = {} with zipfile.ZipFile(self.contribution) as zipped_contrib: with zipped_contrib.open('metadata.json') as metadata_file: click.echo( f'Pulling relevant information from {metadata_file.name}', color=LOG_COLORS.NATIVE) metadata = json.loads(metadata_file.read()) # a name passed on the cmd line should take precedence over one pulled # from contribution metadata pack_name = self.name or self.format_pack_dir_name( metadata.get('name', 'ContributionPack')) # a description passed on the cmd line should take precedence over one pulled # from contribution metadata metadata_dict[ 'description'] = self.description or metadata.get( 'description') metadata_dict['name'] = pack_name metadata_dict['author'] = metadata.get('author', '') metadata_dict['support'] = metadata.get('support', '') metadata_dict['url'] = metadata.get('supportDetails', {}).get('url', '') metadata_dict['email'] = metadata.get( 'supportDetails', {}).get('email', '') metadata_dict['categories'] = metadata.get( 'categories') if metadata.get('categories') else [] metadata_dict['tags'] = metadata.get( 'tags') if metadata.get('tags') else [] metadata_dict['useCases'] = metadata.get( 'useCases') if metadata.get('useCases') else [] metadata_dict['keywords'] = metadata.get( 'keywords') if metadata.get('keywords') else [] while os.path.exists(os.path.join(packs_dir, pack_name)): click.echo( f'Modifying pack name because pack {pack_name} already exists in the content repo', color=LOG_COLORS.NATIVE) if len(pack_name) >= 2 and pack_name[-2].lower( ) == 'v' and pack_name[-1].isdigit(): # increment by one pack_name = pack_name[:-1] + str(int(pack_name[-1]) + 1) else: pack_name += 'V2' click.echo(f'New pack name is "{pack_name}"', color=LOG_COLORS.NATIVE) pack_dir = os.path.join(packs_dir, pack_name) os.mkdir(pack_dir) shutil.unpack_archive(filename=self.contribution, extract_dir=pack_dir) pack_subdirectories = get_child_directories(pack_dir) for pack_subdir in pack_subdirectories: basename = os.path.basename(pack_subdir) if basename in ENTITY_TYPE_TO_DIR: dst_name = ENTITY_TYPE_TO_DIR.get(basename) src_path = os.path.join(pack_dir, basename) dst_path = os.path.join(pack_dir, dst_name) content_item_dir = shutil.move(src_path, dst_path) if basename in {SCRIPT, AUTOMATION, INTEGRATION}: self.content_item_to_package_format(content_item_dir, del_unified=True) # create pack's base files self.full_output_path = pack_dir self.create_pack_base_files() metadata_dict = Initiator.create_metadata(fill_manually=False, data=metadata_dict) metadata_path = os.path.join(self.full_output_path, 'pack_metadata.json') with open(metadata_path, 'w') as pack_metadata_file: json.dump(metadata_dict, pack_metadata_file, indent=4) # remove metadata.json file os.remove(os.path.join(pack_dir, 'metadata.json')) click.echo( f'Executing \'format\' on the restructured contribution zip files at "{pack_dir}"' ) format_manager(input=pack_dir) except Exception as e: click.echo( f'Creating a Pack from the contribution zip failed with error: {e}\n {traceback.format_exc()}', color=LOG_COLORS.RED) finally: if self.contrib_conversion_errs: click.echo( 'The following errors occurred while converting unified content YAMLs to package structure:' ) click.echo( textwrap.indent('\n'.join(self.contrib_conversion_errs), '\t'))
def format_yml(use_git=False, file_type=None, **kwargs): return format_manager(use_git, file_type, **kwargs)
def format_yml(input=None, output=None, from_version=None, no_validate=None): return format_manager(input, output, from_version, no_validate)