This repository was archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathdebug.py
220 lines (191 loc) · 7.13 KB
/
debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Binux<i@binux.me>
# http://binux.me
# Created on 2014-02-23 00:19:06
import sys
import time
import socket
import inspect
import datetime
import traceback
from flask import render_template, request, json
try:
import flask_login as login
except ImportError:
from flask.ext import login
from pyspider.libs import utils, sample_handler, dataurl
from pyspider.libs.response import rebuild_response
from pyspider.processor.project_module import ProjectManager, ProjectFinder
from .app import app
default_task = {
'taskid': 'data:,on_start',
'project': '',
'url': 'data:,on_start',
'process': {
'callback': 'on_start',
},
}
default_script = inspect.getsource(sample_handler)
@app.route('/debug/<project>', methods=['GET', 'POST'])
def debug(project):
projectdb = app.config['projectdb']
if not projectdb.verify_project_name(project):
return 'project name is not allowed!', 400
info = projectdb.get(project, fields=['name', 'script'])
if info:
script = info['script']
else:
script = (default_script
.replace('__DATE__', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
.replace('__PROJECT_NAME__', project)
.replace('__START_URL__', request.values.get('start-urls') or '__START_URL__'))
taskid = request.args.get('taskid')
if taskid:
taskdb = app.config['taskdb']
task = taskdb.get_task(
project, taskid, ['taskid', 'project', 'url', 'fetch', 'process'])
else:
task = default_task
default_task['project'] = project
return render_template("debug.html", task=task, script=script, project_name=project)
@app.before_first_request
def enable_projects_import():
sys.meta_path.append(ProjectFinder(app.config['projectdb']))
@app.route('/debug/<project>/run', methods=['POST', ])
def run(project):
start_time = time.time()
try:
task = utils.decode_unicode_obj(json.loads(request.form['task']))
except Exception:
result = {
'fetch_result': "",
'logs': u'task json error',
'follows': [],
'messages': [],
'result': None,
'time': time.time() - start_time,
}
return json.dumps(utils.unicode_obj(result)), \
200, {'Content-Type': 'application/json'}
project_info = {
'name': project,
'status': 'DEBUG',
'script': request.form['script'],
}
if request.form.get('webdav_mode') == 'true':
projectdb = app.config['projectdb']
info = projectdb.get(project, fields=['name', 'script'])
if not info:
result = {
'fetch_result': "",
'logs': u' in wevdav mode, cannot load script',
'follows': [],
'messages': [],
'result': None,
'time': time.time() - start_time,
}
return json.dumps(utils.unicode_obj(result)), \
200, {'Content-Type': 'application/json'}
project_info['script'] = info['script']
fetch_result = {}
try:
module = ProjectManager.build_module(project_info, {
'debugger': True,
'process_time_limit': app.config['process_time_limit'],
})
# The code below is to mock the behavior that crawl_config been joined when selected by scheduler.
# but to have a better view of joined tasks, it has been done in BaseHandler.crawl when `is_debugger is True`
# crawl_config = module['instance'].crawl_config
# task = module['instance'].task_join_crawl_config(task, crawl_config)
fetch_result = app.config['fetch'](task)
response = rebuild_response(fetch_result)
ret = module['instance'].run_task(module['module'], task, response)
except Exception:
type, value, tb = sys.exc_info()
tb = utils.hide_me(tb, globals())
logs = ''.join(traceback.format_exception(type, value, tb))
result = {
'fetch_result': fetch_result,
'logs': logs,
'follows': [],
'messages': [],
'result': None,
'time': time.time() - start_time,
}
else:
result = {
'fetch_result': fetch_result,
'logs': ret.logstr(),
'follows': ret.follows,
'messages': ret.messages,
'result': ret.result,
'time': time.time() - start_time,
}
result['fetch_result']['content'] = response.text
if (response.headers.get('content-type', '').startswith('image')):
result['fetch_result']['dataurl'] = dataurl.encode(
response.content, response.headers['content-type'])
try:
# binary data can't encode to JSON, encode result as unicode obj
# before send it to frontend
return json.dumps(utils.unicode_obj(result)), 200, {'Content-Type': 'application/json'}
except Exception:
type, value, tb = sys.exc_info()
tb = utils.hide_me(tb, globals())
logs = ''.join(traceback.format_exception(type, value, tb))
result = {
'fetch_result': "",
'logs': logs,
'follows': [],
'messages': [],
'result': None,
'time': time.time() - start_time,
}
return json.dumps(utils.unicode_obj(result)), 200, {'Content-Type': 'application/json'}
@app.route('/debug/<project>/save', methods=['POST', ])
def save(project):
projectdb = app.config['projectdb']
if not projectdb.verify_project_name(project):
return 'project name is not allowed!', 400
script = request.form['script']
project_info = projectdb.get(project, fields=['name', 'status', 'group'])
if project_info and 'lock' in projectdb.split_group(project_info.get('group')) \
and not login.current_user.is_active():
return app.login_response
if project_info:
info = {
'script': script,
}
if project_info.get('status') in ('DEBUG', 'RUNNING', ):
info['status'] = 'CHECKING'
projectdb.update(project, info)
else:
info = {
'name': project,
'script': script,
'status': 'TODO',
'rate': app.config.get('max_rate', 1),
'burst': app.config.get('max_burst', 3),
}
projectdb.insert(project, info)
rpc = app.config['scheduler_rpc']
if rpc is not None:
try:
rpc.update_project()
except socket.error as e:
app.logger.warning('connect to scheduler rpc error: %r', e)
return 'rpc error', 200
return 'ok', 200
@app.route('/debug/<project>/get')
def get_script(project):
projectdb = app.config['projectdb']
if not projectdb.verify_project_name(project):
return 'project name is not allowed!', 400
info = projectdb.get(project, fields=['name', 'script'])
return json.dumps(utils.unicode_obj(info)), \
200, {'Content-Type': 'application/json'}
@app.route('/blank.html')
def blank_html():
return ""