11import os
2+ import re
23import subprocess
34import logging
45import time
78from traitlets import Integer , default
89from traitlets .config import Configurable
910from functools import partial
11+ from .config import NbGitPullerFeatures
1012
1113
1214def execute_cmd (cmd , ** kwargs ):
@@ -80,6 +82,9 @@ def __init__(self, git_url, repo_dir, **kwargs):
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
85+ self ._features = NbGitPullerFeatures (parent = kwargs .get ("parent" ))
86+ self ._autorun = any (( re .match (pattern , git_url ) for pattern in (self ._features .autorun_allow or []) ))
87+
8388 self .repo_dir = repo_dir
8489 newargs = {k : v for k , v in kwargs .items () if v is not None }
8590 super (GitPuller , self ).__init__ (** newargs )
@@ -143,6 +148,20 @@ def pull(self):
143148 else :
144149 yield from self .update ()
145150
151+ def autorun (self , operation ):
152+ """
153+ Search for and execute the autorun script.
154+ """
155+ if not (self ._autorun and self ._features .autorun_script ):
156+ return
157+
158+ script = next (( s for s in self ._features .autorun_script if os .path .exists (os .path .join (self .repo_dir , s )) ), None )
159+ if not script :
160+ return
161+
162+ logging .info (f'Running "{ script } { operation } ' )
163+ yield from execute_cmd ([ script , operation ], cwd = self .repo_dir , shell = True )
164+
146165 def initialize_repo (self ):
147166 """
148167 Clones repository
@@ -154,6 +173,7 @@ def initialize_repo(self):
154173 clone_args .extend (['--branch' , self .branch_name ])
155174 clone_args .extend (["--" , self .git_url , self .repo_dir ])
156175 yield from execute_cmd (clone_args )
176+ self ._autorun ('init' )
157177 logging .info ('Repo {} initialized' .format (self .repo_dir ))
158178
159179 def reset_deleted_files (self ):
@@ -343,6 +363,7 @@ def update(self):
343363 yield from self .ensure_lock ()
344364 yield from self .merge ()
345365
366+ self ._autorun ('update' )
346367
347368def main ():
348369 """
0 commit comments