1
1
import os
2
+ import re
2
3
import subprocess
3
4
import logging
4
5
import time
5
6
import argparse
6
7
import datetime
7
- from traitlets import Integer , default
8
+ from traitlets import Bool , CRegExp , Integer , List , Unicode , Union , default
8
9
from traitlets .config import Configurable
9
10
from functools import partial
10
11
@@ -49,6 +50,33 @@ def flush():
49
50
50
51
51
52
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
+
52
80
depth = Integer (
53
81
config = True ,
54
82
help = """
@@ -143,6 +171,23 @@ def pull(self):
143
171
else :
144
172
yield from self .update ()
145
173
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
+
146
191
def initialize_repo (self ):
147
192
"""
148
193
Clones repository
@@ -154,6 +199,7 @@ def initialize_repo(self):
154
199
clone_args .extend (['--branch' , self .branch_name ])
155
200
clone_args .extend (["--" , self .git_url , self .repo_dir ])
156
201
yield from execute_cmd (clone_args )
202
+ self .autorun ('init' )
157
203
logging .info ('Repo {} initialized' .format (self .repo_dir ))
158
204
159
205
def reset_deleted_files (self ):
@@ -343,6 +389,7 @@ def update(self):
343
389
yield from self .ensure_lock ()
344
390
yield from self .merge ()
345
391
392
+ self .autorun ('update' )
346
393
347
394
def main ():
348
395
"""
0 commit comments