Skip to content

Instantly share code, notes, and snippets.

@ptone
Created November 3, 2009 17:14
Show Gist options
  • Save ptone/225231 to your computer and use it in GitHub Desktop.
Save ptone/225231 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Preston Holmes on 2009-10-28.
preston@ptone.com
Copyright (c) 2009
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from __future__ import with_statement
import sys
import os
import re
from contextlib import closing
from sets import Set
from modulefinder import ModuleFinder
# import pygraphviz as pgv
import simplejson as json
import pprint
applist = (
# external
'notification', # must be first
'django_openid',
'emailconfirmation',
# 'django_extensions',
'robots',
'friends',
'mailer',
'messages',
'announcements',
'oembed',
'djangodblog',
'pagination',
# 'gravatar',
'threadedcomments',
'threadedcomments_extras',
'wiki',
'swaps',
'timezones',
'voting',
'voting_extras',
'tagging',
'bookmarks',
'blog',
'ajax_validation',
'photologue',
'avatar',
'flag',
'microblogging',
'locations',
'uni_form',
'django_sorting',
'django_markup',
'staticfiles',
# internal (for now)
'analytics',
'profiles',
'account',
'signup_codes',
'tribes',
'photos',
'tag_app',
'topics',
'groups',
)
env_dir = '/Users/preston/Projects/Python/virtualenvs/pinax-env/lib/python2.5/site-packages/'
dirs = [env_dir]
pinax_dir = os.path.join(env_dir,'pinax','apps')
dirs.append (pinax_dir)
dirs.append (os.path.join(env_dir,'pinax','projects','social_project','apps'))
# in case one is not running this tool inside the pinax env
for p in dirs:
sys.path.append(p)
def get_py_files(start_dir):
"""lists all py files"""
file_list = []
for root, dirs, files in os.walk(start_dir):
for f in files:
if f.lower().endswith('py'):
file_list.append(os.path.join(root,f))
return file_list
# def find_usage(app):
# global files
# usage_set = Set()
# patterns = ['from %s\S*\ import', 'import\ \S*%s']
# for f in files:
# finder.run_script(f)
# if len(finder.modules):
# mods = finder.modules.keys()
# related_apps = [app for app in mods if app in applist]
# if related_apps:
# used_by = f.replace(env_dir,'').lstrip('/').split('/')[0]
# usage_set.add (used_by)
def check_explicit(app,file):
for line in open(file):
if 'import' in line and app in line:
return True
return False
def main():
references = {}
for app_pkg in applist:
app_path = ''
for path in dirs:
# sys.stderr.write (str(pprint.pprint(os.listdir(path))))
app_pkg_path = os.path.join(path,app_pkg.replace('.','/'))
if os.path.exists(app_pkg_path):
app_path = app_pkg_path
current_path = path
break
if not app_path:
raise ('unable to find %s on path' % app_pkg)
files = get_py_files (app_path)
for f in files:
sys.stderr.write(f + '\n')
# skip tests
if 'test' in f: continue
finder = ModuleFinder()
finder.run_script(f)
app_file = f.replace(current_path,'').replace(app_pkg,'').lstrip('/')
if len(finder.modules):
mods = list(finder.modules.keys())
# filter to only apps from applist
related_apps = [app for app in mods if app in applist]
if related_apps:
if app_pkg not in references:
references[app_pkg] = {}
for app in related_apps:
if app == app_pkg or 'django.' in app: continue
if check_explicit(app,f):
if app in references[app_pkg]:
references[app_pkg][app].append(app_file)
else:
references[app_pkg][app] = [app_file]
sys.stderr.write('------------app: %s file: %s mod: %s\n' % (app_pkg, app_file, app))
print json.dumps(references)
if __name__ == '__main__':
main()
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Preston Holmes on 2009-11-02.
preston@ptone.com
Copyright (c) 2009
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import os
import pygraphviz as pgv
import simplejson as json
graph_data = '/Users/preston/Desktop/temp/pinax_graph.json'
def main():
graph = pgv.AGraph(directed=False)
data = json.load(open(graph_data))
# k = data.keys()[0]
for k in data.keys():
first_order_apps = data[k].keys()
for n in first_order_apps:
graph.add_edge(k,n)
graph.draw("test.png",prog="circo")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment