import glob, re, os files = glob.glob("helm/templates/scan-*.yaml") + glob.glob("helm/templates/upload-*.yaml") + ["helm/templates/enforce-policy.yaml"] for f in files: with open(f) as file: content = file.read() match = re.search(r'spec:\n templates:\n(.*)(?:{{- end }})', content, re.DOTALL) if match: template_content = match.group(1).strip() # Extract the base name e.g. scan-kics base_name = os.path.basename(f).replace('.yaml', '') new_content = f'{{{{- define "template.{base_name}" }}}}\n{template_content}\n{{{{- end }}}}\n' new_filename = os.path.join(os.path.dirname(f), f"_{base_name}.yaml") with open(new_filename, "w") as out: out.write(new_content) os.remove(f)