Skip to content

Instantly share code, notes, and snippets.

@gnmerritt
Created February 22, 2017 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnmerritt/fecb5869ec40c99aef8c228976836c4e to your computer and use it in GitHub Desktop.
Save gnmerritt/fecb5869ec40c99aef8c228976836c4e to your computer and use it in GitHub Desktop.
Reflux store dependency parser
from __future__ import print_function
import os
import re
import sys
from subprocess import check_output
IMPORT_RE = r".*from '(.*)';"
class RefluxParser(object):
def __init__(self, working_dir):
self.working_dir = working_dir
self.stores = self.find_stores()
self.reset()
self.process_stores()
def run(self, args, split="\n"):
output_byt = check_output(args)
return output_byt.decode("utf-8").split(split)
def reset(self):
self.deps_by_store = {}
self.paths_by_store = {}
for store in self.stores:
filename = self.store_filename(store)
self.deps_by_store[filename] = set()
self.paths_by_store[filename] = store
def store_filename(self, store):
filename = os.path.basename(store)
return filename.replace(u'.js', '')
def find_stores(self):
return [s for s in self.run(['grep', '-rl', 'createStore', '.']) if s]
def process_stores(self):
for store, filename in self.paths_by_store.items():
path = os.path.join(self.working_dir, filename)
if not os.path.isfile(path):
continue
with open(path, 'r') as store_file:
for line in store_file:
if line.find('import ') == 0:
self.check_import(line, store)
def check_import(self, line, store_name):
matches = re.search(IMPORT_RE, line)
if matches:
imported_filename = matches.group(1)
imported_store = os.path.basename(imported_filename)
if imported_store in self.deps_by_store:
self.deps_by_store[store_name].add(imported_store)
def print_output(self):
print("digraph reflux {")
for store, deps in self.deps_by_store.items():
for dep in deps:
print(" {} -> {}".format(store, dep))
print("}")
if __name__ == "__main__":
if not len(sys.argv) == 2:
print("Usage: python reflux_dag_parser.py <path-to-ui-dir>")
working_dir = sys.argv[1]
os.chdir(working_dir)
parser = RefluxParser(working_dir)
parser.print_output()
$ python3 reflux_day_parser.py 'path-to-ui-directory'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment