|
import urllib.parse |
|
import requests |
|
import io |
|
import json |
|
|
|
|
|
def download_gitlab_repo_to_hfspace(api_url, project_id, version): |
|
try: |
|
|
|
encoded_project_id = urllib.parse.quote(project_id, safe="") |
|
url = f"{api_url}/projects/{encoded_project_id}/repository/archive.zip?sha={version}" |
|
|
|
|
|
response = requests.get(url, stream=True) |
|
|
|
if response.status_code == 200: |
|
_upload_file_to_hfspace(response, api, DATA_DIR, HF_SPACE_NAME) |
|
else: |
|
print(f"Failed to download the release: {response.status_code} - {response.reason}") |
|
print(response.text) |
|
|
|
except FileNotFoundError: |
|
print("The config.json file was not found. Please ensure it exists in the project directory.") |
|
except json.JSONDecodeError: |
|
print("Failed to parse the config.json file. Please ensure it contains valid JSON.") |
|
except Exception as e: |
|
print(f"An error occurred: {e}") |
|
|
|
|
|
def _upload_file_to_hfspace(response, api, DATA_DIR, HF_SPACE_NAME): |
|
archive_bytes = io.BytesIO(response.content) |
|
|
|
content_disposition = response.headers.get("content-disposition") |
|
if content_disposition and "filename=" in content_disposition: |
|
filename = content_disposition.split("filename=")[-1].strip('"') |
|
|
|
api.upload_file( |
|
path_or_fileobj=archive_bytes, |
|
path_in_repo=f"{DATA_DIR}/{filename}", |
|
repo_id=HF_SPACE_NAME, |
|
repo_type="space" |
|
) |