Skip to content

Commit bc09520

Browse files
committed
autorun support
disabled by default, and configurable via trailets
1 parent abed309 commit bc09520

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

nbgitpuller/pull.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import subprocess
34
import logging
45
import time
@@ -7,6 +8,7 @@
78
from traitlets import Integer, default
89
from traitlets.config import Configurable
910
from functools import partial
11+
from .config import NbGitPullerFeatures
1012

1113

1214
def 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 ))
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:
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

347368
def main():
348369
"""

0 commit comments

Comments
 (0)