Spaces:
Runtime error
Runtime error
Delete download_repo.py
Browse files- download_repo.py +0 -76
download_repo.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
import urllib.parse
|
| 2 |
-
import requests
|
| 3 |
-
import io
|
| 4 |
-
import json
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
import logging
|
| 8 |
-
|
| 9 |
-
# Set up logging
|
| 10 |
-
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
-
|
| 12 |
-
def download_gitlab_repo_to_hfspace(api_url, project_id, version, target_folder, hf_api, hf_space_name):
|
| 13 |
-
try:
|
| 14 |
-
# Construct the URL for the release's zip file
|
| 15 |
-
encoded_project_id = urllib.parse.quote(project_id, safe="")
|
| 16 |
-
url = f"{api_url}/projects/{encoded_project_id}/repository/archive.zip?sha={version}"
|
| 17 |
-
print("URL: ", url)
|
| 18 |
-
|
| 19 |
-
logging.info(f"Constructed URL: {url}")
|
| 20 |
-
|
| 21 |
-
# Send GET request to download the zip file
|
| 22 |
-
response = requests.get(url)
|
| 23 |
-
logging.info(f"HTTP GET Request sent to {url}, Status Code: {response.status_code}")
|
| 24 |
-
|
| 25 |
-
if response.status_code == 200:
|
| 26 |
-
content_disposition = response.headers.get('content-disposition')
|
| 27 |
-
print("Content_disposition:", content_disposition)
|
| 28 |
-
if content_disposition:
|
| 29 |
-
filename = content_disposition.split('filename=')[-1].strip('\"')
|
| 30 |
-
print("FILENAME: ", filename)
|
| 31 |
-
else:
|
| 32 |
-
filename = 'archive.zip' # Fallback to a default name if not found
|
| 33 |
-
|
| 34 |
-
logging.info(f"Filename determined: {filename}")
|
| 35 |
-
else:
|
| 36 |
-
logging.error(f"Failed to download the file. HTTP Status: {response.status_code}")
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
existing_files = hf_api.list_repo_files(repo_id=hf_space_name, repo_type='space')
|
| 40 |
-
target_path = f"{target_folder}/{filename}"
|
| 41 |
-
print("TARGET PATH: ", target_path)
|
| 42 |
-
|
| 43 |
-
logging.info(f"Target Path: '{target_path}'")
|
| 44 |
-
logging.info(f"Existing Files: {[repr(file) for file in existing_files]}")
|
| 45 |
-
|
| 46 |
-
if target_path in existing_files:
|
| 47 |
-
logging.warning(f"File '{target_path}' already exists in the repository. Skipping upload...")
|
| 48 |
-
print(f"File '{target_path}' already exists in the repository. Skipping upload...")
|
| 49 |
-
else:
|
| 50 |
-
repo_content= io.BytesIO(response.content)
|
| 51 |
-
print(repo_content)
|
| 52 |
-
_upload_file_to_hfspace(repo_content, hf_api, target_path, hf_space_name)
|
| 53 |
-
|
| 54 |
-
except FileNotFoundError:
|
| 55 |
-
logging.error("The config.json file was not found. Please ensure it exists in the project directory.")
|
| 56 |
-
print("The config.json file was not found. Please ensure it exists in the project directory.")
|
| 57 |
-
except json.JSONDecodeError:
|
| 58 |
-
logging.error("Failed to parse the config.json file. Please ensure it contains valid JSON.")
|
| 59 |
-
print("Failed to parse the config.json file. Please ensure it contains valid JSON.")
|
| 60 |
-
except Exception as e:
|
| 61 |
-
logging.error(f"An unexpected error occurred: {e}")
|
| 62 |
-
print(f"An unexpected error occurred: {e}")
|
| 63 |
-
|
| 64 |
-
def _upload_file_to_hfspace(content, hf_api, target_path, hf_space_name):
|
| 65 |
-
try:
|
| 66 |
-
logging.info(f"Preparing to upload file to '{target_path}'")
|
| 67 |
-
hf_api.upload_file(
|
| 68 |
-
path_or_fileobj=content,
|
| 69 |
-
path_in_repo=target_path,
|
| 70 |
-
repo_id=hf_space_name,
|
| 71 |
-
repo_type="space"
|
| 72 |
-
)
|
| 73 |
-
logging.info(f"File successfully uploaded to '{target_path}'")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
logging.error(f"Error during file upload: {e}")
|
| 76 |
-
print(f"Error during file upload: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|