From 76d268a38c627bf4aebebcd064f5b6d380eb8b20 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 1 Sep 2015 07:21:10 -0400 Subject: [PATCH] Fixed #42 -- use a user cache directory --- rply/parsergenerator.py | 46 ++++++++++++++--------------------------- setup.py | 1 + 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/rply/parsergenerator.py b/rply/parsergenerator.py index f2ed8ee..995f78e 100644 --- a/rply/parsergenerator.py +++ b/rply/parsergenerator.py @@ -8,6 +8,8 @@ import tempfile import warnings +from appdirs import AppDirs + from rply.errors import ParserGeneratorError, ParserGeneratorWarning from rply.grammar import Grammar from rply.parser import LRParser @@ -175,44 +177,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, + "rply-%s-%s-%s.json" % ( + self.VERSION, self.cache_id, 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) + + 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" % ( diff --git a/setup.py b/setup.py index df6af7e..1072f5e 100644 --- a/setup.py +++ b/setup.py @@ -13,4 +13,5 @@ author="Alex Gaynor", author_email="alex.gaynor@gmail.com", packages=["rply"], + install_requires=["appdirs"], )