Last active
July 31, 2018 05:27
-
-
Save pedroburon/f3805c8e94515caeb2dac5d10f03dbd3 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
''' | |
usage: python list_top_level_modules.py | tr "\n" "\0" | xargs -n1 -0 -i{} git grep {} -- '*.py' | grep import | |
''' | |
import os | |
import importlib | |
import site | |
def main(): | |
site_package_dirs = site.getsitepackages() | |
for site_packages in site_package_dirs: | |
egg_infos = [dir_name for dir_name in os.listdir(site_packages) if dir_name.endswith('dist-info')] | |
for dir_name in egg_infos: | |
dir_path = os.path.join(site_packages, dir_name) | |
file_names = os.listdir(dir_path) | |
if 'top_level.txt' in file_names: | |
with open(os.path.join(dir_path, 'top_level.txt')) as f: | |
for module_name in [name.strip() for name in f.read().split('\n') if name.strip()]: | |
try: | |
importlib.import_module(module_name) | |
print(module_name) | |
except ImportError: | |
pass | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment