switch to the DAG model

This commit is contained in:
Martin A. Brown 2016-02-26 10:10:29 -08:00
parent 9e660eb67a
commit f112fc98d1
1 changed files with 144 additions and 123 deletions

View File

@ -5,13 +5,15 @@ from __future__ import absolute_import, division, print_function
import os import os
import logging import logging
logger = logging.getLogger(__name__) import networkx as nx
from tldp.utils import which, execute, firstfoundfile from tldp.utils import which, firstfoundfile
from tldp.utils import arg_isexecutable, isexecutable from tldp.utils import arg_isexecutable, isexecutable
from tldp.utils import arg_isreadablefile, isreadablefile from tldp.utils import arg_isreadablefile, isreadablefile
from tldp.doctypes.common import BaseDoctype, SignatureChecker from tldp.doctypes.common import BaseDoctype, SignatureChecker, depends
logger = logging.getLogger(__name__)
def docbookdsl_finder(): def docbookdsl_finder():
@ -46,58 +48,77 @@ class DocbookSGML(BaseDoctype, SignatureChecker):
'docbooksgml_docbookdsl': isreadablefile, 'docbooksgml_docbookdsl': isreadablefile,
} }
buildorder = ['buildindex', 'buildall'] graph = nx.DiGraph()
indexscript = '''#! /bin/bash buildorder = ['buildall']
#
# -- generate usable index.sgml from DocBook SGML 3.x/4.x
set -x def chdir_output(self):
set -e os.chdir(self.output.dirname)
set -o pipefail return True
cd "{output.dirname}" @depends(graph, chdir_output)
def make_blank_indexsgml(self):
"{config.docbooksgml_collateindex}" \\ '''generate an empty index.sgml file (in output dir)'''
s = '''"{config.docbooksgml_collateindex}" \\
-N \\ -N \\
-o \\ -o \\
"{source.dirname}/index.sgml" "index.sgml"'''
return self.shellscript(s)
"{config.docbooksgml_openjade}" \\ @depends(graph, make_blank_indexsgml)
def make_data_indexsgml(self):
'''collect document's index entries into a data file (HTML.index)'''
s = '''"{config.docbooksgml_openjade}" \\
-t sgml \\ -t sgml \\
-V html-index \\ -V html-index \\
-d "{config.docbooksgml_docbookdsl}" \\ -d "{config.docbooksgml_docbookdsl}" \\
"{source.filename}" "{source.filename}"'''
return self.shellscript(s)
"{config.docbooksgml_collateindex}" \\ @depends(graph, make_data_indexsgml)
def make_indexsgml(self):
'''generate the final document index file (index.sgml)'''
s = '''"{config.docbooksgml_collateindex}" \\
-g \\ -g \\
-t Index \\ -t Index \\
-i doc-index \\ -i doc-index \\
-o "index.sgml" \\ -o "index.sgml" \\
"HTML.index" \\ "HTML.index" \\
"{source.filename}" "{source.filename}"'''
return self.shellscript(s)
mv \\ @depends(graph, make_indexsgml)
def move_indexsgml_into_source(self):
'''move the generated index.sgml file into the source tree'''
indexsgml = os.path.join(self.source.dirname, 'index.sgml')
s = '''mv \\
--no-clobber \\ --no-clobber \\
--verbose \\ --verbose \\
-- "index.sgml" "{source.dirname}/index.sgml" -- "index.sgml" "{source.dirname}/index.sgml"'''
moved = self.shellscript(s)
if moved:
self.removals = indexsgml
logger.debug("%s created %s", self.source.stem, indexsgml)
return True
return os.path.exists(indexsgml)
find . -mindepth 1 -maxdepth 1 -type f -print0 \ @depends(graph, move_indexsgml_into_source)
| xargs --null --no-run-if-empty -- rm -f -- def cleaned_indexsgml(self):
'''clean the junk from the output dir after building the index.sgml'''
# -- be super cautious before removing a bunch of files
cwd = os.getcwd()
if not os.path.samefile(cwd, self.output.dirname):
logger.error("%s (cowardly) refusing to clean directory %s", cwd)
logger.error("%s expected to find %s", self.output.dirname)
return False
s = '''find . -mindepth 1 -maxdepth 1 -type f -print0 \
| xargs --null --no-run-if-empty -- rm -f --'''
return self.shellscript(s)
# -- end of file''' @depends(graph, cleaned_indexsgml)
def make_htmls(self):
mainscript = '''#! /bin/bash '''create a single page HTML output (with incorrect name)'''
# s = '''"{config.docbooksgml_jw}" \\
# -- generate LDP outputs from DocBook SGML 3.x/4.x
set -x
set -e
set -o pipefail
cd "{output.dirname}"
"{config.docbooksgml_jw}" \\
-f docbook \\ -f docbook \\
-b html \\ -b html \\
--dsl "{config.docbooksgml_ldpdsl}#html" \\ --dsl "{config.docbooksgml_ldpdsl}#html" \\
@ -105,70 +126,70 @@ cd "{output.dirname}"
-V '%callout-graphics-path%=images/callouts/' \\ -V '%callout-graphics-path%=images/callouts/' \\
-V '%stock-graphics-extension%=.png' \\ -V '%stock-graphics-extension%=.png' \\
--output . \\ --output . \\
"{source.filename}" "{source.filename}"'''
return self.shellscript(s)
mv \\ @depends(graph, make_htmls)
--no-clobber \\ def make_name_htmls(self):
--verbose \\ '''correct the single page HTML output name'''
-- "{output.name_html}" "{output.name_htmls}" s = 'mv -v --no-clobber -- "{output.name_html}" "{output.name_htmls}"'
return self.shellscript(s)
"{config.docbooksgml_html2text}" > "{output.name_txt}" \\ @depends(graph, make_name_htmls)
def make_name_txt(self):
'''create text output (from single-page HTML)'''
s = '''"{config.docbooksgml_html2text}" > "{output.name_txt}" \\
-style pretty \\ -style pretty \\
-nobs \\ -nobs \\
"{output.name_htmls}" "{output.name_htmls}"'''
return self.shellscript(s)
"{config.docbooksgml_jw}" \\ def make_pdf_with_jw(self):
s = '''"{config.docbooksgml_jw}" \\
-f docbook \\ -f docbook \\
-b pdf \\ -b pdf \\
--output . \\ --output . \\
"{source.filename}" \\ "{source.filename}"'''
|| "{config.docbooksgml_dblatex}" \\ return self.shellscript(s)
def make_pdf_with_dblatex(self):
s = '''"{config.docbooksgml_dblatex}" \\
-F sgml \\ -F sgml \\
-t pdf \\ -t pdf \\
-o "{output.name_pdf}" \\ -o "{output.name_pdf}" \\
"{source.filename}" "{source.filename}"'''
return self.shellscript(s)
"{config.docbooksgml_jw}" \\ @depends(graph, cleaned_indexsgml)
def make_name_pdf(self):
if self.make_pdf_with_jw():
return True
return self.make_pdf_with_dblatex()
@depends(graph, make_name_htmls)
def make_html(self):
'''create final index.html symlink'''
s = '''"{config.docbooksgml_jw}" \\
-f docbook \\ -f docbook \\
-b html \\ -b html \\
--dsl "{config.docbooksgml_ldpdsl}#html" \\ --dsl "{config.docbooksgml_ldpdsl}#html" \\
-V '%callout-graphics-path%=images/callouts/' \\ -V '%callout-graphics-path%=images/callouts/' \\
-V '%stock-graphics-extension%=.png' \\ -V '%stock-graphics-extension%=.png' \\
--output . \\ --output . \\
"{source.filename}" "{source.filename}"'''
return self.shellscript(s)
mv \\ @depends(graph, make_html)
--no-clobber \\ def make_name_html(self):
--verbose \\ '''rename openjade's index.html to LDP standard name STEM.html'''
-- "{output.name_indexhtml}" "{output.name_html}" s = 'mv -v --no-clobber -- "{output.name_indexhtml}" "{output.name_html}"'
return self.shellscript(s)
ln \\ @depends(graph, make_name_html)
--symbolic \\ def make_name_indexhtml(self):
--relative \\ '''create final index.html symlink'''
--verbose \\ s = 'ln -svr -- "{output.name_html}" "{output.name_indexhtml}"'
-- "{output.name_html}" "{output.name_indexhtml}" return self.shellscript(s)
# -- end of file'''
def buildindex(self):
indexsgml = os.path.join(self.source.dirname, 'index.sgml')
if os.path.isfile(indexsgml):
self.indexsgml = lambda: None
return True
def unlink_indexsgml():
os.unlink(indexsgml)
self.indexsgml = unlink_indexsgml
return self.shellscript(self.indexscript)
def buildall(self):
return self.shellscript(self.mainscript)
def post_buildall(self):
self.indexsgml()
return True
@staticmethod @staticmethod
def argparse(p): def argparse(p):