提交 ea89ea63 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Always convert compiledir to real path to avoid relative paths and symlinks mismatch

上级 d62503f3
...@@ -4,7 +4,7 @@ import os, sys ...@@ -4,7 +4,7 @@ import os, sys
import platform import platform
import re import re
from theano.configparser import config, AddConfigVar, StrParam from theano.configparser import config, AddConfigVar, ConfigParam, StrParam
def default_compiledirname(): def default_compiledirname():
platform_id = '-'.join([ platform_id = '-'.join([
...@@ -14,7 +14,15 @@ def default_compiledirname(): ...@@ -14,7 +14,15 @@ def default_compiledirname():
platform_id = re.sub("[\(\)\s]+", "_", platform_id) platform_id = re.sub("[\(\)\s]+", "_", platform_id)
return 'compiledir_' + platform_id return 'compiledir_' + platform_id
def is_valid_compiledir(path):
def filter_compiledir(path):
# Turn path into the 'real' path. This ensures that:
# 1. There is no relative path, which would fail e.g. when trying to
# import modules from the compile dir.
# 2. The path is stable w.r.t. e.g. symlinks (which makes it easier
# to re-use compiled modules).
path = os.path.realpath(path)
valid = True
if not os.access(path, os.R_OK | os.W_OK): if not os.access(path, os.R_OK | os.W_OK):
try: try:
os.makedirs(path, 0770) #read-write-execute for this user only os.makedirs(path, 0770) #read-write-execute for this user only
...@@ -22,17 +30,22 @@ def is_valid_compiledir(path): ...@@ -22,17 +30,22 @@ def is_valid_compiledir(path):
# Maybe another parallel execution of theano was trying to create # Maybe another parallel execution of theano was trying to create
# the same directory at the same time. # the same directory at the same time.
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
return False valid = False
if valid:
try: try:
# PROBLEM: sometimes the first approach based on os.system('touch') # PROBLEM: sometimes the initial approach based on
# returned -1 for an unknown reason; the alternate approach here worked # os.system('touch') returned -1 for an unknown reason; the
# in all cases... it was weird. # alternate approach here worked in all cases... it was weird.
open(os.path.join(path, '__init__.py'), 'w').close() open(os.path.join(path, '__init__.py'), 'w').close()
except: except:
return False valid = False
if not valid:
raise ValueError('Invalid value for compiledir: %s' % path)
return path
return True
AddConfigVar('base_compiledir', AddConfigVar('base_compiledir',
"arch-independent cache directory for compiled modules", "arch-independent cache directory for compiled modules",
...@@ -40,9 +53,9 @@ AddConfigVar('base_compiledir', ...@@ -40,9 +53,9 @@ AddConfigVar('base_compiledir',
AddConfigVar('compiledir', AddConfigVar('compiledir',
"arch-dependent cache directory for compiled modules", "arch-dependent cache directory for compiled modules",
StrParam( ConfigParam(
os.path.join( os.path.join(
os.path.expanduser(config.base_compiledir), os.path.expanduser(config.base_compiledir),
default_compiledirname()), default_compiledirname()),
is_valid=is_valid_compiledir, filter=filter_compiledir,
allow_override=False)) allow_override=False))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论