Created
September 20, 2016 14:46
-
-
Save gary-liguoliang/10260cacf4d4a372f2528885067e82bf to your computer and use it in GitHub Desktop.
count Java import classes
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
""" | |
Copyright 2015 Guoliang Li | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
""" | |
import collections | |
import os | |
def get_java_source_files(source_folder): | |
java_source_files = list() | |
for root, dirs, files in os.walk(source_folder): | |
for f in files: | |
if f.endswith(".java"): | |
java_source_files.append(os.path.join(root, f)) | |
return java_source_files | |
def count_imports(source_folder): | |
import_counts = dict() | |
for source_file in get_java_source_files(source_folder): | |
with open(source_file) as f: | |
for l in f.readlines(): | |
if l.startswith("import "): | |
klass = l[len("import"):].strip()[:-1] | |
import_counts[klass] = import_counts.get(klass, 0) + 1 | |
return collections.OrderedDict(sorted(import_counts.items())) | |
if __name__ == '__main__': | |
source_folder = raw_input("source folder: ").strip() | |
import_class_count = count_imports(source_folder) | |
for (klass, count) in import_class_count.items(): | |
print klass, count | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment