#!/usr/bin/python3 """ Entrouvert test """ import datetime import shutil import tempfile import warnings from collections.abc import Sequence from typing import Generator import git from git.repo.base import Repo from git.objects.commit import Commit def in_office_hours(moment:datetime.datetime=datetime.datetime.now(), starthour:datetime.time=datetime.time(8,0,0), stophour:datetime.time=datetime.time(20,0,0), weekend:Sequence[int]=(5,6))->bool: """ Indicates if a moment is in office hours. Office hours are localized, comparisons are done without taking care of tzoffset : in fact, if a working day starts at 08:00, 07:59:59+0200 is off as 07:59:59+0000 is. Arguments : - moment : the moment to compare with office hours - starthour : standard day start hour - stophour : standard day stop hour - weekend : list of days off (0,1,....,6) """ for dow in weekend: if dow < 0 or dow > 6: raise ValueError("Weekend days are integer in [0..6]") if starthour.tzinfo is not None or stophour.tzinfo is not None: warnings.warn("Start & stop hours should not indicate any \ tzinfo : comparisons are done without taking tzoffset in considaration") if moment.weekday() in weekend: return False localtime = moment.time() return starthour <= localtime <= stophour def iter_commits(repo:Repo)->Generator[Commit, None, None]: """ Generator on all git commits in given repository. Recursively iterate on each commit in each branch/ref not yielding duplicates commits (based on commit hashes) Arguments : - repo : The repository instance to fetch commits from """ encountered = set() for ref in repo.refs: for commit in repo.iter_commits(ref.name): if commit.binsha not in encountered: encountered.add(commit.binsha) yield commit class TempRemoteRepo(Repo): """ A temporary repository referencing a remote repository Allows to iterate on all commits without cloning the remote """ def __init__(self, remote_url:str): """ Initialize a new empty repository referencing a remote repo Arguments : - remote_url : The url of the remote repo to reference """ self.temppath = tempfile.mkdtemp(prefix="git_oh_") git.Repo.init(self.temppath) super().__init__(self.temppath) self.create_remote("origin", remote_url).fetch() def __del__(self): shutil.rmtree(self.temppath) super().__del__() def __iter__(self)->Generator[Commit, None, None]: return iter_commits(self)