Skip to content

Commit

Permalink
removing openw class
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11owSub committed Sep 13, 2024
1 parent 401a386 commit b0da6a5
Showing 1 changed file with 63 additions and 112 deletions.
175 changes: 63 additions & 112 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

import argparse
import glob
import io as cStringIO
import os
import pathlib
import re
import shutil
import sys
import sysconfig
import time
from collections import defaultdict
from itertools import chain
from pathlib import Path
from subprocess import PIPE, Popen

import numpy
Expand All @@ -26,7 +26,7 @@
from setuptools.command.install import install

# non-empty DEBUG variable turns off optimization and adds -g flag
DEBUG = bool(os.getenv("DEBUG", ""))
DEBUG = os.getenv("DEBUG", False)
WIN = sys.platform.startswith("win")
MAC = sys.platform.startswith("darwin")

Expand All @@ -47,76 +47,39 @@ def create_all(generated_dir, pymoldir="."):
create_buildinfo(generated_dir, pymoldir)


class openw(object):
"""
File-like object for writing files. File is actually only
written if the content changed.
"""

def __init__(self, filename):
if os.path.exists(filename):
self.out = cStringIO.StringIO()
self.filename = filename
else:
os.makedirs(os.path.dirname(filename), exist_ok=True)
self.out = open(filename, "w")
self.filename = None

def close(self):
if self.out.closed:
return
if self.filename:
with open(self.filename) as handle:
oldcontents = handle.read()
newcontents = self.out.getvalue()
if oldcontents != newcontents:
self.out = open(self.filename, "w")
self.out.write(newcontents)
self.out.close()

def __getattr__(self, name):
return getattr(self.out, name)

def __enter__(self):
return self

def __exit__(self, *a, **k):
self.close()

def __del__(self):
self.close()


def create_shadertext(shaderdir, shaderdir2, outputheader, outputfile):
outputheader = openw(outputheader)
outputfile = openw(outputfile)
def create_shadertext(
shader_dir: str,
shader_dir2: str,
output_header_file_name: str,
output_file_name: str,
):
varname = "_shader_cache_raw"

include_deps = defaultdict(set)
ifdef_deps = defaultdict(set)
shader_dir_path = Path(shader_dir)
shader_dir2_path = Path(shader_dir2)
extension_regexp = "*.[gs][vs][fs][shared][tsc][tse]"

# get all *.gs *.vs *.fs *.shared from the two input directories
shaderfiles = set()
for sdir in [shaderdir, shaderdir2]:
for ext in ["gs", "vs", "fs", "shared", "tsc", "tse"]:
shaderfiles.update(
map(os.path.basename, sorted(glob.glob(os.path.join(sdir, "*." + ext))))
)

varname = "_shader_cache_raw"
outputheader.write("extern const char * %s[];\n" % varname)
outputfile.write("const char * %s[] = {\n" % varname)

for filename in sorted(shaderfiles):
shaderfile = os.path.join(shaderdir, filename)
if not os.path.exists(shaderfile):
shaderfile = os.path.join(shaderdir2, filename)
shaderfiles = set(
chain(
shader_dir_path.glob(extension_regexp),
shader_dir2_path.glob(extension_regexp),
)
)

with open(shaderfile, "r") as handle:
contents = handle.read()
with (
open(output_header_file_name, "w") as output_header,
open(output_file_name, "w") as output_file,
):
output_header.write(f"extern const char * {varname}[];\n")
output_file.write(f"const char * {varname}[] = {{\n")

if True:
outputfile.write('"%s", ""\n' % (filename))
for filename in shaderfiles:
output_file.write(f'"{filename.name}", ""\n')

contents = filename.read_text()
for line in contents.splitlines():
line = line.strip()

Expand All @@ -125,52 +88,43 @@ def create_shadertext(shaderdir, shaderdir2, outputheader, outputfile):
continue

# write line, quoted, escaped and with a line feed
outputfile.write(
'"%s\\n"\n' % line.replace("\\", "\\\\").replace('"', r"\"")
)
escaped_line = line.replace("\\", "\\\\").replace('"', r"\"")
output_file.write(f'"{escaped_line}\\n"\n')

# include and ifdef dependencies
if line.startswith("#include"):
include_deps[line.split()[1]].add(filename)
elif line.startswith("#ifdef") or line.startswith("#ifndef"):
ifdef_deps[line.split()[1]].add(filename)

outputfile.write(",\n")

outputfile.write("0};\n")

