TranslateMarkdown / github_utils.py
Hisab Cloud
Upload 5 files
08b7f89 verified
import os
import shutil
from git import Repo
def clone_repo(repo_url, clone_dir='cloned_repo'):
"""
Clones a GitHub repository to the local machine.
:param repo_url: URL of the GitHub repository
:param clone_dir: Directory where the repository will be cloned
:return: List of markdown file paths
"""
if os.path.exists(clone_dir):
shutil.rmtree(clone_dir)
Repo.clone_from(repo_url, clone_dir)
markdown_files = []
for root, _, files in os.walk(clone_dir):
for file in files:
if file.endswith('.md'):
markdown_files.append(os.path.join(root, file))
return markdown_files
def push_translated_files(repo_url, translated_files, clone_dir='cloned_repo'):
"""
Pushes translated files back to the GitHub repository.
:param repo_url: URL of the GitHub repository
:param translated_files: List of translated file data
:param clone_dir: Directory where the repository is cloned
"""
repo = Repo(clone_dir)
origin = repo.remote(name='origin')
for file in translated_files:
with open(os.path.join(clone_dir, file['filename']), 'w', encoding='utf-8') as f:
f.write(file['content'])
repo.index.add([file['filename'] for file in translated_files])
repo.index.commit('Add translated files')
origin.push()
def clean_local_repo(clone_dir='cloned_repo'):
"""
Cleans up the local cloned repository.
:param clone_dir: Directory where the repository is cloned
"""
if os.path.exists(clone_dir):
shutil.rmtree(clone_dir)