| 1 |
# -*- coding: ISO-8859-1 -*- |
| 2 |
# |
| 3 |
# dblatex python setup script - See the COPYRIGHT |
| 4 |
# |
| 5 |
from __future__ import print_function |
| 6 |
|
| 7 |
import os |
| 8 |
import sys |
| 9 |
import re |
| 10 |
import glob |
| 11 |
import subprocess |
| 12 |
from io import open |
| 13 |
|
| 14 |
try: |
| 15 |
from setuptools import setup |
| 16 |
from setuptools.command.install import install |
| 17 |
except ImportError: |
| 18 |
from distutils.core import setup |
| 19 |
from distutils.command.install import install |
| 20 |
|
| 21 |
from distutils.command.build import build |
| 22 |
from distutils.command.build_scripts import build_scripts |
| 23 |
from distutils.command.install_data import install_data |
| 24 |
from distutils.command.sdist import sdist |
| 25 |
from distutils import log |
| 26 |
from subprocess import Popen, PIPE |
| 27 |
sys.path.append("lib") |
| 28 |
from contrib.debian.installer import DebianInstaller |
| 29 |
|
| 30 |
# |
| 31 |
# Build the command line script |
| 32 |
# |
| 33 |
class BuildScripts(build_scripts): |
| 34 |
|
| 35 |
SHELL_SCRIPT = """#!%(env_executable)s%(env_args)s%(py_executable)s |
| 36 |
import sys |
| 37 |
import os |
| 38 |
|
| 39 |
package_base = %(package_base)s |
| 40 |
|
| 41 |
%(lib_path)s |
| 42 |
%(catalogs)s |
| 43 |
%(style_set)s |
| 44 |
from %(package_path)s import %(package)s |
| 45 |
%(package)s.main(base=package_base) |
| 46 |
""" |
| 47 |
|
| 48 |
CATALOGS = """cat = os.environ.get("SGML_CATALOG_FILES") |
| 49 |
if cat: |
| 50 |
cat += ":%s" |
| 51 |
else: |
| 52 |
cat = "%s" |
| 53 |
os.environ["SGML_CATALOG_FILES"] = cat |
| 54 |
""" |
| 55 |
|
| 56 |
def run(self): |
| 57 |
""" |
| 58 |
Create the proper script for the current platform. |
| 59 |
""" |
| 60 |
if not self.scripts: |
| 61 |
return |
| 62 |
|
| 63 |
# The script can only work with package data |
| 64 |
self.data_files = self.distribution.data_files |
| 65 |
if not(self.data_files): |
| 66 |
return |
| 67 |
|
| 68 |
if self.dry_run: |
| 69 |
return |
| 70 |
|
| 71 |
# Ensure the destination directory exists |
| 72 |
self.mkpath(self.build_dir) |
| 73 |
|
| 74 |
# Data useful for building the script |
| 75 |
install = self.distribution.get_command_obj("install") |
| 76 |
if not(install.install_data): |
| 77 |
return |
| 78 |
|
| 79 |
self._install_lib = os.path.normpath(install.install_lib) |
| 80 |
self._root = install.root |
| 81 |
if self._root: |
| 82 |
self._root = os.path.normpath(self._root) |
| 83 |
self._package_base = os.path.join(install.install_data, |
| 84 |
self.data_files[0][0]) |
| 85 |
self._catalogs = install.catalogs |
| 86 |
self._style = install.style |
| 87 |
self._use_py_path = install.use_python_path |
| 88 |
print(self._package_base) |
| 89 |
|
| 90 |
# Build the command line script |
| 91 |
self.build_script() |
| 92 |
|
| 93 |
def _strip_root(self, *paths): |
| 94 |
if not(self._root): |
| 95 |
return paths |
| 96 |
|
| 97 |
newpaths = [] |
| 98 |
for path in paths: |
| 99 |
if path.startswith(self._root): |
| 100 |
newpaths.append(path[len(self._root):]) |
| 101 |
else: |
| 102 |
newpaths.append(path) |
| 103 |
return newpaths |
| 104 |
|
| 105 |
def build_script(self): |
| 106 |
script_name = self.scripts[0] |
| 107 |
|
| 108 |
# prepare args for the bang path at the top of the script |
| 109 |
ENV_BIN = '/usr/bin/env' |
| 110 |
env_args = '' |
| 111 |
py_exec_bname = os.path.basename(sys.executable) |
| 112 |
if self._use_py_path: |
| 113 |
env_exec = '' |
| 114 |
py_exec = sys.executable |
| 115 |
elif os.name == 'posix': |
| 116 |
# Some Solaris platforms may not have an 'env' binary. |
| 117 |
# If /usr/bin/env exists, use '#!/usr/bin/env python' |
| 118 |
# otherwise, use '#!' + sys.executable |
| 119 |
env_exec = os.path.isfile(ENV_BIN) and \ |
| 120 |
os.access(ENV_BIN, os.X_OK) and ENV_BIN or '' |
| 121 |
py_exec = env_exec and py_exec_bname or sys.executable |
| 122 |
else: |
| 123 |
# shouldn't matter on non-POSIX; we'll just use defaults |
| 124 |
env_exec = ENV_BIN |
| 125 |
py_exec = py_exec_bname |
| 126 |
|
| 127 |
# Retrieve actual installation paths |
| 128 |
lib_path, package_base = self._strip_root(self._install_lib, |
| 129 |
self._package_base) |
| 130 |
|
| 131 |
# Just help for non standard installation paths |
| 132 |
if lib_path in sys.path: |
| 133 |
lib_path = "" |
| 134 |
else: |
| 135 |
lib_path = "sys.path.append(r\"%s\")" % lib_path |
| 136 |
|
| 137 |
# Things to adapt when building an egg |
| 138 |
if "/egg" in lib_path: |
| 139 |
lib_path = "" |
| 140 |
package_base = 'os.path.abspath(os.path.join(os.path.dirname('\ |
| 141 |
'__file__), "..", "..", "share", "dblatex"))' |
| 142 |
else: |
| 143 |
package_base = 'r"%s"' % (package_base) |
| 144 |
|
| 145 |
if self._catalogs: |
| 146 |
catalogs = self.CATALOGS % (self._catalogs, self._catalogs) |
| 147 |
else: |
| 148 |
catalogs = "" |
| 149 |
|
| 150 |
if self._style: |
| 151 |
style_set = "sys.argv.insert(1, '-T%s')" % self._style |
| 152 |
else: |
| 153 |
style_set = "" |
| 154 |
|
| 155 |
script_args = { 'env_executable': env_exec, |
| 156 |
'env_args': env_exec and (' %s' % env_args) or '', |
| 157 |
'py_executable': py_exec, |
| 158 |
'lib_path': lib_path, |
| 159 |
'style_set': style_set, |
| 160 |
'package': "dblatex", |
| 161 |
'package_path': "dbtexmf.dblatex", |
| 162 |
'catalogs': catalogs, |
| 163 |
'package_base': package_base } |
| 164 |
|
| 165 |
script = self.SHELL_SCRIPT % script_args |
| 166 |
script_name = os.path.basename(script_name) |
| 167 |
outfile = os.path.join(self.build_dir, script_name) |
| 168 |
fd = os.open(outfile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0o755) |
| 169 |
os.write(fd, script.encode('ascii')) |
| 170 |
os.close(fd) |
| 171 |
|
| 172 |
|
| 173 |
class Build(build): |
| 174 |
""" |
| 175 |
Build the documentation if missing or required to rebuild |
| 176 |
""" |
| 177 |
user_options = build.user_options + \ |
| 178 |
[('docbook-xsl=', None, |
| 179 |
'DocBook Project Stylesheet base directory (build_doc)')] |
| 180 |
|
| 181 |
def initialize_options(self): |
| 182 |
build.initialize_options(self) |
| 183 |
self.docbook_xsl = None |
| 184 |
|
| 185 |
def run(self): |
| 186 |
# Do the default tasks |
| 187 |
build.run(self) |
| 188 |
# And build the doc |
| 189 |
self.build_doc() |
| 190 |
|
| 191 |
def build_doc(self): |
| 192 |
log.info("running build_doc") |
| 193 |
htmldir = os.path.join("docs", "xhtml") |
| 194 |
pdfdocs = glob.glob(os.path.join("docs", "[mr]*.pdf")) |
| 195 |
manpage = os.path.join("docs", "manpage", "dblatex.1.gz") |
| 196 |
|
| 197 |
# Lazy check to avoid a rebuild for nothing |
| 198 |
if (not(self.force) and os.path.exists(htmldir) and len(pdfdocs) >= 2 |
| 199 |
and os.path.exists(manpage)): |
| 200 |
return |
| 201 |
|
| 202 |
# Assumes that make is the GNU make |
| 203 |
cmd = ["make", "-C", "docs", "VERSION=%s" % (get_version())] |
| 204 |
if self.docbook_xsl: |
| 205 |
cmd.append("XSLDBK=%s" % os.path.abspath(self.docbook_xsl)) |
| 206 |
|
| 207 |
subprocess.call(cmd) |
| 208 |
|
| 209 |
|
| 210 |
def find_programs(utils): |
| 211 |
sys.path.append("lib") |
| 212 |
from contrib.which import which |
| 213 |
util_paths = {} |
| 214 |
missed = [] |
| 215 |
for util in utils: |
| 216 |
try: |
| 217 |
path = which.which(util) |
| 218 |
util_paths[util] = path |
| 219 |
except which.WhichError: |
| 220 |
missed.append(util) |
| 221 |
sys.path.remove("lib") |
| 222 |
return (util_paths, missed) |
| 223 |
|
| 224 |
def kpsewhich(tex_file): |
| 225 |
if os.name == "nt": |
| 226 |
close_fds = False |
| 227 |
else: |
| 228 |
close_fds = True |
| 229 |
p = Popen("kpsewhich %s" % tex_file, shell=True, |
| 230 |
stdin=PIPE, stdout=PIPE, close_fds=close_fds) |
| 231 |
data = p.communicate()[0] |
| 232 |
if isinstance(data, bytes): |
| 233 |
data = data.decode(sys.getdefaultencoding()) |
| 234 |
return data.strip() |
| 235 |
|
| 236 |
|
| 237 |
class Sdist(sdist): |
| 238 |
""" |
| 239 |
Make the source package, and remove the .pyc files |
| 240 |
""" |
| 241 |
def prune_file_list(self): |
| 242 |
sdist.prune_file_list(self) |
| 243 |
self.filelist.exclude_pattern(r'.*.pyc', is_regex=1) |
| 244 |
|
| 245 |
|
| 246 |
class Install(install): |
| 247 |
|
| 248 |
user_options = install.user_options + \ |
| 249 |
[('catalogs=', None, 'default SGML catalogs'), |
| 250 |
('nodeps', None, 'don\'t check the dependencies'), |
| 251 |
('style=', None, 'default style to use'), |
| 252 |
('use-python-path', None, 'don\'t use env to locate python')] |
| 253 |
|
| 254 |
def initialize_options(self): |
| 255 |
install.initialize_options(self) |
| 256 |
self.catalogs = None |
| 257 |
self.nodeps = None |
| 258 |
self.style = None |
| 259 |
self.use_python_path = None |
| 260 |
# Prevents from undefined 'install_layout' attribute |
| 261 |
if not(getattr(self, "install_layout", None)): |
| 262 |
self.install_layout = None |
| 263 |
|
| 264 |
def check_util_dependencies(self): |
| 265 |
# First, check non critical graphic tools |
| 266 |
found, missed = find_programs(("epstopdf", "convert", "fig2dev")) |
| 267 |
for util in found: |
| 268 |
print("+checking %s... yes" % util) |
| 269 |
for util in missed: |
| 270 |
print("+checking %s... no" % util) |
| 271 |
if missed: |
| 272 |
print(("warning: not found: %s" % ", ".join(missed))) |
| 273 |
|
| 274 |
# Now, be serious |
| 275 |
found, missed = find_programs(("latex", "makeindex", |
| 276 |
"pdflatex", "kpsewhich")) |
| 277 |
for util in found: |
| 278 |
print("+checking %s... yes" % util) |
| 279 |
for util in missed: |
| 280 |
print("+checking %s... no" % util) |
| 281 |
if missed: |
| 282 |
raise OSError("not found: %s" % ", ".join(missed)) |
| 283 |
|
| 284 |
def check_xslt_dependencies(self): |
| 285 |
sys.path.insert(0, "lib") |
| 286 |
from dbtexmf.xslt import xslt |
| 287 |
sys.path.remove("lib") |
| 288 |
|
| 289 |
# At least one XSLT must be available |
| 290 |
deplists = xslt.get_deplists() |
| 291 |
if not(deplists): |
| 292 |
raise OSError("no XSLT available") |
| 293 |
|
| 294 |
# For each XSLT check the programs they depend on |
| 295 |
xslt_found = [] |
| 296 |
xslt_missed = [] |
| 297 |
for (mod, deplist) in deplists: |
| 298 |
if not(deplist): |
| 299 |
xslt_found.append(mod) |
| 300 |
print("+checking XSLT %s... yes" % mod) |
| 301 |
continue |
| 302 |
found, missed = find_programs(deplist) |
| 303 |
if missed: |
| 304 |
xslt_missed.append(mod) |
| 305 |
print("+checking XSLT %s... no (missing %s)" % \ |
| 306 |
(mod, ", ".join(missed))) |
| 307 |
else: |
| 308 |
xslt_found.append(mod) |
| 309 |
print("+checking XSLT %s... yes" % mod) |
| 310 |
|
| 311 |
if not(xslt_found): |
| 312 |
raise OSError("XSLT not installed: %s" % ", ".join(xslt_missed)) |
| 313 |
elif xslt_missed: |
| 314 |
print("warning: XSLT not found: %s" % ", ".join(xslt_missed)) |
| 315 |
|
| 316 |
def check_latex_dependencies(self): |
| 317 |
# Find the Latex files from the package |
| 318 |
stys = [] |
| 319 |
for root, dirs, files in os.walk('latex/'): |
| 320 |
stys += glob.glob(os.path.join(root, "*.sty")) |
| 321 |
if stys: |
| 322 |
own_stys = [os.path.basename(s)[:-4] for s in stys] |
| 323 |
else: |
| 324 |
own_stys = [] |
| 325 |
|
| 326 |
# Find the used packages |
| 327 |
used_stys = [] |
| 328 |
re_sty = re.compile(r"\\usepackage\s*\[?.*\]?{(\w+)}") |
| 329 |
for sty in stys: |
| 330 |
f = open(sty, "rt", encoding="latin-1") |
| 331 |
for line in f: |
| 332 |
line = line.split("%")[0] |
| 333 |
m = re_sty.search(line) |
| 334 |
if m: |
| 335 |
p = m.group(1) |
| 336 |
try: |
| 337 |
used_stys.index(p) |
| 338 |
except: |
| 339 |
used_stys.append(p) |
| 340 |
f.close() |
| 341 |
|
| 342 |
# Now look if they are found |
| 343 |
found_stys = [] |
| 344 |
mis_stys = [] |
| 345 |
used_stys.sort() |
| 346 |
|
| 347 |
# Dirty... |
| 348 |
for f in ("truncate", "elfonts", "CJKutf8", "pinyin", "ifxetex"): |
| 349 |
try: |
| 350 |
used_stys.remove(f) |
| 351 |
except: |
| 352 |
pass |
| 353 |
|
| 354 |
for sty in used_stys: |
| 355 |
if sty in found_stys: |
| 356 |
continue |
| 357 |
status = "+checking %s... " % sty |
| 358 |
if sty in own_stys: |
| 359 |
status += "found in package" |
| 360 |
found_stys.append(sty) |
| 361 |
print(status) |
| 362 |
continue |
| 363 |
stypath = kpsewhich("%s.sty" % sty) |
| 364 |
if stypath: |
| 365 |
status += "yes" |
| 366 |
found_stys.append(sty) |
| 367 |
else: |
| 368 |
status += "no" |
| 369 |
mis_stys.append(sty) |
| 370 |
print(status) |
| 371 |
|
| 372 |
if mis_stys: |
| 373 |
raise OSError("not found: %s" % ", ".join(mis_stys)) |
| 374 |
|
| 375 |
def run(self): |
| 376 |
if self.install_layout == "deb": |
| 377 |
db = DebianInstaller(self) |
| 378 |
else: |
| 379 |
db = None |
| 380 |
|
| 381 |
if not(db) and not(self.nodeps): |
| 382 |
try: |
| 383 |
self.check_xslt_dependencies() |
| 384 |
self.check_util_dependencies() |
| 385 |
self.check_latex_dependencies() |
| 386 |
except Exception as e: |
| 387 |
print("Error: %s" % e, file=sys.stderr) |
| 388 |
sys.exit(1) |
| 389 |
|
| 390 |
if db: db.adapt_paths() |
| 391 |
|
| 392 |
# If no build is required, at least build the script |
| 393 |
if self.skip_build: |
| 394 |
self.run_command('build_scripts') |
| 395 |
|
| 396 |
install.run(self) |
| 397 |
|
| 398 |
if db: db.finalize() |
| 399 |
|
| 400 |
|
| 401 |
class InstallData(install_data): |
| 402 |
|
| 403 |
def run(self): |
| 404 |
ignore_pattern = os.path.sep + r"(CVS|RCS)" + os.path.sep |
| 405 |
# literal backslash must be doubled in regular expressions |
| 406 |
ignore_pattern = ignore_pattern.replace('\\', r'\\') |
| 407 |
|
| 408 |
# Walk through sub-dirs, specified in data_files and build the |
| 409 |
# full data files list accordingly |
| 410 |
full_data_files = [] |
| 411 |
for install_base, paths in self.data_files: |
| 412 |
base_files = [] |
| 413 |
for path in paths: |
| 414 |
if os.path.isdir(path): |
| 415 |
pref = os.path.dirname(path) |
| 416 |
for root, dirs, files in os.walk(path): |
| 417 |
if re.search(ignore_pattern, root + os.sep): |
| 418 |
continue |
| 419 |
# Only the last directory is copied, not the full path |
| 420 |
if not(pref): |
| 421 |
iroot = root |
| 422 |
else: |
| 423 |
iroot = root.split(pref + os.path.sep, 1)[1] |
| 424 |
idir = os.path.join(install_base, iroot) |
| 425 |
files = [os.path.join(root, i) for i in files] |
| 426 |
if files: |
| 427 |
full_data_files += [(idir, files)] |
| 428 |
else: |
| 429 |
base_files.append(path) |
| 430 |
|
| 431 |
if base_files: |
| 432 |
full_data_files += [(install_base, base_files)] |
| 433 |
|
| 434 |
# Replace synthetic data_files by the full one, and do the actual job |
| 435 |
self.data_files = full_data_files |
| 436 |
rc = install_data.run(self) |
| 437 |
|
| 438 |
if self.distribution.get_command_obj("install").install_layout != "deb": |
| 439 |
self.adapt_installed_data() |
| 440 |
return rc |
| 441 |
|
| 442 |
def adapt_installed_data(self): |
| 443 |
installed = self.get_outputs() |
| 444 |
for data_file in installed: |
| 445 |
if os.path.basename(data_file) == "param.xsl": |
| 446 |
self._set_texlive_version(data_file) |
| 447 |
break |
| 448 |
|
| 449 |
def _set_texlive_version(self, param_file): |
| 450 |
"""Detect the installed Texlive version from hyperref.sty version, and |
| 451 |
override the texlive.version param accordingly.""" |
| 452 |
hyper_sty = kpsewhich("hyperref.sty") |
| 453 |
if not(hyper_sty): |
| 454 |
# Cannot do anything, give up |
| 455 |
return |
| 456 |
|
| 457 |
# Grab the value from package version |
| 458 |
d = open(hyper_sty, "rt", encoding="latin-1").read() |
| 459 |
m = re.search(r"\\ProvidesPackage{hyperref}\s+\[(\d+)", d, re.M) |
| 460 |
if not(m): |
| 461 |
return |
| 462 |
year = m.group(1) |
| 463 |
|
| 464 |
# Patch the parameter with the found value |
| 465 |
p = open(param_file, "rt", encoding="latin-1").read() |
| 466 |
p2 = re.sub('name="texlive.version">.*<', |
| 467 |
'name="texlive.version">%s<' % year, p) |
| 468 |
f = open(param_file, "wt", encoding="latin-1") |
| 469 |
f.write(p2) |
| 470 |
f.close() |
| 471 |
|
| 472 |
|
| 473 |
def get_version(): |
| 474 |
sys.path.insert(0, "lib") |
| 475 |
from dbtexmf.dblatex import dblatex |
| 476 |
d = dblatex.DbLatex(base=os.getcwd()) |
| 477 |
sys.path.remove("lib") |
| 478 |
return d.get_version() |
| 479 |
|
| 480 |
|
| 481 |
if __name__ == "__main__": |
| 482 |
pdfdocs = glob.glob(os.path.join("docs", "[mr]*.pdf")) |
| 483 |
htmldoc = [os.path.join("docs", "xhtml")] |
| 484 |
classifiers = [ |
| 485 |
"Operating System :: OS Independent", |
| 486 |
"Topic :: Text Processing :: Markup :: XML", |
| 487 |
"License :: OSI Approved :: GNU General Public License (GPL)" |
| 488 |
] |
| 489 |
|
| 490 |
description = """ |
| 491 |
dblatex is a program that transforms your SGML/XML DocBook documents to |
| 492 |
DVI, PostScript or PDF by translating them into pure LaTeX as a first |
| 493 |
process. MathML 2.0 markups are supported, too. It started as a clone |
| 494 |
of DB2LaTeX. |
| 495 |
""" |
| 496 |
|
| 497 |
setup(name="dblatex", |
| 498 |
version=get_version(), |
| 499 |
description='DocBook to LaTeX/ConTeXt Publishing', |
| 500 |
author='Benoit Guillon', |
| 501 |
author_email='[email protected]', |
| 502 |
url='http://dblatex.sf.net', |
| 503 |
license='GPL Version 2 or later', |
| 504 |
long_description=description, |
| 505 |
classifiers=classifiers, |
| 506 |
packages=['dbtexmf', |
| 507 |
'dbtexmf.core', |
| 508 |
'dbtexmf.xslt', |
| 509 |
'dbtexmf.dblatex', |
| 510 |
'dbtexmf.dblatex.xetex', |
| 511 |
'dbtexmf.dblatex.grubber'], |
| 512 |
package_dir={'dbtexmf':'lib/dbtexmf'}, |
| 513 |
package_data={'dbtexmf.core':['sgmlent.txt'], |
| 514 |
'dbtexmf.dblatex.grubber':['xindylang.xml']}, |
| 515 |
data_files=[('share/dblatex', ['xsl', 'latex', 'etc/schema']), |
| 516 |
('share/doc/dblatex', pdfdocs), |
| 517 |
('share/doc/dblatex', htmldoc), |
| 518 |
('share/man/man1', ['docs/manpage/dblatex.1.gz'])], |
| 519 |
scripts=['scripts/dblatex'], |
| 520 |
cmdclass={'build': Build, |
| 521 |
'build_scripts': BuildScripts, |
| 522 |
'install': Install, |
| 523 |
'install_data': InstallData, |
| 524 |
'sdist': Sdist} |
| 525 |
) |
| 526 |
|