Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #288 from matrix-org/markjh/unused_definitions
Browse files Browse the repository at this point in the history
Remove some of the unused definitions from synapse
  • Loading branch information
NegativeMjark committed Sep 28, 2015
2 parents 7417772 + 314aabb commit 3011415
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 588 deletions.
142 changes: 142 additions & 0 deletions scripts-dev/definitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#! /usr/bin/python

import ast
import yaml

class DefinitionVisitor(ast.NodeVisitor):
def __init__(self):
super(DefinitionVisitor, self).__init__()
self.functions = {}
self.classes = {}
self.names = {}
self.attrs = set()
self.definitions = {
'def': self.functions,
'class': self.classes,
'names': self.names,
'attrs': self.attrs,
}

def visit_Name(self, node):
self.names.setdefault(type(node.ctx).__name__, set()).add(node.id)

def visit_Attribute(self, node):
self.attrs.add(node.attr)
for child in ast.iter_child_nodes(node):
self.visit(child)

def visit_ClassDef(self, node):
visitor = DefinitionVisitor()
self.classes[node.name] = visitor.definitions
for child in ast.iter_child_nodes(node):
visitor.visit(child)

def visit_FunctionDef(self, node):
visitor = DefinitionVisitor()
self.functions[node.name] = visitor.definitions
for child in ast.iter_child_nodes(node):
visitor.visit(child)


def non_empty(defs):
functions = {name: non_empty(f) for name, f in defs['def'].items()}
classes = {name: non_empty(f) for name, f in defs['class'].items()}
result = {}
if functions: result['def'] = functions
if classes: result['class'] = classes
names = defs['names']
uses = []
for name in names.get('Load', ()):
if name not in names.get('Param', ()) and name not in names.get('Store', ()):
uses.append(name)
uses.extend(defs['attrs'])
if uses: result['uses'] = uses
result['names'] = names
result['attrs'] = defs['attrs']
return result


def definitions_in_code(input_code):
input_ast = ast.parse(input_code)
visitor = DefinitionVisitor()
visitor.visit(input_ast)
definitions = non_empty(visitor.definitions)
return definitions


def definitions_in_file(filepath):
with open(filepath) as f:
return definitions_in_code(f.read())


def defined_names(prefix, defs, names):
for name, funcs in defs.get('def', {}).items():
names.setdefault(name, {'defined': []})['defined'].append(prefix + name)
defined_names(prefix + name + ".", funcs, names)

for name, funcs in defs.get('class', {}).items():
names.setdefault(name, {'defined': []})['defined'].append(prefix + name)
defined_names(prefix + name + ".", funcs, names)


def used_names(prefix, defs, names):
for name, funcs in defs.get('def', {}).items():
used_names(prefix + name + ".", funcs, names)

for name, funcs in defs.get('class', {}).items():
used_names(prefix + name + ".", funcs, names)

for used in defs.get('uses', ()):
if used in names:
names[used].setdefault('used', []).append(prefix.rstrip('.'))


if __name__ == '__main__':
import sys, os, argparse, re

parser = argparse.ArgumentParser(description='Find definitions.')
parser.add_argument(
"--unused", action="store_true", help="Only list unused definitions"
)
parser.add_argument(
"--ignore", action="append", metavar="REGEXP", help="Ignore a pattern"
)
parser.add_argument(
"--pattern", action="append", metavar="REGEXP",
help="Search for a pattern"
)
parser.add_argument(
"directories", nargs='+', metavar="DIR",
help="Directories to search for definitions"
)
args = parser.parse_args()

definitions = {}
for directory in args.directories:
for root, dirs, files in os.walk(directory):
for filename in files:
if filename.endswith(".py"):
filepath = os.path.join(root, filename)
definitions[filepath] = definitions_in_file(filepath)

names = {}
for filepath, defs in definitions.items():
defined_names(filepath + ":", defs, names)

for filepath, defs in definitions.items():
used_names(filepath + ":", defs, names)

patterns = [re.compile(pattern) for pattern in args.pattern or ()]
ignore = [re.compile(pattern) for pattern in args.ignore or ()]

result = {}
for name, definition in names.items():
if patterns and not any(pattern.match(name) for pattern in patterns):
continue
if ignore and any(pattern.match(name) for pattern in ignore):
continue
if args.unused and definition.get('used'):
continue
result[name] = definition

