Skip to content

Instantly share code, notes, and snippets.

@nyov
Created July 13, 2015 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyov/5d085ec49d4497bb97ba to your computer and use it in GitHub Desktop.
Save nyov/5d085ec49d4497bb97ba to your computer and use it in GitHub Desktop.
GitHub repo stats markdown printer, used for a project relicensing callout
#!/usr/bin/python3
# by nyov, Public Domain
from __future__ import unicode_literals, print_function
import six
from six.moves import cPickle as pickle
# https://github.com/sigmavirus24/github3.py
# develop branch (1.0+)
from github3 import GitHub
TOKEN = '' # <github-api-token>
PROJECT = ('wardi','urwid') # (user, repo)
TMPFILE = 'urwid-commits.dump'
def write():
if TOKEN:
gh = GitHub(token=TOKEN)
else:
gh = GitHub() # try anon
print('Ratelimit: %s' % gh.ratelimit_remaining)
repo = gh.repository(*PROJECT)
commits = list(repo.commits())
with open(TMPFILE, 'wb') as f:
pickle.dump(commits, f)
print('Ratelimit: %s' % gh.ratelimit_remaining)
def read():
github_commits = []
unknown_commits = []
invalid_commits = []
with open(TMPFILE, 'rb') as f:
c = pickle.load(f)
commits = {}
commits_n = {}
for commit in c:
if str(commit.author) in ('None',):
unknown_commits.append(commit)
elif str(commit.author) in ('invalid-email-address',):
invalid_commits.append(commit)
else:
commits[str(commit.author)] = commit
try:
commits_n[str(commit.author)] += 1
except KeyError:
commits_n[str(commit.author)] = 1
github_commits.append(commit)
print(' * GitHub commit authors: ')
for _, commit in commits.items():
#print('https://github.com/%s' % commit.author)
print(
'- [ ] @{author} ([{commits} commits](https://github.com/{user}/{repo}/commits?author={author})) '.format(
user = PROJECT[0],
repo = PROJECT[1],
author = str(commit.author),
commits = commits_n[str(commit.author)],
)
)
print()
print(' * Git commit authors (email contact only): ')
unknown = []
unknown_c = {}
for commit in unknown_commits:
author, email = author_for_sha(str(commit.sha))
unknown += [(author, email)]
try:
unknown_c[author] += [str(commit.sha)]
except KeyError:
unknown_c[author] = [str(commit.sha)]
#print(str(commit.sha), author, email)
unknown = set(unknown)
for author, email in unknown:
#print(author, '<%s>' % email, ', '.join(['@%s' % c for c in unknown_c[author]]))
print(
'- [ ] {author} (commits: {commits} ) '.format(
author = author,
commits = ', '.join(['%s' % c for c in unknown_c[author]]),
)
)
print()
print(' * Other commit authors (invalid email): ')
invalid = []
invalid_c = {}
for commit in invalid_commits:
author, email = author_for_sha(str(commit.sha))
invalid += [(author, email)]
try:
invalid_c[author] += [str(commit.sha)]
except KeyError:
invalid_c[author] = [str(commit.sha)]
#print(str(commit.sha), author, email)
invalid = set(invalid)
for author, email in invalid:
print(
'- [ ] {author} (commits: {commits} ) '.format(
author = author,
commits = ', '.join(['%s' % c for c in invalid_c[author]]),
)
)
print()
## git cli, no API here
# needs to run in the actual cloned repo directory
# (could be done using the GH API, if you have commit access to the repo. I didn't)
import os
import subprocess
GIT = '/usr/bin/git'
def git(filestream, *params):
cli = [GIT]
for param in params:
cli.extend([param])
#cli.extend(['-', '-'])
os.environ['PYTHONIOENCODING'] = 'utf-8'
proc = subprocess.Popen(
cli,
shell=False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output = proc.communicate(filestream)[0]
exit_code = proc.wait()
return exit_code, output
def author_for_sha(sha):
_, g = git(b'', 'log', '-1', str(sha))
for line in g.decode('utf-8').split('\n'):
if 'Author:' in line:
author, email = line.strip('Author:').strip(' ').strip('>').split('<')
return author.strip(), email
return None, None
if __name__=="__main__":
#write() # download once
read() # test it often
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment