129 lines
3.8 KiB
Python
129 lines
3.8 KiB
Python
import argparse
|
|
import sys
|
|
import time
|
|
import dataclasses
|
|
import git
|
|
import datetime
|
|
from collections.abc import Iterator, Sequence
|
|
from pathlib import Path
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def parse_arguments():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('repositories', action='extend', nargs='+', type=Path)
|
|
return parser.parse_args()
|
|
|
|
@dataclasses.dataclass(frozen=True, order=True)
|
|
class WorkSample:
|
|
registered_at: datetime.datetime
|
|
labels: Sequence[str]
|
|
|
|
|
|
def determine_default(repo: git.Repo):
|
|
try:
|
|
repo.commit('main')
|
|
return 'main'
|
|
except:
|
|
return 'master'
|
|
|
|
HIDDEN_LABEL_PREFIX = '__'
|
|
HIDDEN_LABEL_TOTAL = HIDDEN_LABEL_PREFIX + 'TOTAL'
|
|
|
|
def determine_project_name(repo: git.Repo) -> str:
|
|
remotes = repo.remotes
|
|
if len(remotes) > 0:
|
|
return remotes.origin.url.removeprefix('git@gitfub.space:')
|
|
return Path(repo.working_tree_dir).name
|
|
|
|
def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]:
|
|
project_name = determine_project_name(repo)
|
|
assert project_name is not None
|
|
|
|
# TODO: Branch on main or master or default
|
|
|
|
repo.commit()
|
|
|
|
for commit in repo.iter_commits(determine_default(repo)):
|
|
labels = [HIDDEN_LABEL_TOTAL]
|
|
labels.append('project:' + project_name)
|
|
labels.append('author:' + commit.author.email)
|
|
yield WorkSample(datetime.datetime.fromtimestamp(commit.authored_date, tz=datetime.UTC), tuple(labels))
|
|
yield WorkSample(datetime.datetime.fromtimestamp(commit.committed_date, tz=datetime.UTC), tuple(labels))
|
|
del labels
|
|
|
|
DEFAULT_EST_TIME=datetime.timedelta(hours=1)
|
|
|
|
ZERO_DURATION = datetime.timedelta(seconds = 0)
|
|
HOUR = datetime.timedelta(hours = 1)
|
|
|
|
def generate_report(samples: list[WorkSample]) -> Iterator[str]:
|
|
SAMPLE_FILTER = {}
|
|
LABEL_FILTER = {}
|
|
|
|
# Time spent per label
|
|
time_per_label: dict[str, datetime.timedelta] = {}
|
|
prev_time = datetime.datetime.fromtimestamp(0, datetime.UTC)
|
|
for sample in samples:
|
|
est_time: datetime.timedelta = DEFAULT_EST_TIME
|
|
est_time = min(sample.registered_at - prev_time, est_time)
|
|
|
|
if len(SAMPLE_FILTER) == 0:
|
|
pass
|
|
elif not set(sample.labels).intersection(SAMPLE_FILTER):
|
|
continue
|
|
|
|
for label in sample.labels:
|
|
time_per_label.setdefault(label,ZERO_DURATION)
|
|
time_per_label[label] += est_time
|
|
|
|
prev_time = sample.registered_at
|
|
del sample, est_time
|
|
|
|
time_and_label = [(duration, label) for label,duration in time_per_label.items()]
|
|
time_and_label.sort(reverse=True)
|
|
|
|
#
|
|
yield '-' * 66
|
|
yield '\n'
|
|
for (total_time, label_and_type) in time_and_label:
|
|
if label_and_type.startswith(HIDDEN_LABEL_PREFIX):
|
|
continue
|
|
|
|
label_type, label = label_and_type.split(':', 1)
|
|
|
|
if len(LABEL_FILTER) > 0 and label_type not in LABEL_FILTER:
|
|
continue
|
|
|
|
label_type = '' # TODO
|
|
|
|
yield f' {label_type:8} {label:40} {total_time / HOUR:-4.2f} hours\n'
|
|
del label, total_time
|
|
|
|
yield '-' * 66
|
|
yield '\n'
|
|
|
|
yield ' {label_type:8} {label:40} {hours:-4.0f} hours\n'.format(label_type='', label='TOTAL', hours = time_per_label.get(HIDDEN_LABEL_TOTAL, ZERO_DURATION) / HOUR)
|
|
|
|
def main():
|
|
logging.basicConfig()
|
|
|
|
args = parse_arguments()
|
|
|
|
shared_time_stamps: set[WorkSample] = set()
|
|
for repo_path in args.repositories:
|
|
try:
|
|
repo = git.Repo(repo_path)
|
|
except git.exc.InvalidGitRepositoryError:
|
|
logger.warning('Ignoring non-repo %s', repo_path)
|
|
continue
|
|
logger.warning('Visit %s', repo_path)
|
|
shared_time_stamps |= set(get_samples_from_project(repo))
|
|
|
|
shared_time_stamps = sorted(shared_time_stamps)
|
|
|
|
for t in generate_report(shared_time_stamps):
|
|
sys.stdout.write(t)
|