Skip to content

Instantly share code, notes, and snippets.

@rohitpaulk
Last active May 23, 2019 17:39
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 rohitpaulk/d6a88e2d9992100a7b22ce944efab3df to your computer and use it in GitHub Desktop.
Save rohitpaulk/d6a88e2d9992100a7b22ce944efab3df to your computer and use it in GitHub Desktop.
from shell import shell
from datetime import date, timedelta
import json
from concurrent.futures import ThreadPoolExecutor
import csv
DAYS_IN_PAST = 180
PROGRESS_EVERY = 5
def asli_shell(cmd):
return shell(f'sh -c "{cmd}"')
def dates_to_calculate():
def _iter():
for days_ago in range(1, DAYS_IN_PAST):
yield date.today() - timedelta(days=days_ago)
return list(_iter())
# {
# "alerts": [
# {"date": "29/01/1993", "count": 234},
# {"date": "30/01/1993", "count": 235}
# ],
# "rms": [
# {"date": "29/01/1993", "count": 234},
# {"date": "30/01/1993", "count": 235}
# ]
# }
def get_loc_map(services, dates):
loc_map = {}
def do(service):
loc_map[service] = []
for i, current_date in enumerate(dates):
if i % PROGRESS_EVERY == 0:
print(f"[{i}/{len(dates)}] {service}")
r = shell(
f"git -C {service} rev-list -n 1 --first-parent --before='{current_date}' master"
)
assert r.code == 0
commit = r.output(raw=True).strip()
r = shell(f"git -C {service} checkout {commit}")
assert r.code == 0
r = asli_shell(
f"cloc --include-ext=py --json --exclude-dir=tests {service}"
)
assert r.code == 0
cloc_json = r.output(raw=True)
if cloc_json.strip() == "":
line_count = 0
else:
line_count = json.loads(cloc_json)["SUM"]["code"]
loc_map[service].append({"date": date, "count": line_count})
# for service in services:
# do(service)
with ThreadPoolExecutor(max_workers=8) as executor:
futures = []
for service in services:
futures.append(executor.submit(do, service))
for future in futures:
future.result()
return loc_map
services = asli_shell("git submodule status | awk '{print $2}'").output()
services = list(reversed(services))
dates = dates_to_calculate()
loc_map = get_loc_map(services, dates)
with open("loc.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=",")
writer.writerow(["SERVICE"] + list(reversed([str(date) for date in dates])))
for service, count_data_arr in loc_map.items():
writer.writerow(
[service] + list(reversed([data["count"] for data in count_data_arr]))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment