public static object ReloadModule(CodeContext /*!*/ context, PythonModule /*!*/ module) { PythonContext pc = context.LanguageContext; // We created the module and it only contains Python code. If the user changes // __file__ we'll reload from that file. string fileName = module.GetFile() as string; // built-in module: if (fileName == null) { ReloadBuiltinModule(context, module); return(module); } string name = module.GetName(); if (name != null) { PythonList path = null; // find the parent module and get it's __path__ property int dotIndex = name.LastIndexOf('.'); if (dotIndex != -1) { PythonModule parentModule; path = GetParentPathAndModule(context, name.Substring(0, dotIndex), out parentModule); } object reloaded; if (TryLoadMetaPathModule(context, module.GetName() as string, path, out reloaded) && reloaded != null) { return(module); } PythonList sysPath; if (context.LanguageContext.TryGetSystemPath(out sysPath)) { object ret = ImportFromPathHook(context, name, name, sysPath, null); if (ret != null) { return(ret); } } } if (!pc.DomainManager.Platform.FileExists(fileName)) { throw PythonOps.SystemError("module source file not found"); } var sourceUnit = pc.CreateFileUnit(fileName, pc.DefaultEncoding, SourceCodeKind.File); pc.GetScriptCode(sourceUnit, name, ModuleOptions.None, Compiler.CompilationMode.Lookup).Run(module.Scope); return(module); }
private static PythonModule LoadModuleFromSource(CodeContext /*!*/ context, string /*!*/ name, string /*!*/ path) { Assert.NotNull(context, name, path); PythonContext pc = context.LanguageContext; string fullPath = GetFullPathAndValidateCase(pc, path, false); if (fullPath == null || !pc.DomainManager.Platform.FileExists(fullPath)) { return(null); } SourceUnit sourceUnit = pc.CreateFileUnit(fullPath, pc.DefaultEncoding, SourceCodeKind.File); return(LoadFromSourceUnit(context, sourceUnit, name, sourceUnit.Path)); }
private static Scope/*!*/ LoadPackageDirectory(PythonContext/*!*/ context, string moduleName, string path) { string initPath = Path.Combine(path, "__init__.py"); SourceUnit sourceUnit = context.CreateFileUnit(initPath, context.DefaultEncoding); return context.CompileModule(initPath, moduleName, sourceUnit, ModuleOptions.Initialize).Scope; }
/// <summary> /// Provides a helper for compiling a group of modules into a single assembly. The assembly can later be /// reloaded using the clr.AddReference API. /// </summary> public static void CompileModules(CodeContext /*!*/ context, string /*!*/ assemblyName, [ParamDictionary] IDictionary <string, object> kwArgs, params string /*!*/[] /*!*/ filenames) { ContractUtils.RequiresNotNull(assemblyName, "assemblyName"); ContractUtils.RequiresNotNullItems(filenames, "filenames"); PythonContext pc = PythonContext.GetContext(context); for (int i = 0; i < filenames.Length; i++) { filenames[i] = Path.GetFullPath(filenames[i]); } Dictionary <string, string> packageMap = BuildPackageMap(filenames); List <SavableScriptCode> code = new List <SavableScriptCode>(); foreach (string filename in filenames) { if (!pc.DomainManager.Platform.FileExists(filename)) { throw PythonOps.IOError("Couldn't find file for compilation: {0}", filename); } ScriptCode sc; string modName; string dname = Path.GetDirectoryName(filename); string outFilename = ""; if (Path.GetFileName(filename) == "__init__.py") { // remove __init__.py to get package name dname = Path.GetDirectoryName(dname); if (String.IsNullOrEmpty(dname)) { modName = Path.GetDirectoryName(filename); } else { modName = Path.GetFileNameWithoutExtension(Path.GetDirectoryName(filename)); } outFilename = Path.DirectorySeparatorChar + "__init__.py"; } else { modName = Path.GetFileNameWithoutExtension(filename); } // see if we have a parent package, if so incorporate it into // our name string parentPackage; if (packageMap.TryGetValue(dname, out parentPackage)) { modName = parentPackage + "." + modName; } outFilename = modName.Replace('.', Path.DirectorySeparatorChar) + outFilename; SourceUnit su = pc.CreateSourceUnit( new FileStreamContentProvider( context.LanguageContext.DomainManager.Platform, filename ), outFilename, pc.DefaultEncoding, SourceCodeKind.File ); sc = PythonContext.GetContext(context).GetScriptCode(su, modName, ModuleOptions.Initialize, Compiler.CompilationMode.ToDisk); code.Add((SavableScriptCode)sc); } object mainModule; if (kwArgs != null && kwArgs.TryGetValue("mainModule", out mainModule)) { string strModule = mainModule as string; if (strModule != null) { if (!pc.DomainManager.Platform.FileExists(strModule)) { throw PythonOps.IOError("Couldn't find main file for compilation: {0}", strModule); } SourceUnit su = pc.CreateFileUnit(strModule, pc.DefaultEncoding, SourceCodeKind.File); code.Add((SavableScriptCode)PythonContext.GetContext(context).GetScriptCode(su, "__main__", ModuleOptions.Initialize, Compiler.CompilationMode.ToDisk)); } } SavableScriptCode.SaveToAssembly(assemblyName, code.ToArray()); }