Commit bce20eb6 authored by Stefan Behnel's avatar Stefan Behnel Committed by GitHub

Merge pull request #2836 from jbrockmendel/uref

REF: make Utils dependency-free by moving search_include_directories
parents 8f1833a3 2cc9535e
...@@ -272,7 +272,7 @@ class Context(object): ...@@ -272,7 +272,7 @@ class Context(object):
def search_include_directories(self, qualified_name, suffix, pos, def search_include_directories(self, qualified_name, suffix, pos,
include=False, sys_path=False): include=False, sys_path=False):
return Utils.search_include_directories( return search_include_directories(
tuple(self.include_directories), qualified_name, suffix, pos, include, sys_path) tuple(self.include_directories), qualified_name, suffix, pos, include, sys_path)
def find_root_package_dir(self, file_path): def find_root_package_dir(self, file_path):
...@@ -620,6 +620,60 @@ def compile(source, options = None, full_module_name = None, **kwds): ...@@ -620,6 +620,60 @@ def compile(source, options = None, full_module_name = None, **kwds):
return compile_multiple(source, options) return compile_multiple(source, options)
@Utils.cached_function
def search_include_directories(dirs, qualified_name, suffix, pos,
include=False, sys_path=False):
"""
Search the list of include directories for the given file name.
If a source file position is given, first searches the directory
containing that file. Returns None if not found, but does not
report an error.
The 'include' option will disable package dereferencing.
If 'sys_path' is True, also search sys.path.
"""
if sys_path:
dirs = dirs + tuple(sys.path)
if pos:
file_desc = pos[0]
if not isinstance(file_desc, FileSourceDescriptor):
raise RuntimeError("Only file sources for code supported")
if include:
dirs = (os.path.dirname(file_desc.filename),) + dirs
else:
dirs = (Utils.find_root_package_dir(file_desc.filename),) + dirs
dotted_filename = qualified_name
if suffix:
dotted_filename += suffix
if not include:
names = qualified_name.split('.')
package_names = tuple(names[:-1])
module_name = names[-1]
module_filename = module_name + suffix
package_filename = "__init__" + suffix
for dirname in dirs:
path = os.path.join(dirname, dotted_filename)
if os.path.exists(path):
return path
if not include:
package_dir = Utils.check_package_dir(dirname, package_names)
if package_dir is not None:
path = os.path.join(package_dir, module_filename)
if os.path.exists(path):
return path
path = os.path.join(dirname, package_dir, module_name,
package_filename)
if os.path.exists(path):
return path
return None
# ------------------------------------------------------------------------ # ------------------------------------------------------------------------
# #
# Main command-line entry point # Main command-line entry point
......
...@@ -135,54 +135,6 @@ def copy_file_to_dir_if_newer(sourcefile, destdir): ...@@ -135,54 +135,6 @@ def copy_file_to_dir_if_newer(sourcefile, destdir):
shutil.copy2(sourcefile, destfile) shutil.copy2(sourcefile, destfile)
@cached_function
def search_include_directories(dirs, qualified_name, suffix, pos,
include=False, sys_path=False):
# Search the list of include directories for the given
# file name. If a source file position is given, first
# searches the directory containing that file. Returns
# None if not found, but does not report an error.
# The 'include' option will disable package dereferencing.
# If 'sys_path' is True, also search sys.path.
if sys_path:
dirs = dirs + tuple(sys.path)
if pos:
file_desc = pos[0]
from Cython.Compiler.Scanning import FileSourceDescriptor
if not isinstance(file_desc, FileSourceDescriptor):
raise RuntimeError("Only file sources for code supported")
if include:
dirs = (os.path.dirname(file_desc.filename),) + dirs
else:
dirs = (find_root_package_dir(file_desc.filename),) + dirs
dotted_filename = qualified_name
if suffix:
dotted_filename += suffix
if not include:
names = qualified_name.split('.')
package_names = tuple(names[:-1])
module_name = names[-1]
module_filename = module_name + suffix
package_filename = "__init__" + suffix
for dirname in dirs:
path = os.path.join(dirname, dotted_filename)
if path_exists(path):
return path
if not include:
package_dir = check_package_dir(dirname, package_names)
if package_dir is not None:
path = os.path.join(package_dir, module_filename)
if path_exists(path):
return path
path = os.path.join(dirname, package_dir, module_name,
package_filename)
if path_exists(path):
return path
return None
@cached_function @cached_function
def find_root_package_dir(file_path): def find_root_package_dir(file_path):
dir = os.path.dirname(file_path) dir = os.path.dirname(file_path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment