1
1
import os
2
+ import re
2
3
import subprocess
3
4
import logging
4
5
import time
7
8
from traitlets import Integer , default
8
9
from traitlets .config import Configurable
9
10
from functools import partial
11
+ from .config import NbGitPullerFeatures
10
12
11
13
12
14
def execute_cmd (cmd , ** kwargs ):
@@ -80,6 +82,9 @@ def __init__(self, git_url, repo_dir, **kwargs):
80
82
elif not self .branch_exists (self .branch_name ):
81
83
raise ValueError (f"Branch: { self .branch_name } -- not found in repo: { self .git_url } " )
82
84
85
+ self ._features = NbGitPullerFeatures (parent = kwargs .get ("parent" ))
86
+ self ._autorun = any (( re .match (pattern , git_url ) for pattern in self ._features .autorun_allow ))
87
+
83
88
self .repo_dir = repo_dir
84
89
newargs = {k : v for k , v in kwargs .items () if v is not None }
85
90
super (GitPuller , self ).__init__ (** newargs )
@@ -143,6 +148,20 @@ def pull(self):
143
148
else :
144
149
yield from self .update ()
145
150
151
+ def autorun (self , operation ):
152
+ """
153
+ Search for and execute the autorun script.
154
+ """
155
+ if not self ._autorun :
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
+
146
165
def initialize_repo (self ):
147
166
"""
148
167
Clones repository
@@ -154,6 +173,7 @@ def initialize_repo(self):
154
173
clone_args .extend (['--branch' , self .branch_name ])
155
174
clone_args .extend (["--" , self .git_url , self .repo_dir ])
156
175
yield from execute_cmd (clone_args )
176
+ self ._autorun ('init' )
157
177
logging .info ('Repo {} initialized' .format (self .repo_dir ))
158
178
159
179
def reset_deleted_files (self ):
@@ -343,6 +363,7 @@ def update(self):
343
363
yield from self .ensure_lock ()
344
364
yield from self .merge ()
345
365
366
+ self ._autorun ('update' )
346
367
347
368
def main ():
348
369
"""
0 commit comments