Skip to content

Instantly share code, notes, and snippets.

@knzm
Created November 23, 2011 16:04
Show Gist options
  • Save knzm/1389062 to your computer and use it in GitHub Desktop.
Save knzm/1389062 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import csv
import urllib
from StringIO import StringIO
def count_hunks(path):
hunks = 0
add_lines = 0
del_lines = 0
f = open(path)
in_header = True
for line in f:
if in_header:
if line.startswith("@@"):
m = re.match(r'@@ -\d+,(\d+) \+\d+,(\d+) @@', line)
n1, n2 = map(int, m.group(1, 2))
in_header = False
hunks += 1
else:
if line.startswith(" "):
n1 -= 1
n2 -= 1
elif line.startswith("+"):
n2 -= 1
add_lines += 1
elif line.startswith("-"):
n1 -= 1
del_lines += 1
if n1 == n2 == 0:
in_header = True
elif n1 < 0 or n2 < 0:
raise RuntimeError
f.close()
return hunks, add_lines, del_lines
def get_base_path(path):
return {
"capi": "c-api/",
}.get(path.rstrip("/"), path)
def get_diff_path(parent_path, target):
for path in (os.path.join(parent_path, "diff"), parent_path):
for ext in (".diff", ".rst.diff"):
diff = os.path.join(path, target + ext)
if os.path.exists(diff):
return diff
return None
def count_lines(path):
f = open(path)
try:
count = 0
for line in f:
count += 1
return count
finally:
f.close()
def main():
base_url = "http://code.google.com/p/python-doc-ja/issues/csv"
colspecs = ["ID", "Status", "Milestone", "Owner", "Summary"]
url = base_url + "?can=2&colspec=%s" % ("%20".join(colspecs))
f = urllib.urlopen(url)
content = f.read()
f.close()
data = []
reader = csv.reader(StringIO(content))
# skip header
header = reader.next()
for row in reader:
if len(row) == 0:
continue
id, status, milestone, owner, summary, labels = row
m = re.match(r'^(.*/)?(.*)の翻訳', summary)
if m is None:
print "Ignored: [%s] %s" % (id, summary)
continue
path = get_base_path(m.group(1).lower())
target = m.group(2).strip()
orig_path = os.path.join(path, target)
diff = get_diff_path(path, target)
if diff is None:
print "Not found diff file: %s in %s" % (target, path)
if os.path.exists(orig_path):
lines = count_lines(orig_path)
data.append((1, lines, 0, orig_path, row))
continue
hunks, add_lines, del_lines = count_hunks(diff)
data.append((hunks, add_lines, del_lines, path + target, row))
def keyfunc(x):
return (x[1] + x[2], x[3])
# return x[3]
for hunks, add_lines, del_lines, path, row in sorted(data, key=keyfunc):
id, status, milestone, owner, summary, labels = row
if status in ("New", "Accepted") and owner == "":
extra = ""
else:
if owner == "":
owner = "someone"
extra = " [%s by %s]" % (status, owner)
print "+%d -%d (%d hunk%s) %s%s" % (
add_lines, del_lines, hunks, ["s", ""][hunks == 1], path, extra)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment