Created
May 14, 2019 19:01
-
-
Save thales17/9178fe78a50ea727fc734822374a50b9 to your computer and use it in GitHub Desktop.
Migration script to upload a large set of repos to github
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/local/bin/python3 | |
import os | |
from github import Github | |
def get_subdirs(p_dir): | |
return [name for name in os.listdir(p_dir) | |
if os.path.isdir(os.path.join(p_dir, name))] | |
def create_github_repo(r_name): | |
token = 'YOUR_TOKEN' | |
g = Github(token) | |
o = g.get_organization('forestgiant') | |
repo = o.create_repo(name=r_name, private=True) | |
print(repo) | |
def exec(cmd): | |
print(cmd) | |
os.system(cmd) | |
def clone_and_push_repo(r_dir): | |
r_name = os.path.splitext(os.path.basename(r_dir))[0] | |
template = 'git clone --mirror %s working/%s/.git' | |
exec(template % (r_dir, r_name)) | |
p = 'working/%s/.git' % r_name | |
exec('git --git-dir %s config --bool core.bare false' % p) | |
exec('git --git-dir %s checkout' % p) | |
create_github_repo(r_name) | |
repo_path = '[email protected]:forestgiant/%s.git' % r_name | |
exec('git --git-dir %s remote add github %s' % (p, repo_path)) | |
exec('git --git-dir %s push --all github' % p) | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) < 2: | |
print('Please supply a repo parent directory') | |
else: | |
p_dir = sys.argv[1] | |
repos = get_subdirs(p_dir) | |
for repo in repos: | |
if repo in ['docs.git', 'scripts.git']: | |
print('Skipping %s, please do this manually' % repo) | |
continue | |
clone_and_push_repo(os.path.join(p_dir, repo)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment