Skip to content

Instantly share code, notes, and snippets.

@elin-moco
Created August 5, 2014 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elin-moco/b2aa9d0c0f8018318750 to your computer and use it in GitHub Desktop.
Save elin-moco/b2aa9d0c0f8018318750 to your computer and use it in GitHub Desktop.
Calculate media query percentage in project
import os
import re
CSS_DIR = 'media/css/'
# CSS_PATTERN = re.compile('.+.less$')
# CSS_PATTERN = re.compile('.+.css$')
CSS_PATTERN = re.compile('.+.-all.css$')
total_files = 0
total_loc = 0
total_lomq = 0
def mq_stats(css):
loc = 0
lomq = 0
ismq = False
while True:
line = css.readline()
if line:
if line.startswith('@media'):
ismq = True
elif line.startswith('}'):
ismq = False
if ismq:
lomq += 1
loc += 1
else:
break
return loc, lomq
for root, folders, files in os.walk(CSS_DIR):
for file in files:
if CSS_PATTERN.match(file) is not None:
with open(root + '/' + file, 'r') as css:
css_loc, css_lomq = mq_stats(css)
if css_lomq > 0:
total_files += 1
total_loc += css_loc
total_lomq += css_lomq
print '(%d/%d) %s' % (css_lomq, css_loc, file)
print 'Total %d files, media query LOC: %.2f%% (%d/%d)' % (total_files, float(total_lomq)/total_loc*100, total_lomq, total_loc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment