Created
April 30, 2015 15:16
-
-
Save athoune/39c9130fc80e6cad746e to your computer and use it in GitHub Desktop.
Find plugins in a Wordpress folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
import glob | |
import os.path | |
GARBAGE = re.compile("^(\s|[*#])+") | |
def headers(lines, keys=["Plugin Name", "Plugin URI", "Version", "Author", | |
"License", "Author URI", "Description", "Network"]): | |
state = None | |
h = dict() | |
for line in lines: | |
if state != 'comment': | |
if line.strip().startswith('/*'): | |
state = 'comment' | |
line = line[2:] | |
elif state == 'comment': | |
if line.strip().endswith('*/'): | |
state = None | |
line = line[:-2] | |
line = GARBAGE.sub("", line).rstrip() | |
if line != "": | |
kv = line.split(': ', 1) | |
if len(kv) == 2: | |
if kv[0] in keys: | |
h[kv[0]] = kv[1] | |
if len(h) == 0: | |
return None | |
return h | |
def plugins(folder): | |
for pattern in ['%s/*.php', '%s/*/*.php']: | |
for p in glob.glob(pattern % folder): | |
with open(p, 'r') as f: | |
h = headers(f) | |
if h is not None and 'Plugin Name' in h: | |
yield h | |
def all_plugins(web_root): | |
for name in ['plugins', 'mu-plugins']: | |
p = "%s/wp-content/%s" % (web_root, name) | |
if os.path.exists(p): | |
for plugin in plugins(p): | |
yield plugin | |
if __name__ == '__main__': | |
import sys | |
for plugin in all_plugins(sys.argv[1]): | |
print plugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment