Created
March 24, 2022 08:20
-
-
Save vladimir-kotikov/ffee6e848db3ce4d5bc6a8bac198e4f7 to your computer and use it in GitHub Desktop.
A simple python script to clone (and optionally archive) all GitHub repos of the single account
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
#!/usr/bin/env python3 | |
from concurrent.futures import ThreadPoolExecutor | |
from subprocess import call | |
from argparse import ArgumentParser | |
# pip install PyGithub | |
from github import Github | |
from github.Repository import Repository | |
def main(): | |
arg_parser = ArgumentParser() | |
arg_parser.add_argument( | |
"--token", | |
help="GitHub access token. Get one at https://github.com/settings/tokens/new", | |
) | |
arg_parser.add_argument( | |
"--compress", | |
action="store_true", | |
help="Download repo archives rather than clone", | |
) | |
args = arg_parser.parse_args() | |
def clone_repo(repo: Repository) -> None: | |
print(f"Cloning {repo.name}...") | |
error = call(["git", "clone", "-q", repo.ssh_url]) | |
if not error and args.compress: | |
print(f"Compressing {repo.name}...") | |
error = call(["tar", "czf", f"{repo.name}.tgz", repo.name]) | |
if not error: | |
call(["rm", "-rf", repo.name]) | |
g = Github(args.token) | |
executor = ThreadPoolExecutor(max_workers=8) | |
executor.map(clone_repo, g.get_user().get_repos()) | |
try: | |
executor.shutdown(wait=True, cancel_futures=False) | |
except KeyboardInterrupt: | |
print("Shutting down...") | |
executor.shutdown(wait=True, cancel_futures=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment