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