Skip to content

Instantly share code, notes, and snippets.

@terhechte
Created July 9, 2019 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terhechte/c5a183bc572500d98570b155adecb575 to your computer and use it in GitHub Desktop.
Save terhechte/c5a183bc572500d98570b155adecb575 to your computer and use it in GitHub Desktop.
Unobtrusive Project Language Distribution
# 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