| 280 | |
| 281 | |
| 282 | def add_content_to_file(repo, |
| 283 | file_name: str, |
| 284 | patterns: List[str], |
| 285 | commit_message: Optional[str] = None, |
| 286 | ignore_push_error=False) -> None: |
| 287 | if isinstance(patterns, str): |
| 288 | patterns = [patterns] |
| 289 | if commit_message is None: |
| 290 | commit_message = f'Add `{patterns[0]}` patterns to {file_name}' |
| 291 | |
| 292 | # Get current file content |
| 293 | repo_dir = repo.model_dir |
| 294 | file_path = os.path.join(repo_dir, file_name) |
| 295 | if os.path.exists(file_path): |
| 296 | with open(file_path, 'r', encoding='utf-8') as f: |
| 297 | current_content = f.read() |
| 298 | else: |
| 299 | current_content = '' |
| 300 | # Add the patterns to file |
| 301 | content = current_content |
| 302 | for pattern in patterns: |
| 303 | if pattern not in content: |
| 304 | if len(content) > 0 and not content.endswith('\n'): |
| 305 | content += '\n' |
| 306 | content += f'{pattern}\n' |
| 307 | |
| 308 | # Write the file if it has changed |
| 309 | if content != current_content: |
| 310 | with open(file_path, 'w', encoding='utf-8') as f: |
| 311 | logger.debug(f'Writing {file_name} file. Content: {content}') |
| 312 | f.write(content) |
| 313 | try: |
| 314 | repo.push(commit_message) |
| 315 | except Exception as e: |
| 316 | if ignore_push_error: |
| 317 | pass |
| 318 | else: |
| 319 | raise e |
| 320 | |
| 321 | |
| 322 | _TIMESINCE_CHUNKS = ( |