Skip to content

Commit f95ded1

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

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

nbgitpuller/pull.py

Lines changed: 50 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.fullmatch()) where
59+
it 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,25 @@ 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.fullmatch(pattern, self.git_url) for pattern in self.autorun_allow )):
182+
logging.info('autorun skipped, URL does not match any rules')
183+
return
184+
185+
script = next(( s for s in self._autorun_script if os.path.exists(os.path.join(self.repo_dir, s)) ), None)
186+
if not script:
187+
logging.info('autorun skipped, no matching script')
188+
return
189+
190+
logging.info('autorun executing: %s %s', script, operation)
191+
yield from execute_cmd([ script, operation ], cwd=self.repo_dir, shell=True)
192+
146193
def initialize_repo(self):
147194
"""
148195
Clones repository
@@ -154,6 +201,7 @@ def initialize_repo(self):
154201
clone_args.extend(['--branch', self.branch_name])
155202
clone_args.extend(["--", self.git_url, self.repo_dir])
156203
yield from execute_cmd(clone_args)
204+
self.autorun('init')
157205
logging.info('Repo {} initialized'.format(self.repo_dir))
158206

159207
def reset_deleted_files(self):
@@ -343,6 +391,7 @@ def update(self):
343391
yield from self.ensure_lock()
344392
yield from self.merge()
345393

394+
self.autorun('update')
346395

347396
def main():
348397
"""

0 commit comments

Comments
 (0)