Skip to content

Instantly share code, notes, and snippets.

@corbinbs
Created January 14, 2011 22:51
Show Gist options
  • Save corbinbs/780445 to your computer and use it in GitHub Desktop.
Save corbinbs/780445 to your computer and use it in GitHub Desktop.
Python snippet to scan a directory for Drools rule files and report on the total number of rules found. Probably could expand this to count other things...
import re
import os
def count_rules(dir, exts=['.drl', '.dslr']):
rulecount = 0
for root, dirs, files in os.walk(dir):
for file in files:
if len([file for ext in exts if file.endswith(ext)]) > 0:
filename = os.path.join(root, file)
f = open(filename, "r")
filetext = "".join(f.readlines())
f.close()
rules = re.findall('rule ".+?".+?\nend', filetext, re.DOTALL)
rulecount += len(rules)
return rulecount
#Call count_rules with the parent directory that contains all of the rules...
print count_rules('/tmp/testrules')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment