ptone (owner)

Revisions

gist: 225231 Download_button fork
public
Public Clone URL: git://gist.github.com/225231.git
Embed All Files: show embed
graph_pinax.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/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()
 
graph_pinax_viz.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/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()