Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimdigriz committed Apr 7, 2024
1 parent 016c44b commit 3db5fb2
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 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,9 +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)
if os.path.exists(os.path.join(self.repo_dir, '.nbgitpuller.script.init')):
logging.info('Running init script')
yield from execute_cmd('. ./.nbgitpuller.script.init', cwd=self.repo_dir, shell=True)
self._autorun('init')
logging.info('Repo {} initialized'.format(self.repo_dir))

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

if os.path.exists(os.path.join(self.repo_dir, '.nbgitpuller.script.update')):
logging.info('Running update script')
yield from execute_cmd('. ./.nbgitpuller.script.update', cwd=self.repo_dir, shell=True)
self._autorun('update')

def main():
"""
Expand Down

0 comments on commit 3db5fb2

Please sign in to comment.