Пример #1
0
        private void InitializeModules()
        {
            string executable = "";
            string prefix     = null;
            // paths
            Assembly entryAssembly = Assembly.GetEntryAssembly();

            // Can be null if called from unmanaged code (VS integration scenario)
            if (entryAssembly != null)
            {
                executable = entryAssembly.Location;
                prefix     = Path.GetDirectoryName(executable);
#if NETCOREAPP || NETSTANDARD
                if (Path.GetExtension(executable) == ".dll")
                {
                    var name = Path.GetFileNameWithoutExtension(executable);
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        var runner = Path.Combine(prefix, name + ".exe");
                        if (File.Exists(runner))
                        {
                            executable = runner;
                        }
                        else
                        {
                            runner = Path.Combine(prefix, name + ".bat");
                            if (File.Exists(runner))
                            {
                                executable = runner;
                            }
                        }
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                    {
                        var runner = Path.Combine(prefix, name);
                        if (File.Exists(runner))
                        {
                            executable = runner;
                        }
                        else
                        {
                            runner = Path.Combine(prefix, name + ".sh");
                            if (File.Exists(runner))
                            {
                                executable = runner;
                            }
                        }
                    }
                }
#endif
            }

            // Make sure there an IronPython Lib directory, and if not keep looking up
            while (prefix != null && !File.Exists(Path.Combine(prefix, "Lib/os.py")))
            {
                prefix = Path.GetDirectoryName(prefix);
            }

            PythonContext.SetHostVariables(prefix ?? "", executable, null);
        }
Пример #2
0
        private void InitializeModules()
        {
            string executable = "";
            string prefix     = null;

#if !SILVERLIGHT // paths
            Assembly entryAssembly = Assembly.GetEntryAssembly();
            //Can be null if called from unmanaged code (VS integration scenario)
            if (entryAssembly != null)
            {
                executable = entryAssembly.Location;
                prefix     = Path.GetDirectoryName(executable);
#if NETSTANDARD
                if (Path.GetExtension(executable) == ".dll")
                {
                    var runner = Path.Combine(prefix, "ipy.bat");
                    if (File.Exists(runner))
                    {
                        executable = runner;
                    }
                }
#endif
            }

            // Make sure there an IronPython Lib directory, and if not keep looking up
            while (prefix != null && !File.Exists(Path.Combine(prefix, "Lib/os.py")))
            {
                prefix = Path.GetDirectoryName(prefix);
            }
#endif

            PythonContext.SetHostVariables(prefix ?? "", executable, null);
        }
Пример #3
0
        public static void PerformModuleReload(PythonContext /*!*/ context, PythonDictionary /*!*/ dict)
        {
            dict["stdin"]  = dict["__stdin__"];
            dict["stdout"] = dict["__stdout__"];
            dict["stderr"] = dict["__stderr__"];

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                dict["platform"] = "win32";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                dict["platform"] = "posix";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                dict["platform"] = "darwin";
            }
            else
            {
                dict["platform"] = "cli";
            }

            // !!! These fields do need to be reset on "reload(sys)". However, the initial value is specified by the
            // engine elsewhere. For now, we initialize them just once to some default value
            dict["warnoptions"] = new List(0);

            PublishBuiltinModuleNames(context, dict);
            context.SetHostVariables(dict);

            dict["meta_path"]  = new List(0);
            dict["path_hooks"] = new List(0);

            // add zipimport to the path hooks for importing from zip files.
            try {
                PythonModule zipimport = Importer.ImportModule(
                    context.SharedClsContext, context.SharedClsContext.GlobalDict,
                    "zipimport", false, 0) as PythonModule;
                if (zipimport != null)
                {
                    object zipimporter = PythonOps.GetBoundAttr(
                        context.SharedClsContext, zipimport, "zipimporter");
                    List path_hooks = dict["path_hooks"] as List;
                    if (path_hooks != null && zipimporter != null)
                    {
                        path_hooks.Add(zipimporter);
                    }
                }
            } catch {
                // this is not a fatal error, so we don't do anything.
            }

            dict["path_importer_cache"] = new PythonDictionary();
        }
Пример #4
0
        private void InitializeModules()
        {
            string executable = "";
            string prefix     = "";

#if !SILVERLIGHT // paths
            Assembly entryAssembly = Assembly.GetEntryAssembly();
            //Can be null if called from unmanaged code (VS integration scenario)
            if (entryAssembly != null)
            {
                executable = entryAssembly.Location;
                prefix     = Path.GetDirectoryName(executable);
            }
#endif
            PythonContext.SetHostVariables(prefix, executable, null);
        }
Пример #5
0
        private void InitializeModules()
        {
            string executable = "";
            string prefix     = null;

            Assembly entryAssembly = Assembly.GetEntryAssembly();

            //Can be null if called from unmanaged code (VS integration scenario)
            if (entryAssembly != null)
            {
                executable = entryAssembly.Location;
                prefix     = Path.GetDirectoryName(executable);
            }

            // Make sure there an IronPython Lib directory, and if not keep looking up
            while (prefix != null && !File.Exists(Path.Combine(prefix, "Lib/os.py")))
            {
                prefix = Path.GetDirectoryName(prefix);
            }

            PythonContext.SetHostVariables(prefix ?? "", executable, null);
        }
        private void InitializeModules()
        {
            string executable = "";
            string prefix     = null;

            Assembly entryAssembly = Assembly.GetEntryAssembly();

            //Can be null if called from unmanaged code (VS integration scenario)
            if (entryAssembly != null)
            {
                executable = entryAssembly.Location;
                prefix     = Path.GetDirectoryName(executable);
#if NETCOREAPP2_0 || NETCOREAPP2_1
                if (Path.GetExtension(executable) == ".dll")
                {
                    var name   = Path.GetFileNameWithoutExtension(executable);
                    var runner = Path.Combine(prefix, name + ".bat");
                    if (Environment.OSVersion.Platform == PlatformID.Unix)
                    {
                        runner = Path.Combine(prefix, name + ".sh");
                    }
                    if (File.Exists(runner))
                    {
                        executable = runner;
                    }
                }
#endif
            }

            // Make sure there an IronPython Lib directory, and if not keep looking up
            while (prefix != null && !File.Exists(Path.Combine(prefix, "Lib/os.py")))
            {
                prefix = Path.GetDirectoryName(prefix);
            }

            PythonContext.SetHostVariables(prefix ?? "", executable, null);
        }
Пример #7
0
        private void InitializeModules()
        {
            string executable = "";
            string prefix     = null;

            Assembly entryAssembly = Assembly.GetEntryAssembly();

            //Can be null if called from unmanaged code (VS integration scenario)
            if (entryAssembly != null)
            {
                executable = entryAssembly.Location;
                prefix     = Path.GetDirectoryName(executable);

                var name = Path.GetFileNameWithoutExtension(executable);
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    var runner = Path.Combine(prefix, name + ".exe");
                    if (File.Exists(runner))
                    {
                        executable = runner;
                    }
                    else
                    {
                        runner = Path.Combine(prefix, name + ".bat");
                        if (File.Exists(runner))
                        {
                            executable = runner;
                        }
                    }
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    var runner = Path.Combine(prefix, name);
                    if (File.Exists(runner))
                    {
                        executable = runner;
                    }
                    else
                    {
                        runner = Path.Combine(prefix, name + ".sh");
                        if (File.Exists(runner))
                        {
                            executable = runner;
                        }
                    }
                }
            }

            if (prefix is not null)
            {
                string pyvenv_prefix = null;

                // look for pyvenv.cfg in the current folder and then the parent folder
                var path = Path.Combine(prefix, "pyvenv.cfg");
                for (var i = 0; i < 2; i++)
                {
                    if (File.Exists(path))
                    {
                        foreach (var line in File.ReadAllLines(path, Encoding.UTF8))   // TODO: this actually needs to be decoded with surrogateescape
                        {
                            if (line.StartsWith("#", StringComparison.Ordinal))
                            {
                                continue;
                            }
                            var split = line.Split(new[] { '=' }, 2);
                            if (split.Length != 2)
                            {
                                continue;
                            }
                            if (split[0].Trim() == "home")
                            {
                                pyvenv_prefix = split[1].Trim();
                                break;
                            }
                        }
                        break;
                    }
                    path = Path.Combine(Path.GetDirectoryName(prefix), "pyvenv.cfg");
                }

                if (pyvenv_prefix is null)
                {
                    // Make sure there an IronPython Lib directory, and if not keep looking up
                    while (prefix != null && !File.Exists(Path.Combine(prefix, "Lib/os.py")))
                    {
                        prefix = Path.GetDirectoryName(prefix);
                    }
                }
                else
                {
                    prefix = pyvenv_prefix;
                }
            }

            PythonContext.SetHostVariables(prefix ?? "", executable, null);
        }