Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # prepare.py
- import os
- import shutil
- import importlib.util
- import glob
- import re
- import setuptools
- name = "pyunity"
- sep = "_SEP_"
- if importlib.util.find_spec("Cython") is None:
- os.system("pip install --pre -U cython")
- if os.path.isdir("src"):
- shutil.rmtree("src")
- sourcefiles = glob.glob(name + "/**/*.py", recursive=True)
- for file in sourcefiles:
- if file.endswith("__init__.py"):
- new = os.path.join("src", file[:-12].replace(os.path.sep, sep), "__init__.py")
- else:
- new = os.path.join("src", file.replace(os.path.sep, sep))
- print(new)
- os.makedirs(os.path.dirname(new), exist_ok=True)
- shutil.copy(file, new)
- os.system("cythonize -3 -q " + new)
- os.remove(new)
- with open(new[:-3] + ".c") as f:
- contents = f.read()
- contents = re.sub("init " + name + sep + "\\w+\"", "init " + new[4:-3].replace(sep, ".") + "\"", contents)
- contents = re.sub("init " + name + "\\w*\\.__init__\"", "init " + new[4:-12].replace(sep, ".") + "\"", contents)
- contents = contents.replace("\"%s\"," % new, "\"%s\"," % (new[4:].replace(sep, os.path.sep)))
- contents = contents.splitlines()
- while "#if !defined(CYTHON_NO_PYINIT_EXPORT) && (defined(_WIN32) || defined(WIN32) || defined(MS_WINDOWS))" in contents:
- index = contents.index("#if !defined(CYTHON_NO_PYINIT_EXPORT) && (defined(_WIN32) || defined(WIN32) || defined(MS_WINDOWS))")
- del contents[index: index + 3]
- with open(new[:-3] + ".c", "w") as f:
- f.write("\n".join(contents))
- os.makedirs("build", exist_ok=True)
- shutil.copy("bootstrap.py", os.path.join("build", "bootstrap.py"))
- with open(os.path.join("build", "bootstrap.py")) as f:
- lines = f.read().splitlines()
- packages = [package for package in setuptools.find_packages() if package.startswith(name)]
- lines[4] = "packages = [\"" + "\", \"".join(packages) + "\"]"
- print(packages)
- with open(os.path.join("build", "bootstrap.py"), "w") as f:
- f.write("\n".join(lines))
- os.system("cythonize -3 -q " + os.path.join("build", "bootstrap.py"))
- os.remove(os.path.join("build", "bootstrap.py"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement