Created
February 15, 2015 21:29
-
-
Save nicwest/4f4791f5a9c975064455 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jedi | |
import fnmatch | |
import os | |
import json | |
DIRNAME = 'django-1.7.4/django' | |
def get_filepaths(): | |
filepaths = [] | |
for root, dirs, filenames in os.walk(DIRNAME): | |
for filename in fnmatch.filter(filenames, '*.py'): | |
if os.path.basename(root) in ['migrations', 'locale', 'tests']: | |
continue | |
filepaths.append(os.path.join(root, filename)) | |
return filepaths | |
def make_import_path(full_name, module_python_path): | |
if full_name.startswith('django.') or full_name == 'django': | |
return full_name | |
return '{path}.{name}'.format(path=module_python_path, name=full_name) | |
def make_module_python_path(module_path): | |
abspath = os.path.abspath(module_path) | |
absdir = os.path.abspath(DIRNAME) | |
is_magic = os.path.basename(module_path).startswith('__') | |
if is_magic: | |
abspath = os.path.dirname(abspath) | |
relpath = os.path.dirname(os.path.relpath(abspath, absdir)) | |
if relpath in ['.', '..']: | |
relpath = '' | |
return 'django{dot}{path}'.format( | |
dot='.' if relpath else '', | |
path=relpath.replace(os.path.sep, '.')) | |
def get_defs(filepath): | |
defs = jedi.api.names(path=filepath) | |
module_python_path = make_module_python_path(filepath) | |
items = [(d.name, make_import_path(d.full_name, module_python_path), filepath) | |
for d in defs | |
if d.is_definition or filepath[-11:] == '__init__.py'] | |
for item in defs: | |
del item | |
return items | |
if __name__ == '__main__': | |
items = {} | |
filepaths = get_filepaths() | |
total_files = len(filepaths) | |
for i, fp in enumerate(filepaths): | |
print i, total_files, int((float(i) / float(total_files)) * 100), '%' | |
defs = get_defs(fp) | |
for name, path, source in defs: | |
if name in items: | |
items[name].append(path) | |
else: | |
items[name] = [path] | |
del defs | |
with open('django-1.7.4.json', 'w') as fh: | |
json.dump(items, fh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment