Skip to content

Instantly share code, notes, and snippets.

@ksator
Last active October 2, 2018 08:08
Show Gist options
  • Save ksator/61dddb01fa14b2340d3990d578ed1353 to your computer and use it in GitHub Desktop.
Save ksator/61dddb01fa14b2340d3990d578ed1353 to your computer and use it in GitHub Desktop.
update all your repos with an MIT license
"""
Description:
I am using this script to add an MIT license to the Github repositories (master branch) of my personal github account.
It first get the lists all repositories from a personal github account, and then add an MIT license to each of them.
To exclude some repositories, I am using the variable ```repo_to_skip```
To use an explicit list of repositories, I am using the variable ```repo_list```
MIT license details used:
Year: '2018'
copyright holders: 'Juniper Networks, Inc. All rights reserved'
requirements:
pip install pygithub3
usage:
python ./github.py
"""
MIT_license = """
MIT License
Copyright (c) 2018 Juniper Networks, Inc. All rights reserved
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import git
import os
def add_license(myrepo):
print "#######################################"
if myrepo.name in repo_to_skip:
print "skipping the MIT license for the repository " + myrepo.name
return "skipping the MIT license for the repository " + myrepo.name
else:
cwd = os.getcwd()
print "about to add MIT license to " + myrepo.name
repo_dir = cwd + '/' + myrepo.name
if os.path.isdir(repo_dir):
print "you already have a local copy of the repository " + myrepo.name
repo = git.Repo(repo_dir)
repo.git.pull('origin', 'master')
else:
print "cloning the repo " + myrepo.name
repo = git.Repo.clone_from(item.clone_url, repo_dir)
f=open(repo_dir + '/license','w')
f.write(MIT_license)
f.close()
repo.git.add('license')
# config = repo.config_reader()
# username = config.get_value("user", "name")
# print username
try:
repo.git.commit(m = "add MIT license using Gitpython")
except git.exc.GitCommandError:
print ('MIT license was already there, nothing to commit on ' + myrepo.name)
else:
repo.git.push('origin', 'master')
print "done. MIT license added to " + myrepo.name
return "done. MIT license added to " + myrepo.name
# repo_list = []
# for item in repo_list:
# add_license(item)
repo_to_skip = ['Imagine-DevOps-song', 'test']
import pygithub3
# login =raw_input("github login :")
# password=raw_input("github password:")
import getpass
password=getpass.getpass("Enter your Github password: ")
gh = pygithub3.Github(login='ksator', password=password)
all_my_repos = gh.repos.list(user='ksator', type='owner').all()
for item in all_my_repos:
# print item.name
# print item.clone_url
# print item.ssh_url
add_license(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment