Last active
October 5, 2023 15:11
-
-
Save deeplook/071f30394648838da65b5949b758e75c to your computer and use it in GitHub Desktop.
Fetch GitHub repo and run cloc on it.
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
Find the default branch of a GitHub repository online, | |
download its zipped archive, unpack it in a temporary | |
directory, and run cloc over it and show the output table. | |
$ python gh_cloc.py zslayton cron | |
22 text files. | |
classified 19 files | |
19 unique files. | |
3 files ignored. | |
github.com/AlDanial/cloc v 1.96 T=0.02 s (1043.8 files/s, 155853.3 lines/s) | |
------------------------------------------------------------------------------- | |
Language files blank comment code | |
------------------------------------------------------------------------------- | |
Rust 16 311 195 2240 | |
Markdown 1 7 0 38 | |
TOML 1 3 0 19 | |
YAML 1 5 0 19 | |
------------------------------------------------------------------------------- | |
SUM: 19 326 195 2316 | |
------------------------------------------------------------------------------- | |
""" | |
import requests | |
import tempfile | |
import zipfile | |
import os | |
import subprocess | |
import typer | |
import shutil | |
# Constants | |
REPO_URL = 'https://api.github.com/repos/{owner}/{repo}' | |
ZIP_URL = 'https://github.com/{owner}/{repo}/archive/refs/heads/{branch}.zip' | |
app = typer.Typer() | |
def download_repo(owner, repo): | |
# Fetch default branch name | |
response = requests.get(REPO_URL.format(owner=owner, repo=repo)) | |
response.raise_for_status() | |
default_branch = response.json()['default_branch'] | |
# Download the zip archive | |
response = requests.get(ZIP_URL.format(owner=owner, repo=repo, branch=default_branch), stream=True) | |
response.raise_for_status() | |
# Save to a temporary file | |
with tempfile.NamedTemporaryFile(delete=False, suffix='.zip') as tmp: | |
for chunk in response.iter_content(chunk_size=8192): | |
tmp.write(chunk) | |
tmp_file_name = tmp.name | |
return tmp_file_name, default_branch | |
def unpack_zip(zip_file): | |
# Create a temporary directory | |
tmp_dir = tempfile.mkdtemp() | |
# Unpack the zip archive | |
with zipfile.ZipFile(zip_file, 'r') as zip_ref: | |
zip_ref.extractall(tmp_dir) | |
return tmp_dir | |
def run_cloc_on_directory(directory): | |
# Run cloc and print the output | |
result = subprocess.run(['cloc', directory], capture_output=True, text=True) | |
print(result.stdout) | |
@app.command() | |
def main(owner: str, repo: str): | |
""" | |
Download and run cloc on a given GitHub repository. | |
Args: | |
- owner: GitHub username. | |
- repo: GitHub repository name. | |
""" | |
zip_file, _ = download_repo(owner, repo) | |
directory = unpack_zip(zip_file) | |
run_cloc_on_directory(directory) | |
# Clean up | |
os.remove(zip_file) | |
shutil.rmtree(directory) | |
if __name__ == '__main__': | |
app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment