|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from __future__ import with_statement |
| 5 | + |
| 6 | +import sys |
| 7 | +import os |
| 8 | +import re |
| 9 | +import json |
| 10 | +import time |
| 11 | +import urllib2 |
| 12 | +import argparse |
| 13 | +import urllib |
| 14 | +import json |
| 15 | +import logging |
| 16 | +from datetime import datetime |
| 17 | +from pprint import pprint |
| 18 | + |
| 19 | +from pygithub3 import Github |
| 20 | + |
| 21 | +## ============================================== |
| 22 | +## LOGGING |
| 23 | +## ============================================== |
| 24 | + |
| 25 | +LOG = logging.getLogger(__name__) |
| 26 | +LOG_handler = logging.StreamHandler() |
| 27 | +LOG_formatter = logging.Formatter(fmt='%(asctime)s [%(funcName)s:%(lineno)03d] %(levelname)-5s: %(message)s', |
| 28 | + datefmt='%m-%d-%Y %H:%M:%S') |
| 29 | +LOG_handler.setFormatter(LOG_formatter) |
| 30 | +LOG.addHandler(LOG_handler) |
| 31 | +LOG.setLevel(logging.INFO) |
| 32 | + |
| 33 | +## ============================================== |
| 34 | +## CONFIGURATION |
| 35 | +## ============================================== |
| 36 | + |
| 37 | +GITHUB_USER = 'cmu-db' |
| 38 | +GITHUB_REPO = 'peloton' |
| 39 | +GITHUB_REPO_API_URL = "https://api.github.com/repos/%s/%s" % (GITHUB_USER, GITHUB_REPO) |
| 40 | + |
| 41 | +FIRST_COMMIT = '35823950d500314811212282bd68c101e34b9a06' |
| 42 | + |
| 43 | +# ============================================== |
| 44 | +# main |
| 45 | +# ============================================== |
| 46 | +if __name__ == '__main__': |
| 47 | + aparser = argparse.ArgumentParser() |
| 48 | + aparser.add_argument('token', type=str, help='Github API Token') |
| 49 | + aparser.add_argument("--debug", action='store_true') |
| 50 | + args = vars(aparser.parse_args()) |
| 51 | + |
| 52 | + ## ---------------------------------------------- |
| 53 | + |
| 54 | + if args['debug']: |
| 55 | + LOG.setLevel(logging.DEBUG) |
| 56 | + |
| 57 | + ## ---------------------------------------------- |
| 58 | + |
| 59 | + gh = Github(token=args['token']) |
| 60 | + r = gh.repos.get(user=GITHUB_USER, repo=GITHUB_REPO) |
| 61 | + |
| 62 | + num_commits = 0 |
| 63 | + collaborators = set() |
| 64 | + c = gh.repos.commits.list(user=GITHUB_USER, repo=GITHUB_REPO) |
| 65 | + for x in c.iterator(): |
| 66 | + if not x.author is None: |
| 67 | + collaborators.add(x.author.login) |
| 68 | + num_commits += 1 |
| 69 | + #pprint(dir(x.author.login)) |
| 70 | + #for k in sorted(x.__dict__.keys()): |
| 71 | + #print k, "=>", x.__dict__[k] |
| 72 | + #pprint(dir(x)) |
| 73 | + # |
| 74 | + #print "="*100 |
| 75 | + |
| 76 | + # Hash of the latest commit is fetched through the GitHub API |
| 77 | + target_url = GITHUB_REPO_API_URL + "/git/refs/heads/master" |
| 78 | + contents = urllib2.urlopen(target_url).read() |
| 79 | + if len(contents) > 0: |
| 80 | + contents = json.loads(contents) |
| 81 | + #pprint(contents) |
| 82 | + target_url = GITHUB_REPO_API_URL + "/compare/" + FIRST_COMMIT + "..." + contents["object"]["sha"] |
| 83 | + contents = urllib2.urlopen(target_url).read() |
| 84 | + github_data = json.loads(contents) |
| 85 | + if "total_commits" in github_data: |
| 86 | + num_commits = int(github_data["total_commits"]) |
| 87 | + ## IF |
| 88 | + ## IF |
| 89 | + |
| 90 | + data = { |
| 91 | + "created": int(time.time()), |
| 92 | + "commits": num_commits, |
| 93 | + "commits_url": r.commits_url, |
| 94 | + "contributors": len(collaborators), |
| 95 | + "contributors_url": r.contributors_url, |
| 96 | + "forks": r.forks_count, |
| 97 | + "stars": r.stargazers_count, |
| 98 | + "stars_url": r.stargazers_url, |
| 99 | + "watchers": r.watchers_count, |
| 100 | + "issues": r.open_issues_count, |
| 101 | + "issues_url": r.issues_url, |
| 102 | + "subscribers": r.subscribers_count, |
| 103 | + "subscribers_url": r.subscribers_url, |
| 104 | + "network": r.network_count, |
| 105 | + } |
| 106 | + print json.dumps(data) |
| 107 | + |
| 108 | +## MAIN |
0 commit comments