-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve download_folder
functionality
#6
Open
GameTaco
wants to merge
1
commit into
leynier:main
Choose a base branch
from
GameTaco:feature/improve-download-order-and-safety
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import os | ||
from os import makedirs | ||
from os.path import exists, join | ||
from pathlib import Path | ||
|
@@ -8,7 +9,7 @@ | |
from github.ContentFile import ContentFile | ||
from github.Repository import Repository | ||
from rich import print | ||
from typer import Option, Typer | ||
from typer import Option, Typer, confirm | ||
from wget import download as wget_download | ||
|
||
app = Typer() | ||
|
@@ -26,6 +27,18 @@ def download_command( | |
token: Optional[str] = Option(None, help="GitHub token"), | ||
force: bool = Option(False, help="Remove existing output folder if it exists"), | ||
) -> None: | ||
""" | ||
Downloads a repository from a given URL and saves it to the specified output folder. | ||
|
||
Args: | ||
url (str): The URL of the repository to download. | ||
output (Path): The folder where the repository will be saved. | ||
token (Optional[str]): The GitHub token to access private repositories. | ||
force (bool): Whether to remove the existing output folder if it already exists. | ||
|
||
Returns: | ||
None | ||
""" | ||
org, repo, branch, path = parse_github_url(url) | ||
github = Github(token) | ||
repository = github.get_repo(f"{org}/{repo}") | ||
|
@@ -82,30 +95,63 @@ def download_folder( | |
force: bool, | ||
) -> None: | ||
""" | ||
Download all contents at server_path with commit tag sha in | ||
the repository. | ||
Downloads a folder from a repository. | ||
|
||
Args: | ||
repository (Repository): The repository to download from. | ||
sha (str): The SHA of the commit or tree to download. | ||
path (str): The path of the folder to download. | ||
output (Path): The output path where the folder will be saved. | ||
force (bool): If True, existing folder will be removed before downloading. | ||
|
||
Returns: | ||
None: This function does not return anything. | ||
""" | ||
fullpath = join(output, path) | ||
existing_items = set(os.listdir(fullpath)) if os.path.exists(fullpath) else set() | ||
|
||
# Get contents of the folder and assign to extra_items | ||
contents = repository.get_dir_contents(path, ref=sha) | ||
github_items = {content.name for content in contents} | ||
extra_items = existing_items - github_items | ||
|
||
if exists(fullpath): | ||
if force: | ||
if extra_items: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that if the if force:
...
elif extra_items:
...
else:
... |
||
# Display confirmation prompt | ||
if confirm( | ||
f"Folder contains extra items: {extra_items}. Do you want to continue?" | ||
): | ||
rmtree(fullpath) | ||
else: | ||
print("Operation cancelled.") | ||
return | ||
elif force: | ||
rmtree(fullpath) | ||
else: | ||
print("Output folder already exists") | ||
return | ||
|
||
makedirs(fullpath) | ||
contents = repository.get_dir_contents(path, ref=sha) | ||
for content in contents: | ||
print(f"Downloading {content.path}") | ||
fullpath = join(output, content.path) | ||
if content.type == "dir": | ||
makedirs(fullpath) | ||
download_folder(repository, sha, content.path, output, force) | ||
else: | ||
try: | ||
file_content = repository.get_contents(content.path, ref=sha) | ||
if not isinstance(file_content, ContentFile): | ||
raise ValueError("Expected ContentFile") | ||
wget_download(file_content.download_url, fullpath) | ||
print("") | ||
except (GithubException, OSError, ValueError) as exc: | ||
print("Error processing %s: %s", content.path, exc) | ||
|
||
# Separate files and directories | ||
files = [content for content in contents if content.type == "file"] | ||
directories = [content for content in contents if content.type == "dir"] | ||
|
||
# First, download all files in the current folder | ||
for file_content in files: | ||
print(f"Downloading {file_content.path}") | ||
fullpath = join(output, file_content.path) | ||
try: | ||
file_data = repository.get_contents(file_content.path, ref=sha) | ||
if not isinstance(file_data, ContentFile): | ||
raise ValueError("Expected ContentFile") | ||
wget_download(file_data.download_url, fullpath) | ||
print("") | ||
except (GithubException, OSError, ValueError) as exc: | ||
print(f"Error processing {file_content.path}: {exc}") | ||
|
||
# Then, download sub-directories | ||
for directory in directories: | ||
print(f"Downloading directory {directory.path}") | ||
download_folder(repository, sha, directory.path, output, force) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.