Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save ksomemo/9887346 to your computer and use it in GitHub Desktop.

Select an option

Save ksomemo/9887346 to your computer and use it in GitHub Desktop.
guthub apiを使ってフォークしていないリポジトリをCloneし、アカウントと違うコミッターが含まれるリポジトリを残す。
# coding: utf-8
import sys
import requests
import subprocess
def main():
"""
guthub apiを使ってフォークしていないリポジトリをCloneし、
アカウントと違うコミッターが含まれるリポジトリを残す。
http://developer.github.com/v3/
requests from pip install requests
"""
if len(sys.argv) <= 2:
raise Exception('引数はファイル名を含めて3つ以上必要です.' + str(len(sys.argv)))
user = sys.argv[1]
passwd = sys.argv[2]
auth_token = sys.argv[3]
committer = sys.argv[4]
# requests.models.Response
# res = requests.get('https://api.github.com', auth=(user, passwd))
headers={'Authorization': 'token %s' % auth_token}
res = requests.get('https://api.github.com', headers=headers)
print 'status code is %d' % res.status_code
res = requests.get('https://api.github.com/users/%s/repos' % user)
# res2json's type is dict
for rep in res.json():
if rep['fork'] == True:
continue
args = ['git', 'clone', 'https://github.com/%s.git' % rep['full_name']]
p = subprocess.Popen(args)
p.wait()
print 'clone end'
try:
p_log = subprocess.Popen(['git', '--git-dir=%s/.git' % rep['name'], 'log', '--pretty=full'], stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', committer), stdin=p_log.stdout)
p_log.wait()
except subprocess.CalledProcessError:
# no output
# shutil.rmtree(rep['name']) # WindowsError
p_rm = subprocess.Popen(['rm', '-rf', rep['name']])
p_rm.wait()
else:
print output
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment