Skip to content

Commit

Permalink
Merge pull request #2770 from tk0miya/2765_bundle_grammars
Browse files Browse the repository at this point in the history
Fix #2765: bundle grammar files
  • Loading branch information
tk0miya committed Aug 15, 2016
2 parents 63d2330 + 1cabc17 commit b03b7e5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
3 changes: 1 addition & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ recursive-include sphinx/search/non-minified-js *.js
recursive-include sphinx/ext/autosummary/templates *
recursive-include tests *
recursive-include utils *
include sphinx/pycode/Grammar-py2.txt
include sphinx/pycode/Grammar-py3.txt
include sphinx/pycode/Grammar-py*

recursive-include doc *
prune doc/_build
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from distutils import log

import sphinx
from sphinx.pycode.pgen2.driver import compile_grammar

long_desc = '''
Sphinx is a tool that makes it easy to create intelligent and beautiful
Expand Down Expand Up @@ -72,6 +73,10 @@
if sys.platform == 'win32':
requires.append('colorama>=0.3.5')

# Compile grammars before packaging
compile_grammar('sphinx/pycode/Grammar-py2.txt')
compile_grammar('sphinx/pycode/Grammar-py3.txt')

# Provide a "compile_catalog" command that also creates the translated
# JavaScript files if Babel is available.

Expand Down
41 changes: 25 additions & 16 deletions sphinx/pycode/pgen2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,36 @@ def generate_lines(text):
yield ""


def load_grammar(gt="Grammar.txt", gp=None,
save=True, force=False, logger=None):
def get_compiled_path(filename):
head, tail = os.path.splitext(filename)
if tail == ".txt":
tail = ""
return "%s%s.pickle" % (head, tail)


def compile_grammar(gt='Grammar.txt', logger=None):
"""Compile the grammer."""
if logger is None:
logger = logging.getLogger()

logger.info("Generating grammar tables from %s", gt)
g = pgen.generate_grammar(gt)
gp = get_compiled_path(gt)
logger.info("Writing grammar tables to %s", gp)
try:
g.dump(gp)
except IOError as e:
logger.info("Writing failed:"+str(e))


def load_grammar(gt="Grammar.txt", logger=None):
"""Load the grammar (maybe from a pickle)."""
if logger is None:
logger = logging.getLogger()
if gp is None:
head, tail = os.path.splitext(gt)
if tail == ".txt":
tail = ""
# embed Sphinx major version for the case we ever change the grammar...
gp = head + tail + "-sphinx" + \
".".join(map(str, sphinx.version_info[:2])) + ".pickle"
if force or not _newer(gp, gt):
gp = get_compiled_path(gt)
if not os.path.exists(gp):
logger.info("Generating grammar tables from %s", gt)
g = pgen.generate_grammar(gt)
if save:
logger.info("Writing grammar tables to %s", gp)
try:
g.dump(gp)
except IOError as e:
logger.info("Writing failed:"+str(e))
else:
g = grammar.Grammar()
g.load(gp)
Expand Down
7 changes: 3 additions & 4 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
from six import iteritems, text_type, binary_type
from six.moves import range
from six.moves.urllib.parse import urlsplit, urlunsplit, quote_plus, parse_qsl, urlencode
import docutils
from docutils.utils import relative_path

import jinja2

import sphinx
from sphinx.errors import PycodeError, SphinxParallelError, ExtensionError
from sphinx.util.console import strip_colors
from sphinx.util.fileutil import copy_asset_file
Expand Down Expand Up @@ -186,6 +182,9 @@ def copy_static_entry(source, targetdir, builder, context={},

def save_traceback(app):
"""Save the current exception's traceback in a temporary file."""
import sphinx
import jinja2
import docutils
import platform
exc = sys.exc_info()[1]
if isinstance(exc, SphinxParallelError):
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ envlist=flake8,py26,py27,py33,py34,py35,pypy,du12,du11,du10

[testenv]
deps=
six
nose
docutils
sqlalchemy
whoosh
html5lib
Expand Down

0 comments on commit b03b7e5

Please sign in to comment.