Beispiel #1
0
        public static void Init()
        {
            string pythonPath = $"{Entity.Type.Resource.Instance.Directory}/{PythonProgram}";
            string kerasPath  = $"{Entity.Type.Resource.Instance.Directory}/{KerasScript}";

            if (!IsProcessRunning && File.Exists(kerasPath))
            {
                IsProcessRunning = true;

                Server = new TcpListener(IPAddress.Loopback, 0);
                Server.Start();

                int port = ((IPEndPoint)Server.LocalEndpoint).Port;

                Debug.WriteLine($"Running server on port {port}");

                PythonProcess.StartInfo.FileName  = pythonPath;
                PythonProcess.StartInfo.Arguments = $"\"{kerasPath}\" -p {port}";
                PythonProcess.Start();

                DateTime startTime = DateTime.Now;

                while ((DateTime.Now - startTime).Seconds < 10)
                {
                    if (Server.Pending())
                    {
                        Client = Server.AcceptTcpClient();
                        break;
                    }
                    Thread.Sleep(100);
                }

                if (Client == null)
                {
                    throw new SocketException((int)SocketError.TimedOut);
                }

                Input = new StreamWriter(Client.GetStream())
                {
                    AutoFlush = false
                };
                Output = new StreamReader(Client.GetStream());

                ProcessThread = Task
                                .Run(() =>
                {
                    PythonProcess.WaitForExit();
                    Server.Stop();
                })
                                .ContinueWith(t => IsProcessRunning = false);
            }
        }
Beispiel #2
0
        internal void StartAndWaitForExit(PythonProcess process)
        {
            bool exited = false;

            try {
                process.Start();
                exited = process.WaitForExit(DefaultWaitForExitTimeout);
            } finally {
                if (!exited && !process.HasExited)
                {
                    process.Terminate();
                    Assert.Fail("Timeout while waiting for Python process to exit.");
                }
            }
        }
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.Equals("Player"))
        {
            byte[] picture = DLMI_Control.controller.GetPictures();
            transform.parent.GetComponent <MoveObject> ().PlayerPos = Manager.manager.Player.transform.position.x;
            pro = new PythonProcess();
            Process p = DLMI_Control.controller.p;
            p.StartInfo.Arguments = DLMI_Control.controller.scriptName + " " + picture;

            pro.p       = p;
            pro.s       = gameObject;
            pro.picture = picture;
            pro.Start();
        }
    }
Beispiel #4
0
        private void Attach(string filename, int lineNo)
        {
            var           debugger = new PythonDebugger();
            PythonProcess process  = debugger.DebugProcess(Version, DebuggerTestPath + filename, (newproc, newthread) => {
                var breakPoint = newproc.AddBreakPointByFileExtension(lineNo, filename);
                breakPoint.Add();
                _evaluator.AttachProcess(newproc, new MockThreadIdMapper());
            },
                                                           debugOptions: PythonDebugOptions.CreateNoWindow);

            _processes.Add(process);

            using (var brkHit = new AutoResetEvent(false))
                using (var procExited = new AutoResetEvent(false)) {
                    EventHandler <BreakpointHitEventArgs> breakpointHitHandler = (s, e) => brkHit.Set();
                    EventHandler <ProcessExitedEventArgs> processExitedHandler = (s, e) => procExited.Set();
                    process.BreakpointHit += breakpointHitHandler;
                    process.ProcessExited += processExitedHandler;

                    try {
                        process.Start();
                    } catch (Win32Exception ex) {
                        _processes.Remove(process);
#if DEV11_OR_LATER
                        if (ex.HResult == -2147467259 /*0x80004005*/)
                        {
                            Assert.Inconclusive("Required Python interpreter is not installed");
                        }
                        else
#endif
                        {
                            Assert.Fail("Process start failed:\r\n" + ex.ToString());
                        }
                    }

                    var handles = new[] { brkHit, procExited };
                    if (WaitHandle.WaitAny(handles, 25000) != 0)
                    {
                        Assert.Fail("Failed to wait on event");
                    }

                    process.BreakpointHit -= breakpointHitHandler;
                    process.ProcessExited -= processExitedHandler;
                }

            _evaluator.AttachProcess(process, new MockThreadIdMapper());
        }
Beispiel #5
0
        internal PythonThread RunAndBreak(string filename, int lineNo, string breakFilename = null, string arguments = "", Action processLoaded = null, PythonDebugOptions debugOptions = PythonDebugOptions.RedirectOutput)
        {
            PythonThread thread;

            var debugger = new PythonDebugger();

            thread = null;
            PythonProcess process = DebugProcess(debugger, DebuggerTestPath + filename, (newproc, newthread) => {
                var breakPoint = newproc.AddBreakPointByFileExtension(lineNo, breakFilename ?? filename);
                breakPoint.Add();
                thread = newthread;
                if (processLoaded != null)
                {
                    processLoaded();
                }
            },
                                                 arguments: arguments,
                                                 debugOptions: debugOptions);

            AutoResetEvent brkHit = new AutoResetEvent(false);

            process.BreakpointHit += (sender, args) => {
                thread = args.Thread;
                brkHit.Set();
            };

            bool ready = false;

            try {
                process.Start();

                AssertWaited(brkHit);
                ready = true;
            } finally {
                if (!ready)
                {
                    process.Terminate();
                }
            }

            return(thread);
        }
Beispiel #6
0
        // Launches a process by means of the debug engine.
        // Normally, Visual Studio launches a program using the IDebugPortEx2::LaunchSuspended method and then attaches the debugger
        // to the suspended program. However, there are circumstances in which the debug engine may need to launch a program
        // (for example, if the debug engine is part of an interpreter and the program being debugged is an interpreted language),
        // in which case Visual Studio uses the IDebugEngineLaunch2::LaunchSuspended method
        // The IDebugEngineLaunch2::ResumeProcess method is called to start the process after the process has been successfully launched in a suspended state.
        int IDebugEngineLaunch2.LaunchSuspended(string pszServer, IDebugPort2 port, string exe, string args, string dir, string env, string options, enum_LAUNCH_FLAGS launchFlags, uint hStdInput, uint hStdOutput, uint hStdError, IDebugEventCallback2 ad7Callback, out IDebugProcess2 process)
        {
            Debug.WriteLine("PythonEngine LaunchSuspended Begin " + launchFlags + " " + GetHashCode());
            AssertMainThread();
            Debug.Assert(_events == null);
            Debug.Assert(_process == null);
            Debug.Assert(_ad7ProgramId == Guid.Empty);

            process = null;

            _events = ad7Callback;

            PythonLanguageVersion version      = DefaultVersion;
            bool            waitOnAbnormalExit = false;
            bool            redirectOutput     = false;
            List <string[]> dirMapping         = null;

            if (options != null)
            {
                var splitOptions = options.Split(new[] { ';' }, 2);
                foreach (var optionSetting in splitOptions)
                {
                    var setting = optionSetting.Split(new[] { '=' }, 2);

                    if (setting.Length == 2)
                    {
                        switch (setting[0])
                        {
                        case VersionSetting: version = GetLanguageVersion(setting[1]); break;

                        case WaitOnAbnormalExitSetting:
                            bool value;
                            if (Boolean.TryParse(setting[1], out value))
                            {
                                waitOnAbnormalExit = value;
                            }
                            break;

                        case RedirectOutputSetting:
                            if (Boolean.TryParse(setting[1], out value))
                            {
                                redirectOutput = value;
                            }
                            break;

                        case DirMappingSetting:
                            string[] dirs = setting[1].Split('|');
                            if (dirs.Length == 2)
                            {
                                if (dirMapping == null)
                                {
                                    dirMapping = new List <string[]>();
                                }
                                Debug.WriteLine(String.Format("Mapping dir {0} to {1}", dirs[0], dirs[1]));
                                dirMapping.Add(dirs);
                            }
                            break;
                        }
                    }
                }
            }

            _process = new PythonProcess(version, exe, args, dir, env, waitOnAbnormalExit, redirectOutput, dirMapping);

            AttachEvents(_process);

            _programCreated = false;
            _loadComplete.Reset();

            _process.Start();

            AD_PROCESS_ID adProcessId = new AD_PROCESS_ID();

            adProcessId.ProcessIdType = (uint)enum_AD_PROCESS_ID.AD_PROCESS_ID_SYSTEM;
            adProcessId.dwProcessId   = (uint)_process.Id;

            EngineUtils.RequireOk(port.GetProcess(adProcessId, out process));
            Debug.WriteLine("PythonEngine LaunchSuspended returning S_OK");
            Debug.Assert(process != null);
            Debug.Assert(!_process.HasExited);

            return(VSConstants.S_OK);
        }