# include and ifdef dependencies
for varname, deps in [("_include_deps", include_deps), ("_ifdef_deps", ifdef_deps)]:
outputheader.write("extern const char * %s[];\n" % varname)
outputfile.write("const char * %s[] = {\n" % varname)
for name, itemdeps in deps.items():
outputfile.write('"%s", "%s", 0,\n' % (name, '", "'.join(sorted(itemdeps))))
outputfile.write("0};\n")

outputheader.close()
outputfile.close()
output_file.write(",\n")
output_file.write("0};\n")


def create_buildinfo(outputdir, pymoldir="."):
try:
sha = (
Popen(["git", "rev-parse", "HEAD"], cwd=pymoldir, stdout=PIPE)
.stdout.read()
.strip()
.decode()
)
except OSError:
sha = ""

with openw(os.path.join(outputdir, "PyMOLBuildInfo.h")) as out:
print(
"""
#define _PyMOL_BUILD_DATE %d
#define _PYMOL_BUILD_GIT_SHA "%s"
"""
% (time.time(), sha),
file=out,
)
# include and ifdef dependencies
for varname, deps in [
("_include_deps", include_deps),
("_ifdef_deps", ifdef_deps),
]:
output_header.write(f"extern const char * {varname}[];\n")
output_file.write(f"const char * {varname}[] = {{\n")
for name, item_deps in deps.items():
item_deps = '", "'.join(sorted(item_deps))
output_file.write(f'"{name}", "{item_deps}", 0,\n')
output_file.write("0};\n")


def create_buildinfo(output_dir_path: str, pymoldir: str = "."):
output_dir = Path(output_dir_path)
sha_raw = Popen(["git", "rev-parse", "HEAD"], cwd=pymoldir, stdout=PIPE).stdout
sha = sha_raw.read().strip().decode() if sha_raw is not None else ""

info_file = output_dir / "PyMOLBuildInfo.h"
info_file.write_text(
f"""
#define _PyMOL_BUILD_DATE {time.time()}
#define _PYMOL_BUILD_GIT_SHA "{sha}"
"""
)


# handle extra arguments
Expand Down Expand Up @@ -302,7 +256,6 @@ def guess_msgpackc():


class CMakeExtension(Extension):

def __init__(
self,
name,
Expand Down Expand Up @@ -336,15 +289,15 @@ def run(self):
self.build_cmake(ext)

def build_cmake(self, ext):
cwd = pathlib.Path().absolute()
cwd = Path().absolute()

# these dirs will be created in build_py, so if you don't have
# any python sources to bundle, the dirs will be missing
name_split = ext.name.split(".")
target_name = name_split[-1]
build_temp = pathlib.Path(self.build_temp) / target_name
build_temp = Path(self.build_temp) / target_name
build_temp.mkdir(parents=True, exist_ok=True)
extdir = pathlib.Path(self.get_ext_fullpath(ext.name))
extdir = Path(self.get_ext_fullpath(ext.name))
extdirabs = extdir.absolute()

extdir.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -394,7 +347,7 @@ def concat_paths(paths):

if WIN:
# Move up from VS release folder
cmake_lib_loc = pathlib.Path(
cmake_lib_loc = Path(
lib_output_dir, "Release", f"{target_name}{shared_suffix}"
)
if cmake_lib_loc.exists():
Expand Down Expand Up @@ -487,7 +440,7 @@ def make_launch_script(self):
launch_script = os.path.join(self.install_scripts, launch_script)

python_exe = os.path.abspath(sys.executable)
site_packages_dir = sysconfig.get_path('purelib')
site_packages_dir = sysconfig.get_path("purelib")
pymol_file = self.unchroot(
os.path.join(site_packages_dir, "pymol", "__init__.py")
)
Expand Down Expand Up @@ -701,8 +654,6 @@ def make_launch_script(self):
libs += [
"opengl32",
]
# TODO: Remove when we move to setup-CMake
ext_comp_args += ["/std:c++17"]

if not (MAC or WIN):
libs += [
Expand Down Expand Up @@ -816,7 +767,7 @@ def get_packages(base, parent="", r=None):

if WIN:
# pyconfig.py forces linking against pythonXY.lib on MSVC
py_lib = pathlib.Path(sysconfig.get_paths()["stdlib"]).parent / "libs"
py_lib = Path(sysconfig.get_paths()["stdlib"]).parent / "libs"
lib_dirs.append(str(py_lib))

ext_modules += [
Expand Down

0 comments on commit b0da6a5

Please sign in to comment.