1
1
from tornado import gen , web , locks
2
2
import traceback
3
3
import urllib .parse
4
-
5
4
from notebook .base .handlers import IPythonHandler
6
5
import threading
7
6
import json
11
10
12
11
from .pull import GitPuller
13
12
from .version import __version__
14
- from .hookspecs import handle_files
15
- from .plugins .zip_puller import ZipSourceGoogleDriveDownloader
16
- from .plugins .zip_puller import ZipSourceDropBoxDownloader
17
- from .plugins .zip_puller import ZipSourceWebDownloader
13
+ from . import hookspecs
18
14
import pluggy
15
+ import nbgitpuller
19
16
20
17
21
18
class SyncHandler (IPythonHandler ):
@@ -43,17 +40,53 @@ def emit(self, data):
43
40
self .write ('data: {}\n \n ' .format (serialized_data ))
44
41
yield self .flush ()
45
42
46
- def setup_plugins (self , repo ):
43
+ def setup_plugins (self , provider ):
47
44
pm = pluggy .PluginManager ("nbgitpuller" )
48
- pm .add_hookspecs (handle_files )
49
- if "drive.google.com" in repo :
50
- pm .register (ZipSourceGoogleDriveDownloader ())
51
- elif "dropbox.com" in repo :
52
- pm .register (ZipSourceDropBoxDownloader ())
53
- else :
54
- pm .register (ZipSourceWebDownloader ())
45
+ pm .add_hookspecs (hookspecs )
46
+ pm .load_setuptools_entrypoints ("nbgitpuller" , name = provider )
55
47
return pm
56
48
49
+ < << << << HEAD
50
+ def handle_provider_zip (self , provider ):
51
+ pm = self .setup_plugins (provider )
52
+ req_args = {k : v [0 ].decode () for k , v in self .request .arguments .items ()}
53
+ download_q = Queue ()
54
+ req_args ["download_q" ] = download_q
55
+ hf_args = {"query_line_args" : req_args }
56
+ dl_thread = ThreadWithResult (target = pm .hook .handle_files , kwargs = hf_args )
57
+ dl_thread .start ()
58
+ self .progress_loop (download_q )
59
+ dl_thread .join ()
60
+ return dl_thread .result
61
+
62
+ == == == =
63
+ >> >> >> > 9 b46037 ... Added async functionality to non - git archives
64
+ @gen .coroutine
65
+ def progress_loop (self , queue ):
66
+ while True :
67
+ try :
68
+ progress = queue .get_nowait ()
69
+ except Empty :
70
+ yield gen .sleep (0.1 )
71
+ continue
72
+ if progress is None :
73
+ yield gen .sleep (5 )
74
+ return
75
+ if isinstance (progress , Exception ):
76
+ self .emit ({
77
+ 'phase' : 'error' ,
78
+ 'message' : str (progress ),
79
+ 'output' : '\n ' .join ([
80
+ line .strip ()
81
+ for line in traceback .format_exception (
82
+ type (progress ), progress , progress .__traceback__
83
+ )
84
+ ])
85
+ })
86
+ return
87
+
88
+ self .emit ({'output' : progress , 'phase' : 'syncing' })
89
+
57
90
@web .authenticated
58
91
@gen .coroutine
59
92
def get (self ):
@@ -69,7 +102,7 @@ def get(self):
69
102
try :
70
103
repo = self .get_argument ('repo' )
71
104
branch = self .get_argument ('branch' , None )
72
- compressed = self .get_argument ('compressed ' , "false" )
105
+ provider = self .get_argument ('provider ' , None )
73
106
depth = self .get_argument ('depth' , None )
74
107
if depth :
75
108
depth = int (depth )
@@ -82,22 +115,31 @@ def get(self):
82
115
# so that all repos are always in scope after cloning. Sometimes
83
116
# server_root_dir will include things like `~` and so the path
84
117
# must be expanded.
85
- repo_parent_dir = os .path .join (os .path .expanduser (self .settings ['server_root_dir' ]),
86
- os .getenv ('NBGITPULLER_PARENTPATH' , '' ))
87
- repo_dir = os .path .join (repo_parent_dir , self .get_argument ('targetpath' , repo .split ('/' )[- 1 ]))
118
+ repo_parent_dir = os .path .join (os .path .expanduser (self .settings ['server_root_dir' ]), os .getenv ('NBGITPULLER_PARENTPATH' , '' ))
119
+ nbgitpuller .REPO_PARENT_DIR = repo_parent_dir
120
+
121
+ repo_dir = os .path .join (
122
+ repo_parent_dir ,
123
+ self .get_argument ('targetpath' , repo .split ('/' )[- 1 ]))
88
124
89
125
# We gonna send out event streams!
90
126
self .set_header ('content-type' , 'text/event-stream' )
91
127
self .set_header ('cache-control' , 'no-cache' )
92
128
93
- if compressed == 'true' :
94
- pm = self .setup_plugins (repo )
95
- results = pm .hook .handle_files (repo = repo , repo_parent_dir = repo_parent_dir )[0 ]
129
+ # if provider is specified then we are dealing with compressed
130
+ # archive and not a git repo
131
+ if provider is not None :
132
+ pm = self .setup_plugins (provider )
133
+ req_args = {k : v [0 ].decode () for k , v in self .request .arguments .items ()}
134
+ download_q = Queue ()
135
+ req_args ["progress_func" ] = lambda : self .progress_loop (download_q )
136
+ req_args ["download_q" ] = download_q
137
+ hf_args = {"query_line_args" : req_args }
138
+ results = pm .hook .handle_files (** hf_args )
96
139
repo_dir = repo_parent_dir + results ["unzip_dir" ]
97
140
repo = "file://" + results ["origin_repo_path" ]
98
141
99
142
gp = GitPuller (repo , repo_dir , branch = branch , depth = depth , parent = self .settings ['nbapp' ])
100
-
101
143
q = Queue ()
102
144
103
145
def pull ():
@@ -110,33 +152,11 @@ def pull():
110
152
q .put_nowait (e )
111
153
raise e
112
154
self .gp_thread = threading .Thread (target = pull )
113
-
114
155
self .gp_thread .start ()
115
-
116
- while True :
117
- try :
118
- progress = q .get_nowait ()
119
- except Empty :
120
- yield gen .sleep (0.5 )
121
- continue
122
- if progress is None :
123
- break
124
- if isinstance (progress , Exception ):
125
- self .emit ({
126
- 'phase' : 'error' ,
127
- 'message' : str (progress ),
128
- 'output' : '\n ' .join ([
129
- line .strip ()
130
- for line in traceback .format_exception (
131
- type (progress ), progress , progress .__traceback__
132
- )
133
- ])
134
- })
135
- return
136
-
137
- self .emit ({'output' : progress , 'phase' : 'syncing' })
138
-
156
+ self .progress_loop (q )
157
+ yield gen .sleep (3 )
139
158
self .emit ({'phase' : 'finished' })
159
+
140
160
except Exception as e :
141
161
self .emit ({
142
162
'phase' : 'error' ,
@@ -170,11 +190,10 @@ def initialize(self):
170
190
@gen .coroutine
171
191
def get (self ):
172
192
app_env = os .getenv ('NBGITPULLER_APP' , default = 'notebook' )
173
-
174
193
repo = self .get_argument ('repo' )
175
194
branch = self .get_argument ('branch' , None )
176
195
depth = self .get_argument ('depth' , None )
177
- compressed = self .get_argument ('compressed ' , "false" )
196
+ provider = self .get_argument ('provider ' , None )
178
197
urlPath = self .get_argument ('urlpath' , None ) or \
179
198
self .get_argument ('urlPath' , None )
180
199
subPath = self .get_argument ('subpath' , None ) or \
@@ -195,14 +214,17 @@ def get(self):
195
214
else :
196
215
path = 'tree/' + path
197
216
217
+ if provider is not None :
218
+ path = "tree/"
219
+
198
220
self .write (
199
221
self .render_template (
200
222
'status.html' ,
201
223
repo = repo ,
202
224
branch = branch ,
203
- compressed = compressed ,
204
225
path = path ,
205
226
depth = depth ,
227
+ provider = provider ,
206
228
targetpath = targetpath ,
207
229
version = __version__
208
230
))
@@ -239,3 +261,10 @@ def get(self):
239
261
)
240
262
241
263
self .redirect (new_url )
264
+
265
+
266
+ class ThreadWithResult (threading .Thread ):
267
+ def __init__ (self , group = None , target = None , name = None , args = (), kwargs = {}, * , daemon = None ):
268
+ def function ():
269
+ self .result = target (* args , ** kwargs )
270
+ super ().__init__ (group = group , target = function , name = name , daemon = daemon )
0 commit comments