def test_python_module_file2(self): filepath = join(FILES_DIR, '000/001') script = join(FILES_DIR, 'python/a.py') assert exists(filepath) assert exists(script) self.assert_(not utils.python_module_file(None)) self.assert_(not utils.python_module_file("")) self.assert_(utils.python_module_file(script), "Expected python file: %s" % script) self.assert_(not utils.python_module_file(filepath), "Expected not python file: %s" % filepath) self.assert_(not utils.python_module_file("not_found_file"))
def test_python_module_file(self): self.assert_(utils.python_module_file(self.pypath)) self.assert_(utils.python_module_file(self.pycpath)) self.assert_(utils.python_module_file(self.pyopath)) self.assert_(not utils.python_module_file(self.notpypath))
def _resolve(self, filepath): if not filepath: raise ImportError("filepath is empty") filepath = normalize_path(filepath) if not os.path.exists(filepath): raise ImportError, "No such file or directory: %s" % filepath if not utils.python_module_file(filepath): raise ImportError, "Not a python script at %s" % filepath # Searching... # # split path components # ignore extention (e.g. '.py', '.pyc') dirpath, modname = os.path.split(filepath) modname, _ = os.path.splitext(modname) skipped_name = None package_name = None compiled_forms = [(dirpath, [modname])] for syspath in self.path: i = 0 while not compiled_forms[i][0] == syspath: # compile i += 1 if i == len(compiled_forms): d, names = compiled_forms[i-1] # Encountered not a package, or not found in # the module search path if not self._resolve_package(d) or d == '/': break names = names[:] # copy d, parent = os.path.split(d) names.insert(0, parent) compiled_forms.append((d, names)) else: d, names = compiled_forms[i] if self._resolve_package(dirpath): package_name = '.'.join(names[:-1]) else: package_name = None if i == 0: if self._resolve_package(d): # script is created under a package, # but its module name is not a package style # (e.g. 'package.module'). # # For example, consider: # # src/packageA/__init__.py # /a.py # # and *sys.path* is: # # ['src/packageA', 'src'] # # a.py is in src/packageA, so module name 'a' is # matched. But better module name is 'packageA.a' # because a.py is created under the package # 'packageA'. # # Store result name, and continues with # the next iteration of the **for** loop. skipped_name = names[0] package_name = None continue else: if names[-1] == '__init__': # The package initialization module. # Remove tail '__init__' del names[-1] return '.'.join(names), package_name if skipped_name is not None: return skipped_name, package_name else: raise ImportError("No module name found: %s" % filepath)