File: dump-timing.py

package info (click to toggle)
magic-wormhole 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: python: 17,148; javascript: 840; makefile: 30; sh: 23
file content (57 lines) | stat: -rw-r--r-- 1,973 bytes parent folder | download
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
# To use the web() option, you should do:
# * cd misc
# * npm install d3@3.5.17 d3-tip@0.6.7 zepto

import os, sys, time, json, random


streams = sys.argv[1:]
if len(streams) != 2:
    print("run like: python dump-timing.py tx.json rx.json")
    sys.exit(1)
# for now, require sender as first file, receiver as second
# later, allow use of only one file.

data = {}
for i,fn in enumerate(streams):
    name = ["send", "receive"][i]
    with open(fn, "rb") as f:
        events = json.load(f)
    data[name] = {"fn": os.path.basename(fn), "events": events}

from pprint import pprint
pprint(data)

here = os.path.dirname(__file__)
web_root = os.path.join(here, "web")
lib_root = os.path.join(here, "node_modules")
if not os.path.isdir(lib_root):
    print("Cannot find 'd3' and 'd3-tip' in misc/node_modules/")
    print("Please run 'npm install d3 d3-tip zepto' from the misc/ directory.")
    sys.exit(1)

def web():
    # set up a server that serves web/ at the root, plus a /data.json built
    # from {timeline}. Quit when it fetches /done .
    from twisted.web import resource, static, server
    from twisted.internet import reactor, endpoints
    ep = endpoints.serverFromString(reactor, "tcp:8066:interface=127.0.0.1")
    root = static.File(web_root)
    root.putChild("data.json", static.Data(json.dumps(data).encode("utf-8"),
                                           "application/json"))
    root.putChild("lib", static.File(lib_root))
    class Shutdown(resource.Resource):
        def render_GET(self, request):
            #print("timeline ready, server shutting down")
            #reactor.stop()
            return "shutting down"
    root.putChild("done", Shutdown())
    site = server.Site(root)
    ep.listen(site)
    import webbrowser
    def launch_browser():
        webbrowser.open("http://localhost:%d/timeline.html" % 8066)
        print("browser opened, waiting for shutdown")
    reactor.callLater(0, launch_browser)
    reactor.run()
web()