Instantly share code, notes, and snippets.

Embed
What would you like to do?
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