Skip to content

Instantly share code, notes, and snippets.

@apg
Created September 7, 2011 19:56
Show Gist options
  • Save apg/1201555 to your computer and use it in GitHub Desktop.
Save apg/1201555 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""This script goes through a bunch of code and determines whether
or not there is the potential for a problem because we're using
an assignment directly rather than a setter. It's pretty specific
to the Meetup entities framework.
"""
from __future__ import with_statement
import sys
import os
import re
ENT_DIR = '/usr/local/meetup/src/com/meetup/db/entity/'
ADP_DIR = '/usr/local/meetup/src/com/meetup/db/adapter/'
ENT_EXT = re.compile('^\s+@Expose protected [a-zA-Z]+ ([0-9a-zA-Z_]+);')
def extract_fields(ent):
fields = []
with open(ent) as f:
for l in f.xreadlines():
m = ENT_EXT.match(l)
if m:
fields.append(m.group(1))
return fields
def fields_regexp(fields):
"""Regexp to see if `field =` exists outside of a String"""
r = '^([^ ]*?)(?:this\.)?(?:%s)\s+=[^=]' % '|'.join(fields)
return re.compile(r)
INVALID_PRE = [
'"',
# field attributes indicators
'private',
'protected',
'public',
'final',
'sql',
]
def check_adapter(adp, reg):
maybes = []
bname = os.path.basename(adp)
with open(adp) as f:
i = 1
for l in f.xreadlines():
m = reg.search(l)
if m:
pre = m.group(1)
if pre.strip():
for n in INVALID_PRE:
# print "checking to see if %s is in %s" % (n, pre)
if n in pre:
break
maybes.append('\tPossible match: %s line %d\n\t\t%s' % (bname, i, l.strip()))
i += 1
if maybes:
print 'Found in %s:' % adp
print '\n'.join(maybes)
def to_adapter(ent):
bits = ent.split('.')
if len(bits) == 2 and bits[1] == 'java':
return os.path.join(ADP_DIR, '%sAdapter.%s' % tuple(bits))
return None
def do_file(ent):
bname = os.path.basename(ent)
apath = to_adapter(bname)
fre = fields_regexp(
extract_fields(os.path.join(ENT_DIR, bname)))
check_adapter(apath, fre)
if __name__ == '__main__':
files = set()
for f in sys.argv[1:]:
files.add(f.replace('Proto.', '.').replace('Entity.', '.'))
for f in files:
do_file(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment