Ejemplo n.º 1
0
 public ExtensionModuleLoader(TaskScheduler startScheduler, JTypeDatabase typeDb, IJInterpreterFactory factory, string moduleName, string extensionFilename, CancellationToken cancel)
 {
     _typeDb = typeDb;
     _factory = factory;
     _moduleName = moduleName;
     _extensionFilename = extensionFilename;
     _cancel = cancel;
     _startScheduler = startScheduler;
 }
Ejemplo n.º 2
0
            /// <summary>
            /// Finds the appropriate entry in our database file and returns the name of the .idb file to be loaded or null
            /// if we do not have a generated .idb file.
            /// </summary>
            private static string FindDbFile(IJInterpreterFactory interpreter, string extensionModuleFilename, List<string> existingModules, string dbFile, FileStream fs)
            {
                var reader = new StreamReader(fs);

                string line;
                while ((line = reader.ReadLine()) != null) {
                    // [FileName]|interpGuid|interpVersion|DateTimeStamp|[db_file.idb]
                    string[] columns = line.Split('|');
                    Guid interpGuid;
                    Version interpVersion;

                    if (columns.Length != 5) {
                        // malformed data...
                        continue;
                    }

                    if (File.Exists(columns[dbFileIndex])) {
                        // db file still exists
                        DateTime lastModified;
                        if (!File.Exists(columns[extensionModuleFilenameIndex]) ||  // extension has been deleted
                            !DateTime.TryParseExact(columns[extensionTimeStamp], "O", null, System.Globalization.DateTimeStyles.RoundtripKind, out lastModified) ||
                            lastModified != new FileInfo(extensionModuleFilename).LastWriteTime) { // extension has been modified

                            // cleanup the stale DB files as we go...
                            try {
                                File.Delete(columns[4]);
                            } catch (IOException) {
                            }
                            continue;
                        }
                    } else {
                        continue;
                    }

                    // check if this is the file we're looking for...
                    if(!Guid.TryParse(columns[interpreterGuidIndex], out interpGuid) ||            // corrupt data
                        interpGuid != interpreter.Id ||                         // not our interpreter
                        !Version.TryParse(columns[interpreterVersionIndex], out interpVersion) ||     // corrupt data
                        interpVersion != interpreter.Configuration.Version ||
                        String.Compare(columns[extensionModuleFilenameIndex], extensionModuleFilename, StringComparison.OrdinalIgnoreCase) != 0) {   // not our interpreter

                        // nope, but remember the line for when we re-write out the DB.
                        existingModules.Add(line);
                        continue;
                    }

                    // this is our file, but continue reading the other lines for when we write out the DB...
                    dbFile = columns[dbFileIndex];
                }
                return dbFile;
            }
Ejemplo n.º 3
0
 public ConfigurableJInterpreterFactory(IJInterpreterFactory realFactory)
 {
     _realFactory = realFactory;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Asynchrousnly loads the specified extension module into the type database making the completions available.
        /// 
        /// If the module has not already been analyzed it will be analyzed and then loaded.
        /// 
        /// If the specified module was already loaded it replaces the existing module.
        /// 
        /// Returns a new Task which can be blocked upon until the analysis of the new extension module is available.
        /// 
        /// If the extension module cannot be analyzed an exception is reproted.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token which can be used to cancel the async loading of the module</param>
        /// <param name="extensionModuleFilename">The filename of the extension module to be loaded</param>
        /// <param name="interpreter">The J interprefer which will be used to analyze the extension module.</param>
        /// <param name="moduleName">The module name of the extension module.</param>
        public Task LoadExtensionModuleAsync(IJInterpreterFactory interpreter, string moduleName, string extensionModuleFilename, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (_modules == null) {
                return MakeExceptionTask(new InvalidOperationException("Can only LoadModules into a cloned JTypeDatabase"));
            }

            return Task.Factory.StartNew(
                new ExtensionModuleLoader(
                    TaskScheduler.FromCurrentSynchronizationContext(),
                    this,
                    interpreter,
                    moduleName,
                    extensionModuleFilename,
                    cancellationToken
                ).LoadExtensionModule
            );
        }
Ejemplo n.º 5
0
 internal static string GetReplId(IJInterpreterFactory interpreter)
 {
     return _replGuid + " " +
         interpreter.Id + " " +
         interpreter.Configuration.Version;
 }
Ejemplo n.º 6
0
 public OpenReplCommand(int cmdId, IJInterpreterFactory factory)
 {
     _cmdId = cmdId;
     _factory = factory;
 }
Ejemplo n.º 7
0
            public InterpreterItem(IJInterpreterFactory interpreter)
            {
                var version = interpreter.Configuration.Version;
                if (version.Revision == 0) {
                    if (version.Build == 0) {
                        if (version.Minor == 0) {
                            Version = version.Major.ToString();
                        } else {
                            Version = string.Format("{0}.{1}", version.Major, version.Minor);
                        }
                    } else {
                        Version = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build);
                    }
                } else {
                    Version = version.ToString();
                }

                Display = string.Format("{0} {1}", interpreter.Description, Version);
                Id = interpreter.Id;
            }
Ejemplo n.º 8
0
 private static bool EvaluatorUsesThisInterpreter(JReplEvaluator pyEval, IJInterpreterFactory interpreter)
 {
     return pyEval != null && pyEval.Interpreter.Id == interpreter.Id && pyEval.Interpreter.Configuration.Version == interpreter.Configuration.Version;
 }
Ejemplo n.º 9
0
 public CJInterpreter(IJInterpreterFactory interpFactory, JTypeDatabase typeDb)
 {
     _typeDb = typeDb;
     _factory = interpFactory;
 }
Ejemplo n.º 10
0
        public void NewInterpreter(IJInterpreterFactory factory)
        {
            bool firstInterpreter;
            if (firstInterpreter = !_showSettingsFor.Enabled) {
                // previously there were no interpreters installed
                _showSettingsFor.Items.Clear();
                _showSettingsFor.Enabled = true;
                _startupScript.Enabled = true;
                _executionMode.Enabled = true;
                _completionModeGroup.Enabled = true;
                _liveCompletionsOnly.Enabled = true;
                EnableOrDisableOptions(true);
            }

            _factories.Add(factory);
            _showSettingsFor.Items.Add(factory.GetInterpreterDisplay());
            OptionsPage._options[factory] = OptionsPage.GetOptions(factory);
            if (firstInterpreter) {
                _showSettingsFor.SelectedIndex = 0;
            }

            RefreshOptions();
        }
Ejemplo n.º 11
0
        private void DatabaseGenerated(IJInterpreterFactory curFactory)
        {
            // default analyzer
            if (JToolsPackage.Instance.DefaultAnalyzer.InterpreterFactory == curFactory) {
                JToolsPackage.Instance.RecreateAnalyzer();
            }

            // all open projects
            foreach (EnvDTE.Project project in JToolsPackage.Instance.DTE.Solution.Projects) {
                var pyProj = project.GetJProject();
                if (pyProj != null) {
                    var analyzer = pyProj.GetAnalyzer();
                    if (analyzer != null && analyzer.InterpreterFactory == curFactory) {
                        pyProj.ClearInterpreter();
                    }
                }
            }
        }
Ejemplo n.º 12
0
        /*!*/
        internal static IReplWindow EnsureReplWindow(IJInterpreterFactory factory)
        {
            var compModel = JToolsPackage.ComponentModel;
            var provider = compModel.GetService<IReplWindowProvider>();

            string replId = JReplEvaluatorProvider.GetReplId(factory);
            var window = provider.FindReplWindow(replId);
            if (window == null) {
                window = provider.CreateReplWindow(
                    JToolsPackage.Instance.ContentType,
                    factory.GetInterpreterDisplay() + " Interactive",
                    typeof(JLanguageInfo).GUID,
                    replId
                );

                window.SetOptionValue(ReplOptions.UseSmartUpDown, JToolsPackage.Instance.InteractiveOptionsPage.GetOptions(factory).ReplSmartHistory);
            }
            return window;
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Create a JInterpreterView with values from an IJInterpreterFactory.
 /// </summary>
 public JInterpreterView(IJInterpreterFactory factory)
 {
     _name = factory.GetInterpreterDisplay();
     _id = factory.Id;
     _version = factory.Configuration.Version;
     _path = factory.Configuration.InterpreterPath;
 }
Ejemplo n.º 14
0
            private string GenerateDbFile(IJInterpreterFactory interpreter, string moduleName, string extensionModuleFilename, List<string> existingModules, string dbFile, FileStream fs)
            {
                // we need to generate the DB file
                dbFile = Path.Combine(_typeDb._sharedState.DatabaseDirectory, moduleName + ".$project.idb");
                int retryCount = 0;
                while (File.Exists(dbFile)) {
                    dbFile = Path.Combine(_typeDb._sharedState.DatabaseDirectory, moduleName + "." + ++retryCount + ".$project.idb");
                }

                var psi = new ProcessStartInfo();
                psi.CreateNoWindow = true;
                psi.UseShellExecute = false;
                psi.FileName = interpreter.Configuration.InterpreterPath;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.Arguments =
                    "\"" + Path.Combine(GetJToolsInstallPath(), "ExtensionScraper.py") + "\"" +      // script to run
                    " scrape" +                                                                           // scrape
                    " -" +                                                                                // no module name
                    " \"" + extensionModuleFilename + "\"" +                                              // extension module path
                    " \"" + dbFile.Substring(0, dbFile.Length - 4) + "\"";                                // output file path (minus .idb)

                var proc = Process.Start(psi);
                OutputDataReceiver receiver = new OutputDataReceiver();
                proc.OutputDataReceived += receiver.OutputDataReceived;
                proc.ErrorDataReceived += receiver.OutputDataReceived;

                proc.BeginOutputReadLine();
                proc.BeginErrorReadLine();

                if (_cancel.CanBeCanceled) {
                    if (WaitHandle.WaitAny(new[] { _cancel.WaitHandle, new ProcessWaitHandle(proc) }) != 1) {
                        // we were cancelled
                        return null;
                    }
                } else {
                    proc.WaitForExit();
                }

                if (proc.ExitCode == 0) {
                    // [FileName]|interpGuid|interpVersion|DateTimeStamp|[db_file.idb]
                    // save the new entry in the DB file
                    existingModules.Add(
                        String.Format("{0}|{1}|{2}|{3}|{4}",
                            extensionModuleFilename,
                            interpreter.Id,
                            interpreter.Configuration.Version,
                            new FileInfo(extensionModuleFilename).LastWriteTime.ToString("O"),
                            dbFile
                        )
                    );

                    fs.Seek(0, SeekOrigin.Begin);
                    fs.SetLength(0);
                    var sw = new StreamWriter(fs);
                    sw.Write(String.Join(Environment.NewLine, existingModules));
                    sw.Flush();
                } else {
                    throw new CannotAnalyzeExtensionException(receiver.Received.ToString());
                }

                return dbFile;
            }
Ejemplo n.º 15
0
        private bool IsSameInterpreter(IJInterpreterFactory interpreter, string interpreterName)
        {
            if (interpreter.GetInterpreterDisplay() == interpreterName) {
                return true;
            }

            return false;
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Asks the interpreter to auto-generate it's completion database if it doesn't already exist and the user
 /// hasn't disabled this option.
 /// </summary>
 internal static void EnsureCompletionDb(IJInterpreterFactory fact) {
     if (JToolsPackage.Instance.OptionsPage.AutoAnalyzeStandardLibrary) {
         IInterpreterWithCompletionDatabase interpWithDb = fact as IInterpreterWithCompletionDatabase;
         if (interpWithDb != null) {
             interpWithDb.AutoGenerateCompletionDatabase();
         }
     }
 }
Ejemplo n.º 17
0
 public AutomationInterpreterOptions(IJInterpreterFactory interpreterFactory)
 {
     _interpreterFactory = interpreterFactory;
 }
Ejemplo n.º 18
0
 public JInteractiveOptions GetOptions(IJInterpreterFactory interpreterFactory)
 {
     JInteractiveOptions options;
     if (!_options.TryGetValue(interpreterFactory, out options)) {
         _options[interpreterFactory] = options = ReadOptions(interpreterFactory.GetInterpreterPath() + "\\");
     }
     return options;
 }
Ejemplo n.º 19
0
        protected override void Connect()
        {
            _interpreter = GetInterpreterFactory();
            var processInfo = new ProcessStartInfo(Path.Combine(JToolsPackage.GetJToolsInstallPath(), (Interpreter.Configuration.Architecture == ProcessorArchitecture.X86 ? "j32" : "j64"), "J.Console.exe"));

            #if DEBUG
            const string debugReplEnv = "DEBUG_REPL";
            Environment.SetEnvironmentVariable(debugReplEnv, "true");
            bool debugMode = Environment.GetEnvironmentVariable(debugReplEnv) != null;
            processInfo.CreateNoWindow = !debugMode;
            processInfo.UseShellExecute = debugMode;
            processInfo.RedirectStandardOutput = !debugMode;
            processInfo.RedirectStandardError = !debugMode;
            processInfo.RedirectStandardInput = !debugMode;
            #else
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;
            processInfo.RedirectStandardOutput = true;
            processInfo.RedirectStandardError = true;
            processInfo.RedirectStandardInput = true;
            #endif

            Socket conn;
            int portNum;
            CreateConnection(out conn, out portNum);

            List<string> args = new List<string>();

            if (!String.IsNullOrWhiteSpace(InterpreterOptions)) {
                args.Add(InterpreterOptions);
            }

            string filename, dir, extraArgs = null;
            VsProjectAnalyzer analyzer;
            if (JToolsPackage.TryGetStartupFileAndDirectory(out filename, out dir, out analyzer)) {
                processInfo.WorkingDirectory = dir;
                var startupProj = JToolsPackage.GetStartupProject();
                if (startupProj != null) {
                    string searchPath = startupProj.GetProjectProperty(CommonConstants.SearchPath, true);
                    string pathEnvVar = Interpreter.Configuration.PathEnvironmentVariable;
                    if (!string.IsNullOrEmpty(searchPath) && !String.IsNullOrWhiteSpace(pathEnvVar)) {
                        processInfo.EnvironmentVariables[pathEnvVar] = searchPath;
                    }

                    string interpArgs = startupProj.GetProjectProperty(CommonConstants.InterpreterArguments, true);
                    if (!String.IsNullOrWhiteSpace(interpArgs)) {
                        args.Add(interpArgs);
                    }

                    extraArgs = startupProj.GetProjectProperty(CommonConstants.CommandLineArguments, true);
                }
                if (analyzer.InterpreterFactory == _interpreter) {
                    if (_replAnalyzer != null && _replAnalyzer != analyzer) {
                        analyzer.SwitchAnalyzers(_replAnalyzer);
                    }
                    _replAnalyzer = analyzer;
                    _ownsAnalyzer = false;
                }
            }

            //args.Add("\"" + Path.Combine(JToolsPackage.GetJToolsInstallPath(), "visualstudio_py_repl.py") + "\"");
            args.Add("--port");
            args.Add(portNum.ToString());
            args.Add("-r");

            if (!String.IsNullOrWhiteSpace(StartupScript)) {
                args.Add("--launch_file");
                args.Add("\"" + StartupScript + "\"");
            }

            _enableAttach = EnableAttach;
            if (EnableAttach) {
                args.Add("--enable-attach");
            }

            bool multipleScopes = true;
            if (!String.IsNullOrWhiteSpace(ExecutionMode)) {
                // change ID to module name if we have a registered mode
                var modes = J.Tools.Options.ExecutionMode.GetRegisteredModes();
                string modeValue = ExecutionMode;
                foreach (var mode in modes) {
                    if (mode.Id == ExecutionMode) {
                        modeValue = mode.Type;
                        multipleScopes = mode.SupportsMultipleScopes;
                        break;
                    }
                }
                args.Add("--execution_mode");
                args.Add(modeValue);
            }

            SetMultipleScopes(multipleScopes);

            processInfo.Arguments = String.Join(" ", args);

            var process = new Process();
            process.StartInfo = processInfo;
            try {
                process.Start();
            } catch (Exception e) {
                Window.WriteError(String.Format("Failed to start interactive process: {0}{1}{2}", Environment.NewLine, e.ToString(), Environment.NewLine));
                return;
            }

            CreateCommandProcessor(conn, null, processInfo.RedirectStandardOutput, process);
        }