Skip to content

Commit

Permalink
Merge pull request #43 from alex/user-cache
Browse files Browse the repository at this point in the history
Fixed #42 -- use a user cache directory
  • Loading branch information
alex committed Sep 1, 2015
2 parents 700daa7 + 9186b68 commit 518f798
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 41 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
@@ -1,5 +1,11 @@
language: python
python: 2.7
sudo: false

cache:
directories:
- $HOME/.cache/pip

env:
- TOX_ENV=py26
- TOX_ENV=py27
Expand All @@ -18,4 +24,3 @@ script:
- ./.travis/run.sh
notifications:
email: false

11 changes: 7 additions & 4 deletions .travis/run.sh
Expand Up @@ -3,17 +3,20 @@
set -e
set -x

export PYPY_LOCATION
case "${TOX_ENV}" in
py26)
PYPY_LOCATION=""
;;
py33)
export PYPY_LOCATION=""
PYPY_LOCATION=""
;;
py34)
export PYPY_LOCATION=""
PYPY_LOCATION=""
;;
*)
export PYPY_LOCATION=`python -c "import glob; import os; print os.path.abspath(glob.glob('../pypy-pypy*')[0])"`
PYPY_LOCATION=`python -c "import glob; import os; print os.path.abspath(glob.glob('../pypy-pypy*')[0])"`
;;
esac

tox -e $TOX_ENV

2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -2,7 +2,7 @@ RPLY
====

.. image:: https://secure.travis-ci.org/alex/rply.png
:target: http://travis-ci.org/alex/rply
:target: https://travis-ci.org/alex/rply

Welcome to RPLY! A pure python parser generator, that also works with RPython.
It is a more-or-less direct port of David Beazley's awesome PLY, with a new
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Expand Up @@ -99,7 +99,7 @@

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'default'
html_theme = 'alabaster'

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand Down Expand Up @@ -259,4 +259,4 @@


# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'http://docs.python.org/': None}
intersphinx_mapping = {'https://docs.python.org/': None}
2 changes: 1 addition & 1 deletion docs/users-guide/parsers.rst
Expand Up @@ -12,7 +12,7 @@ expressions as defined by the following BNF_ grammar::
| "(" <expression> ")"


.. _BNF: http://en.wikipedia.org/wiki/Backus-Naur_Form
.. _BNF: https://en.wikipedia.org/wiki/Backus-Naur_Form


Furthermore we use the following lexer:
Expand Down
48 changes: 16 additions & 32 deletions rply/parsergenerator.py
Expand Up @@ -2,12 +2,12 @@
import json
import os
import random
import stat
import string
import sys
import tempfile
import warnings

from appdirs import AppDirs

from rply.errors import ParserGeneratorError, ParserGeneratorWarning
from rply.grammar import Grammar
from rply.parser import LRParser
Expand Down Expand Up @@ -175,44 +175,28 @@ def build(self):
g.compute_first()
g.compute_follow()

# win32 temp directories are already per-user
if os.name == "nt":
cache_file = os.path.join(
tempfile.gettempdir(),
"rply-%s-%s-%s.json" % (
self.VERSION, self.cache_id, self.compute_grammar_hash(g)
)
)
else:
cache_file = os.path.join(
tempfile.gettempdir(),
"rply-%s-%s-%s-%s.json" % (
self.VERSION,
os.getuid(),
self.cache_id,
self.compute_grammar_hash(g)
)
cache_dir = AppDirs("rply").user_cache_dir
cache_file = os.path.join(
cache_dir,
"%s-%s-%s.json" % (
self.cache_id, self.VERSION, self.compute_grammar_hash(g)
)
)

table = None
if os.path.exists(cache_file):
with open(cache_file) as f:
data = json.load(f)
stat_result = os.fstat(f.fileno())
if (
os.name == "nt" or (
stat_result.st_uid == os.getuid() and
stat.S_IMODE(stat_result.st_mode) == 0o0600
)
):
if self.data_is_valid(g, data):
table = LRTable.from_cache(g, data)
if self.data_is_valid(g, data):
table = LRTable.from_cache(g, data)
if table is None:
table = LRTable.from_grammar(g)
fd = os.open(
cache_file, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o0600
)
with os.fdopen(fd, "w") as f:
if not os.path.exists(cache_dir):
os.makedirs(cache_dir, mode=0o0700)

with open(cache_file, "w") as f:
json.dump(self.serialize_table(table), f)

if table.sr_conflicts:
warnings.warn(
"%d shift/reduce conflict%s" % (
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -13,4 +13,5 @@
author="Alex Gaynor",
author_email="alex.gaynor@gmail.com",
packages=["rply"],
install_requires=["appdirs"],
)

0 comments on commit 518f798

Please sign in to comment.