1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import io
import os
import platform
import logging
import sys
from setuptools import setup, find_packages, Extension
logging.basicConfig(level=logging.INFO)
with io.open("README.md", mode="rt", encoding="utf-8") as readme_file:
readme = readme_file.read()
# Setup flags
USE_STATIC = False
USE_CYTHON = False
PLATFORM = "windows_nt" if platform.system() == "Windows" else "posix"
INCLUDE_LEXBOR = bool(os.environ.get("USE_LEXBOR", True))
INCLUDE_MODEST = bool(os.environ.get("USE_MODEST", True))
ARCH = platform.architecture()[0]
try:
from Cython.Build import cythonize
HAS_CYTHON = True
USE_CYTHON = True
except ImportError as err:
HAS_CYTHON = False
if "--static" in sys.argv:
USE_STATIC = True
sys.argv.remove("--static")
if "--lexbor" in sys.argv:
INCLUDE_LEXBOR = True
sys.argv.remove("--lexbor")
if "--disable-modest" in sys.argv:
INCLUDE_MODEST = False
sys.argv.remove("--disable-modest")
if "--cython" in sys.argv:
if HAS_CYTHON:
USE_CYTHON = True
else:
raise ImportError("No module named 'Cython'")
sys.argv.remove("--cython")
# If there are no pretranspiled source files
if HAS_CYTHON and not os.path.exists("selectolax/parser.c"):
USE_CYTHON = True
COMPILER_DIRECTIVES = {
"language_level": 3,
"embedsignature": True,
"annotation_typing": False,
"emit_code_comments": True,
"boundscheck": False,
"wraparound": False,
}
def find_modest_files(modest_path="modest/source"):
c_files = []
if os.path.exists(modest_path):
for root, dirs, files in os.walk(modest_path):
for file in files:
if file.endswith(".c"):
file_path = os.path.join(root, file)
# Filter platform specific files
if (file_path.find("myport") >= 0) and (
not file_path.find(PLATFORM) >= 0
):
continue
if INCLUDE_LEXBOR:
if (file_path.find("ports") >= 0) and (
not file_path.find(PLATFORM) >= 0
):
continue
c_files.append(file_path)
return c_files
def make_extensions():
logging.info(f"USE_CYTHON: {USE_CYTHON}")
logging.info(f"INCLUDE_LEXBOR: {INCLUDE_LEXBOR}")
logging.info(f"INCLUDE_MODEST: {INCLUDE_MODEST}")
logging.info(f"USE_STATIC: {USE_STATIC}")
files_to_compile_lxb = []
files_to_compile = []
extra_objects_lxb, extra_objects = [], []
if USE_CYTHON:
if INCLUDE_MODEST:
files_to_compile = [
"selectolax/parser.pyx",
]
if INCLUDE_LEXBOR:
files_to_compile_lxb = [
"selectolax/lexbor.pyx",
]
else:
if INCLUDE_MODEST:
files_to_compile = ["selectolax/parser.c"]
if INCLUDE_LEXBOR:
files_to_compile_lxb = [
"selectolax/lexbor.c",
]
if USE_STATIC:
if INCLUDE_MODEST:
extra_objects = ["modest/lib/libmodest_static.a"]
if INCLUDE_LEXBOR:
extra_objects_lxb = ["lexbor/liblexbor_static.a"]
else:
if INCLUDE_MODEST:
files_to_compile.extend(find_modest_files("modest/source"))
if INCLUDE_LEXBOR:
files_to_compile_lxb.extend(find_modest_files("lexbor/source"))
compile_arguments_lxb = [
"-DLEXBOR_STATIC",
]
compile_arguments = [
"-DMODEST_BUILD_OS=%s" % platform.system(),
"-DMyCORE_OS_%s" % platform.system(),
"-DMODEST_PORT_NAME=%s" % PLATFORM,
"-DMyCORE_BUILD_WITHOUT_THREADS=YES",
"-DMyCORE_BUILD_DEBUG=NO",
]
if PLATFORM == "posix":
args = [
"-pedantic",
"-fPIC",
"-Wno-unused-variable",
"-Wno-unused-function",
"-std=c99",
"-O2",
"-g",
]
compile_arguments.extend(args)
compile_arguments_lxb.extend(args)
elif PLATFORM == "windows_nt":
compile_arguments_lxb.extend(
[
"-D_WIN64" if ARCH == "64bit" else "-D_WIN32",
]
)
extensions = []
if INCLUDE_MODEST:
extensions.append(
Extension(
"selectolax.parser",
files_to_compile,
language="c",
include_dirs=[
"modest/include/",
],
extra_objects=extra_objects,
extra_compile_args=compile_arguments,
)
)
if INCLUDE_LEXBOR:
extensions.append(
Extension(
"selectolax.lexbor",
files_to_compile_lxb,
language="c",
include_dirs=[
"lexbor/source/",
],
extra_objects=extra_objects_lxb,
extra_compile_args=compile_arguments_lxb,
)
)
if USE_CYTHON:
extensions = cythonize(extensions, compiler_directives=COMPILER_DIRECTIVES)
return extensions
setup(
name="selectolax",
version="0.4.6",
description="A fast HTML5 parser with CSS selectors, written in Cython, using Modest and Lexbor engines.",
long_description=readme,
author="Artem Golubin",
author_email="me@rushter.com",
url="https://github.com/rushter/selectolax",
packages=find_packages(include=["selectolax"]),
package_data={"selectolax": ["py.typed"]},
include_package_data=True,
zip_safe=False,
ext_modules=make_extensions(),
)
|