Created
July 5, 2013 22:18
-
-
Save s-zeid/5937592 to your computer and use it in GitHub Desktop.
List CraftBukkit plugin names and versions. Depends on <http://code.s.zeid.me/bin/src/master/cb>.
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 os | |
import re | |
import subprocess | |
import sys | |
from collections import OrderedDict | |
CB_CMD = ["cb"] | |
def list_plugins(): | |
p = subprocess.Popen(CB_CMD + ["plugins"], stdout=subprocess.PIPE) | |
r = p.wait() | |
if r != 0: | |
raise RuntimeError("process returned exit code %d", r) | |
stdout = p.communicate()[0] | |
plugins_match = re.match(r"^Plugins \([0-9]+\): ((?:[^,]+, )*[^,]+)$", | |
strip_ansi_codes(stdout)) | |
if not plugins_match: | |
raise RuntimeError("process returned invalid output:\n\n%s", stdout) | |
return sorted((i.strip() for i in plugins_match.groups()[0].split(", ")), | |
key=lambda s: s.lower()) | |
def plugin_version(plugin): | |
plugin = strip_ansi_codes(plugin) | |
p = subprocess.Popen(CB_CMD + ["version", plugin], stdout=subprocess.PIPE) | |
r = p.wait() | |
if r != 0: | |
raise RuntimeError("process returned exit code %d", r) | |
stdout = p.communicate()[0] | |
return re.sub(r"^[^ ]+ version ", "", strip_ansi_codes(stdout).splitlines()[0]) | |
def plugin_versions(): | |
versions = OrderedDict() | |
for plugin in list_plugins(): | |
versions[plugin] = plugin_version(plugin) | |
return versions | |
def strip_ansi_codes(s): | |
return re.sub("\x1b\\[[0-9;]*[mK]", "", s) | |
def main(argv): | |
ansi = os.isatty(sys.stdout.fileno()) or (len(argv) > 1 and argv[1] in ("-a", "--ansi")) | |
fmt = "\x1b[0m%s\x1b[0m \x1b[0;1m%s\x1b[0m" if ansi else "%s %s" | |
try: | |
versions = plugin_versions() | |
for plugin in versions: | |
print fmt % (plugin, versions[plugin]) | |
except RuntimeError as exc: | |
print >> sys.stderr, "error:", exc | |
if __name__ == "__main__": | |
try: | |
sys.exit(main(sys.argv)) | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment