This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site_diff.py
executable file
·379 lines (305 loc) · 11.8 KB
/
site_diff.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python
# Copyright 2013 Brett Slatkin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility for doing incremental diffs for a live website.
Example usage:
./dpxdt/tools/site_diff.py \
--upload_build_id=1234 \
--release_server_prefix=https://my-dpxdt-apiserver.example.com/api \
--release_client_id=<your api key> \
--release_client_secret=<your api secret> \
--crawl_depth=1 \
--width=320 \
--height=480 \
http://www.example.com/my/website/here
"""
import HTMLParser
import Queue
import datetime
import json
import logging
import os
import re
import sys
import urlparse
# Local Libraries
import gflags
FLAGS = gflags.FLAGS
# Local modules
from dpxdt.client import release_worker
from dpxdt.client import fetch_worker
from dpxdt.client import workers
import flags
gflags.DEFINE_integer(
'crawl_depth', -1,
'How deep to crawl. Depth of 0 means only the given page. 1 means pages '
'that are one click away, 2 means two clicks, and so on. Set to -1 to '
'scan every URL with the supplied prefix.')
gflags.DEFINE_spaceseplist(
'ignore_prefixes', [],
'URL prefixes that should not be crawled.')
gflags.DEFINE_bool(
'keep_query_string', False,
'Keep the query string when cleaning URLs')
# URL regex rewriting code originally from mirrorrr
# http://code.google.com/p/mirrorrr/source/browse/trunk/transform_content.py
# URLs that have absolute addresses
ABSOLUTE_URL_REGEX = r"(?P<url>(http(s?):)?//[^\"'> \t]+)"
# URLs that are relative to the base of the current hostname.
BASE_RELATIVE_URL_REGEX = (
r"/(?!(/)|(mailto:)|(http(s?)://)|(url\())(?P<url>[^\"'> \t]*)")
# URLs that have '../' or './' to start off their paths.
TRAVERSAL_URL_REGEX = (
r"(?P<relative>\.(\.)?)/(?!(/)|"
r"(http(s?)://)|(url\())(?P<url>[^\"'> \t]*)")
# URLs that are in the same directory as the requested URL.
SAME_DIR_URL_REGEX = r"(?!(/)|(mailto:)|(http(s?)://)|(#)|(url\())(?P<url>[^\"'> \t]+)"
# URL matches the root directory.
ROOT_DIR_URL_REGEX = r"(?!//(?!>))/(?P<url>)(?=[ \t\n]*[\"'> /])"
# Start of a tag using 'src' or 'href'
TAG_START = (
r"(?i)(?P<tag>\ssrc|href|action|url|background)"
r"(?P<equals>[\t ]*=[\t ]*)(?P<quote>[\"']?)")
# Potential HTML document URL with no fragments.
MAYBE_HTML_URL_REGEX = (
TAG_START + r"(?P<absurl>(http(s?):)?//[^\"'> \t]+)")
REPLACEMENT_REGEXES = [
(TAG_START + SAME_DIR_URL_REGEX,
"\g<tag>\g<equals>\g<quote>%(accessed_dir)s\g<url>"),
(TAG_START + TRAVERSAL_URL_REGEX,
"\g<tag>\g<equals>\g<quote>%(accessed_dir)s/\g<relative>/\g<url>"),
(TAG_START + BASE_RELATIVE_URL_REGEX,
"\g<tag>\g<equals>\g<quote>%(base)s/\g<url>"),
(TAG_START + ROOT_DIR_URL_REGEX,
"\g<tag>\g<equals>\g<quote>%(base)s/"),
(TAG_START + ABSOLUTE_URL_REGEX,
"\g<tag>\g<equals>\g<quote>\g<url>"),
]
def clean_url(url, force_scheme=None):
"""Cleans the given URL."""
# URL should be ASCII according to RFC 3986
url = str(url)
# Collapse ../../ and related
url_parts = urlparse.urlparse(url)
path_parts = []
for part in url_parts.path.split('/'):
if part == '.':
continue
elif part == '..':
if path_parts:
path_parts.pop()
else:
path_parts.append(part)
url_parts = list(url_parts)
if force_scheme:
url_parts[0] = force_scheme
url_parts[2] = '/'.join(path_parts)
if FLAGS.keep_query_string == False:
url_parts[4] = '' # No query string
url_parts[5] = '' # No path
# Always have a trailing slash
if not url_parts[2]:
url_parts[2] = '/'
return urlparse.urlunparse(url_parts)
def extract_urls(url, data, unescape=HTMLParser.HTMLParser().unescape):
"""Extracts the URLs from an HTML document."""
parts = urlparse.urlparse(url)
prefix = '%s://%s' % (parts.scheme, parts.netloc)
accessed_dir = os.path.dirname(parts.path)
if not accessed_dir.endswith('/'):
accessed_dir += '/'
for pattern, replacement in REPLACEMENT_REGEXES:
fixed = replacement % {
'base': prefix,
'accessed_dir': accessed_dir,
}
data = re.sub(pattern, fixed, data)
result = set()
for match in re.finditer(MAYBE_HTML_URL_REGEX, data):
found_url = unescape(match.groupdict()['absurl'])
found_url = clean_url(
found_url,
force_scheme=parts[0]) # Use the main page's scheme
result.add(found_url)
return result
IGNORE_SUFFIXES = frozenset([
'jpg', 'jpeg', 'png', 'css', 'js', 'xml', 'json', 'gif', 'ico', 'doc'])
def prune_urls(url_set, start_url, allowed_list, ignored_list):
"""Prunes URLs that should be ignored."""
result = set()
for url in url_set:
allowed = False
for allow_url in allowed_list:
if url.startswith(allow_url):
allowed = True
break
if not allowed:
continue
ignored = False
for ignore_url in ignored_list:
if url.startswith(ignore_url):
ignored = True
break
if ignored:
continue
prefix, suffix = (url.rsplit('.', 1) + [''])[:2]
if suffix.lower() in IGNORE_SUFFIXES:
continue
result.add(url)
return result
class SiteDiff(workers.WorkflowItem):
"""Workflow for coordinating the site diff.
Args:
start_url: URL to begin the site diff scan.
ignore_prefixes: Optional. List of URL prefixes to ignore during
the crawl; start_url should be a common prefix with all of these.
upload_build_id: Build ID of the site being compared.
upload_release_name: Optional. Release name to use for the build. When
not supplied, a new release based on the current time will be
created.
heartbeat: Function to call with progress status.
"""
def run(self,
start_url,
ignore_prefixes,
upload_build_id,
upload_release_name=None,
heartbeat=None):
if not ignore_prefixes:
ignore_prefixes = []
pending_urls = set([clean_url(start_url)])
seen_urls = set()
good_urls = set()
yield heartbeat('Scanning for content')
http_username = FLAGS.http_username
http_password = FLAGS.http_password
limit_depth = FLAGS.crawl_depth >= 0
depth = 0
while (not limit_depth or depth <= FLAGS.crawl_depth) and pending_urls:
# TODO: Enforce a job-wide timeout on the whole process of
# URL discovery, to make sure infinitely deep sites do not
# cause this job to never stop.
seen_urls.update(pending_urls)
yield heartbeat(
'Scanning %d pages for good urls' % len(pending_urls))
output = yield [fetch_worker.FetchItem(
u,
username=http_username,
password=http_password)
for u in pending_urls]
pending_urls.clear()
for item in output:
if not item.data:
logging.debug('No data from url=%r', item.url)
continue
if item.content_type != 'text/html':
logging.debug('Skipping non-HTML document url=%r',
item.url)
continue
good_urls.add(item.url)
found = extract_urls(item.url, item.data)
pruned = prune_urls(
found, start_url, [start_url], ignore_prefixes)
new = pruned - seen_urls
pending_urls.update(new)
yield heartbeat('Found %d new URLs from %s' % (
len(new), item.url))
yield heartbeat('Finished crawl at depth %d' % depth)
depth += 1
yield heartbeat(
'Found %d total URLs, %d good HTML pages; starting '
'screenshots' % (len(seen_urls), len(good_urls)))
# TODO: Make the default release name prettier.
if not upload_release_name:
upload_release_name = str(datetime.datetime.utcnow())
release_number = yield release_worker.CreateReleaseWorkflow(
upload_build_id, upload_release_name, start_url)
run_requests = []
for url in good_urls:
yield heartbeat('Requesting run for %s' % url)
parts = urlparse.urlparse(url)
run_name = parts.path
if FLAGS.keep_query_string == True:
run_name += '?' + parts.query
config_dict = {
'viewportSize': {
'width': 1280,
'height': 1024,
}
}
if FLAGS.inject_css:
config_dict['injectCss'] = FLAGS.inject_css
if FLAGS.inject_js:
config_dict['injectJs'] = FLAGS.inject_js
if FLAGS.cookies:
config_dict['cookies'] = json.loads(
open(FLAGS.cookies).read())
if http_username:
config_dict['httpUserName'] = http_username
if http_password:
config_dict['httpPassword'] = http_password
if FLAGS.width:
config_dict['viewportSize']['width'] = FLAGS.width
if FLAGS.height:
config_dict['viewportSize']['height'] = FLAGS.height
config_data = json.dumps(config_dict)
run_requests.append(release_worker.RequestRunWorkflow(
upload_build_id, upload_release_name, release_number,
run_name, url, config_data))
yield run_requests
yield heartbeat('Marking runs as complete')
release_url = yield release_worker.RunsDoneWorkflow(
upload_build_id, upload_release_name, release_number)
yield heartbeat('Results viewable at: %s' % release_url)
def real_main(start_url=None,
ignore_prefixes=None,
upload_build_id=None,
upload_release_name=None):
"""Runs the site_diff."""
coordinator = workers.get_coordinator()
fetch_worker.register(coordinator)
coordinator.start()
item = SiteDiff(
start_url=start_url,
ignore_prefixes=ignore_prefixes,
upload_build_id=upload_build_id,
upload_release_name=upload_release_name,
heartbeat=workers.PrintWorkflow)
item.root = True
coordinator.input_queue.put(item)
coordinator.wait_one()
coordinator.stop()
coordinator.join()
def main(argv):
try:
argv = FLAGS(argv)
except gflags.FlagsError, e:
print '%s\nUsage: %s ARGS\n%s' % (e, sys.argv[0], FLAGS)
sys.exit(1)
if len(argv) != 2:
print 'Must supply a website URL as the first argument.'
sys.exit(1)
assert FLAGS.upload_build_id
assert FLAGS.release_server_prefix
if FLAGS.verbose:
logging.getLogger().setLevel(logging.DEBUG)
if FLAGS.verbose_workers:
logging.getLogger('dpxdt.client.workers').setLevel(logging.DEBUG)
real_main(
start_url=argv[1],
ignore_prefixes=FLAGS.ignore_prefixes,
upload_build_id=FLAGS.upload_build_id,
upload_release_name=FLAGS.upload_release_name)
if __name__ == '__main__':
main(sys.argv)