Skip to content

Instantly share code, notes, and snippets.

@fragmede
Created February 3, 2014 12:16
Show Gist options
  • Save fragmede/8782876 to your computer and use it in GitHub Desktop.
Save fragmede/8782876 to your computer and use it in GitHub Desktop.
count used c files
#!/usr/bin/env python
import os
headers = 0
c_asm = 0
mf = 0
mod_c = 0
dir_hash = {}
def countlines(filename):
return sum(1 for i in open(path_filename).xreadlines())
#count all .h files 'cause
files = os.popen("find . -name '*.h' | grep -v [.]git").read().strip().split('\n')
for path_filename in files:
#print path_filename
headers += countlines(path_filename)
#count .c/S corresponding source files
files = os.popen("find . -name '*.o' | grep -v [.]git").read().strip().split('\n')
for path_filename in files:
path = os.path.dirname(path_filename)
filename = os.path.basename(path_filename)
slug = filename[:filename.rfind('.')]
c_path_filename = os.path.join(path,slug+'.c')
if os.path.exists(c_path_filename):
c_asm += countlines(c_path_filename)
S_path_filename = os.path.join(path,slug+'.S')
if os.path.exists(S_path_filename):
c_asm += countlines(S_path_filename)
mod_c_filename = os.path.join(path,slug+'.mod.c')
if os.path.exists(mod_c_filename):
mod_c += countlines(mod_c_filename)
# Count makefile and Kconfig while we're at it
if path in dir_hash:
continue
dir_hash[path] = 1
mf_filename = os.path.join(path,'Makefile')
if os.path.exists(mf_filename):
mf += countlines(mf_filename)
kc_filename = os.path.join(path,'Kconfig')
if os.path.exists(kc_filename):
mf += countlines(kc_filename)
#Count root makefile
mf += countlines('Makefile')
print 'headers:', headers
print 'c/asm source:', c_asm
print 'generated .mod.c:', mod_c
print 'makefile/kconfig:', mf
print 'total', headers + c_asm + mod_c + mf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment