2017-03-17 20:30:19 +01:00
|
|
|
"""
|
|
|
|
$ python ./tools/js-dep-visualizer.py
|
|
|
|
$ dot -Tpng var/zulip-deps.dot -o var/zulip-deps.png
|
|
|
|
"""
|
|
|
|
|
2017-03-18 21:10:40 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-03-17 20:30:19 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
TOOLS_DIR = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
ROOT_DIR = os.path.dirname(TOOLS_DIR)
|
|
|
|
sys.path.insert(0, ROOT_DIR)
|
|
|
|
from tools.lib.graph import Graph, make_dot_file
|
|
|
|
|
2017-03-18 21:10:40 +01:00
|
|
|
JS_FILES_DIR = os.path.join(ROOT_DIR, 'static/js')
|
|
|
|
OUTPUT_FILE_PATH = os.path.relpath(os.path.join(ROOT_DIR, 'var/zulip-deps.dot'))
|
2017-03-17 20:30:19 +01:00
|
|
|
|
2017-03-19 14:01:40 +01:00
|
|
|
names = set()
|
2017-03-17 20:30:19 +01:00
|
|
|
modules = [] # type: List[Dict[str, Any]]
|
|
|
|
for js_file in os.listdir(JS_FILES_DIR):
|
2017-03-18 21:10:40 +01:00
|
|
|
if not js_file.endswith('.js'):
|
|
|
|
continue
|
2017-03-17 20:30:19 +01:00
|
|
|
name = js_file[:-3] # remove .js
|
2017-03-18 21:10:40 +01:00
|
|
|
path = os.path.join(JS_FILES_DIR, js_file)
|
|
|
|
names.add(name)
|
|
|
|
modules.append(dict(
|
|
|
|
name=name,
|
|
|
|
path=path,
|
|
|
|
regex=re.compile('[^_]{}\.\w+\('.format(name))
|
|
|
|
))
|
2017-03-17 20:30:19 +01:00
|
|
|
|
2017-03-19 14:01:40 +01:00
|
|
|
COMMENT_REGEX = re.compile('\s+//')
|
|
|
|
REGEX = re.compile('[^_](\w+)\.\w+\(')
|
|
|
|
|
2017-03-17 20:30:19 +01:00
|
|
|
tuples = set()
|
|
|
|
for module in modules:
|
2017-03-19 14:01:40 +01:00
|
|
|
parent = module['name']
|
2017-03-17 20:30:19 +01:00
|
|
|
|
|
|
|
with open(module['path']) as f:
|
2017-03-19 14:01:40 +01:00
|
|
|
for line in f:
|
|
|
|
if COMMENT_REGEX.match(line):
|
|
|
|
continue
|
|
|
|
if 'subs.forEach' in line:
|
|
|
|
continue
|
|
|
|
m = REGEX.search(line)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
for child in m.groups():
|
|
|
|
if (child in names) and (child != parent):
|
|
|
|
tup = (parent, child)
|
|
|
|
tuples.add(tup)
|
2017-03-17 20:30:19 +01:00
|
|
|
|
2017-03-18 01:43:20 +01:00
|
|
|
IGNORE_TUPLES = [
|
|
|
|
# We ignore the following tuples to de-clutter the graph, since there is a
|
|
|
|
# pretty clear roadmap on how to break the dependencies. You can comment
|
|
|
|
# these out to see what the "real" situation looks like now, and if you do
|
|
|
|
# the work of breaking the dependency, you can remove it.
|
|
|
|
|
2017-03-18 21:10:40 +01:00
|
|
|
('echo', 'message_events'), # do something slimy here
|
|
|
|
('echo', 'ui'),
|
2017-03-18 01:43:20 +01:00
|
|
|
|
|
|
|
('stream_data', 'narrow'), # split out narrow.by_foo functions
|
2017-03-18 21:10:40 +01:00
|
|
|
('activity', 'narrow'),
|
|
|
|
|
|
|
|
('subs', 'narrow'), # data functions
|
|
|
|
('subs', 'compose'), # data functions
|
|
|
|
|
|
|
|
('narrow', 'ui'), # just three functions
|
2017-03-18 01:43:20 +01:00
|
|
|
|
|
|
|
('stream_data', 'stream_color'), # split out stream_color data/UI
|
|
|
|
('stream_color', 'tab_bar'), # only one call
|
|
|
|
('stream_color', 'subs'), # only one call
|
|
|
|
|
2017-03-18 21:10:40 +01:00
|
|
|
('subs', 'stream_events'), # see TODOs related to mark_{un,}subscribed
|
|
|
|
|
|
|
|
('subs', 'hashchange'), # modal stuff
|
|
|
|
|
|
|
|
('message_store', 'compose'), # split out compose_data
|
|
|
|
|
2017-03-18 01:43:20 +01:00
|
|
|
('search', 'search_suggestion'), # move handler into search_suggestion
|
|
|
|
|
|
|
|
('unread', 'narrow'), # create narrow_state.js
|
|
|
|
|
|
|
|
('navigate', 'stream_list'), # move cycle_stream into stream_list.js
|
|
|
|
|
|
|
|
# This one is kind of tricky, but basically we want to split all the basic
|
|
|
|
# code out of both of these modules that's essentially just string manipulation.
|
|
|
|
('narrow', 'hashchange'),
|
|
|
|
|
|
|
|
('composebox_typeahead', 'compose'), # introduce compose_state.js
|
|
|
|
|
|
|
|
# This one might require some work, but the idea is to split out something
|
|
|
|
# like narrow_state.js.
|
|
|
|
('pm_list', 'narrow'),
|
|
|
|
|
|
|
|
('settings', 'subs'), # not much to fix, can call stream_data directly, maybe
|
|
|
|
|
|
|
|
('modals', 'subs'), # add some kind of onClose mechanism in new modals.open
|
|
|
|
|
|
|
|
('channel', 'reload'), # just one call to fix somehow
|
|
|
|
('compose', 'reload'), # use channel stuff more directly?
|
|
|
|
|
|
|
|
('settings', 'muting_ui'), # inline call or split out muting_settings.js
|
|
|
|
|
|
|
|
('resize', 'navigate'), # split out scroll.js
|
2017-03-18 21:10:40 +01:00
|
|
|
('resize', 'popovers'), # only three interactions
|
|
|
|
|
2017-03-18 01:43:20 +01:00
|
|
|
]
|
|
|
|
|
2017-03-18 21:10:40 +01:00
|
|
|
for tup in IGNORE_TUPLES:
|
|
|
|
try:
|
|
|
|
tuples.remove(tup)
|
|
|
|
except KeyError:
|
|
|
|
print('''
|
|
|
|
{} no longer needs to be ignored. Help us celebrate
|
|
|
|
by removing it from IGNORE_TUPLES!
|
|
|
|
'''.format(tup))
|
|
|
|
sys.exit(1)
|
2017-03-18 01:43:20 +01:00
|
|
|
|
|
|
|
|
2017-03-17 20:30:19 +01:00
|
|
|
# print(tuples)
|
|
|
|
graph = Graph(*tuples)
|
2017-03-18 01:43:20 +01:00
|
|
|
ignore_modules = [
|
2017-03-18 21:10:40 +01:00
|
|
|
'blueslip',
|
|
|
|
'message_edit',
|
|
|
|
'message_util',
|
|
|
|
'modals',
|
2017-03-18 01:43:20 +01:00
|
|
|
'notifications',
|
2017-03-18 21:10:40 +01:00
|
|
|
'popovers',
|
|
|
|
'server_events',
|
2017-03-18 01:43:20 +01:00
|
|
|
'stream_popover',
|
2017-03-18 21:10:40 +01:00
|
|
|
'topic_list',
|
|
|
|
'tutorial',
|
|
|
|
'unread_ops',
|
|
|
|
'rows', # message_store
|
2017-03-17 20:30:19 +01:00
|
|
|
]
|
2017-03-18 01:43:20 +01:00
|
|
|
for node in ignore_modules:
|
2017-03-17 20:30:19 +01:00
|
|
|
graph.remove(node)
|
|
|
|
graph.remove_exterior_nodes()
|
2017-03-18 21:10:40 +01:00
|
|
|
graph.report()
|
2017-03-17 20:30:19 +01:00
|
|
|
buffer = make_dot_file(graph)
|
|
|
|
|
|
|
|
with open(OUTPUT_FILE_PATH, 'w') as f:
|
|
|
|
f.write(buffer)
|
2017-03-18 21:10:40 +01:00
|
|
|
print()
|
|
|
|
print('see dot file here: {}'.format(OUTPUT_FILE_PATH))
|