From 5f57cf955c1b9089854337b2df8b8fb6af83c831 Mon Sep 17 00:00:00 2001 From: LaChal Date: Fri, 15 Sep 2017 22:00:55 +0200 Subject: [PATCH] Reworked the NBT and PND build script This script no require CLI arguments. This helps to build all or only one library. README.md updated accordingly. --- README.md | 49 ++++++++++++++++++++++------------- setup.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 38ad25c16..ff3657366 100644 --- a/README.md +++ b/README.md @@ -89,32 +89,45 @@ Linux users must install python-xlib 0.14 (or over). It may be possible that the You should now be able to run MCEdit-Unified with `python mcedit.py` assuming you've installed all the dependencies correctly. On Linux, it is recommended to use the `mcedit.sh` shell script. -## INSTALLING _nbt.pyx +## BUILDING NBT AND PNG SUPPORT -pymclevel contains a cython version of _nbt. This one is a lot faster, but has to be build manually. -It requires cython 0.21.2 (Note: there are issues with 0.22) +__Please, mind to adapt the following information according to your operating system!__ -```sh -pip install cython==0.21.2 -``` +### DEPENDENCIES -It's also worth upgrading setuptools: +To build these libaries, you'll need _Cython 0.21_ and _setuptools_. -```sh -pip install setuptools --upgrade -``` +First, install or update _setuptools_: -With cython you should be able to build the file. +* Download [get-pypi.py](https://bootstrap.pypa.io/get-pip.py) +* Run it: `python pypi.py` -```sh -python setup.py develop -``` +Then, install Cython: -If no errors occured, only thing left to do is see if it worked correctly: +`pip install cython==0.21.2` + + +### SETUP SCRIPT + +This script is intended to be run in a shell opened in MCEdit-Unified folder. + +It takes arguments on the command line to work correctly. +Invoke it like this: + +`python setup.py [argument [argument [...]]]` + +Without argument, it will fail. (And let you know...) + + +Use the `all` argument to build all the libraries the script can handle. +The `nbt` one will build only the NBT support. +The `png` one will build only the PNG support. +The `help` one can, ehhh, help... + +After the NBT support is built, you can run a very simple test: + +`python setuptest.py` -```sh -python setuptest.py -``` ### Pocket Edition Support diff --git a/setup.py b/setup.py index 3cb04bd36..3d1ed76b1 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,7 @@ +import sys +import os +import platform +import distutils.file_util from setuptools import setup from Cython.Build import cythonize @@ -5,8 +9,77 @@ import Cython.Compiler.Options Cython.Compiler.Options.annotate = True -ext_modules = cythonize(["cpngfilters.pyx", "pymclevel/_nbt.pyx"]) +modules_map = { + "png": {"source": "cpngfilters.pyx", + "description": "Build the accelerator to work with PNG images."}, + "nbt": {"source": "pymclevel/_nbt.pyx", + "description": "Build the accelerator to work with NBT data."} + } + +__help__ = """setup.py +Build Cython extensions for MCEdit-Unified. + +You have to use at least one argument on the command line to build extensions. + +Valid arguments are: + +help Print this message. +all Build all extensions. +%s +-- Makes this script send all remaing arguments to setuptool. + +In case setuptools commands like 'build' or 'install are given after a doule +dash ('--'), you can expect unwanted behaviour, because the 'build_ext' and + '--inplace' are forced (added dynamicaly on the command line). +""" % "\n".join(["%s %s" % (k, v["description"]) for k, v in modules_map.items()]) + +# Let people choose what to build. +# If no argument is given on the command line, display help message. +# If a wrong argument is given, break. +if len(sys.argv) == 1: + print __help__ + sys.exit(0) +else: + ext_modules = [] + args = sys.argv[1:] + msg = "Following extensions will be built: %s." + ext_list = [] + for arg in args: + # Let send setuptools argument be on the command line. + print arg + if arg == '--': + sys.argv.remove(arg) + break + if not arg.startswith('-'): + if arg == 'help': + print __help__ + sys.exit(0) + elif arg == 'all': + ext_list = list(modules_map.keys()) + ext_modules = [v["source"] for v in modules_map.values()] + elif arg not in modules_map.keys(): + print "'%s' is not a valid argument. Use 'help' one for information." % arg + sys.exit(1) + else: + src = modules_map[arg]["source"] + if src not in ext_modules: + ext_list.append(arg) + ext_modules.append(src) + sys.argv.remove(arg) + print msg % ", ".join(ext_list) + +sys.argv.insert(1, '--inplace') +sys.argv.insert(1, 'build_ext') setup( - ext_modules=ext_modules + ext_modules=cythonize(ext_modules) ) + +# On Linux, we want _nbt.so in pymclevel. +if platform.system() == "Linux": + if os.path.isfile("_nbt.so"): + nbt_dest = os.path.join("pymclevel", "_nbt.so") + if os.path.isfile(nbt_dest): + os.remove(nbt_dest) + distutils.file_util.move_file("_nbt.so", nbt_dest) +