Created
July 9, 2019 14:36
-
-
Save terhechte/c5a183bc572500d98570b155adecb575 to your computer and use it in GitHub Desktop.
Unobtrusive Project Language Distribution
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 ./count_loc.py [code-repository-folder] | |
# example: | |
# python ./count_loc.py ~/Development/our_code/ | |
# Will count the number of files as well as the total lines | |
# of code in your project for different types (cpp, m, h, swift) | |
# and print out a json blob with the answers. | |
import os, sys, json | |
path = None | |
try: | |
path = sys.argv[1] | |
except IndexError: | |
print "Usage: python ./code_distribution.py [path-to-source-folder]" | |
sys.exit() | |
class Entry: | |
name = "" | |
lines = 0 | |
files = 0 | |
count_lines = True | |
def __init__(self, name, count_lines = True): | |
self.name = name | |
counts = { | |
".swift": Entry("Swift"), | |
".m": Entry("Objective-C Implementation"), | |
".h": Entry("Header"), | |
".c": Entry("C Implementation"), | |
".cpp": Entry("C++"), | |
".mm": Entry("Objective-C++"), | |
".xib": Entry("Interface Builder", count_lines = False), | |
".storyboard": Entry("Storyboard", count_lines = False), | |
} | |
for root, dirs, files in os.walk(unicode(path)): | |
path = root.split(os.sep) | |
for file in files: | |
(_, ext) = os.path.splitext(file) | |
ext = ext.lower() | |
if not counts.has_key(ext): continue | |
if counts[ext].count_lines == True: | |
try: | |
counts[ext].lines += sum(1 for i in open(os.path.join(root, file), 'rb')) | |
except: | |
pass | |
counts[ext].files += 1 | |
print json.dumps(map(lambda x: {"type": x.name, "lines": x.lines, "files": x.files}, counts.values())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment