Spaces:
Runtime error
Runtime error
File size: 1,643 Bytes
08b7f89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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)
|