Beispiel #1
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;
            }
Beispiel #2
0
        private static void GetLibDirs(JTypeDatabaseCreationRequest request, out string libDir, out string virtualEnvPackages)
        {
            libDir = Path.Combine(Path.GetDirectoryName(request.Factory.Configuration.InterpreterPath), "Lib");
            virtualEnvPackages = null;
            if (!Directory.Exists(libDir)) {
                string virtualEnvLibDir = Path.Combine(Path.GetDirectoryName(request.Factory.Configuration.InterpreterPath), "..\\Lib");
                string prefixFile = Path.Combine(virtualEnvLibDir, "orig-prefix.txt");
                if (Directory.Exists(virtualEnvLibDir) && File.Exists(prefixFile)) {
                    // virtual env is setup differently.  The EXE is in a Scripts directory with the Lib dir being at ..\Lib
                    // relative to the EXEs dir.  There is alos an orig-prefix.txt which points at the normal full J
                    // install.  Parse that file and include the normal J install in the analysis.
                    try {
                        var lines = File.ReadAllLines(Path.Combine(prefixFile));
                        if (lines.Length >= 1 && lines[0].IndexOfAny(Path.GetInvalidPathChars()) == -1) {

                            string origLibDir = Path.Combine(lines[0], "Lib");
                            if (Directory.Exists(origLibDir)) {
                                // virtual env install
                                libDir = origLibDir;

                                virtualEnvPackages = Path.Combine(virtualEnvLibDir, "site-packages");
                            }
                        }
                    } catch (IOException) {
                    } catch (UnauthorizedAccessException) {
                    } catch (System.Security.SecurityException) {
                    }
                } else {
                    // try and find the lib dir based upon where site.py lives
                    var psi = new ProcessStartInfo(
                        request.Factory.Configuration.InterpreterPath,
                        "-c \"import site; print site.__file__\""
                    );
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardError = true;
                    psi.UseShellExecute = false;
                    psi.CreateNoWindow = true;

                    var proc = Process.Start(psi);

                    OutputDataReceiver receiver = new OutputDataReceiver();
                    proc.OutputDataReceived += receiver.OutputDataReceived;
                    proc.ErrorDataReceived += receiver.OutputDataReceived;

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

                    proc.WaitForExit();

                    string siteFilename = receiver.Received.ToString().Trim();

                    if (!String.IsNullOrWhiteSpace(siteFilename) &&
                        siteFilename.IndexOfAny(Path.GetInvalidPathChars()) == -1) {
                        var dirName = Path.GetDirectoryName(siteFilename);
                        if (Directory.Exists(dirName)) {
                            libDir = dirName;
                        }
                    }
                }
            }
        }