Advertisement
Guest User

prepare.py

a guest
Sep 2nd, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. # prepare.py
  2. import os
  3. import shutil
  4. import importlib.util
  5. import glob
  6. import re
  7. import setuptools
  8.  
  9. name = "pyunity"
  10. sep = "_SEP_"
  11.  
  12. if importlib.util.find_spec("Cython") is None:
  13.     os.system("pip install --pre -U cython")
  14.  
  15. if os.path.isdir("src"):
  16.     shutil.rmtree("src")
  17.  
  18. sourcefiles = glob.glob(name + "/**/*.py", recursive=True)
  19. for file in sourcefiles:
  20.     if file.endswith("__init__.py"):
  21.         new = os.path.join("src", file[:-12].replace(os.path.sep, sep), "__init__.py")
  22.     else:
  23.         new = os.path.join("src", file.replace(os.path.sep, sep))
  24.     print(new)
  25.     os.makedirs(os.path.dirname(new), exist_ok=True)
  26.     shutil.copy(file, new)
  27.     os.system("cythonize -3 -q " + new)
  28.     os.remove(new)
  29.  
  30.     with open(new[:-3] + ".c") as f:
  31.         contents = f.read()
  32.    
  33.     contents = re.sub("init " + name + sep + "\\w+\"", "init " + new[4:-3].replace(sep, ".") + "\"", contents)
  34.     contents = re.sub("init " + name + "\\w*\\.__init__\"", "init " + new[4:-12].replace(sep, ".") + "\"", contents)
  35.     contents = contents.replace("\"%s\"," % new, "\"%s\"," % (new[4:].replace(sep, os.path.sep)))
  36.    
  37.     contents = contents.splitlines()
  38.     while "#if !defined(CYTHON_NO_PYINIT_EXPORT) && (defined(_WIN32) || defined(WIN32) || defined(MS_WINDOWS))" in contents:
  39.         index = contents.index("#if !defined(CYTHON_NO_PYINIT_EXPORT) && (defined(_WIN32) || defined(WIN32) || defined(MS_WINDOWS))")
  40.         del contents[index: index + 3]
  41.  
  42.     with open(new[:-3] + ".c", "w") as f:
  43.         f.write("\n".join(contents))
  44.  
  45. os.makedirs("build", exist_ok=True)
  46. shutil.copy("bootstrap.py", os.path.join("build", "bootstrap.py"))
  47.  
  48. with open(os.path.join("build", "bootstrap.py")) as f:
  49.     lines = f.read().splitlines()
  50. packages = [package for package in setuptools.find_packages() if package.startswith(name)]
  51. lines[4] = "packages = [\"" + "\", \"".join(packages) + "\"]"
  52. print(packages)
  53. with open(os.path.join("build", "bootstrap.py"), "w") as f:
  54.     f.write("\n".join(lines))
  55.  
  56. os.system("cythonize -3 -q " + os.path.join("build", "bootstrap.py"))
  57. os.remove(os.path.join("build", "bootstrap.py"))
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement