Skip to content

Instantly share code, notes, and snippets.

@GeeWee
Created May 1, 2018 07:05
Show Gist options
  • Save GeeWee/ff613830eff0da7b812c3451c6ebc35c to your computer and use it in GitHub Desktop.
Save GeeWee/ff613830eff0da7b812c3451c6ebc35c to your computer and use it in GitHub Desktop.
import os
import collections
fileImports = []
def getImports(file):
with open(file) as file:
fileContents = file.readlines();
# See if line contains import and the "." character - that (probably) means it's a relative import.
# Imports are not case sensitive on all platforms, so to aggregate them, we make them all lower case
importStrings = [line.lower() for line in fileContents if 'import' in line if '.' in line]
# Get the path to the file
importStrings = [line.split("'")[-2] for line in importStrings]
# Only get the name
importStrings = [line.split("/")[-1] for line in importStrings]
if (importStrings):
# If not empty - add it to the global list
fileImports.extend(importStrings)
#Walk through all files and get the imports
for root, dirs, files in os.walk("./src"):
path = root.split(os.sep)
for file in files:
getImports(os.path.join(root, file));
counter=collections.Counter(fileImports)
print(counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment