private ModulePath?FindModuleInSearchPath( IEnumerable <PythonLibraryPath> searchPaths, IReadOnlyDictionary <string, string> packages, string name ) { if (searchPaths == null || packages == null) { return(null); } int i = name.IndexOf('.'); var firstBit = i < 0 ? name : name.Remove(i); string searchPath; if (packages.TryGetValue(firstBit, out searchPath) && !string.IsNullOrEmpty(searchPath)) { try { return(ModulePath.FromBasePathAndName(searchPath, name)); } catch (ArgumentException) { } } foreach (var sp in searchPaths.MaybeEnumerate()) { try { return(ModulePath.FromBasePathAndName(sp.Path, name)); } catch (ArgumentException) { } } return(null); }
private IPythonModule LoadModuleFromDirectory(string searchPath, string moduleName) { Func <string, bool> isPackage = null; if (!ModulePath.PythonVersionRequiresInitPyFiles(_langVersion)) { isPackage = Directory.Exists; } ModulePath package; try { package = ModulePath.FromBasePathAndName(searchPath, moduleName, isPackage); } catch (ArgumentException) { return(null); } EnsureSearchPathDB(); if (package.IsNativeExtension || package.IsCompiled) { _searchPathDb.LoadExtensionModule(package); } else { _searchPathDb.AddModule(package.FullName, AstPythonModule.FromFile( this, package.SourceFile, _factory.GetLanguageVersion(), package.FullName )); } var mod = _searchPathDb.GetModule(package.FullName); if (!package.IsSpecialName) { int i = package.FullName.LastIndexOf('.'); if (i >= 1) { var parent = package.FullName.Remove(i); var parentMod = _searchPathDb.GetModule(parent) as AstPythonModule; if (parentMod != null) { parentMod.AddChildModule(package.Name, mod); } } } return(mod); }
private IPythonModule LoadModuleFromZipFile(string zipFile, string moduleName) { ModulePath name; HashSet <string> packages = null; var cache = _zipPackageCache; if (cache == null) { cache = _zipPackageCache = new Dictionary <string, HashSet <string> >(); } if (!cache.TryGetValue(zipFile, out packages) || packages == null) { using (var stream = new FileStream(zipFile, FileMode.Open, FileAccess.Read)) using (var zip = new ZipArchive(stream, ZipArchiveMode.Read, true)) { cache[zipFile] = packages = new HashSet <string>( zip.Entries.Select(e => e.FullName.Replace('/', '\\')) ); } } try { name = ModulePath.FromBasePathAndName( "", moduleName, packageName => packages.Contains(packageName + '\\'), new GetModuleCallable(packages).GetModule ); } catch (ArgumentException) { return(null); } using (var stream = new FileStream(zipFile, FileMode.Open, FileAccess.Read)) using (var zip = new ZipArchive(stream, ZipArchiveMode.Read, true)) using (var sourceStream = zip.GetEntry(name.SourceFile.Replace('\\', '/'))?.Open()) { if (sourceStream == null) { return(null); } return(PythonModuleLoader.FromStream( this, sourceStream, PathUtils.GetAbsoluteFilePath(zipFile, name.SourceFile), _factory.GetLanguageVersion() )); } }
private IPythonModule LoadModuleFromDirectory(string searchPath, string moduleName) { Func <string, bool> isPackage = null; if (!ModulePath.PythonVersionRequiresInitPyFiles(_langVersion)) { isPackage = Directory.Exists; } ModulePath package; try { package = ModulePath.FromBasePathAndName(searchPath, moduleName, isPackage); } catch (ArgumentException) { return(null); } var db = EnsureSearchPathDB(); if (package.IsNativeExtension || package.IsCompiled) { db.LoadExtensionModule(package); } else { db.AddModule(package.FullName, PythonModuleLoader.FromFile( this, package.SourceFile, _factory.GetLanguageVersion(), package.FullName )); } if (db != _searchPathDb) { // Racing with the DB being invalidated. // It's okay if we miss it here, so don't worry // about taking the lock. return(null); } var mod = db.GetModule(package.FullName); if (!package.IsSpecialName) { var i = package.FullName.LastIndexOf('.'); if (i >= 1) { var parent = package.FullName.Remove(i); var parentMod = db.GetModule(parent) as AstPythonModule; if (parentMod != null) { parentMod.AddChildModule(package.Name, mod); } } } lock (_searchPathDbLock) { if (db != _searchPathDb) { // Raced with the DB being invalidated return(null); } } return(mod); }