-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_metrics.py
executable file
·119 lines (96 loc) · 2.96 KB
/
gen_metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module to re(generate) metrics.json database.
"""
from __future__ import absolute_import
import argparse
from argparse import RawDescriptionHelpFormatter as RawDescriptionHelp
import json
import os
import shlex
import shutil
import subprocess as sub
import tempfile
ROOT = os.path.abspath(os.path.dirname(__file__))
if os.path.dirname(__file__) == '':
ROOT = os.getcwd()
def create_args_parser():
"""
Create argument parser.
"""
prog_name = os.path.basename(__file__)[0:-3]
mesg = """
Generate the metrics.json database that
contains metrics scraped from plugins.
Do NOT edit the generated file. Just regenerate it.
""".format(prog_name.capitalize(), prog_name)
mesg = mesg[0:-5]
parser = argparse.ArgumentParser(prog=prog_name, description=mesg,
formatter_class=RawDescriptionHelp)
parser.add_argument('input', default=os.path.join(ROOT, 'db.json'),
help='input json file to transform')
parser.add_argument('output', nargs='?',
help='output json, by default metrics.json')
return parser
def github_uri(plug_name):
"""
Just make the standard public https uri.
"""
return 'https://github.com/' + plug_name
def run_cmd(cmd):
"""
Just run commands and return output. It is 1 long string.
"""
return sub.check_output(shlex.split(cmd))
def git_dates(plug_name):
"""
Get first and last author commit dates.
Returns:
(first_commit_date, latest_commit_date)
"""
try:
orig_dir = os.getcwd()
tempd = tempfile.mkdtemp()
run_cmd('git clone {} {}'.format(github_uri(plug_name), tempd))
os.chdir(tempd)
first_rev = run_cmd('git rev-list --max-parents=0 HEAD')
start = run_cmd('git log --pretty=format:"%ad" ' + first_rev)
end = run_cmd('git log --pretty=format:"%ad" -n 1')
return (start, end)
finally:
os.chdir(orig_dir)
shutil.rmtree(tempd)
def add_metrics(metrics, plug_name):
"""
Add useful metrics to the db.
Includes:
- # of stars/watches on github.
- Last commit date.
- First commit date.
"""
start, end = git_dates(plug_name)
# TODO: Fetch github stars.
# TODO: Fetch github dotfiles inclusions.
metrics[plug_name] = {
'start_date': start,
'end_date': end,
}
def main():
"""
Main entry point.
"""
parser = create_args_parser()
args = parser.parse_args()
if args.output is None:
args.output = os.path.join(os.path.dirname(args.input), 'metrics.json')
with open(args.input, 'r') as fin:
plugs = json.load(fin)
metrics = {}
for plug in plugs:
add_metrics(metrics, plug)
with open(args.output, 'w') as fout:
json.dump(metrics, fout, sort_keys=True,
indent=2, separators=(',', ': '))
if __name__ == "__main__":
main()