Skip to content

Commit

Permalink
autorun support
Browse files Browse the repository at this point in the history
disabled by default, and configurable via trailets
  • Loading branch information
jimdigriz committed Jan 3, 2025
1 parent f415428 commit a8fa9d0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions nbgitpuller/pull.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import subprocess
import logging
import time
Expand All @@ -7,6 +8,7 @@
from traitlets import Integer, default
from traitlets.config import Configurable
from functools import partial
from .config import NbGitPullerFeatures


def execute_cmd(cmd, **kwargs):
Expand Down Expand Up @@ -80,6 +82,9 @@ def __init__(self, git_url, repo_dir, **kwargs):
elif not self.branch_exists(self.branch_name):
raise ValueError(f"Branch: {self.branch_name} -- not found in repo: {self.git_url}")

self._features = NbGitPullerFeatures(parent=kwargs.get("parent"))
self._autorun = any(( re.match(pattern, git_url) for pattern in self._features.autorun_allow ))

self.repo_dir = repo_dir
newargs = {k: v for k, v in kwargs.items() if v is not None}
super(GitPuller, self).__init__(**newargs)
Expand Down Expand Up @@ -143,6 +148,20 @@ def pull(self):
else:
yield from self.update()

def autorun(self, operation):
"""
Search for and execute the autorun script.
"""
if not self._autorun:
return

script = next(( s for s in self._features.autorun_script if os.path.exists(os.path.join(self.repo_dir, s)) ), None)
if not script:
return

logging.info(f'Running "{script} {operation}')
yield from execute_cmd([ script, operation ], cwd=self.repo_dir, shell=True)

def initialize_repo(self):
"""
Clones repository
Expand All @@ -154,6 +173,7 @@ def initialize_repo(self):
clone_args.extend(['--branch', self.branch_name])
clone_args.extend(["--", self.git_url, self.repo_dir])
yield from execute_cmd(clone_args)
self._autorun('init')
logging.info('Repo {} initialized'.format(self.repo_dir))

def reset_deleted_files(self):
Expand Down Expand Up @@ -343,6 +363,7 @@ def update(self):
yield from self.ensure_lock()
yield from self.merge()

self._autorun('update')

def main():
"""
Expand Down

0 comments on commit a8fa9d0

Please sign in to comment.