11import os
2+ import re
23import subprocess
34import logging
45import time
@@ -69,18 +70,21 @@ def _depth_default(self):
6970 where the GitPuller class hadn't been loaded already."""
7071 return int (os .environ .get ('NBGITPULLER_DEPTH' , 1 ))
7172
72- def __init__ (self , git_url , repo_dir , ** kwargs ):
73+ def __init__ (self , git_url , repo_dir , branch , ** kwargs ):
7374 assert git_url
7475
7576 self .git_url = git_url
76- self .branch_name = kwargs .pop ("branch" )
77+ self .repo_dir = repo_dir
78+ self .branch_name = branch
7779
7880 if self .branch_name is None :
7981 self .branch_name = self .resolve_default_branch ()
8082 elif not self .branch_exists (self .branch_name ):
8183 raise ValueError (f"Branch: { self .branch_name } -- not found in repo: { self .git_url } " )
8284
83- self .repo_dir = repo_dir
85+ self .autorun_allow = kwargs .pop ('autorun_allow' , False )
86+ self .autorun_script = kwargs .pop ('autorun_script' , [])
87+
8488 newargs = {k : v for k , v in kwargs .items () if v is not None }
8589 super (GitPuller , self ).__init__ (** newargs )
8690
@@ -143,6 +147,30 @@ def pull(self):
143147 else :
144148 yield from self .update ()
145149
150+ def autorun (self , operation = "method" ):
151+ """
152+ Search for and execute the autorun script.
153+ """
154+
155+ if not self .autorun_allow :
156+ return
157+ if not any (( re .fullmatch (pattern , self .git_url ) for pattern in self .autorun_allow )):
158+ logging .info ('autorun skipped, URL does not match any rules' )
159+ return
160+
161+ script = next (( s for s in self .autorun_script if os .path .exists (os .path .join (self .repo_dir , s )) ), None )
162+ if not script :
163+ logging .info ('autorun skipped, no matching script' )
164+ return
165+
166+ try :
167+ for line in execute_cmd ([ os .path .join (self .repo_dir , script ), operation ], cwd = self .repo_dir , close_fds = True ):
168+ yield line
169+ except subprocess .CalledProcessError :
170+ m = f"Problem autorunning { script } "
171+ logging .exception (m )
172+ raise ValueError (m )
173+
146174 def initialize_repo (self ):
147175 """
148176 Clones repository
@@ -154,6 +182,7 @@ def initialize_repo(self):
154182 clone_args .extend (['--branch' , self .branch_name ])
155183 clone_args .extend (["--" , self .git_url , self .repo_dir ])
156184 yield from execute_cmd (clone_args )
185+ yield from self .autorun ('init' )
157186 logging .info ('Repo {} initialized' .format (self .repo_dir ))
158187
159188 def reset_deleted_files (self ):
@@ -343,6 +372,7 @@ def update(self):
343372 yield from self .ensure_lock ()
344373 yield from self .merge ()
345374
375+ yield from self .autorun ('update' )
346376
347377def main ():
348378 """
@@ -361,7 +391,7 @@ def main():
361391 for line in GitPuller (
362392 args .git_url ,
363393 args .repo_dir ,
364- branch = args .branch_name if args .branch_name else None
394+ args .branch_name if args .branch_name else None
365395 ).pull ():
366396 print (line )
367397
0 commit comments