MCPcopy Create free account
hub / github.com/modelscope/modelscope / add_content_to_file

Function add_content_to_file

modelscope/hub/utils/utils.py:282–319  ·  view source on GitHub ↗
(repo,
                        file_name: str,
                        patterns: List[str],
                        commit_message: Optional[str] = None,
                        ignore_push_error=False)

Source from the content-addressed store, hash-verified

280
281
282def 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 = (

Callers

nothing calls this directly

Calls 4

existsMethod · 0.45
readMethod · 0.45
writeMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…