Skip to content

Instantly share code, notes, and snippets.

@Sigmanificient
Created September 29, 2022 04:23
Show Gist options
  • Save Sigmanificient/55a58811b77ad35339fa635225662412 to your computer and use it in GitHub Desktop.
Save Sigmanificient/55a58811b77ad35339fa635225662412 to your computer and use it in GitHub Desktop.
import os
from github import Github
BASE_DIR = '...'
GITHUB_TOKEN = '...'
CWD = os.getcwd()
def normalize_name(repo_name: str) -> str:
words = ['']
last_id = len(repo_name) - 1
for idx, char in enumerate(repo_name):
if char == '-':
words.append('')
continue
check_next = True
if idx != last_id:
next_char = repo_name[idx + 1]
check_next = (
not next_char.isdigit()
and not next_char.isupper()
and next_char != '-'
)
if (
char.isupper() and len(words[-1]) > 2
and check_next
):
words.append(char.lower())
continue
words[-1] += char.lower()
return '_'.join(words)
def main():
g = Github(GITHUB_TOKEN)
for repo in g.get_user().get_repos():
owner = repo.organization or repo.owner
directory = normalize_name(owner.login)
subdir = normalize_name(repo.name)
group_dir = f"{BASE_DIR}/{directory}"
os.makedirs(group_dir, exist_ok=True)
os.chdir(group_dir)
print(f"Cloning {owner.login}/{repo.name} => {directory}/{subdir}")
os.chdir(group_dir)
os.system(f"git clone {repo.clone_url} {subdir}")
os.chdir(CWD)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment