Minecraft-Overviewer/contribManager.py

79 lines
2.2 KiB
Python
Raw Normal View History

2019-03-18 17:52:32 +00:00
#!/usr/bin/env python3
2011-08-17 00:52:43 +00:00
2019-03-14 12:30:22 +00:00
"""The contrib manager is used to help control the contrib scripts
that are shipped with overviewer in Windows packages."""
2011-08-17 00:52:43 +00:00
import ast
import os.path
import sys
2019-03-14 12:30:22 +00:00
scripts = { # keys are names, values are scripts
"convertCyrillic": "cyrillic_convert.py",
"playerInspect": "playerInspect.py",
"testRender": "testRender.py",
"pngit": "png-it.py",
"gallery": "gallery.py",
"regionTrimmer": "regionTrimmer.py",
"contributors": "contributors.py"
}
2011-08-17 00:52:43 +00:00
# you can symlink or hardlink contribManager.py to another name to have it
# automatically find the right script to run. For example:
2019-04-06 18:12:19 +00:00
# > ln -s contribManager.py pngit.exe
# > chmod +x pngit.exe
# > ./pngit.exe -h
2011-08-17 00:52:43 +00:00
# figure out what script to execute
2019-03-14 12:30:22 +00:00
argv = os.path.basename(sys.argv[0])
2011-08-17 00:52:43 +00:00
if argv[-4:] == ".exe":
2019-03-14 12:30:22 +00:00
argv = argv[0:-4]
2011-08-17 00:52:43 +00:00
if argv[-3:] == ".py":
2019-03-14 12:30:22 +00:00
argv = argv[0:-3]
2011-08-17 00:52:43 +00:00
2019-03-14 12:30:22 +00:00
usage = """Usage:
%s --list-contribs | <script name> <arguments>
2019-03-14 12:30:22 +00:00
Executes a contrib script.
Options:
--list-contribs Lists the supported contrib scripts
""" % os.path.basename(sys.argv[0])
2011-08-17 00:52:43 +00:00
if argv in scripts.keys():
script = scripts[argv]
sys.argv[0] = script
2011-08-17 00:52:43 +00:00
else:
if "--list-contribs" in sys.argv:
for contrib in scripts.keys():
# use an AST to extract the docstring for this module
script = scripts[contrib]
2019-03-14 12:30:22 +00:00
with open(os.path.join("contrib", script)) as f:
d = f.read()
2019-03-14 12:30:22 +00:00
node = ast.parse(d, script)
docstring = ast.get_docstring(node)
if docstring:
docstring = docstring.strip().splitlines()[0]
else:
2019-03-14 12:30:22 +00:00
docstring = "(No description found. Add one by adding a docstring to %s.)" % script
print("%s : %s" % (contrib, docstring))
sys.exit(0)
if len(sys.argv) > 1 and sys.argv[1] in scripts.keys():
2011-08-17 00:52:43 +00:00
script = scripts[sys.argv[1]]
sys.argv = [script] + sys.argv[2:]
2011-08-17 00:52:43 +00:00
else:
print(usage, file=sys.stderr)
2011-08-17 00:52:43 +00:00
sys.exit(1)
torun = os.path.join("contrib", script)
if not os.path.exists(torun):
print("Script '%s' is missing!" % script, file=sys.stderr)
sys.exit(1)
2019-03-18 17:52:32 +00:00
exec(compile(open(torun, "rb").read(), torun, 'exec'))