-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path__init__.py
88 lines (73 loc) · 2.59 KB
/
__init__.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
import argparse
import BaseHTTPServer
import SocketServer
import mimetypes
import os
import pkg_resources
from rpaths import Path
import shutil
import tempfile
import webbrowser
from reprounzip.common import RPZPack
from reprounzip.unpackers.common import COMPAT_OK
from reprounzip.unpackers.graph import generate
__version__ = '0.1'
class VisHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
server_version = 'ReproUnzip/' + __version__
def do_GET(self):
print("Serving %s" % self.path)
if self.path == '/provenance.json':
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
with open(self.provenance_json, 'rb') as f:
shutil.copyfileobj(f, self.wfile)
else:
try:
f = pkg_resources.resource_stream('reprounzip_vis',
'static/' + self.path)
except IOError:
self.send_response(404)
else:
self.send_response(200)
if self.path == '/':
ctype = 'text/html'
else:
ctype = mimetypes.guess_type(self.path)[0]
self.send_header('Content-Type', ctype)
self.end_headers()
shutil.copyfileobj(f, self.wfile)
f.close()
def show_vis(args):
# Extract JSON from package
fd, json_file = tempfile.mkstemp(prefix='reprounzip_vis_', suffix='.json')
try:
rpz_pack = RPZPack(args.pack)
with rpz_pack.with_config() as config:
with rpz_pack.with_trace() as trace:
generate(Path(json_file), config, trace, graph_format='json')
os.close(fd)
VisHTTPHandler.provenance_json = json_file
# Serve static files and JSON document to browser
port = 8002
httpd = SocketServer.TCPServer(('', port), VisHTTPHandler)
print("serving at port %d" % port)
# Open web browser
webbrowser.open('http://localhost:%d/index.html' % port)
# Serve until killed
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
httpd.server_close()
finally:
os.remove(json_file)
def setup_vis(parser, **kwargs):
"""Visualizes the provenance of a package as a D3 graph in the browser.
"""
parser.add_argument(
'pack', nargs=argparse.OPTIONAL,
help="Pack to visualize")
parser.set_defaults(func=show_vis)
return {'test_compatibility': COMPAT_OK}