| 1 |
|
| 2 |
|
| 3 |
u""" |
| 4 |
:Copyright: |
| 5 |
|
| 6 |
Copyright 2011 - 2025 |
| 7 |
Andr\xe9 Malo or his licensors, as applicable |
| 8 |
|
| 9 |
:License: |
| 10 |
|
| 11 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 |
you may not use this file except in compliance with the License. |
| 13 |
You may obtain a copy of the License at |
| 14 |
|
| 15 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 16 |
|
| 17 |
Unless required by applicable law or agreed to in writing, software |
| 18 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 19 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 |
See the License for the specific language governing permissions and |
| 21 |
limitations under the License. |
| 22 |
|
| 23 |
=========================================== |
| 24 |
rJSmin - A Javascript Minifier For Python |
| 25 |
=========================================== |
| 26 |
|
| 27 |
rJSmin - A Javascript Minifier For Python. |
| 28 |
""" |
| 29 |
from __future__ import print_function |
| 30 |
__author__ = u"Andr\xe9 Malo" |
| 31 |
|
| 32 |
import os as _os |
| 33 |
import posixpath as _posixpath |
| 34 |
import sys as _sys |
| 35 |
|
| 36 |
|
| 37 |
import setuptools as _setuptools |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
def _doc(filename): |
| 43 |
"""Read docs file""" |
| 44 |
|
| 45 |
args = {} if str is bytes else dict(encoding="utf-8") |
| 46 |
try: |
| 47 |
with open(_os.path.join("docs", filename), **args) as fp: |
| 48 |
return fp.read() |
| 49 |
except IOError: |
| 50 |
return None |
| 51 |
|
| 52 |
|
| 53 |
package = dict( |
| 54 |
name="rjsmin", |
| 55 |
top=".", |
| 56 |
pathname=".", |
| 57 |
desc="Javascript Minifier", |
| 58 |
longdesc=_doc("DESCRIPTION"), |
| 59 |
author=__author__, |
| 60 |
email="[email protected]", |
| 61 |
url="https://opensource.perlig.de/rjsmin/", |
| 62 |
license="Apache License, Version 2.0", |
| 63 |
license_files=["LICENSE"], |
| 64 |
|
| 65 |
packages=False, |
| 66 |
py_modules=["rjsmin"], |
| 67 |
version_file="rjsmin.py", |
| 68 |
install_requires=[], |
| 69 |
|
| 70 |
entry_points={}, |
| 71 |
|
| 72 |
classifiers=[ |
| 73 |
"Development Status :: 5 - Production/Stable", |
| 74 |
"Environment :: Web Environment", |
| 75 |
"Intended Audience :: Developers", |
| 76 |
"License :: OSI Approved", |
| 77 |
"Operating System :: OS Independent", |
| 78 |
"Programming Language :: C", |
| 79 |
"Programming Language :: Python", |
| 80 |
"Programming Language :: Python :: 2", |
| 81 |
"Programming Language :: Python :: 2.7", |
| 82 |
"Programming Language :: Python :: 3", |
| 83 |
"Programming Language :: Python :: 3.6", |
| 84 |
"Programming Language :: Python :: 3.7", |
| 85 |
"Programming Language :: Python :: 3.8", |
| 86 |
"Programming Language :: Python :: 3.9", |
| 87 |
"Programming Language :: Python :: 3.10", |
| 88 |
"Programming Language :: Python :: 3.11", |
| 89 |
"Programming Language :: Python :: 3.12", |
| 90 |
"Programming Language :: Python :: 3.13", |
| 91 |
"Programming Language :: Python :: 3.14", |
| 92 |
"Programming Language :: Python :: Implementation :: CPython", |
| 93 |
"Programming Language :: Python :: Implementation :: Jython", |
| 94 |
"Programming Language :: Python :: Implementation :: PyPy", |
| 95 |
"Topic :: Internet :: WWW/HTTP :: Dynamic Content", |
| 96 |
"Topic :: Software Development :: Libraries", |
| 97 |
"Topic :: Software Development :: Libraries :: Python Modules", |
| 98 |
"Topic :: Text Processing", |
| 99 |
"Topic :: Text Processing :: Filters", |
| 100 |
"Topic :: Utilities", |
| 101 |
], |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
class BuildFailed(Exception): |
| 106 |
"""The build has failed""" |
| 107 |
|
| 108 |
|
| 109 |
from distutils.command import build_ext as _build_ext |
| 110 |
from distutils import errors as _errors |
| 111 |
class build_ext(_build_ext.build_ext): |
| 112 |
"""Improved extension building code""" |
| 113 |
|
| 114 |
def run(self): |
| 115 |
"""Unify exception""" |
| 116 |
try: |
| 117 |
_build_ext.build_ext.run(self) |
| 118 |
except _errors.DistutilsPlatformError: |
| 119 |
raise BuildFailed() |
| 120 |
|
| 121 |
def build_extension(self, ext): |
| 122 |
""" |
| 123 |
Build C extension - with extended functionality |
| 124 |
|
| 125 |
The following features are added here: |
| 126 |
|
| 127 |
- The macros ``EXT_PACKAGE`` and ``EXT_MODULE`` will be filled (or |
| 128 |
unset) depending on the extensions name, but only if they are not |
| 129 |
already defined. |
| 130 |
|
| 131 |
- "." is added to the include directories (for cext.h) |
| 132 |
|
| 133 |
:Parameters: |
| 134 |
`ext` : `Extension` |
| 135 |
The extension to build |
| 136 |
|
| 137 |
:Return: whatever ``distutils.command.build_ext.build_ext`` returns |
| 138 |
:Rtype: any |
| 139 |
""" |
| 140 |
|
| 141 |
macros = dict(ext.define_macros or ()) |
| 142 |
tup = ext.name.split(".") |
| 143 |
if len(tup) == 1: |
| 144 |
pkg, mod = None, tup[0] |
| 145 |
else: |
| 146 |
pkg, mod = ".".join(tup[:-1]), tup[-1] |
| 147 |
if pkg is not None and "EXT_PACKAGE" not in macros: |
| 148 |
ext.define_macros.append(("EXT_PACKAGE", pkg)) |
| 149 |
if "EXT_MODULE" not in macros: |
| 150 |
ext.define_macros.append(("EXT_MODULE", mod)) |
| 151 |
if pkg is None: |
| 152 |
macros = dict(ext.undef_macros or ()) |
| 153 |
if "EXT_PACKAGE" not in macros: |
| 154 |
ext.undef_macros.append("EXT_PACKAGE") |
| 155 |
|
| 156 |
try: |
| 157 |
return _build_ext.build_ext.build_extension(self, ext) |
| 158 |
except ( |
| 159 |
_errors.CCompilerError, |
| 160 |
_errors.DistutilsExecError, |
| 161 |
_errors.DistutilsPlatformError, |
| 162 |
IOError, |
| 163 |
ValueError, |
| 164 |
) as e: |
| 165 |
raise BuildFailed(str(e)) |
| 166 |
|
| 167 |
|
| 168 |
class Extension(_setuptools.Extension): |
| 169 |
"""improved functionality""" |
| 170 |
|
| 171 |
def __init__(self, *args, **kwargs): |
| 172 |
"""Initialization""" |
| 173 |
version = kwargs.pop("version") |
| 174 |
self.depends = [] |
| 175 |
if "depends" in kwargs: |
| 176 |
self.depends = kwargs["depends"] |
| 177 |
_setuptools.Extension.__init__(self, *args, **kwargs) |
| 178 |
self.define_macros.append(("EXT_VERSION", version)) |
| 179 |
|
| 180 |
|
| 181 |
included = "." |
| 182 |
if included not in self.include_dirs: |
| 183 |
self.include_dirs.append(included) |
| 184 |
|
| 185 |
|
| 186 |
cext_h = _posixpath.normpath(_posixpath.join(included, "cext.h")) |
| 187 |
for item in self.depends: |
| 188 |
if _posixpath.normpath(item) == cext_h: |
| 189 |
break |
| 190 |
else: |
| 191 |
self.depends.append(cext_h) |
| 192 |
|
| 193 |
|
| 194 |
EXTENSIONS = lambda v: [Extension("_rjsmin", ["rjsmin.c"], version=v)] |
| 195 |
|
| 196 |
|
| 197 |
def do_setup(cext): |
| 198 |
"""Main""" |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
args = {} if str is bytes else dict(encoding="utf-8") |
| 203 |
version_file = "%s/%s" % ( |
| 204 |
package["pathname"], |
| 205 |
package.get("version_file", "__init__.py"), |
| 206 |
) |
| 207 |
with open(version_file, **args) as fp: |
| 208 |
for line in fp: |
| 209 |
if line.startswith("__version__"): |
| 210 |
version = line.split("=", 1)[1].strip() |
| 211 |
if version.startswith(("'", '"')): |
| 212 |
version = version[1:-1].strip() |
| 213 |
break |
| 214 |
else: |
| 215 |
raise RuntimeError("Version not found") |
| 216 |
|
| 217 |
kwargs = {} |
| 218 |
|
| 219 |
if not cext or "java" in _sys.platform.lower(): |
| 220 |
extensions = [] |
| 221 |
else: |
| 222 |
extensions = EXTENSIONS(version) |
| 223 |
|
| 224 |
if extensions: |
| 225 |
if "build_ext" in globals(): |
| 226 |
kwargs.setdefault("cmdclass", {})["build_ext"] = build_ext |
| 227 |
kwargs["ext_modules"] = extensions |
| 228 |
|
| 229 |
cflags = None |
| 230 |
if _os.environ.get("CFLAGS") is None: |
| 231 |
from distutils import ccompiler as _ccompiler |
| 232 |
|
| 233 |
compiler = _ccompiler.get_default_compiler() |
| 234 |
try: |
| 235 |
with open("debug.%s.cflags" % compiler) as fp: |
| 236 |
cflags = ( |
| 237 |
" ".join( |
| 238 |
[ |
| 239 |
line |
| 240 |
for line in (line.strip() for line in fp) |
| 241 |
if line and not line.startswith("#") |
| 242 |
] |
| 243 |
).split() |
| 244 |
or None |
| 245 |
) |
| 246 |
except IOError: |
| 247 |
pass |
| 248 |
|
| 249 |
if cflags: |
| 250 |
gcov = "coverage" in " ".join(cflags) |
| 251 |
for ext in extensions: |
| 252 |
|
| 253 |
ext.extra_compile_args = ( |
| 254 |
getattr(ext, "extra_compile_args", []) + cflags |
| 255 |
) |
| 256 |
if gcov: |
| 257 |
ext.libraries.append("gcov") |
| 258 |
|
| 259 |
if package.get("packages", True): |
| 260 |
kwargs["packages"] = [package["top"]] + [ |
| 261 |
"%s.%s" % (package["top"], item) |
| 262 |
for item in _setuptools.find_packages(package["pathname"]) |
| 263 |
] |
| 264 |
|
| 265 |
if package.get("py_modules"): |
| 266 |
kwargs["py_modules"] = package["py_modules"] |
| 267 |
if package.get("license_files"): |
| 268 |
kwargs["license_files"] = package["license_files"] |
| 269 |
if package.get("license"): |
| 270 |
kwargs["license"] = package["license"] |
| 271 |
if package.get("classifiers"): |
| 272 |
kwargs["classifiers"] = package["classifiers"] |
| 273 |
if package.get("entry_points"): |
| 274 |
kwargs["entry_points"] = package["entry_points"] |
| 275 |
|
| 276 |
_setuptools.setup( |
| 277 |
name=package["name"], |
| 278 |
author=package["author"], |
| 279 |
author_email=package["email"], |
| 280 |
description=package["desc"], |
| 281 |
long_description=package["longdesc"], |
| 282 |
url=package["url"], |
| 283 |
install_requires=package["install_requires"], |
| 284 |
version=version, |
| 285 |
zip_safe=False, |
| 286 |
**kwargs |
| 287 |
) |
| 288 |
|
| 289 |
|
| 290 |
def setup(): |
| 291 |
"""Run setup""" |
| 292 |
try: |
| 293 |
do_setup(True) |
| 294 |
except BuildFailed: |
| 295 |
env = "SETUP_CEXT_REQUIRED" |
| 296 |
if _os.environ.get(env, "") not in ("", "0"): |
| 297 |
raise |
| 298 |
print( |
| 299 |
"C extension build failed - building python only version now. " |
| 300 |
"Set '%s' environment variable to '1' to make it fail." % (env,), |
| 301 |
file=_sys.stderr, |
| 302 |
) |
| 303 |
do_setup(False) |
| 304 |
|
| 305 |
|
| 306 |
if __name__ == "__main__": |
| 307 |
setup() |