Skip to content

Instantly share code, notes, and snippets.

@chkumar246
Last active October 3, 2016 12:39
Show Gist options
  • Select an option

  • Save chkumar246/29eb4d28a85c5837b48ee574c60a71f6 to your computer and use it in GitHub Desktop.

Select an option

Save chkumar246/29eb4d28a85c5837b48ee574c60a71f6 to your computer and use it in GitHub Desktop.

Revisions

  1. chkumar246 revised this gist Oct 3, 2016. 1 changed file with 16 additions and 4 deletions.
    20 changes: 16 additions & 4 deletions findimports.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,12 @@
    # Script to find all third party imports from python projects
    # How to Run
    # sudo pip install find-import virtualenv
    # sudo pip install find-import virtualenv pymod2pkg
    # python findimports.py <path to python code repository>
    # Author: Chandan Kumar <[email protected]>

    import pymod2pkg
    import os
    import shutil
    import subprocess
    import shlex
    import sys
    @@ -52,21 +54,31 @@ def check_import(modules):
    virtualenv.create_environment(path, clear=True)
    venv_path = os.path.join(path, 'bin', 'python')
    for module in modules:
    # FIX ME corner case for import import
    if module != 'import':
    # FIX ME corner case for import import and import the statement
    if not module in ['import', 'the']:
    cmd = shlex.split("%s -c 'try:\n import %s\n print('True')\nexcept ImportError: print('False')'" % (venv_path, module))
    out = subprocess.check_output(cmd)
    if 'False'in out.split():
    third_party_imports.append(module)

    # Remove virtualenv
    shutil.rmtree(path)
    return third_party_imports

    def list_third_party_pkgs(third_party_imports):
    """
    List all third party packages from imports
    """
    pkgs = [pymod2pkg.module2package(module, pymod2pkg.platform.linux_distribution()[0]) for module in third_party_imports]
    return pkgs

    if __name__ == "__main__":
    if len(sys.argv) > 1:
    if os.path.exists(sys.argv[1]):
    files = list_all_pyfiles(sys.argv[1])
    imports = get_all_imports(files)
    third_party_imports = check_import(imports)
    print('\n'.join(third_party_imports))
    pkgs = list_third_party_pkgs(third_party_imports)
    print('\n'.join(pkgs))
    else:
    print("Path Not found.")
  2. chkumar246 created this gist Oct 3, 2016.
    72 changes: 72 additions & 0 deletions findimports.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    # Script to find all third party imports from python projects
    # How to Run
    # sudo pip install find-import virtualenv
    # python findimports.py <path to python code repository>
    # Author: Chandan Kumar <[email protected]>

    import os
    import subprocess
    import shlex
    import sys
    import virtualenv
    import tempfile

    def list_all_pyfiles(path):
    """
    list down all .py files with complete path
    """
    files_path = []
    for root, subdir, files in os.walk(path):
    for file in files:
    if file.endswith('.py'):
    files_path.append(os.path.join(root, file))
    return files_path

    def get_imports(filepath):
    """
    Returns a list of all imports
    """
    output = subprocess.check_output(['find-import', filepath])
    if output.split():
    return output.split()

    def get_all_imports(file_list):
    """
    Return a dict of all imports with all imports
    {'file_path':[a, b, c, d]}
    """
    imports, import_list = {}, []
    for file in file_list:
    if get_imports(file) != None:
    imports.update({file: get_imports(file)})
    for data in imports.values():
    import_list.extend(data)
    return list(set(import_list))

    def check_import(modules):
    """
    Find thirdparty modules
    """
    third_party_imports = []
    path = tempfile.mkdtemp()
    virtualenv.create_environment(path, clear=True)
    venv_path = os.path.join(path, 'bin', 'python')
    for module in modules:
    # FIX ME corner case for import import
    if module != 'import':
    cmd = shlex.split("%s -c 'try:\n import %s\n print('True')\nexcept ImportError: print('False')'" % (venv_path, module))
    out = subprocess.check_output(cmd)
    if 'False'in out.split():
    third_party_imports.append(module)

    return third_party_imports

    if __name__ == "__main__":
    if len(sys.argv) > 1:
    if os.path.exists(sys.argv[1]):
    files = list_all_pyfiles(sys.argv[1])
    imports = get_all_imports(files)
    third_party_imports = check_import(imports)
    print('\n'.join(third_party_imports))
    else:
    print("Path Not found.")