Skip to content

Instantly share code, notes, and snippets.

@pianomanfrazier
Last active August 16, 2018 22:07
Show Gist options
  • Save pianomanfrazier/4455d53da26ddbb7dc9c2625d4220df8 to your computer and use it in GitHub Desktop.
Save pianomanfrazier/4455d53da26ddbb7dc9c2625d4220df8 to your computer and use it in GitHub Desktop.
Fuzzy search openstorefront permissions (run on WSL)
#!./venv/bin/python
import os, sys, glob, re
from fuzzywuzzy import fuzz
LOWER = 60
UPPER = 99
pattern = re.compile(r'".+?"|\'.+?\'')
permFile = "/mnt/c/dev/openstorefront/server/openstorefront/openstorefront-core/model/src/main/java/edu/usu/sdl/openstorefront/core/entity/SecurityPermission.java"
rootDir = "/mnt/c/dev/openstorefront/server/openstorefront"
ext = ("jsp","js")
def traverse(root, ext):
for root, dirs, files in os.walk(root):
for file in files:
if file.endswith(ext):
yield os.path.join(root, file)
def searchFile(file, pattern, lower, upper, tokens):
# print("Searching {}".format(file.name))
line_cnt = 0
for line in file:
line_cnt += 1
strings = pattern.findall(line)
if strings:
for s in strings:
s = s.strip("\"")
s = s.strip("\'")
for token in filter(lambda x: ' ' not in x and x.isupper(), tokens):
score = fuzz.ratio(token, s)
if score >= lower and score <= upper and s not in tokens and ('_' in s or '-' in s):
print("------------------")
print("Found fuzzy match between: (p)'{}' and (f)'{}'".format(token, s))
print("file: {}".format(file.name))
print("line: {}".format(line_cnt))
print("fuzzy score: {}".format(score))
print("------------------")
def getPermissions(file):
with open(file) as fout:
for line in fout:
strings = pattern.findall(line)
if strings:
for s in strings:
s = s.strip("\"")
s = s.strip("\'")
yield(s)
if __name__ == "__main__":
permissions = list(getPermissions(permFile))
for i in traverse(rootDir, ext):
if 'webjars' not in i and 'target' not in i and 'mobile' not in i and 'login' not in i:
with open(i) as file:
searchFile(file, pattern, LOWER, UPPER, permissions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment