NEW: Added environment variable "SCYTHERCACHEDIR" to determine cache usage.

If this variable is unset, Scyther writes into /tmp/Scyther-cache (or similar).
If this variable is set to "", caching is disabled.
Otherwise, Scyther writes into $SCYTHERCACHEDIR/Scyther-cache
This commit is contained in:
Cas Cremers 2012-09-28 12:00:24 +02:00
parent 6c7493838c
commit ba5166bd84

View File

@ -69,9 +69,30 @@ def getBinDir():
""" """
Return Cache prefix path Return Cache prefix path
Returns None if not existent
""" """
def getCacheDir(): def getCacheDir():
return os.path.join(getMyDir(),"Cache")
tmpdir = None
# Check if user chose the path
cachedirkey = "SCYTHERCACHEDIR"
if cachedirkey in os.environ.keys():
tmpdir = os.environ[cachedirkey]
if tmpdir == "":
# Special value: if the variable is present, but equals the empty string, we disable caching.
return None
else:
# Otherwise take from path
tmpdir = tempfile.gettempdir()
# If not none, append special name
if tmpdir != None:
tmpdir = os.path.join(tmpdir,"Scyther-cache")
return tmpdir
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
@ -247,18 +268,31 @@ class Scyther(object):
def doScytherCommand(self, spdl, args, checkKnown=False): def doScytherCommand(self, spdl, args, checkKnown=False):
""" """
Cached version of the 'real' below Cached version of the 'real' below
TODO: CC: One possible problem with the caching is the side-effect, e.g., scyther writing to specific named output files. These are not
captured in the cache. I don't have a good solution for that yet.
""" """
global HASHLIB global HASHLIB
if not HASHLIB: # Can we use the cache?
canCache = False
if HASHLIB:
cacheDir = getCacheDir()
if cacheDir != None:
canCache = True
else:
cacheDir = None
# If we cannot use the cache, we either need to compute or, if checking for cache presense,...
if not canCache:
if checkKnown == True: if checkKnown == True:
# No hashlib, so we don't have it already # not using the cache, so we don't have it already
return False return False
else: else:
# Need to compute # Need to compute
return self.doScytherCommandReal(spdl,args) return self.doScytherCommandReal(spdl,args)
# So we have the hashing libs # Apparently we are supporsed to be able to use the cache
m = hashlib.sha256() m = hashlib.sha256()
if spdl == None: if spdl == None:
m.update("[spdl:None]") m.update("[spdl:None]")
@ -280,7 +314,7 @@ class Scyther(object):
# Possibly we could also decide to store input and arguments in the cache to analyze things later # Possibly we could also decide to store input and arguments in the cache to analyze things later
# Construct: cachePath/uid1/uid2/... # Construct: cachePath/uid1/uid2/...
path = os.path.join(getCacheDir(),uid1,uid2) path = os.path.join(cacheDir,uid1,uid2)
name1 = "%s.out" % (uid3) name1 = "%s.out" % (uid3)
name2 = "%s.err" % (uid3) name2 = "%s.err" % (uid3)
@ -304,6 +338,8 @@ class Scyther(object):
# Not checking cache, we need the result # Not checking cache, we need the result
return (out,err) return (out,err)
except: except:
pass
""" """
Something went wrong, do the real thing and cache afterwards Something went wrong, do the real thing and cache afterwards
""" """
@ -313,7 +349,8 @@ class Scyther(object):
(out,err) = self.doScytherCommandReal(spdl,args) (out,err) = self.doScytherCommandReal(spdl,args)
# Store result in cache try:
# Try to store result in cache
ensurePath(path) ensurePath(path)
fh1 = open(fname1,"w") fh1 = open(fname1,"w")
@ -323,6 +360,8 @@ class Scyther(object):
fh2 = open(fname2,"w") fh2 = open(fname2,"w")
fh2.write(err) fh2.write(err)
fh2.close() fh2.close()
except:
pass
return (out,err) return (out,err)