yaml.dump(result, sys.stdout, default_flow_style=False)
2 changes: 0 additions & 2 deletions scripts/synapse_port_db
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ class Store(object):
_simple_update_one = SQLBaseStore.__dict__["_simple_update_one"]
_simple_update_one_txn = SQLBaseStore.__dict__["_simple_update_one_txn"]

_execute_and_decode = SQLBaseStore.__dict__["_execute_and_decode"]

def runInteraction(self, desc, func, *args, **kwargs):
def r(conn):
try:
Expand Down
5 changes: 0 additions & 5 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ def error_dict(self):
)


class RoomError(SynapseError):
"""An error raised when a room event fails."""
pass


class RegistrationError(SynapseError):
"""An error raised when a registration event fails."""
pass
Expand Down
7 changes: 1 addition & 6 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@
logger = logging.getLogger("synapse.app.homeserver")


class GzipFile(File):
def getChild(self, path, request):
child = File.getChild(self, path, request)
return EncodingResourceWrapper(child, [GzipEncoderFactory()])


def gz_wrap(r):
return EncodingResourceWrapper(r, [GzipEncoderFactory()])

Expand Down Expand Up @@ -134,6 +128,7 @@ def build_resource_for_web_client(self):
# (It can stay enabled for the API resources: they call
# write() with the whole body and then finish() straight
# after and so do not trigger the bug.
# GzipFile was removed in commit 184ba09
# return GzipFile(webclient_path) # TODO configurable?
return File(webclient_path) # TODO configurable?

Expand Down
49 changes: 0 additions & 49 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,52 +1456,3 @@ def get_next(it, opt=None):
},
"missing": [e.event_id for e in missing_locals],
})

@defer.inlineCallbacks
def _handle_auth_events(self, origin, auth_events):
auth_ids_to_deferred = {}

def process_auth_ev(ev):
auth_ids = [e_id for e_id, _ in ev.auth_events]

prev_ds = [
auth_ids_to_deferred[i]
for i in auth_ids
if i in auth_ids_to_deferred
]

d = defer.Deferred()

auth_ids_to_deferred[ev.event_id] = d

@defer.inlineCallbacks
def f(*_):
ev.internal_metadata.outlier = True

try:
auth = {
(e.type, e.state_key): e for e in auth_events
if e.event_id in auth_ids
}

yield self._handle_new_event(
origin, ev, auth_events=auth
)
except:
logger.exception(
"Failed to handle auth event %s",
ev.event_id,
)

d.callback(None)

if prev_ds:
dx = defer.DeferredList(prev_ds)
dx.addBoth(f)
else:
f()

for e in auth_events:
process_auth_ev(e)

yield defer.DeferredList(auth_ids_to_deferred.values())
26 changes: 0 additions & 26 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,32 +492,6 @@ def _do_join(self, event, context, room_hosts=None, do_auth=True):
"user_joined_room", user=user, room_id=room_id
)

@defer.inlineCallbacks
def _should_invite_join(self, room_id, prev_state, do_auth):
logger.debug("_should_invite_join: room_id: %s", room_id)

# XXX: We don't do an auth check if we are doing an invite
# join dance for now, since we're kinda implicitly checking
# that we are allowed to join when we decide whether or not we
# need to do the invite/join dance.

# Only do an invite join dance if a) we were invited,
# b) the person inviting was from a differnt HS and c) we are
# not currently in the room
room_host = None
if prev_state and prev_state.membership == Membership.INVITE:
room = yield self.store.get_room(room_id)
inviter = UserID.from_string(
prev_state.sender
)

is_remote_invite_join = not self.hs.is_mine(inviter) and not room
room_host = inviter.domain
else:
is_remote_invite_join = False

defer.returnValue((is_remote_invite_join, room_host))

@defer.inlineCallbacks
def get_joined_rooms_for_user(self, user):
"""Returns a list of roomids that the user has any of the given
Expand Down
4 changes: 0 additions & 4 deletions synapse/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
logger = logging.getLogger(__name__)


def _get_state_key_from_event(event):
return event.state_key


KeyStateTuple = namedtuple("KeyStateTuple", ("context", "type", "state_key"))


Expand Down
Loading

0 comments on commit 3011415

Please sign in to comment.