-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpreview.py
executable file
·83 lines (65 loc) · 2.65 KB
/
preview.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
#!/usr/bin/env python2.7
import os
import time
import sys
import subprocess
import requests
import urllib
from fin.contextlog import Log
from selenium import webdriver
TEMPLATE_DIR = os.path.abspath(os.path.dirname(__file__))
def upload_screenshot(data):
url = "https://i2xwshcjfa.execute-api.eu-west-1.amazonaws.com/live/pythonjobs-commentbot/screenshots"
req = requests.post(url, data, headers={"Content-Type": "image/png"})
req.raise_for_status()
return req.json()['body']
def get_preview(driver, filename, pull_request_num):
basename, ext = os.path.splitext(filename)
url = "http://localhost:8080/jobs/%s.html" % urllib.quote(basename)
with Log(url):
driver.get(url)
while driver.execute_script("return document.readyState") != "complete":
time.sleep(0.5)
png = driver.get_screenshot_as_png()
with Log("Uploading Screenshot"):
return upload_screenshot(png)
def get_file_previews(pull_request_num):
with Log("Generating Previews"):
preview_ids = []
driver = webdriver.PhantomJS()
hyde_root = os.path.join(TEMPLATE_DIR, 'hyde')
with Log("Starting Server"):
server_proc = subprocess.Popen(['hyde', '-s', hyde_root, 'serve'])
time.sleep(4)
try:
driver.set_window_size(800, 600)
for filename in get_modified_files():
with Log(filename):
image_id = get_preview(driver, filename, pull_request_num)
preview_ids.append(image_id)
finally:
driver.close()
server_proc.terminate()
server_proc.wait()
if not preview_ids:
return
link_template = ''
image_links = [link_template % preview_id for preview_id in preview_ids]
message = """Here are some screenshots of what the live listing should look like:
%s
""" % ("\n".join(image_links), )
proc = subprocess.Popen([
sys.executable,
os.path.join(TEMPLATE_DIR, "comment.py")
], stdin=subprocess.PIPE)
proc.communicate(message)
def get_modified_files():
modified_files = subprocess.check_output(['git', 'diff', '--name-only', 'master'])
for line in modified_files.splitlines():
if line.startswith("jobs/"):
yield line[5:]
if __name__ == '__main__':
pull_request_num = os.environ.get('TRAVIS_PULL_REQUEST', 'false')
if pull_request_num == 'false':
sys.exit("Not creating pull-request screenshot, as can't get PR number")
get_file_previews(pull_request_num)