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 17, 2025
1 parent f415428 commit 201d089
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion nbgitpuller/pull.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import re
import subprocess
import logging
import time
import argparse
import datetime
from traitlets import Integer, default
from traitlets import Bool, CRegExp, Integer, List, Unicode, Union, default
from traitlets.config import Configurable
from functools import partial

Expand Down Expand Up @@ -49,6 +50,33 @@ def flush():


class GitPuller(Configurable):
autorun_allow = Union(
[List(CRegExp()), Bool()],
default_value=False,
config=True,
help="""
List of URLs described as Python regular expressions (using re.match()) where it
is permitted to autorun scripts from the pulled project as a pre-initialisation
step. Enable this only if you understand and accept the risks of AUTORUN.INF.
When set to boolean True, all URLs are allowed, whilst False (default) autorun
is disabled completely.
"""
)

autorun_script = List(
Unicode(),
default_value=[],
config=True,
help="""
List of scripts to search for when attempting to autorun. The first match will
be run with a single argument of 'init' or 'update' depending on what nbgitpuller
is doing.
Enable this only if you understand and accept the risks of AUTORUN.INF.
"""
)

depth = Integer(
config=True,
help="""
Expand Down Expand Up @@ -143,6 +171,23 @@ def pull(self):
else:
yield from self.update()

def autorun(self, operation="method"):
"""
Search for and execute the autorun script.
"""

if not self.autorun_allow:
return
if not any(( re.match(pattern, self.git_url) for pattern in self.autorun_allow )):
return

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

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

def initialize_repo(self):
"""
Clones repository
Expand All @@ -154,6 +199,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 +389,7 @@ def update(self):
yield from self.ensure_lock()
yield from self.merge()

self.autorun('update')

def main():
"""
Expand Down

0 comments on commit 201d089

Please sign in to comment.