Skip to main content

A library for quickly retriving files and directories from GitHub reposities by utilizing git's sparse checkout feature.

Project description

GitHub Fast Downloader

github-fast-downloader is a python library for quickly retriving files and directories from GitHub reposities by utilizing git's sparse checkout feature.

This libaray was origanizall deisgn for a project which reqwuired donwnaling large nyumber of rpsiecec fiels and directies form a range of different repos. This was orioally done using the API but on nthe sacel at which I wanted to test and run the system, I hit the limit on the API and quarring the API and then downloading the files individually was too slow.

The solution I prefered was to be able to clone a repo locally in a temperary directory and pluck out the files and directories I needed since git clonig was well optmized and I could quicly traverse and copy out files and directoriews as needed. However, "by defualt" git will clone the entire repo and I only needed a small subset of the files and directories making it very slow for large repors I was pulling from.

However, if a user knows ahread of

Installation

pip install github-fast-downloader

or

uv add github-fast-downloader

Usage

Basic Usage of GithubFastDownloader

This example demonstrates how to use GithubFastDownloader to clone a GitHub repository, perform sparse checkouts of specific directories, and verify the existence of files and directories after checkouts.

from github_fast_downloader import GithubFastDownloader

# Initialize the GithubFastDownloader with repository information
gfd = GithubFastDownloader("vtr-verilog-to-routing", "verilog-to-routing")
  
# Clone the repository (will be empty at first)
gfd.clone_repo()

# Enable sparse checkout to selectively fetch directories
gfd.enable_sparse_checkout()

# Checkout the "fpu" directory and verify its existence
gfd.checkout_stuff(["vtr_flow/benchmarks/fpu"], reset=False)
assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "fpu").exists()

# Checkout additional directories and files
gfd.checkout_stuff([
    "vtr_flow/benchmarks/blif", 
    "vtr_flow/benchmarks/vexriscv/VexRiscvSmallest.v"
], reset=False)

# Verify the existence of the newly checked out directories and files
assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "blif").exists()
assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "fpu").exists()
assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "vexriscv" / "VexRiscvSmallest.v").exists()

# Reset sparse checkout list
gfd.reset_sparse_checkout_list()

# Checkout only the "fpu" directory again and verify
gfd.checkout_stuff(["vtr_flow/benchmarks/fpu"], reset=False)
assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "fpu").exists()

# Ensure other directories were removed from checkout
assert not (gfd.repo_dir / "vtr_flow" / "benchmarks" / "blif").exists()
assert not (gfd.repo_dir / "vtr_flow" / "benchmarks" / "vexriscv" / "VexRiscvSmallest.v").exists()

# Reset sparse checkout list and clear checkout, by default reset=True for checkout_stuff
# This means that the sparse checkout list will cleared before checking out the new list
# The previous items on the sparse checkout list will also be removed from disk when cleared from the list
gfd.reset_sparse_checkout_list()
gfd.checkout_stuff([])

# Verify only the ".git" directory exists
stuff_in_dir = list(gfd.repo_dir.iterdir())
assert len(stuff_in_dir) == 1
assert stuff_in_dir[0].name == ".git"

# Cleanup resources after usage
gfd.cleanup()

Using GithubFastDownloader as a Context Manager

This example demonstrates how to use GithubFastDownloader as a context manager. This allows the downloader to automatically handle resource cleanup when the context is exited.

from github_fast_downloader import GithubFastDownloader

# Use the context manager to handle setup and cleanup automatically
with GithubFastDownloader("vtr-verilog-to-routing", "verilog-to-routing") as gfd:
    
    # Checkout the "fpu" directory and verify its existence
    gfd.checkout_stuff(["vtr_flow/benchmarks/fpu"])
    assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "fpu").exists()

    # Checkout additional directories and files
    gfd.checkout_stuff([
        "vtr_flow/benchmarks/blif", 
        "vtr_flow/benchmarks/vexriscv/VexRiscvSmallest.v"
    ], reset=False)

    # Verify the existence of the newly checked out directories and files
    assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "blif").exists()
    assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "fpu").exists()
    assert (gfd.repo_dir / "vtr_flow" / "benchmarks" / "vexriscv" /  "VexRiscvSmallest.v").exists()

    # Reset sparse checkout list and clear checkout
    gfd.reset_sparse_checkout_list()
    gfd.checkout_stuff([])

    # Verify only the ".git" directory exists
    stuff_in_dir = list(gfd.repo_dir.iterdir())
    assert len(stuff_in_dir) == 1
    assert stuff_in_dir[0].name == ".git"

Dev and Testing

If you would like to contribute to the development of this library, you can clone the repository and install the development dependencies.

I have included two simple tests under tests/, which are just the two examples above. You can run the tests using the following command:

pytest

or

uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

github_fast_downloader-0.1.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

github_fast_downloader-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file github_fast_downloader-0.1.1.tar.gz.

File metadata

File hashes

Hashes for github_fast_downloader-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2627d0c14267564a62b8d1ce112521cb3c97bd2fd81c9e72481dfcf0f60281c4
MD5 21c2a4d6ce6cfabbc4dc50401f4ab585
BLAKE2b-256 4682fb24f1c1694ff3d9a8de2efd341ca0ff3ee5220ff53a5106c9de5f5276e8

See more details on using hashes here.

File details

Details for the file github_fast_downloader-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for github_fast_downloader-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ebb0b0e548b5f8d8dfa9a069e2e7e57a6778b1a0bba9e761884d1e718e90d936
MD5 d8002442bf05430e7845b02acf5dc235
BLAKE2b-256 5a5e77a239fb8e57ce8f94db84d4fcb105d36676436e1254241fbd72b1f4a0ec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page