@@ -27,13 +27,16 @@ class GitCommittersPlugin(BasePlugin):
2727 ('branch' , config_options .Type (str , default = 'master' )),
2828 ('docs_path' , config_options .Type (str , default = 'docs/' )),
2929 ('enabled' , config_options .Type (bool , default = True )),
30+ ('cache_dir' , config_options .Type (str , default = '.cache/plugin/git-committers' )),
3031 )
3132
3233 def __init__ (self ):
3334 self .total_time = 0
3435 self .branch = 'master'
3536 self .enabled = True
3637 self .authors = dict ()
38+ self .cache_page_authors = dict ()
39+ self .cache_date = ''
3740
3841 def on_config (self , config ):
3942 self .enabled = self .config ['enabled' ]
@@ -61,17 +64,26 @@ def list_contributors(self, path):
6164 # Use the last commit and get the date
6265 last_commit_date = time .strftime ("%Y-%m-%d" , time .gmtime (c .authored_date ))
6366
67+ # File not committed yet
68+ if last_commit_date == "" :
69+ last_commit_date = datetime .now ().strftime ("%Y-%m-%d" )
70+ return [], last_commit_date
71+
72+ # Try to leverage the cache
73+ if self .cache_date and time .strptime (last_commit_date , "%Y-%m-%d" ) < time .strptime (self .cache_date , "%Y-%m-%d" ):
74+ return self .cache_page_authors [path ]['authors' ], self .cache_page_authors [path ]['last_commit_date' ]
75+
6476 url_contribs = self .githuburl + self .config ['repository' ] + "/contributors-list/" + self .config ['branch' ] + "/" + path
65- LOG .info ("Fetching contributors for " + path )
77+ LOG .info ("git-committers: fetching contributors for " + path )
6678 LOG .debug (" from " + url_contribs )
6779 authors = []
6880 try :
6981 response = requests .get (url_contribs )
7082 response .raise_for_status ()
7183 except HTTPError as http_err :
72- LOG .error (f'HTTP error occurred: { http_err } \n (404 is normal if file is not on GitHub yet or Git submodule)' )
84+ LOG .error (f'git-committers: HTTP error occurred: { http_err } \n (404 is normal if file is not on GitHub yet or Git submodule)' )
7385 except Exception as err :
74- LOG .error (f'Other error occurred: { err } ' )
86+ LOG .error (f'git-committers: Other error occurred: { err } ' )
7587 else :
7688 html = response .text
7789 # Parse the HTML
@@ -86,6 +98,8 @@ def list_contributors(self, path):
8698 avatar = img_tags [0 ]['src' ]
8799 avatar = re .sub (r'\?.*$' , '' , avatar )
88100 authors .append ({'login' :login , 'name' : name , 'url' : url , 'avatar' : avatar })
101+ # Update global cache_page_authors
102+ self .cache_page_authors [path ] = {'last_commit_date' : last_commit_date , 'authors' : authors }
89103
90104 return authors , last_commit_date
91105
@@ -104,3 +118,20 @@ def on_page_context(self, context, page, config, nav):
104118 self .total_time += (end - start )
105119
106120 return context
121+
122+ def on_post_build (self , config ):
123+ LOG .info ("git-committers: saving page authors cache file" )
124+ json_data = json .dumps ({'cache_date' : datetime .now ().strftime ("%Y-%m-%d" ), 'page_authors' : self .cache_page_authors })
125+ os .makedirs (self .config ['cache_dir' ], exist_ok = True )
126+ f = open (self .config ['cache_dir' ] + "/page-authors.json" , "w" )
127+ f .write (json_data )
128+ f .close ()
129+
130+ def on_pre_build (self , config ):
131+ if os .path .exists (self .config ['cache_dir' ] + "/page-authors.json" ):
132+ LOG .info ("git-committers: found page authors cache file - loading it" )
133+ f = open (self .config ['cache_dir' ] + "/page-authors.json" , "r" )
134+ cache = json .loads (f .read ())
135+ self .cache_date = cache ['cache_date' ]
136+ self .cache_page_authors = cache ['page_authors' ]
137+ f .close ()
0 commit comments