Skip to content

Commit 201d089

Browse files
committed
autorun support
disabled by default, and configurable via trailets
1 parent f415428 commit 201d089

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

nbgitpuller/pull.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
2+
import re
23
import subprocess
34
import logging
45
import time
56
import argparse
67
import datetime
7-
from traitlets import Integer, default
8+
from traitlets import Bool, CRegExp, Integer, List, Unicode, Union, default
89
from traitlets.config import Configurable
910
from functools import partial
1011

@@ -49,6 +50,33 @@ def flush():
4950

5051

5152
class GitPuller(Configurable):
53+
autorun_allow = Union(
54+
[List(CRegExp()), Bool()],
55+
default_value=False,
56+
config=True,
57+
help="""
58+
List of URLs described as Python regular expressions (using re.match()) where it
59+
is permitted to autorun scripts from the pulled project as a pre-initialisation
60+
step. Enable this only if you understand and accept the risks of AUTORUN.INF.
61+
62+
When set to boolean True, all URLs are allowed, whilst False (default) autorun
63+
is disabled completely.
64+
"""
65+
)
66+
67+
autorun_script = List(
68+
Unicode(),
69+
default_value=[],
70+
config=True,
71+
help="""
72+
List of scripts to search for when attempting to autorun. The first match will
73+
be run with a single argument of 'init' or 'update' depending on what nbgitpuller
74+
is doing.
75+
76+
Enable this only if you understand and accept the risks of AUTORUN.INF.
77+
"""
78+
)
79+
5280
depth = Integer(
5381
config=True,
5482
help="""
@@ -143,6 +171,23 @@ def pull(self):
143171
else:
144172
yield from self.update()
145173

174+
def autorun(self, operation="method"):
175+
"""
176+
Search for and execute the autorun script.
177+
"""
178+
179+
if not self.autorun_allow:
180+
return
181+
if not any(( re.match(pattern, self.git_url) for pattern in self.autorun_allow )):
182+
return
183+
184+
script = next(( s for s in self._autorun_script if os.path.exists(os.path.join(self.repo_dir, s)) ), None)
185+
if not script:
186+
return
187+
188+
logging.info('Running "%s" %s', script, operation)
189+
yield from execute_cmd([ script, operation ], cwd=self.repo_dir, shell=True)
190+
146191
def initialize_repo(self):
147192
"""
148193
Clones repository
@@ -154,6 +199,7 @@ def initialize_repo(self):
154199
clone_args.extend(['--branch', self.branch_name])
155200
clone_args.extend(["--", self.git_url, self.repo_dir])
156201
yield from execute_cmd(clone_args)
202+
self.autorun('init')
157203
logging.info('Repo {} initialized'.format(self.repo_dir))
158204

159205
def reset_deleted_files(self):
@@ -343,6 +389,7 @@ def update(self):
343389
yield from self.ensure_lock()
344390
yield from self.merge()
345391

392+
self.autorun('update')
346393

347394
def main():
348395
"""

0 commit comments

Comments
 (0)